product_material_group.go
2.9 KB
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
package domain
import (
"fmt"
"strconv"
"time"
)
// 物料分组
type ProductMaterialGroup struct {
// 物料分组ID
ProductMaterialGroupId int `json:"productMaterialGroupId"`
// 企业id
CompanyId int `json:"companyId"`
// 组织ID
OrgId int `json:"orgId"`
// 父级ID
Pid int `json:"pid"`
// 路径 (不使用,如果父级改变的话,子级的Path要做更新)
//Path string `json:"path"`
// 物料分组名称
MaterialGroupName string `json:"materialGroupName"`
// 物料分组编码
MaterialGroupNumber string `json:"materialGroupNumber"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt"`
// 删除时间
DeletedAt time.Time `json:"deletedAt"`
// 扩展数据
Ext *Ext `json:"ext,omitempty"`
}
type ProductMaterialGroupRepository interface {
Save(productMaterialGroup *ProductMaterialGroup) (*ProductMaterialGroup, error)
Remove(productMaterialGroup *ProductMaterialGroup) (*ProductMaterialGroup, error)
FindOne(queryOptions map[string]interface{}) (*ProductMaterialGroup, error)
Find(queryOptions map[string]interface{}) (int64, []*ProductMaterialGroup, error)
}
func (productMaterialGroup *ProductMaterialGroup) Identify() interface{} {
if productMaterialGroup.ProductMaterialGroupId == 0 {
return nil
}
return productMaterialGroup.ProductMaterialGroupId
}
func (productMaterialGroup *ProductMaterialGroup) Update(data map[string]interface{}) error {
return nil
}
func (productMaterialGroup *ProductMaterialGroup) GroupNumberComposeUp(parent *ProductMaterialGroup, latestNumber string, total int) string {
iNumber, err := strconv.ParseInt(latestNumber, 10, 64)
if err == nil {
iNumber += 1
return formatFunc(iNumber)
}
if parent != nil {
return parent.MaterialGroupNumber + formatFunc(int64(total+1))
}
return formatFunc(int64(total + 1))
}
func formatFunc(iNumber int64) string {
sNumber := fmt.Sprintf("%d", iNumber)
if len(sNumber)%2 == 1 {
return "0" + sNumber
}
return sNumber
}
/***** 1.实现树 *****/
func (productMaterialGroup *ProductMaterialGroup) PID() string {
return fmt.Sprintf("%d", productMaterialGroup.Pid)
}
func (productMaterialGroup *ProductMaterialGroup) ID() string {
return fmt.Sprintf("%d", productMaterialGroup.ProductMaterialGroupId)
}
type ProductMaterialGroups []*ProductMaterialGroup
func (tree ProductMaterialGroups) Len() int {
return len(tree)
}
func (tree ProductMaterialGroups) Less(i, j int) bool {
if tree[i].Pid < tree[j].Pid {
return true
}
if tree[i].Pid == tree[j].Pid {
return tree[i].ProductMaterialGroupId < tree[j].ProductMaterialGroupId
}
return false
}
func (tree ProductMaterialGroups) Swap(i, j int) {
tree[i], tree[j] = tree[j], tree[i]
}
func (tree ProductMaterialGroups) ToMapByGroupNumber() map[string]*ProductMaterialGroup {
var result = make(map[string]*ProductMaterialGroup)
for i := range tree {
result[tree[i].MaterialGroupNumber] = tree[i]
}
return result
}