pg_material_service.go
9.3 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
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
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"sort"
"time"
)
type PGMaterialService struct {
transactionContext *pgTransaction.TransactionContext
}
// 物料
func (ptr *PGMaterialService) AddMaterial(opt *domain.OperateInfo, item *domain.ProductMaterial) (*domain.ProductMaterial, error) {
var (
user *domain.User
org *domain.Org
err error
newProductMaterial *domain.ProductMaterial
productMaterialRepository, _ = repository.NewProductMaterialRepository(ptr.transactionContext)
productGroupRepository, _ = repository.NewProductGroupRepository(ptr.transactionContext)
)
if user, err = NewUserService().User(opt.UserId); err != nil {
return nil, err
}
if org, err = NewUserService().Organization(opt.OrgId); err != nil {
return nil, err
}
_, err = productGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productGroupId": item.ProductMaterialGroupId})
if err != nil {
return nil, err
}
if newProductMaterial, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialNumber": item.MaterialNumber}); err == nil || newProductMaterial != nil {
return nil, fmt.Errorf("物料编码已存在")
}
newProductMaterial = &domain.ProductMaterial{
CompanyId: item.CompanyId,
OrgId: item.OrgId,
ProductMaterialGroupId: item.ProductMaterialGroupId,
MaterialName: item.MaterialName,
MaterialNumber: item.MaterialNumber,
MaterialAttribute: item.MaterialAttribute,
MaterialCategory: item.MaterialCategory,
ProductMaterialExt: item.ProductMaterialExt,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Ext: domain.NewExt(org.OrgName).WithOperator(user),
}
newProductMaterial, err = productMaterialRepository.Save(newProductMaterial)
return newProductMaterial, err
}
// 物料分组
func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
var (
user *domain.User
org *domain.Org
err error
parent *domain.ProductMaterialGroup
newProductMaterialGroup *domain.ProductMaterialGroup
productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
)
if user, err = NewUserService().User(opt.UserId); err != nil {
return nil, err
}
if org, err = NewUserService().Organization(opt.OrgId); err != nil {
return nil, err
}
if item.Pid != 0 {
if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil {
return nil, fmt.Errorf("上级物料分组不存在")
}
}
if newProductMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || newProductMaterialGroup != nil {
return nil, fmt.Errorf("物料分组编码已存在")
}
newProductMaterialGroup = &domain.ProductMaterialGroup{
CompanyId: opt.CompanyId,
OrgId: opt.OrgId,
Pid: item.Pid,
//Path: item.GetPath(parent),
MaterialGroupName: item.MaterialGroupName,
MaterialGroupNumber: item.MaterialGroupNumber,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Ext: domain.NewExt(org.OrgName).WithOperator(user),
}
newProductMaterialGroup, err = productMaterialGroupRepository.Save(newProductMaterialGroup)
return newProductMaterialGroup, err
}
func (ptr *PGMaterialService) UpdateMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
var (
user *domain.User
org *domain.Org
err error
parent *domain.ProductMaterialGroup
productMaterialGroup *domain.ProductMaterialGroup
productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
)
if user, err = NewUserService().User(opt.UserId); err != nil {
return nil, err
}
if org, err = NewUserService().Organization(opt.OrgId); err != nil {
return nil, err
}
if item.ProductMaterialGroupId == item.Pid {
return nil, fmt.Errorf("当前物料分组不能做为自己上级")
}
if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.ProductMaterialGroupId}); err != nil || productMaterialGroup == nil {
return nil, fmt.Errorf("物料分组不存在")
}
if item.Pid != productMaterialGroup.ProductMaterialGroupId && item.Pid != 0 {
if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil {
return nil, fmt.Errorf("上级物料分组不存在")
}
}
if productMaterialGroup.MaterialGroupNumber != item.MaterialGroupNumber {
if group, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || group != nil {
return nil, fmt.Errorf("物料分组编码已存在")
}
}
productMaterialGroup.MaterialGroupNumber = item.MaterialGroupNumber
productMaterialGroup.MaterialGroupName = item.MaterialGroupName
productMaterialGroup.UpdatedAt = item.UpdatedAt
productMaterialGroup.Ext = domain.NewExt(org.OrgName).WithOperator(user)
productMaterialGroup, err = productMaterialGroupRepository.Save(productMaterialGroup)
return productMaterialGroup, err
}
func (ptr *PGMaterialService) AllMaterialGroupChild(opt *domain.OperateInfo, productMaterialGroupId int) ([]*domain.ProductMaterialGroup, []int, error) {
var (
err error
listId []int
productMaterialGroup *domain.ProductMaterialGroup
productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
groups domain.ProductMaterialGroups
)
if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": productMaterialGroupId}); err != nil || productMaterialGroup == nil {
return nil, listId, fmt.Errorf("物料分组不存在")
}
_, groups, err = productMaterialGroupRepository.Find(map[string]interface{}{"companyId": opt.CompanyId}) //,"orderByProductMaterialGroupId":"asc"
if len(groups) == 0 || err != nil {
return nil, listId, err
}
// 先排序 sort pid,id asc
sort.Stable(groups)
var treeNodes = make([]utils.TreeNode, len(groups))
for i := 0; i < len(groups); i++ {
treeNodes[i] = groups[i]
}
tree := utils.NewTree(treeNodes)
child := tree.AllChildNode(productMaterialGroup)
var result = make([]*domain.ProductMaterialGroup, 0)
for i := range child {
if v, ok := child[i].(*domain.ProductMaterialGroup); ok {
result = append(result, v)
listId = append(listId, v.ProductMaterialGroupId)
}
}
return result, listId, err
}
func (ptr *PGMaterialService) AllMaterialGroupParent(opt *domain.OperateInfo, productMaterialGroupId int) ([]*domain.ProductMaterialGroup, []int, error) {
var (
err error
listId []int
productMaterialGroup *domain.ProductMaterialGroup
productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
groups domain.ProductMaterialGroups
)
if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": productMaterialGroupId}); err != nil || productMaterialGroup == nil {
return nil, listId, fmt.Errorf("物料分组不存在")
}
_, groups, err = productMaterialGroupRepository.Find(map[string]interface{}{"companyId": opt.CompanyId}) //,"orderByProductMaterialGroupId":"asc"
if len(groups) == 0 || err != nil {
return nil, listId, err
}
// 先排序 sort pid,id asc
sort.Stable(groups)
var treeNodes = make([]utils.TreeNode, len(groups))
for i := 0; i < len(groups); i++ {
treeNodes[i] = groups[i]
}
tree := utils.NewTree(treeNodes)
child := tree.TreeNodePaths(productMaterialGroup)
var result = make([]*domain.ProductMaterialGroup, 0)
for i := range child {
if v, ok := child[i].(*domain.ProductMaterialGroup); ok {
result = append(result, v)
listId = append(listId, v.ProductMaterialGroupId)
}
}
return result, listId, err
}
func NewPGMaterialService(transactionContext *pgTransaction.TransactionContext) (*PGMaterialService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PGMaterialService{
transactionContext: transactionContext,
}, nil
}
}