zhangqian
2023-10-19 c52ee4d80de792008522edfb3f06d67b20df4cb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package model
 
import (
    "aps_crm/constvar"
    "aps_crm/pkg/mysqlx"
    "errors"
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
)
 
type (
    // Receipt 收款单
    Receipt struct {
        Id            int                        `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        ClientId      int                        `gorm:"column:client_id;type:int;not null;default 0;comment:客户id" json:"clientId"`                                // 客户id
        Client        Client                     `gorm:"foreignKey:ClientId" json:"client"`                                                                        // 客户id
        SourceType    constvar.ReceiptSourceType `gorm:"column:source_type;type:int;not null;default 0;comment:来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)" json:"sourceType"` // 来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)
        SourceId      int                        `gorm:"column:source_id;type:int;not null;default 0;comment:源单id " json:"sourceId"`                               // 源单id
        PrincipalId   int                        `gorm:"column:principal_id;type:int;not null;default 0;comment:负责人id" json:"principalId"`                         // 负责人id
        Principal     User                       `gorm:"foreignKey:PrincipalId" json:"principal"`                                                                  // 负责人id
        ReceiptDate   string                     `gorm:"column:receipt_date;type:varchar(255);not null;default '';comment:收款日期" json:"receiptDate"`                // 收款日期
        MoneyType     string                     `gorm:"column:money_type;type:varchar(255);not null;default '';comment:币种" json:"moneyType"`                      // 币种
        Amount        decimal.Decimal            `gorm:"column:amount;type:decimal(12,2);not null;default '0.00';comment:收款金额" json:"amount"`                      // 收款金额
        PaymentTypeId int                        `gorm:"column:payment_type_id;type:int;not null;default 0;comment:收款方式ID" json:"paymentTypeId"`                   // 收款方式ID
        PaymentType   PaymentType                `gorm:"foreignKey:PaymentTypeId" json:"paymentType"`
        BankAccountId int                        `gorm:"column:bank_account_id;type:int;not null;default 0;comment:账户id" json:"bankAccountId"` // 账户id
        BankAccount   BankAccount                `gorm:"foreignKey:BankAccountId" json:"bankAccount"`
        Remark        string                     `gorm:"column:remark;type:varchar(255);not null;default '';comment:备注" json:"remark"` // 备注
        FileId        int                        `gorm:"column:file_id;type:int;not null;default 0;comment:附件id" json:"fileId"`        // 附件id
        CrmModel
    }
 
    // ReceiptSearch 收款单搜索条件
    ReceiptSearch struct {
        Receipt
        Orm         *gorm.DB
        QueryClass  constvar.ReceiptQueryClass
        KeywordType constvar.ReceiptKeywordType
        Keyword     string
        PageNum     int
        PageSize    int
    }
)
 
func (slf *Receipt) TableName() string {
    return "receipt"
}
 
func (slf *Receipt) AfterFind(db *gorm.DB) (err error) {
    err = slf.CrmModel.AfterFind(db)
    if slf.CrmModel.ID == 0 {
        slf.CrmModel.ID = uint(slf.Id)
    }
    slf.CrmModel.SetNumber(constvar.NumberPrefixOfReceipt)
    return nil
}
 
func NewReceiptSearch() *ReceiptSearch {
    return &ReceiptSearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *ReceiptSearch) SetSourceType(sourceType constvar.ReceiptSourceType) *ReceiptSearch {
    slf.SourceType = sourceType
    return slf
}
 
func (slf *ReceiptSearch) SetSourceId(sourceId int) *ReceiptSearch {
    slf.SourceId = sourceId
    return slf
}
 
func (slf *ReceiptSearch) SetPage(page, size int) *ReceiptSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *ReceiptSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&Receipt{})
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
    if slf.SourceType != 0 {
        db = db.Where("source_type = ?", slf.SourceType)
    }
    if slf.SourceId != 0 {
        db = db.Where("source_id = ?", slf.SourceId)
    }
    if slf.ClientId != 0 {
        db = db.Where("client_id = ?", slf.ClientId)
    }
 
    return db
}
 
func (slf *ReceiptSearch) Create(record *Receipt) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *ReceiptSearch) CreateBatch(records []*Receipt) error {
    var db = slf.build()
    return db.Create(records).Error
}
 
func (slf *ReceiptSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&Receipt{}).Error
}
 
func (slf *ReceiptSearch) Update(record *Receipt) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *ReceiptSearch) FindAll() ([]*Receipt, error) {
    var db = slf.build()
    var record = make([]*Receipt, 0)
    err := db.Find(&record).Error
    return record, err
}
 
func (slf *ReceiptSearch) SetId(id int) *ReceiptSearch {
    slf.Id = id
    return slf
}
 
func (slf *ReceiptSearch) SetClientId(clientId int) *ReceiptSearch {
    slf.ClientId = clientId
    return slf
}
 
func (slf *ReceiptSearch) SetOrm(tx *gorm.DB) *ReceiptSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *ReceiptSearch) First() (*Receipt, error) {
    var db = slf.build()
    var record = new(Receipt)
    err := db.First(record).Error
    return record, err
}
 
func (slf *ReceiptSearch) Updates(values interface{}) error {
    var db = slf.build()
    return db.Updates(values).Error
}
 
func (slf *ReceiptSearch) Save(record *Receipt) error {
    if record.Id == 0 {
        return errors.New("id为空")
    }
    var db = slf.build()
 
    if err := db.Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *ReceiptSearch) Find() ([]*Receipt, int64, error) {
    var db = slf.build()
    var records = make([]*Receipt, 0)
    var total int64
    if err := db.Count(&total).Error; err != nil {
        return records, total, err
    }
    if slf.PageNum > 0 && slf.PageSize > 0 {
        db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
    }
 
    err := db.Preload("Principal").Preload("Client").Preload("PaymentType").Preload("BankAccount").Order("created_at desc").Find(&records).Error
    return records, total, err
}
 
// InitDefaultData 初始化数据
func (slf *ReceiptSearch) InitDefaultData() error {
    var (
        db          = slf.Orm.Table(slf.TableName())
        total int64 = 0
    )
    if err := db.Count(&total).Error; err != nil {
        return err
    }
    if total != 0 {
        return nil
    }
    records := []*Receipt{}
    return slf.CreateBatch(records)
}