-
Notifications
You must be signed in to change notification settings - Fork 0
/
material.go
251 lines (237 loc) · 8.79 KB
/
material.go
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package mysql_model
import (
"fmt"
"github.com/XC-Zero/yinwan/pkg/client"
"github.com/XC-Zero/yinwan/pkg/utils/es_tool"
"github.com/XC-Zero/yinwan/pkg/utils/logger"
"github.com/XC-Zero/yinwan/pkg/utils/math_plus"
"github.com/pkg/errors"
"gorm.io/gorm"
"log"
)
// Material 原材料
type Material struct {
BasicModel
BookNameInfo
MaterialName string `gorm:"type:varchar(50);not null" json:"material_name" form:"material_name" cn:"原材料名称"`
MaterialTypeID int `gorm:"type:int;not null;index" json:"material_type_id" form:"material_type_id" cn:"原材料类型ID"`
MaterialTypeName string `gorm:"type:varchar(50);not null" json:"material_type_name" form:"material_type_name" cn:"原材料类型名称"`
MaterialStyle string `gorm:"type:varchar(50);not null" json:"material_style" form:"material_style" cn:"原材料规格"`
MaterialOwnerID *int `gorm:"type:int" json:"material_owner_id,omitempty" form:"material_owner_id,omitempty" cn:"原材料负责人ID"`
MaterialOwnerName *string `gorm:"type:varchar(50)" json:"material_owner_name,omitempty" form:"material_owner_name" cn:"原材料负责人名称"`
AverageUnitPrice *string `gorm:"type:varchar(50)" json:"average_unit_price,omitempty" form:"average_unit_price,omitempty" cn:"原材料平均价格"`
MaterialPresentCount int `form:"material_present_count" json:"material_present_count" cn:"当前余量"`
MaterialPicUrl *string `gorm:"type:varchar(500)" json:"material_pic_url,omitempty" form:"material_pic_url,omitempty" cn:"原材料展示图"`
Remark *string `gorm:"type:varchar(200)" json:"remark,omitempty" form:"remark,omitempty" cn:"原材料备注"`
}
func (m *Material) AfterCreate(tx *gorm.DB) error {
bookName := tx.Statement.Context.Value("book_name").(string)
bk, ok := client.ReadBookMap(bookName)
if !ok {
return errors.New("There is no book name!")
}
m.BookNameID = bk.StorageName
m.BookName = bk.BookName
err := client.PutIntoIndex(m)
if err != nil {
return err
}
return nil
}
// AfterUpdate 同步更新原材料
func (m *Material) AfterUpdate(tx *gorm.DB) error {
bookName := tx.Statement.Context.Value("book_name").(string)
bk, ok := client.ReadBookMap(bookName)
if !ok {
return errors.New("There is no book name!")
}
m.BookNameID = bk.StorageName
m.BookName = bk.BookName
err := client.UpdateIntoIndex(m, m.RecID, tx.Statement.Context, es_tool.ESDocToUpdateScript(m.ToESDoc()))
if err != nil {
return err
}
return nil
}
// AfterDelete 同步删除原材料
func (m Material) AfterDelete(tx *gorm.DB) error {
err := client.DeleteFromIndex(m, m.RecID, tx.Statement.Context)
if err != nil {
return err
}
return nil
}
func (m Material) TableName() string {
return "materials"
}
func (m Material) TableCnName() string {
return "原材料"
}
func (m Material) Mapping() map[string]interface{} {
ma := mapping{
"settings": mapping{},
"mappings": mapping{
"properties": mapping{
"rec_id": mapping{
"type": "keyword",
},
"material_name": mapping{
"type": "text", //字符串类型且进行分词, 允许模糊匹配
"analyzer": IK_SMART, //设置分词工具
"search_analyzer": IK_SMART,
"fields": mapping{ //当需要对模糊匹配的字符串也允许进行精确匹配时假如此配置
"keyword": mapping{
"type": "keyword",
"ignore_above": 256,
},
},
},
"remark": mapping{
"type": "text",
"analyzer": IK_SMART,
"search_analyzer": IK_SMART,
},
"created_at": mapping{
"type": "text",
},
"material_pic_url": mapping{
"type": "text",
},
"book_name": mapping{
"type": "keyword",
},
"book_name_id": mapping{
"type": "keyword",
},
},
},
}
return ma
}
func (m Material) ToESDoc() map[string]interface{} {
return map[string]interface{}{
"rec_id": m.RecID,
"created_at": m.CreatedAt,
"remark": m.Remark,
"material_name": m.MaterialName,
"material_pic_url": m.MaterialPicUrl,
"book_name": m.BookName,
"book_name_id": m.BookNameID,
}
}
// MaterialBatch 原材料批次
type MaterialBatch struct {
BasicModel
MaterialID int `gorm:"type:int;not null" json:"material_id" cn:"原材料ID"`
MaterialName string `gorm:"type:varchar(50);not null" json:"material_name" cn:"原材料名称"`
StockInRecordID int `gorm:"type:int;not null" json:"stock_in_record_id" cn:"关联入库单号"`
MaterialBatchOwnerID *int `gorm:"type:int;index" json:"material_batch_owner_id,omitempty" cn:"负责人ID"`
MaterialBatchOwnerName *string `gorm:"type:varchar(50)" json:"material_batch_owner_name,omitempty" cn:"负责人名称"`
MaterialBatchTotalPrice string `gorm:"type:varchar(50);not null" json:"material_batch_total_price" cn:"批次总价"`
MaterialBatchNumber int `gorm:"type:int;not null" json:"material_batch_number" cn:"批次原材料总数"`
MaterialBatchSurplusNumber int `gorm:"type:int;not null" json:"material_batch_surplus_number" cn:"当前批次原材料剩余数量"`
MaterialBatchUnitPrice string `gorm:"type:varchar(50);not null" json:"material_batch_unit_price" cn:"单价"`
WarehouseID *int `gorm:"type:int;index" json:"warehouse_id,omitempty" cn:"仓库ID"`
WarehouseName *string `gorm:"type:varchar(50)" json:"warehouse_name,omitempty" cn:"仓库名称"`
StockInTime *string `gorm:"type:timestamp " json:"stock_in_time" cn:"入库时间"`
Remark *string `gorm:"type:varchar(200)" json:"remark,omitempty" cn:"批次备注"`
}
type tempScan struct {
Surplus int64 `json:"surplus"`
TotalPrice int64 `json:"total_price"`
TotalNumber int64 `json:"total_number"`
}
// AfterCreate 同步创建历史成本
// 事务里不能嵌套事务,否则内层事务将会把外层事务重复执行x遍
func (m MaterialBatch) AfterCreate(tx *gorm.DB) error {
id := tx.Statement.Context.Value("material_id")
rec, ok := id.(int)
if id == nil || !ok {
logger.Error(errors.New("Context have not material_id"), "")
return nil
}
var cost = MaterialHistoryCost{
MaterialID: rec,
Num: m.MaterialBatchNumber,
Price: m.MaterialBatchUnitPrice,
RelatedMaterialBatchID: *m.RecID,
}
//err := tx.Transaction(func(tx *gorm.DB) error {
var res MaterialHistoryCost
err2 := tx.Model(cost).Where("related_material_batch_id = ?", *m.RecID).Find(&res).Error
if err2 != nil {
log.Println(errors.WithStack(err2))
return err2
}
if res.RecID == nil {
err2 := tx.Model(cost).Create(&cost).Error
if err2 != nil {
log.Println("@@@@ Create ERROR is ", errors.WithStack(err2))
return err2
}
} else {
err2 := tx.Where("rec_id", *res.RecID).Updates(&cost).Error
if err2 != nil {
log.Println("@@@@ Updates ERROR is ", errors.WithStack(err2))
return err2
}
}
var tempScan tempScan
err2 = tx.Model(MaterialBatch{}).Where("material_id = ? and deleted_at is null ", rec).Select(
"sum(material_batch_surplus_number) as surplus," +
"sum(material_batch_total_price) as total_price," +
"sum(material_batch_number) as total_number ").Scan(&tempScan).Error
if err2 != nil {
log.Println("[ERROR] Statics ERROR is ", errors.WithStack(err2))
return err2
}
var averagePrice string
if tempScan.TotalNumber == 0 {
averagePrice = "0"
} else {
fraction, err := math_plus.New(tempScan.TotalPrice, tempScan.TotalNumber)
if err != nil {
averagePrice = "0"
}
averagePrice = fmt.Sprintf("%.2f", fraction.Float64())
}
err2 = tx.Model(Material{}).Where("rec_id = ?", rec).
UpdateColumn("average_unit_price", averagePrice).
UpdateColumn("material_present_count", tempScan.Surplus).Error
if err2 != nil {
log.Println(errors.WithStack(err2))
return err2
}
return nil
//})
//if err != nil {
// return err
//}
//return nil
}
func (m MaterialBatch) AfterUpdate(tx *gorm.DB) error {
return m.AfterCreate(tx)
}
func (m MaterialBatch) AfterDelete(tx *gorm.DB) error {
return m.AfterCreate(tx)
}
func (m MaterialBatch) TableName() string {
return "material_batches"
}
func (m MaterialBatch) TableCnName() string {
return "原材料批次"
}
// MaterialHistoryCost 原材料历史进货价
type MaterialHistoryCost struct {
BasicModel
MaterialID int `gorm:"type:int;not null;index" json:"material_id" form:"material_id"`
Price string `gorm:"type:varchar(20)" json:"price" form:"price"`
Num int `json:"num" form:"num"`
RelatedMaterialBatchID int `gorm:"type:int;not null;index" json:"related_material_batch_id" form:"related_material_batch_id"`
}
func (m MaterialHistoryCost) TableName() string {
return "material_history_costs"
}
func (m MaterialHistoryCost) TableCnName() string {
return "原材料历史进货价"
}