liujiandao
2024-03-30 1e65185f0ecb8d4f8f1fd9ea822137e0c841a47f
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
195
196
197
198
199
200
201
202
203
204
205
206
207
package model
 
import (
    "aps_crm/constvar"
    "aps_crm/pkg/mysqlx"
    "errors"
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
)
 
type (
    // ServiceCollectionPlan 收款计划
    ServiceCollectionPlan struct {
        Id               int                           `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        CollectionType   int                           `gorm:"column:collection_type;type:tinyint;not null;default 0;comment:类型(1 计划收款日期 2 项目状态)" json:"collectionType"` // 类型(1 计划收款日期 2 项目状态)
        SourceType       constvar.CollectionSourceType `gorm:"column:source_type;type:tinyint;not null;default 0;comment:源单类型(1销售明细2服务合同3销售发票)" json:"sourceType"`       // 源单类型(1销售明细2服务合同3销售发票)
        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
        Term             int                           `gorm:"column:term;type:tinyint;not null;default 0;comment:期次" json:"term"`                                       // 期次
        Percent          decimal.Decimal               `gorm:"column:percent;type:decimal(5,2);not null;default 0.00;comment:收款比例" gorm:"" json:"percent"`               // 比例
        Amount           decimal.Decimal               `gorm:"column:amount;type:decimal(12,2);not null;default '0.00';comment:金额" gorm:"" json:"amount"`                // 金额
        MoneyType        string                        `gorm:"column:money_type;type:varchar(255);not null;default '';comment:币种" json:"moneyType"`                      // 币种
        CollectionDate   string                        `gorm:"column:collection_date;type:varchar(255);not null;default '';comment:计划收款日期" json:"collectionDate"`        // 计划收款日期
        Remark           string                        `gorm:"column:remark;type:varchar(512);not null;default '';comment:备注" json:"remark"`                             // 备注
        Status           constvar.CollectionStatus     `gorm:"column:status;type:tinyint;not null;default '';comment:状态(1未收2部分已收3已收)" json:"status"`                     // 状态(1未收2部分已收3已收)
        AmountReceivable decimal.Decimal               `gorm:"column:amount_receivable;type:decimal(12,2);comment:应收金额" json:"amountReceivable"`                         // 应收金额
        AmountReceived   decimal.Decimal               `gorm:"column:amount_received;type:decimal(12,2);comment:已收金额" json:"amountReceived"`                             // 已收金额
        AmountTotal      decimal.Decimal               `gorm:"column:amount_total;type:decimal(12,2);comment:总额" json:"amountTotal"`                                     // 总额
        FileId           int                           `gorm:"column:file_id;type:int;comment:附件id" json:"fileId"`                                                       // 附件id
    }
 
    // ServiceCollectionPlanSearch 收款计划搜索条件
    ServiceCollectionPlanSearch struct {
        ServiceCollectionPlan
        Orm         *gorm.DB
        QueryClass  constvar.ServiceCollectionPlanQueryClass
        KeywordType constvar.ServiceCollectionPlanKeywordType
        Keyword     string
        PageNum     int
        PageSize    int
    }
)
 
func (ServiceCollectionPlan) TableName() string {
    return "collection_plan"
}
 
func NewServiceCollectionPlanSearch() *ServiceCollectionPlanSearch {
    return &ServiceCollectionPlanSearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *ServiceCollectionPlanSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&ServiceCollectionPlan{})
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
 
    if slf.SourceId != 0 {
        db = db.Where("source_id = ?", slf.SourceId)
    }
 
    if slf.SourceType != 0 {
        db = db.Where("source_type = ?", slf.SourceType)
    }
 
    db = db.Preload("Principal")
    return db
}
 
func (slf *ServiceCollectionPlanSearch) Create(record *ServiceCollectionPlan) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *ServiceCollectionPlanSearch) CreateBatch(records []*ServiceCollectionPlan) error {
    var db = slf.build()
    return db.Create(records).Error
}
 
func (slf *ServiceCollectionPlanSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&ServiceCollectionPlan{}).Error
}
 
func (slf *ServiceCollectionPlanSearch) Update(record *ServiceCollectionPlan) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *ServiceCollectionPlanSearch) FindAll() ([]*ServiceCollectionPlan, error) {
    var db = slf.build()
    var record = make([]*ServiceCollectionPlan, 0)
    err := db.Find(&record).Error
    return record, err
}
 
func (slf *ServiceCollectionPlanSearch) SetId(id int) *ServiceCollectionPlanSearch {
    slf.Id = id
    return slf
}
 
func (slf *ServiceCollectionPlanSearch) SetOrm(tx *gorm.DB) *ServiceCollectionPlanSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *ServiceCollectionPlanSearch) SetSourceId(id int) *ServiceCollectionPlanSearch {
    slf.SourceId = id
    return slf
}
 
func (slf *ServiceCollectionPlanSearch) SetSourceType(sourceType constvar.CollectionSourceType) *ServiceCollectionPlanSearch {
    slf.SourceType = sourceType
    return slf
}
 
func (slf *ServiceCollectionPlanSearch) First() (*ServiceCollectionPlan, error) {
    var db = slf.build()
    var record = new(ServiceCollectionPlan)
    err := db.First(record).Error
    return record, err
}
 
func (slf *ServiceCollectionPlanSearch) Updates(values interface{}) error {
    var db = slf.build()
    return db.Updates(values).Error
}
 
func (slf *ServiceCollectionPlanSearch) Save(record *ServiceCollectionPlan) 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 *ServiceCollectionPlanSearch) Find() ([]*ServiceCollectionPlan, int64, error) {
    var db = slf.build()
    var records = make([]*ServiceCollectionPlan, 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.Find(&records).Error
    return records, total, err
}
 
func (slf *ServiceCollectionPlanSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
 
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
    }
 
    return nil
}
 
func (slf *ServiceCollectionPlanSearch) UpdateStatusAndAmount(collection *ServiceCollectionPlan, receiveAmount decimal.Decimal) error {
    receivedAmount := collection.AmountReceived.Add(receiveAmount)
    var status constvar.CollectionStatus
    if receiveAmount.GreaterThanOrEqual(collection.AmountTotal) {
        status = constvar.CollectionStatusCollected
    } else {
        status = constvar.CollectionStatusSubCollected
    }
    receivableAmount := collection.AmountTotal.Add(receivedAmount)
    err := slf.SetId(collection.Id).UpdateByMap(map[string]interface{}{
        "status":            status,
        "amount_received":   receivedAmount,
        "amount_receivable": receivableAmount})
    if err != nil {
        return err
    }
 
    return nil
}
 
// InitDefaultData 初始化数据
func (slf *ServiceCollectionPlanSearch) 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 := []*ServiceCollectionPlan{}
    return slf.CreateBatch(records)
}