正在显示
27 个修改的文件
包含
1394 行增加
和
2 行删除
@@ -413,6 +413,32 @@ func FastPgDeviceDailyRunningRecord(transactionContext application.TransactionCo | @@ -413,6 +413,32 @@ func FastPgDeviceDailyRunningRecord(transactionContext application.TransactionCo | ||
413 | return rep, mod, err | 413 | return rep, mod, err |
414 | } | 414 | } |
415 | 415 | ||
416 | +// FastProductMaterialGroup 快速返回物料 | ||
417 | +// | ||
418 | +// transactionContext 事务 | ||
419 | +// id 对象唯一标识 | ||
420 | +func FastProductMaterialGroup(transactionContext application.TransactionContext, id int, options ...option) (domain.ProductMaterialGroupRepository, *domain.ProductMaterialGroup, error) { | ||
421 | + var rep domain.ProductMaterialGroupRepository | ||
422 | + var mod *domain.ProductMaterialGroup | ||
423 | + var err error | ||
424 | + if value, err := CreateProductMaterialGroupRepository(map[string]interface{}{ | ||
425 | + "transactionContext": transactionContext, | ||
426 | + }); err != nil { | ||
427 | + return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
428 | + } else { | ||
429 | + rep = value | ||
430 | + } | ||
431 | + if id > 0 { | ||
432 | + if mod, err = rep.FindOne(map[string]interface{}{"productMaterialGroupId": id}); err != nil { | ||
433 | + if err == domain.ErrorNotFound { | ||
434 | + return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该物料分组不存在") | ||
435 | + } | ||
436 | + return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
437 | + } | ||
438 | + } | ||
439 | + return rep, mod, err | ||
440 | +} | ||
441 | + | ||
416 | /***** 2.配置 *****/ | 442 | /***** 2.配置 *****/ |
417 | 443 | ||
418 | type FastOptions struct { | 444 | type FastOptions struct { |
@@ -141,3 +141,11 @@ func CreateDeviceDailyRunningRecordRepository(options map[string]interface{}) (d | @@ -141,3 +141,11 @@ func CreateDeviceDailyRunningRecordRepository(options map[string]interface{}) (d | ||
141 | } | 141 | } |
142 | return repository.NewDeviceDailyRunningRecordRepository(transactionContext) | 142 | return repository.NewDeviceDailyRunningRecordRepository(transactionContext) |
143 | } | 143 | } |
144 | + | ||
145 | +func CreateProductMaterialGroupRepository(options map[string]interface{}) (domain.ProductMaterialGroupRepository, error) { | ||
146 | + var transactionContext *pg.TransactionContext | ||
147 | + if value, ok := options["transactionContext"]; ok { | ||
148 | + transactionContext = value.(*pg.TransactionContext) | ||
149 | + } | ||
150 | + return repository.NewProductMaterialGroupRepository(transactionContext) | ||
151 | +} |
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "reflect" | ||
6 | + "strings" | ||
7 | + | ||
8 | + "github.com/beego/beego/v2/core/validation" | ||
9 | +) | ||
10 | + | ||
11 | +type CreateProductMaterialGroupCommand struct { | ||
12 | + // 企业id | ||
13 | + CompanyId int `cname:"企业id" json:"companyId" valid:"Required"` | ||
14 | + // 组织ID | ||
15 | + OrgId int `cname:"组织ID" json:"orgId" valid:"Required"` | ||
16 | + // 父级ID | ||
17 | + Pid int `cname:"父级ID" json:"pid"` | ||
18 | + // 物料分组名称 | ||
19 | + MaterialGroupName string `cname:"物料分组名称" json:"materialGroupName" valid:"Required"` | ||
20 | + // 物料分组编码 | ||
21 | + MaterialGroupNumber string `cname:"物料分组编码" json:"materialGroupNumber,omitempty"` | ||
22 | +} | ||
23 | + | ||
24 | +func (createProductMaterialGroupCommand *CreateProductMaterialGroupCommand) Valid(validation *validation.Validation) { | ||
25 | + | ||
26 | +} | ||
27 | + | ||
28 | +func (createProductMaterialGroupCommand *CreateProductMaterialGroupCommand) ValidateCommand() error { | ||
29 | + valid := validation.Validation{} | ||
30 | + b, err := valid.Valid(createProductMaterialGroupCommand) | ||
31 | + if err != nil { | ||
32 | + return err | ||
33 | + } | ||
34 | + if !b { | ||
35 | + elem := reflect.TypeOf(createProductMaterialGroupCommand).Elem() | ||
36 | + for _, validErr := range valid.Errors { | ||
37 | + field, isExist := elem.FieldByName(validErr.Field) | ||
38 | + if isExist { | ||
39 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
40 | + } else { | ||
41 | + return fmt.Errorf(validErr.Message) | ||
42 | + } | ||
43 | + } | ||
44 | + } | ||
45 | + return nil | ||
46 | +} |
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "reflect" | ||
6 | + "strings" | ||
7 | + | ||
8 | + "github.com/beego/beego/v2/core/validation" | ||
9 | +) | ||
10 | + | ||
11 | +type RemoveProductMaterialGroupCommand struct { | ||
12 | + // 物料分组ID | ||
13 | + ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"` | ||
14 | +} | ||
15 | + | ||
16 | +func (removeProductMaterialGroupCommand *RemoveProductMaterialGroupCommand) Valid(validation *validation.Validation) { | ||
17 | + | ||
18 | +} | ||
19 | + | ||
20 | +func (removeProductMaterialGroupCommand *RemoveProductMaterialGroupCommand) ValidateCommand() error { | ||
21 | + valid := validation.Validation{} | ||
22 | + b, err := valid.Valid(removeProductMaterialGroupCommand) | ||
23 | + if err != nil { | ||
24 | + return err | ||
25 | + } | ||
26 | + if !b { | ||
27 | + elem := reflect.TypeOf(removeProductMaterialGroupCommand).Elem() | ||
28 | + for _, validErr := range valid.Errors { | ||
29 | + field, isExist := elem.FieldByName(validErr.Field) | ||
30 | + if isExist { | ||
31 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
32 | + } else { | ||
33 | + return fmt.Errorf(validErr.Message) | ||
34 | + } | ||
35 | + } | ||
36 | + } | ||
37 | + return nil | ||
38 | +} |
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "reflect" | ||
6 | + "strings" | ||
7 | + | ||
8 | + "github.com/beego/beego/v2/core/validation" | ||
9 | +) | ||
10 | + | ||
11 | +type UpdateProductMaterialGroupCommand struct { | ||
12 | + // 物料分组ID | ||
13 | + ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"` | ||
14 | + // 物料分组名称 | ||
15 | + MaterialGroupName string `cname:"物料分组名称" json:"materialGroupName" valid:"Required"` | ||
16 | + // 物料分组编码 | ||
17 | + MaterialGroupNumber string `cname:"物料分组编码" json:"materialGroupNumber" valid:"Required"` | ||
18 | +} | ||
19 | + | ||
20 | +func (updateProductMaterialGroupCommand *UpdateProductMaterialGroupCommand) Valid(validation *validation.Validation) { | ||
21 | + | ||
22 | +} | ||
23 | + | ||
24 | +func (updateProductMaterialGroupCommand *UpdateProductMaterialGroupCommand) ValidateCommand() error { | ||
25 | + valid := validation.Validation{} | ||
26 | + b, err := valid.Valid(updateProductMaterialGroupCommand) | ||
27 | + if err != nil { | ||
28 | + return err | ||
29 | + } | ||
30 | + if !b { | ||
31 | + elem := reflect.TypeOf(updateProductMaterialGroupCommand).Elem() | ||
32 | + for _, validErr := range valid.Errors { | ||
33 | + field, isExist := elem.FieldByName(validErr.Field) | ||
34 | + if isExist { | ||
35 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
36 | + } else { | ||
37 | + return fmt.Errorf(validErr.Message) | ||
38 | + } | ||
39 | + } | ||
40 | + } | ||
41 | + return nil | ||
42 | +} |
1 | +package dto | ||
2 | + | ||
3 | +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
4 | + | ||
5 | +type MaterialGroupDto struct { | ||
6 | + // 物料分组ID | ||
7 | + ProductMaterialGroupId int `json:"id"` | ||
8 | + // 企业id | ||
9 | + //CompanyId int `json:"companyId"` | ||
10 | + // 组织ID | ||
11 | + //OrgId int `json:"orgId"` | ||
12 | + // 父级ID | ||
13 | + Pid int `json:"pid"` | ||
14 | + // 路径 (不使用,如果父级改变的话,子级的Path要做更新) | ||
15 | + //Path string `json:"path"` | ||
16 | + // 物料分组名称 | ||
17 | + MaterialGroupName string `json:"materialGroupName"` | ||
18 | + // 物料分组编码 | ||
19 | + MaterialGroupNumber string `json:"materialGroupNumber"` | ||
20 | +} | ||
21 | + | ||
22 | +func (d *MaterialGroupDto) LoadDto(m *domain.ProductMaterialGroup, orgId int) *MaterialGroupDto { | ||
23 | + d.ProductMaterialGroupId = m.ProductMaterialGroupId | ||
24 | + d.Pid = m.Pid | ||
25 | + d.MaterialGroupName = m.MaterialGroupName | ||
26 | + d.MaterialGroupNumber = m.MaterialGroupNumber | ||
27 | + return d | ||
28 | +} |
1 | +package query | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "reflect" | ||
6 | + "strings" | ||
7 | + | ||
8 | + "github.com/beego/beego/v2/core/validation" | ||
9 | +) | ||
10 | + | ||
11 | +type GetProductMaterialGroupQuery struct { | ||
12 | + // 物料分组ID | ||
13 | + ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"` | ||
14 | +} | ||
15 | + | ||
16 | +func (getProductMaterialGroupQuery *GetProductMaterialGroupQuery) Valid(validation *validation.Validation) { | ||
17 | + | ||
18 | +} | ||
19 | + | ||
20 | +func (getProductMaterialGroupQuery *GetProductMaterialGroupQuery) ValidateQuery() error { | ||
21 | + valid := validation.Validation{} | ||
22 | + b, err := valid.Valid(getProductMaterialGroupQuery) | ||
23 | + if err != nil { | ||
24 | + return err | ||
25 | + } | ||
26 | + if !b { | ||
27 | + elem := reflect.TypeOf(getProductMaterialGroupQuery).Elem() | ||
28 | + for _, validErr := range valid.Errors { | ||
29 | + field, isExist := elem.FieldByName(validErr.Field) | ||
30 | + if isExist { | ||
31 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
32 | + } else { | ||
33 | + return fmt.Errorf(validErr.Message) | ||
34 | + } | ||
35 | + } | ||
36 | + } | ||
37 | + return nil | ||
38 | +} |
1 | +package query | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "reflect" | ||
6 | + "strings" | ||
7 | + | ||
8 | + "github.com/beego/beego/v2/core/validation" | ||
9 | +) | ||
10 | + | ||
11 | +type ListProductMaterialGroupQuery struct { | ||
12 | + // 查询偏离量 | ||
13 | + Offset int `cname:"查询偏离量" json:"offset" valid:"Required"` | ||
14 | + // 查询限制 | ||
15 | + Limit int `cname:"查询限制" json:"limit" valid:"Required"` | ||
16 | +} | ||
17 | + | ||
18 | +func (listProductMaterialGroupQuery *ListProductMaterialGroupQuery) Valid(validation *validation.Validation) { | ||
19 | + | ||
20 | +} | ||
21 | + | ||
22 | +func (listProductMaterialGroupQuery *ListProductMaterialGroupQuery) ValidateQuery() error { | ||
23 | + valid := validation.Validation{} | ||
24 | + b, err := valid.Valid(listProductMaterialGroupQuery) | ||
25 | + if err != nil { | ||
26 | + return err | ||
27 | + } | ||
28 | + if !b { | ||
29 | + elem := reflect.TypeOf(listProductMaterialGroupQuery).Elem() | ||
30 | + for _, validErr := range valid.Errors { | ||
31 | + field, isExist := elem.FieldByName(validErr.Field) | ||
32 | + if isExist { | ||
33 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
34 | + } else { | ||
35 | + return fmt.Errorf(validErr.Message) | ||
36 | + } | ||
37 | + } | ||
38 | + } | ||
39 | + return nil | ||
40 | +} |
1 | +package query | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
6 | + "reflect" | ||
7 | + "strings" | ||
8 | + | ||
9 | + "github.com/beego/beego/v2/core/validation" | ||
10 | +) | ||
11 | + | ||
12 | +type SearchProductMaterialGroupQuery struct { | ||
13 | + // 查询偏离量 | ||
14 | + Offset int `cname:"查询偏离量" json:"offset"` | ||
15 | + // 查询限制 | ||
16 | + Limit int `cname:"查询限制" json:"limit"` | ||
17 | + | ||
18 | + // 当前公司 | ||
19 | + CompanyId int `cname:"当前公司" json:"companyId,omitempty" valid:"Required"` | ||
20 | + // 当前登录的组织 | ||
21 | + //OrgId int `cname:"当前登录的组织" json:"orgId,omitempty"` | ||
22 | + // 匹配多个组织 | ||
23 | + //InOrgIds []int `cname:"匹配多个组织" json:"inOrgIds,omitempty" valid:"Required"` | ||
24 | + // 页码 | ||
25 | + PageNumber int `cname:"页码" json:"pageNumber,omitempty"` | ||
26 | + // 页数 | ||
27 | + PageSize int `cname:"页数" json:"pageSize,omitempty"` | ||
28 | +} | ||
29 | + | ||
30 | +func (cmd *SearchProductMaterialGroupQuery) Valid(validation *validation.Validation) { | ||
31 | + cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize) | ||
32 | +} | ||
33 | + | ||
34 | +func (cmd *SearchProductMaterialGroupQuery) ValidateQuery() error { | ||
35 | + valid := validation.Validation{} | ||
36 | + b, err := valid.Valid(cmd) | ||
37 | + if err != nil { | ||
38 | + return err | ||
39 | + } | ||
40 | + if !b { | ||
41 | + elem := reflect.TypeOf(cmd).Elem() | ||
42 | + for _, validErr := range valid.Errors { | ||
43 | + field, isExist := elem.FieldByName(validErr.Field) | ||
44 | + if isExist { | ||
45 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
46 | + } else { | ||
47 | + return fmt.Errorf(validErr.Message) | ||
48 | + } | ||
49 | + } | ||
50 | + } | ||
51 | + return nil | ||
52 | +} |
1 | +package service | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "github.com/linmadan/egglib-go/core/application" | ||
6 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
7 | + "github.com/linmadan/egglib-go/utils/tool_funs" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/command" | ||
10 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/dto" | ||
11 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/query" | ||
12 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
13 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService" | ||
14 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils" | ||
15 | +) | ||
16 | + | ||
17 | +// 物料分组服务 | ||
18 | +type ProductMaterialGroupService struct { | ||
19 | +} | ||
20 | + | ||
21 | +// 创建物料分组服务 | ||
22 | +func (productMaterialGroupService *ProductMaterialGroupService) CreateProductMaterialGroup(operateInfo *domain.OperateInfo, createProductMaterialGroupCommand *command.CreateProductMaterialGroupCommand) (interface{}, error) { | ||
23 | + if err := createProductMaterialGroupCommand.ValidateCommand(); err != nil { | ||
24 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
25 | + } | ||
26 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
27 | + if err != nil { | ||
28 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
29 | + } | ||
30 | + if err := transactionContext.StartTransaction(); err != nil { | ||
31 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
32 | + } | ||
33 | + defer func() { | ||
34 | + transactionContext.RollbackTransaction() | ||
35 | + }() | ||
36 | + newProductMaterialGroup := &domain.ProductMaterialGroup{ | ||
37 | + Pid: createProductMaterialGroupCommand.Pid, | ||
38 | + MaterialGroupName: createProductMaterialGroupCommand.MaterialGroupName, | ||
39 | + MaterialGroupNumber: createProductMaterialGroupCommand.MaterialGroupNumber, | ||
40 | + } | ||
41 | + var productMaterialGroup *domain.ProductMaterialGroup | ||
42 | + materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext)) | ||
43 | + if productMaterialGroup, err = materialService.AddMaterialGroup(operateInfo, newProductMaterialGroup); err != nil { | ||
44 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
45 | + } | ||
46 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
47 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
48 | + } | ||
49 | + return productMaterialGroup, nil | ||
50 | +} | ||
51 | + | ||
52 | +// 返回物料分组服务 | ||
53 | +func (productMaterialGroupService *ProductMaterialGroupService) GetProductMaterialGroup(getProductMaterialGroupQuery *query.GetProductMaterialGroupQuery) (interface{}, error) { | ||
54 | + if err := getProductMaterialGroupQuery.ValidateQuery(); err != nil { | ||
55 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
56 | + } | ||
57 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
58 | + if err != nil { | ||
59 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
60 | + } | ||
61 | + if err := transactionContext.StartTransaction(); err != nil { | ||
62 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
63 | + } | ||
64 | + defer func() { | ||
65 | + transactionContext.RollbackTransaction() | ||
66 | + }() | ||
67 | + var productMaterialGroupRepository domain.ProductMaterialGroupRepository | ||
68 | + if value, err := factory.CreateProductMaterialGroupRepository(map[string]interface{}{ | ||
69 | + "transactionContext": transactionContext, | ||
70 | + }); err != nil { | ||
71 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
72 | + } else { | ||
73 | + productMaterialGroupRepository = value | ||
74 | + } | ||
75 | + productMaterialGroup, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"productMaterialGroupId": getProductMaterialGroupQuery.ProductMaterialGroupId}) | ||
76 | + if err != nil { | ||
77 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
78 | + } | ||
79 | + if productMaterialGroup == nil { | ||
80 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductMaterialGroupQuery.ProductMaterialGroupId))) | ||
81 | + } | ||
82 | + //测试 | ||
83 | + //materialService,_:= domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext)) | ||
84 | + //nodes,child,_:=materialService.AllMaterialGroupChild(&domain.OperateInfo{CompanyId: productMaterialGroup.CompanyId},productMaterialGroup.ProductMaterialGroupId) | ||
85 | + //log.Logger.Debug("节点的Child", map[string]interface{}{"data":child,"nodes":nodes}) | ||
86 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
87 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
88 | + } | ||
89 | + materialGroupDto := dto.MaterialGroupDto{} | ||
90 | + return materialGroupDto.LoadDto(productMaterialGroup, 0), nil | ||
91 | + | ||
92 | +} | ||
93 | + | ||
94 | +// 返回物料分组服务列表 | ||
95 | +func (productMaterialGroupService *ProductMaterialGroupService) ListProductMaterialGroup(listProductMaterialGroupQuery *query.ListProductMaterialGroupQuery) (interface{}, error) { | ||
96 | + if err := listProductMaterialGroupQuery.ValidateQuery(); err != nil { | ||
97 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
98 | + } | ||
99 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
100 | + if err != nil { | ||
101 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
102 | + } | ||
103 | + if err := transactionContext.StartTransaction(); err != nil { | ||
104 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
105 | + } | ||
106 | + defer func() { | ||
107 | + transactionContext.RollbackTransaction() | ||
108 | + }() | ||
109 | + var productMaterialGroupRepository domain.ProductMaterialGroupRepository | ||
110 | + if value, err := factory.CreateProductMaterialGroupRepository(map[string]interface{}{ | ||
111 | + "transactionContext": transactionContext, | ||
112 | + }); err != nil { | ||
113 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
114 | + } else { | ||
115 | + productMaterialGroupRepository = value | ||
116 | + } | ||
117 | + if count, productMaterialGroups, err := productMaterialGroupRepository.Find(tool_funs.SimpleStructToMap(listProductMaterialGroupQuery)); err != nil { | ||
118 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
119 | + } else { | ||
120 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
121 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
122 | + } | ||
123 | + return map[string]interface{}{ | ||
124 | + "count": count, | ||
125 | + "productMaterialGroups": productMaterialGroups, | ||
126 | + }, nil | ||
127 | + } | ||
128 | +} | ||
129 | + | ||
130 | +// 移除物料分组服务 | ||
131 | +func (productMaterialGroupService *ProductMaterialGroupService) RemoveProductMaterialGroup(removeProductMaterialGroupCommand *command.RemoveProductMaterialGroupCommand) (interface{}, error) { | ||
132 | + if err := removeProductMaterialGroupCommand.ValidateCommand(); err != nil { | ||
133 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
134 | + } | ||
135 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
136 | + if err != nil { | ||
137 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
138 | + } | ||
139 | + if err := transactionContext.StartTransaction(); err != nil { | ||
140 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
141 | + } | ||
142 | + defer func() { | ||
143 | + transactionContext.RollbackTransaction() | ||
144 | + }() | ||
145 | + var productMaterialGroupRepository domain.ProductMaterialGroupRepository | ||
146 | + if value, err := factory.CreateProductMaterialGroupRepository(map[string]interface{}{ | ||
147 | + "transactionContext": transactionContext, | ||
148 | + }); err != nil { | ||
149 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
150 | + } else { | ||
151 | + productMaterialGroupRepository = value | ||
152 | + } | ||
153 | + productMaterialGroup, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"productMaterialGroupId": removeProductMaterialGroupCommand.ProductMaterialGroupId}) | ||
154 | + if err != nil { | ||
155 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
156 | + } | ||
157 | + if productMaterialGroup == nil { | ||
158 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("已删除 %s", string(removeProductMaterialGroupCommand.ProductMaterialGroupId))) | ||
159 | + } | ||
160 | + if productMaterialGroup, err := productMaterialGroupRepository.Remove(productMaterialGroup); err != nil { | ||
161 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
162 | + } else { | ||
163 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
164 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
165 | + } | ||
166 | + return productMaterialGroup, nil | ||
167 | + } | ||
168 | +} | ||
169 | + | ||
170 | +// 更新物料分组服务 | ||
171 | +func (productMaterialGroupService *ProductMaterialGroupService) UpdateProductMaterialGroup(operateInfo *domain.OperateInfo, cmd *command.UpdateProductMaterialGroupCommand) (interface{}, error) { | ||
172 | + if err := cmd.ValidateCommand(); err != nil { | ||
173 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
174 | + } | ||
175 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
176 | + if err != nil { | ||
177 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
178 | + } | ||
179 | + if err := transactionContext.StartTransaction(); err != nil { | ||
180 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
181 | + } | ||
182 | + defer func() { | ||
183 | + transactionContext.RollbackTransaction() | ||
184 | + }() | ||
185 | + productMaterialGroup := &domain.ProductMaterialGroup{ | ||
186 | + ProductMaterialGroupId: cmd.ProductMaterialGroupId, | ||
187 | + MaterialGroupName: cmd.MaterialGroupName, | ||
188 | + MaterialGroupNumber: cmd.MaterialGroupNumber, | ||
189 | + } | ||
190 | + materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext)) | ||
191 | + if productMaterialGroup, err := materialService.UpdateMaterialGroup(operateInfo, productMaterialGroup); err != nil { | ||
192 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
193 | + } else { | ||
194 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
195 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
196 | + } | ||
197 | + materialGroupDto := dto.MaterialGroupDto{} | ||
198 | + return materialGroupDto.LoadDto(productMaterialGroup, 0), nil | ||
199 | + } | ||
200 | +} | ||
201 | + | ||
202 | +// 搜索物料分组服务列表 | ||
203 | +func (productMaterialGroupService *ProductMaterialGroupService) SearchProductMaterialGroup(operateInfo *domain.OperateInfo, cmd *query.SearchProductMaterialGroupQuery) (int64, interface{}, error) { | ||
204 | + if err := cmd.ValidateQuery(); err != nil { | ||
205 | + return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
206 | + } | ||
207 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
208 | + if err != nil { | ||
209 | + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
210 | + } | ||
211 | + if err := transactionContext.StartTransaction(); err != nil { | ||
212 | + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
213 | + } | ||
214 | + defer func() { | ||
215 | + transactionContext.RollbackTransaction() | ||
216 | + }() | ||
217 | + var productMaterialGroupRepository domain.ProductMaterialGroupRepository | ||
218 | + productMaterialGroupRepository, _, _ = factory.FastProductMaterialGroup(transactionContext, 0) | ||
219 | + queryOptions := utils.ObjectToMap(cmd) | ||
220 | + count, productGroups, err := productMaterialGroupRepository.Find(queryOptions) | ||
221 | + if err != nil { | ||
222 | + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
223 | + } | ||
224 | + var results = make([]*dto.MaterialGroupDto, 0) | ||
225 | + for i := range productGroups { | ||
226 | + newItem := &dto.MaterialGroupDto{} | ||
227 | + newItem.LoadDto(productGroups[i], operateInfo.OrgId) | ||
228 | + results = append(results, newItem) | ||
229 | + } | ||
230 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
231 | + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
232 | + } | ||
233 | + return count, results, nil | ||
234 | +} | ||
235 | + | ||
236 | +func NewProductMaterialGroupService(options map[string]interface{}) *ProductMaterialGroupService { | ||
237 | + newProductMaterialGroupService := &ProductMaterialGroupService{} | ||
238 | + return newProductMaterialGroupService | ||
239 | +} |
@@ -2,6 +2,9 @@ package domain | @@ -2,6 +2,9 @@ package domain | ||
2 | 2 | ||
3 | // 冗余附加数据 | 3 | // 冗余附加数据 |
4 | type Ext struct { | 4 | type Ext struct { |
5 | + // 操作人 | ||
6 | + Operator *User `json:"operator,omitempty"` | ||
7 | + | ||
5 | // 组织名称 | 8 | // 组织名称 |
6 | OrgName string `json:"orgName,omitempty"` | 9 | OrgName string `json:"orgName,omitempty"` |
7 | 10 | ||
@@ -35,3 +38,8 @@ func (e *Ext) WithProductPlanExt(ext *ProductPlanExt) *Ext { | @@ -35,3 +38,8 @@ func (e *Ext) WithProductPlanExt(ext *ProductPlanExt) *Ext { | ||
35 | e.ProductPlanExt = ext | 38 | e.ProductPlanExt = ext |
36 | return e | 39 | return e |
37 | } | 40 | } |
41 | + | ||
42 | +func (e *Ext) WithOperator(ext *User) *Ext { | ||
43 | + e.Operator = ext | ||
44 | + return e | ||
45 | +} |
pkg/domain/product_material_group.go
0 → 100644
1 | +package domain | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "time" | ||
6 | +) | ||
7 | + | ||
8 | +// 物料分组 | ||
9 | +type ProductMaterialGroup struct { | ||
10 | + // 物料分组ID | ||
11 | + ProductMaterialGroupId int `json:"productMaterialGroupId"` | ||
12 | + // 企业id | ||
13 | + CompanyId int `json:"companyId"` | ||
14 | + // 组织ID | ||
15 | + OrgId int `json:"orgId"` | ||
16 | + // 父级ID | ||
17 | + Pid int `json:"pid"` | ||
18 | + // 路径 (不使用,如果父级改变的话,子级的Path要做更新) | ||
19 | + //Path string `json:"path"` | ||
20 | + // 物料分组名称 | ||
21 | + MaterialGroupName string `json:"materialGroupName"` | ||
22 | + // 物料分组编码 | ||
23 | + MaterialGroupNumber string `json:"materialGroupNumber"` | ||
24 | + // 创建时间 | ||
25 | + CreatedAt time.Time `json:"createdAt"` | ||
26 | + // 更新时间 | ||
27 | + UpdatedAt time.Time `json:"updatedAt"` | ||
28 | + // 删除时间 | ||
29 | + DeletedAt time.Time `json:"deletedAt"` | ||
30 | + // 扩展数据 | ||
31 | + Ext *Ext `json:"ext,omitempty"` | ||
32 | +} | ||
33 | + | ||
34 | +type ProductMaterialGroupRepository interface { | ||
35 | + Save(productMaterialGroup *ProductMaterialGroup) (*ProductMaterialGroup, error) | ||
36 | + Remove(productMaterialGroup *ProductMaterialGroup) (*ProductMaterialGroup, error) | ||
37 | + FindOne(queryOptions map[string]interface{}) (*ProductMaterialGroup, error) | ||
38 | + Find(queryOptions map[string]interface{}) (int64, []*ProductMaterialGroup, error) | ||
39 | +} | ||
40 | + | ||
41 | +func (productMaterialGroup *ProductMaterialGroup) Identify() interface{} { | ||
42 | + if productMaterialGroup.ProductMaterialGroupId == 0 { | ||
43 | + return nil | ||
44 | + } | ||
45 | + return productMaterialGroup.ProductMaterialGroupId | ||
46 | +} | ||
47 | + | ||
48 | +func (productMaterialGroup *ProductMaterialGroup) Update(data map[string]interface{}) error { | ||
49 | + return nil | ||
50 | +} | ||
51 | + | ||
52 | +/***** 1.实现树 *****/ | ||
53 | + | ||
54 | +func (productMaterialGroup *ProductMaterialGroup) PID() string { | ||
55 | + return fmt.Sprintf("%d", productMaterialGroup.Pid) | ||
56 | +} | ||
57 | +func (productMaterialGroup *ProductMaterialGroup) ID() string { | ||
58 | + return fmt.Sprintf("%d", productMaterialGroup.ProductMaterialGroupId) | ||
59 | +} | ||
60 | + | ||
61 | +type ProductMaterialGroups []*ProductMaterialGroup | ||
62 | + | ||
63 | +func (tree ProductMaterialGroups) Len() int { | ||
64 | + return len(tree) | ||
65 | +} | ||
66 | + | ||
67 | +func (tree ProductMaterialGroups) Less(i, j int) bool { | ||
68 | + if tree[i].Pid < tree[j].Pid { | ||
69 | + return true | ||
70 | + } | ||
71 | + if tree[i].Pid == tree[j].Pid { | ||
72 | + return tree[i].ProductMaterialGroupId < tree[j].ProductMaterialGroupId | ||
73 | + } | ||
74 | + return false | ||
75 | +} | ||
76 | + | ||
77 | +func (tree ProductMaterialGroups) Swap(i, j int) { | ||
78 | + tree[i], tree[j] = tree[j], tree[i] | ||
79 | +} |
@@ -3,6 +3,6 @@ package domainService | @@ -3,6 +3,6 @@ package domainService | ||
3 | import "testing" | 3 | import "testing" |
4 | 4 | ||
5 | func TestSectionProductive(t *testing.T) { | 5 | func TestSectionProductive(t *testing.T) { |
6 | - svr := NewByteBankService() | ||
7 | - svr.SectionProductive() | 6 | + //svr := NewByteBankService() |
7 | + //svr.SectionProductive() | ||
8 | } | 8 | } |
1 | +package domainService | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils" | ||
9 | + "sort" | ||
10 | + "time" | ||
11 | +) | ||
12 | + | ||
13 | +type PGMaterialService struct { | ||
14 | + transactionContext *pgTransaction.TransactionContext | ||
15 | +} | ||
16 | + | ||
17 | +func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) { | ||
18 | + var ( | ||
19 | + user *domain.User | ||
20 | + org *domain.Org | ||
21 | + err error | ||
22 | + parent *domain.ProductMaterialGroup | ||
23 | + newProductMaterialGroup *domain.ProductMaterialGroup | ||
24 | + productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext) | ||
25 | + ) | ||
26 | + if user, err = NewUserService().User(opt.UserId); err != nil { | ||
27 | + return nil, err | ||
28 | + } | ||
29 | + if org, err = NewUserService().Organization(opt.OrgId); err != nil { | ||
30 | + return nil, err | ||
31 | + } | ||
32 | + if item.Pid != 0 { | ||
33 | + if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil { | ||
34 | + return nil, fmt.Errorf("上级物料分组不存在") | ||
35 | + } | ||
36 | + } | ||
37 | + if newProductMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || newProductMaterialGroup != nil { | ||
38 | + return nil, fmt.Errorf("物料分组编码已存在") | ||
39 | + } | ||
40 | + newProductMaterialGroup = &domain.ProductMaterialGroup{ | ||
41 | + CompanyId: opt.CompanyId, | ||
42 | + OrgId: opt.OrgId, | ||
43 | + Pid: item.Pid, | ||
44 | + //Path: item.GetPath(parent), | ||
45 | + MaterialGroupName: item.MaterialGroupName, | ||
46 | + MaterialGroupNumber: item.MaterialGroupNumber, | ||
47 | + CreatedAt: time.Now(), | ||
48 | + UpdatedAt: time.Now(), | ||
49 | + Ext: domain.NewExt(org.OrgName).WithOperator(user), | ||
50 | + } | ||
51 | + newProductMaterialGroup, err = productMaterialGroupRepository.Save(newProductMaterialGroup) | ||
52 | + return newProductMaterialGroup, err | ||
53 | +} | ||
54 | + | ||
55 | +func (ptr *PGMaterialService) UpdateMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) { | ||
56 | + var ( | ||
57 | + user *domain.User | ||
58 | + org *domain.Org | ||
59 | + err error | ||
60 | + parent *domain.ProductMaterialGroup | ||
61 | + productMaterialGroup *domain.ProductMaterialGroup | ||
62 | + productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext) | ||
63 | + ) | ||
64 | + if user, err = NewUserService().User(opt.UserId); err != nil { | ||
65 | + return nil, err | ||
66 | + } | ||
67 | + if org, err = NewUserService().Organization(opt.OrgId); err != nil { | ||
68 | + return nil, err | ||
69 | + } | ||
70 | + if item.ProductMaterialGroupId == item.Pid { | ||
71 | + return nil, fmt.Errorf("当前物料分组不能做为自己上级") | ||
72 | + } | ||
73 | + if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.ProductMaterialGroupId}); err != nil || productMaterialGroup == nil { | ||
74 | + return nil, fmt.Errorf("物料分组不存在") | ||
75 | + } | ||
76 | + if item.Pid != productMaterialGroup.ProductMaterialGroupId && item.Pid != 0 { | ||
77 | + if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil { | ||
78 | + return nil, fmt.Errorf("上级物料分组不存在") | ||
79 | + } | ||
80 | + } | ||
81 | + if productMaterialGroup.MaterialGroupNumber != item.MaterialGroupNumber { | ||
82 | + if group, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || group != nil { | ||
83 | + return nil, fmt.Errorf("物料分组编码已存在") | ||
84 | + } | ||
85 | + } | ||
86 | + | ||
87 | + productMaterialGroup.MaterialGroupNumber = item.MaterialGroupNumber | ||
88 | + productMaterialGroup.MaterialGroupName = item.MaterialGroupName | ||
89 | + productMaterialGroup.UpdatedAt = item.UpdatedAt | ||
90 | + productMaterialGroup.Ext = domain.NewExt(org.OrgName).WithOperator(user) | ||
91 | + | ||
92 | + productMaterialGroup, err = productMaterialGroupRepository.Save(productMaterialGroup) | ||
93 | + return productMaterialGroup, err | ||
94 | +} | ||
95 | + | ||
96 | +func (ptr *PGMaterialService) AllMaterialGroupChild(opt *domain.OperateInfo, productMaterialGroupId int) ([]*domain.ProductMaterialGroup, []int, error) { | ||
97 | + var ( | ||
98 | + err error | ||
99 | + listId []int | ||
100 | + productMaterialGroup *domain.ProductMaterialGroup | ||
101 | + productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext) | ||
102 | + groups domain.ProductMaterialGroups | ||
103 | + ) | ||
104 | + if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": productMaterialGroupId}); err != nil || productMaterialGroup == nil { | ||
105 | + return nil, listId, fmt.Errorf("物料分组不存在") | ||
106 | + } | ||
107 | + _, groups, err = productMaterialGroupRepository.Find(map[string]interface{}{"companyId": opt.CompanyId}) //,"orderByProductMaterialGroupId":"asc" | ||
108 | + if len(groups) == 0 || err != nil { | ||
109 | + return nil, listId, err | ||
110 | + } | ||
111 | + // 先排序 sort pid,id asc | ||
112 | + sort.Stable(groups) | ||
113 | + var treeNodes = make([]utils.TreeNode, len(groups)) | ||
114 | + for i := 0; i < len(groups); i++ { | ||
115 | + treeNodes[i] = groups[i] | ||
116 | + } | ||
117 | + tree := utils.NewTree(treeNodes) | ||
118 | + child := tree.AllChildNode(productMaterialGroup) | ||
119 | + var result = make([]*domain.ProductMaterialGroup, 0) | ||
120 | + for i := range child { | ||
121 | + if v, ok := child[i].(*domain.ProductMaterialGroup); ok { | ||
122 | + result = append(result, v) | ||
123 | + listId = append(listId, v.ProductMaterialGroupId) | ||
124 | + } | ||
125 | + } | ||
126 | + return result, listId, err | ||
127 | +} | ||
128 | + | ||
129 | +func (ptr *PGMaterialService) AllMaterialGroupParent(opt *domain.OperateInfo, productMaterialGroupId int) ([]*domain.ProductMaterialGroup, []int, error) { | ||
130 | + var ( | ||
131 | + err error | ||
132 | + listId []int | ||
133 | + productMaterialGroup *domain.ProductMaterialGroup | ||
134 | + productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext) | ||
135 | + groups domain.ProductMaterialGroups | ||
136 | + ) | ||
137 | + if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": productMaterialGroupId}); err != nil || productMaterialGroup == nil { | ||
138 | + return nil, listId, fmt.Errorf("物料分组不存在") | ||
139 | + } | ||
140 | + _, groups, err = productMaterialGroupRepository.Find(map[string]interface{}{"companyId": opt.CompanyId}) //,"orderByProductMaterialGroupId":"asc" | ||
141 | + if len(groups) == 0 || err != nil { | ||
142 | + return nil, listId, err | ||
143 | + } | ||
144 | + // 先排序 sort pid,id asc | ||
145 | + sort.Stable(groups) | ||
146 | + var treeNodes = make([]utils.TreeNode, len(groups)) | ||
147 | + for i := 0; i < len(groups); i++ { | ||
148 | + treeNodes[i] = groups[i] | ||
149 | + } | ||
150 | + tree := utils.NewTree(treeNodes) | ||
151 | + child := tree.TreeNodePaths(productMaterialGroup) | ||
152 | + var result = make([]*domain.ProductMaterialGroup, 0) | ||
153 | + for i := range child { | ||
154 | + if v, ok := child[i].(*domain.ProductMaterialGroup); ok { | ||
155 | + result = append(result, v) | ||
156 | + listId = append(listId, v.ProductMaterialGroupId) | ||
157 | + } | ||
158 | + } | ||
159 | + return result, listId, err | ||
160 | +} | ||
161 | + | ||
162 | +func NewPGMaterialService(transactionContext *pgTransaction.TransactionContext) (*PGMaterialService, error) { | ||
163 | + if transactionContext == nil { | ||
164 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
165 | + } else { | ||
166 | + return &PGMaterialService{ | ||
167 | + transactionContext: transactionContext, | ||
168 | + }, nil | ||
169 | + } | ||
170 | +} |
@@ -48,6 +48,7 @@ func init() { | @@ -48,6 +48,7 @@ func init() { | ||
48 | (*models.DeviceDailyRunningRecord)(nil), | 48 | (*models.DeviceDailyRunningRecord)(nil), |
49 | (*models.DeviceRunningRecord)(nil), | 49 | (*models.DeviceRunningRecord)(nil), |
50 | (*models.WorkshopPlanCompletionRecord)(nil), | 50 | (*models.WorkshopPlanCompletionRecord)(nil), |
51 | + (*models.ProductMaterialGroup)(nil), | ||
51 | } { | 52 | } { |
52 | err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ | 53 | err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ |
53 | Temp: false, | 54 | Temp: false, |
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
5 | + "time" | ||
6 | +) | ||
7 | + | ||
8 | +type ProductMaterialGroup struct { | ||
9 | + tableName string `comment:"物料分组" pg:"manufacture.product_material_group"` | ||
10 | + // 物料分组ID | ||
11 | + ProductMaterialGroupId int `comment:"物料分组ID" pg:"pk:product_material_group_id"` | ||
12 | + // 企业id | ||
13 | + CompanyId int `comment:"企业id"` | ||
14 | + // 组织ID | ||
15 | + OrgId int `comment:"组织ID"` | ||
16 | + // 父级ID | ||
17 | + Pid int `comment:"父级ID"` | ||
18 | + // 路径 | ||
19 | + // Path string `comment:"路径"` | ||
20 | + // 物料分组名称 | ||
21 | + MaterialGroupName string `comment:"物料分组名称"` | ||
22 | + // 物料分组编码 | ||
23 | + MaterialGroupNumber string `comment:"物料分组编码"` | ||
24 | + // 创建时间 | ||
25 | + CreatedAt time.Time `comment:"创建时间"` | ||
26 | + // 更新时间 | ||
27 | + UpdatedAt time.Time `comment:"更新时间"` | ||
28 | + // 删除时间 | ||
29 | + DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"` | ||
30 | + // 扩展 | ||
31 | + Ext *domain.Ext `comment:"扩展"` | ||
32 | +} |
1 | +package transform | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models" | ||
6 | +) | ||
7 | + | ||
8 | +func TransformToProductMaterialGroupDomainModelFromPgModels(productMaterialGroupModel *models.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) { | ||
9 | + return &domain.ProductMaterialGroup{ | ||
10 | + ProductMaterialGroupId: productMaterialGroupModel.ProductMaterialGroupId, | ||
11 | + CompanyId: productMaterialGroupModel.CompanyId, | ||
12 | + OrgId: productMaterialGroupModel.OrgId, | ||
13 | + Pid: productMaterialGroupModel.Pid, | ||
14 | + //Path: productMaterialGroupModel.Path, | ||
15 | + MaterialGroupName: productMaterialGroupModel.MaterialGroupName, | ||
16 | + MaterialGroupNumber: productMaterialGroupModel.MaterialGroupNumber, | ||
17 | + CreatedAt: productMaterialGroupModel.CreatedAt, | ||
18 | + UpdatedAt: productMaterialGroupModel.UpdatedAt, | ||
19 | + DeletedAt: productMaterialGroupModel.DeletedAt, | ||
20 | + Ext: productMaterialGroupModel.Ext, | ||
21 | + }, nil | ||
22 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "github.com/go-pg/pg/v10" | ||
6 | + | ||
7 | + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | ||
8 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
9 | + "github.com/linmadan/egglib-go/utils/snowflake" | ||
10 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
11 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models" | ||
12 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform" | ||
13 | +) | ||
14 | + | ||
15 | +type ProductMaterialGroupRepository struct { | ||
16 | + transactionContext *pgTransaction.TransactionContext | ||
17 | +} | ||
18 | + | ||
19 | +func (repository *ProductMaterialGroupRepository) nextIdentify() (int64, error) { | ||
20 | + IdWorker, err := snowflake.NewIdWorker(1) | ||
21 | + if err != nil { | ||
22 | + return 0, err | ||
23 | + } | ||
24 | + id, err := IdWorker.NextId() | ||
25 | + return id, err | ||
26 | +} | ||
27 | +func (repository *ProductMaterialGroupRepository) Save(productMaterialGroup *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) { | ||
28 | + sqlBuildFields := []string{ | ||
29 | + "product_material_group_id", | ||
30 | + "company_id", | ||
31 | + "org_id", | ||
32 | + "pid", | ||
33 | + //"path", | ||
34 | + "material_group_name", | ||
35 | + "material_group_number", | ||
36 | + "created_at", | ||
37 | + "updated_at", | ||
38 | + "deleted_at", | ||
39 | + "ext", | ||
40 | + } | ||
41 | + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_group_id", "deleted_at")) | ||
42 | + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_group_id", "deleted_at")) | ||
43 | + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) | ||
44 | + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_group_id", "deleted_at") | ||
45 | + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields) | ||
46 | + tx := repository.transactionContext.PgTx | ||
47 | + if productMaterialGroup.Identify() == nil { | ||
48 | + if _, err := tx.QueryOne( | ||
49 | + pg.Scan( | ||
50 | + &productMaterialGroup.ProductMaterialGroupId, | ||
51 | + &productMaterialGroup.CompanyId, | ||
52 | + &productMaterialGroup.OrgId, | ||
53 | + &productMaterialGroup.Pid, | ||
54 | + //&productMaterialGroup.Path, | ||
55 | + &productMaterialGroup.MaterialGroupName, | ||
56 | + &productMaterialGroup.MaterialGroupNumber, | ||
57 | + &productMaterialGroup.CreatedAt, | ||
58 | + &productMaterialGroup.UpdatedAt, | ||
59 | + &productMaterialGroup.DeletedAt, | ||
60 | + &productMaterialGroup.Ext, | ||
61 | + ), | ||
62 | + fmt.Sprintf("INSERT INTO manufacture.product_material_group (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), | ||
63 | + productMaterialGroup.CompanyId, | ||
64 | + productMaterialGroup.OrgId, | ||
65 | + productMaterialGroup.Pid, | ||
66 | + //productMaterialGroup.Path, | ||
67 | + productMaterialGroup.MaterialGroupName, | ||
68 | + productMaterialGroup.MaterialGroupNumber, | ||
69 | + productMaterialGroup.CreatedAt, | ||
70 | + productMaterialGroup.UpdatedAt, | ||
71 | + productMaterialGroup.Ext, | ||
72 | + ); err != nil { | ||
73 | + return productMaterialGroup, err | ||
74 | + } | ||
75 | + } else { | ||
76 | + if _, err := tx.QueryOne( | ||
77 | + pg.Scan( | ||
78 | + &productMaterialGroup.ProductMaterialGroupId, | ||
79 | + &productMaterialGroup.CompanyId, | ||
80 | + &productMaterialGroup.OrgId, | ||
81 | + &productMaterialGroup.Pid, | ||
82 | + //&productMaterialGroup.Path, | ||
83 | + &productMaterialGroup.MaterialGroupName, | ||
84 | + &productMaterialGroup.MaterialGroupNumber, | ||
85 | + &productMaterialGroup.CreatedAt, | ||
86 | + &productMaterialGroup.UpdatedAt, | ||
87 | + &productMaterialGroup.DeletedAt, | ||
88 | + &productMaterialGroup.Ext, | ||
89 | + ), | ||
90 | + fmt.Sprintf("UPDATE manufacture.product_material_group SET %s WHERE product_material_group_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
91 | + productMaterialGroup.CompanyId, | ||
92 | + productMaterialGroup.OrgId, | ||
93 | + productMaterialGroup.Pid, | ||
94 | + //productMaterialGroup.Path, | ||
95 | + productMaterialGroup.MaterialGroupName, | ||
96 | + productMaterialGroup.MaterialGroupNumber, | ||
97 | + productMaterialGroup.CreatedAt, | ||
98 | + productMaterialGroup.UpdatedAt, | ||
99 | + productMaterialGroup.Ext, | ||
100 | + productMaterialGroup.Identify(), | ||
101 | + ); err != nil { | ||
102 | + return productMaterialGroup, err | ||
103 | + } | ||
104 | + } | ||
105 | + return productMaterialGroup, nil | ||
106 | +} | ||
107 | +func (repository *ProductMaterialGroupRepository) Remove(productMaterialGroup *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) { | ||
108 | + tx := repository.transactionContext.PgTx | ||
109 | + productMaterialGroupModel := new(models.ProductMaterialGroup) | ||
110 | + productMaterialGroupModel.ProductMaterialGroupId = productMaterialGroup.Identify().(int) | ||
111 | + if _, err := tx.Model(productMaterialGroupModel).WherePK().Delete(); err != nil { | ||
112 | + return productMaterialGroup, err | ||
113 | + } | ||
114 | + return productMaterialGroup, nil | ||
115 | +} | ||
116 | +func (repository *ProductMaterialGroupRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProductMaterialGroup, error) { | ||
117 | + tx := repository.transactionContext.PgTx | ||
118 | + productMaterialGroupModel := new(models.ProductMaterialGroup) | ||
119 | + query := sqlbuilder.BuildQuery(tx.Model(productMaterialGroupModel), queryOptions) | ||
120 | + query.SetWhereByQueryOption("product_material_group_id = ?", "productMaterialGroupId") | ||
121 | + query.SetWhereByQueryOption("company_id = ?", "companyId") | ||
122 | + query.SetWhereByQueryOption("material_group_number = ?", "materialGroupNumber") | ||
123 | + if err := query.First(); err != nil { | ||
124 | + if err.Error() == "pg: no rows in result set" { | ||
125 | + return nil, domain.ErrorNotFound | ||
126 | + } else { | ||
127 | + return nil, err | ||
128 | + } | ||
129 | + } | ||
130 | + if productMaterialGroupModel.ProductMaterialGroupId == 0 { | ||
131 | + return nil, nil | ||
132 | + } else { | ||
133 | + return transform.TransformToProductMaterialGroupDomainModelFromPgModels(productMaterialGroupModel) | ||
134 | + } | ||
135 | +} | ||
136 | +func (repository *ProductMaterialGroupRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ProductMaterialGroup, error) { | ||
137 | + tx := repository.transactionContext.PgTx | ||
138 | + var productMaterialGroupModels []*models.ProductMaterialGroup | ||
139 | + productMaterialGroups := make([]*domain.ProductMaterialGroup, 0) | ||
140 | + query := sqlbuilder.BuildQuery(tx.Model(&productMaterialGroupModels), queryOptions) | ||
141 | + query.SetOffsetAndLimit(domain.MaxQueryRow) | ||
142 | + if v, ok := queryOptions["orderByProductMaterialGroupId"]; ok { | ||
143 | + query.SetOrderDirect("product_material_group_id", v.(string)) | ||
144 | + } else { | ||
145 | + query.SetOrderDirect("product_material_group_id", "DESC") | ||
146 | + } | ||
147 | + if count, err := query.SelectAndCount(); err != nil { | ||
148 | + return 0, productMaterialGroups, err | ||
149 | + } else { | ||
150 | + for _, productMaterialGroupModel := range productMaterialGroupModels { | ||
151 | + if productMaterialGroup, err := transform.TransformToProductMaterialGroupDomainModelFromPgModels(productMaterialGroupModel); err != nil { | ||
152 | + return 0, productMaterialGroups, err | ||
153 | + } else { | ||
154 | + productMaterialGroups = append(productMaterialGroups, productMaterialGroup) | ||
155 | + } | ||
156 | + } | ||
157 | + return int64(count), productMaterialGroups, nil | ||
158 | + } | ||
159 | +} | ||
160 | +func NewProductMaterialGroupRepository(transactionContext *pgTransaction.TransactionContext) (*ProductMaterialGroupRepository, error) { | ||
161 | + if transactionContext == nil { | ||
162 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
163 | + } else { | ||
164 | + return &ProductMaterialGroupRepository{ | ||
165 | + transactionContext: transactionContext, | ||
166 | + }, nil | ||
167 | + } | ||
168 | +} |
1 | +package controllers | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/linmadan/egglib-go/web/beego" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/command" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/query" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/service" | ||
8 | +) | ||
9 | + | ||
10 | +type ProductMaterialGroupController struct { | ||
11 | + beego.BaseController | ||
12 | +} | ||
13 | + | ||
14 | +func (controller *ProductMaterialGroupController) CreateProductMaterialGroup() { | ||
15 | + productMaterialGroupService := service.NewProductMaterialGroupService(nil) | ||
16 | + cmd := &command.CreateProductMaterialGroupCommand{} | ||
17 | + controller.Unmarshal(cmd) | ||
18 | + operateInfo := ParseOperateInfo(controller.BaseController) | ||
19 | + cmd.CompanyId = operateInfo.CompanyId | ||
20 | + cmd.OrgId = operateInfo.OrgId | ||
21 | + data, err := productMaterialGroupService.CreateProductMaterialGroup(operateInfo, cmd) | ||
22 | + controller.Response(data, err) | ||
23 | +} | ||
24 | + | ||
25 | +func (controller *ProductMaterialGroupController) UpdateProductMaterialGroup() { | ||
26 | + productMaterialGroupService := service.NewProductMaterialGroupService(nil) | ||
27 | + updateProductMaterialGroupCommand := &command.UpdateProductMaterialGroupCommand{} | ||
28 | + controller.Unmarshal(updateProductMaterialGroupCommand) | ||
29 | + productMaterialGroupId, _ := controller.GetInt(":productMaterialGroupId") | ||
30 | + updateProductMaterialGroupCommand.ProductMaterialGroupId = productMaterialGroupId | ||
31 | + operateInfo := ParseOperateInfo(controller.BaseController) | ||
32 | + data, err := productMaterialGroupService.UpdateProductMaterialGroup(operateInfo, updateProductMaterialGroupCommand) | ||
33 | + controller.Response(data, err) | ||
34 | +} | ||
35 | + | ||
36 | +func (controller *ProductMaterialGroupController) GetProductMaterialGroup() { | ||
37 | + productMaterialGroupService := service.NewProductMaterialGroupService(nil) | ||
38 | + getProductMaterialGroupQuery := &query.GetProductMaterialGroupQuery{} | ||
39 | + productMaterialGroupId, _ := controller.GetInt(":productMaterialGroupId") | ||
40 | + getProductMaterialGroupQuery.ProductMaterialGroupId = productMaterialGroupId | ||
41 | + data, err := productMaterialGroupService.GetProductMaterialGroup(getProductMaterialGroupQuery) | ||
42 | + controller.Response(data, err) | ||
43 | +} | ||
44 | + | ||
45 | +func (controller *ProductMaterialGroupController) RemoveProductMaterialGroup() { | ||
46 | + productMaterialGroupService := service.NewProductMaterialGroupService(nil) | ||
47 | + removeProductMaterialGroupCommand := &command.RemoveProductMaterialGroupCommand{} | ||
48 | + controller.Unmarshal(removeProductMaterialGroupCommand) | ||
49 | + productMaterialGroupId, _ := controller.GetInt(":productMaterialGroupId") | ||
50 | + removeProductMaterialGroupCommand.ProductMaterialGroupId = productMaterialGroupId | ||
51 | + data, err := productMaterialGroupService.RemoveProductMaterialGroup(removeProductMaterialGroupCommand) | ||
52 | + controller.Response(data, err) | ||
53 | +} | ||
54 | + | ||
55 | +func (controller *ProductMaterialGroupController) ListProductMaterialGroup() { | ||
56 | + productMaterialGroupService := service.NewProductMaterialGroupService(nil) | ||
57 | + listProductMaterialGroupQuery := &query.ListProductMaterialGroupQuery{} | ||
58 | + offset, _ := controller.GetInt("offset") | ||
59 | + listProductMaterialGroupQuery.Offset = offset | ||
60 | + limit, _ := controller.GetInt("limit") | ||
61 | + listProductMaterialGroupQuery.Limit = limit | ||
62 | + data, err := productMaterialGroupService.ListProductMaterialGroup(listProductMaterialGroupQuery) | ||
63 | + controller.Response(data, err) | ||
64 | +} | ||
65 | + | ||
66 | +func (controller *ProductMaterialGroupController) SearchProductMaterialGroup() { | ||
67 | + productMaterialGroupService := service.NewProductMaterialGroupService(nil) | ||
68 | + cmd := &query.SearchProductMaterialGroupQuery{} | ||
69 | + offset, _ := controller.GetInt("offset") | ||
70 | + cmd.Offset = offset | ||
71 | + limit, _ := controller.GetInt("limit") | ||
72 | + cmd.Limit = limit | ||
73 | + operateInfo := ParseOperateInfo(controller.BaseController) | ||
74 | + cmd.CompanyId = operateInfo.CompanyId | ||
75 | + total, data, err := productMaterialGroupService.SearchProductMaterialGroup(ParseOperateInfo(controller.BaseController), cmd) | ||
76 | + ResponseGrid(controller.BaseController, total, data, err) | ||
77 | +} | ||
78 | + | ||
79 | +func (controller *ProductMaterialGroupController) SelectorProductMaterialGroup() { | ||
80 | + productMaterialGroupService := service.NewProductMaterialGroupService(nil) | ||
81 | + cmd := &query.SearchProductMaterialGroupQuery{} | ||
82 | + offset, _ := controller.GetInt("offset") | ||
83 | + cmd.Offset = offset | ||
84 | + limit, _ := controller.GetInt("limit") | ||
85 | + cmd.Limit = limit | ||
86 | + operateInfo := ParseOperateInfo(controller.BaseController) | ||
87 | + cmd.CompanyId = operateInfo.CompanyId | ||
88 | + count, data, err := productMaterialGroupService.SearchProductMaterialGroup(ParseOperateInfo(controller.BaseController), cmd) | ||
89 | + controller.Response(map[string]interface{}{ | ||
90 | + "groups": data, | ||
91 | + "count": count, | ||
92 | + }, err) | ||
93 | +} |
1 | +package routers | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/beego/beego/v2/server/web" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego/controllers" | ||
6 | +) | ||
7 | + | ||
8 | +func init() { | ||
9 | + web.Router("/product-material-groups/", &controllers.ProductMaterialGroupController{}, "Post:CreateProductMaterialGroup") | ||
10 | + web.Router("/product-material-groups/:productMaterialGroupId", &controllers.ProductMaterialGroupController{}, "Put:UpdateProductMaterialGroup") | ||
11 | + web.Router("/product-material-groups/:productMaterialGroupId", &controllers.ProductMaterialGroupController{}, "Get:GetProductMaterialGroup") | ||
12 | + web.Router("/product-material-groups/:productMaterialGroupId", &controllers.ProductMaterialGroupController{}, "Delete:RemoveProductMaterialGroup") | ||
13 | + web.Router("/product-material-groups/", &controllers.ProductMaterialGroupController{}, "Get:ListProductMaterialGroup") | ||
14 | + web.Router("/product-material-groups/search", &controllers.ProductMaterialGroupController{}, "Post:SearchProductMaterialGroup") | ||
15 | + web.Router("/product-material-groups/selector", &controllers.ProductMaterialGroupController{}, "Post:SelectorProductMaterialGroup") | ||
16 | +} |
1 | +package product_material_group | ||
2 | + | ||
3 | +import ( | ||
4 | + "net/http" | ||
5 | + | ||
6 | + "github.com/gavv/httpexpect" | ||
7 | + . "github.com/onsi/ginkgo" | ||
8 | + . "github.com/onsi/gomega" | ||
9 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
10 | +) | ||
11 | + | ||
12 | +var _ = Describe("创建物料分组服务", func() { | ||
13 | + Describe("提交数据创建物料分组服务", func() { | ||
14 | + Context("提交正确的新物料分组数据", func() { | ||
15 | + It("返回物料分组数据", func() { | ||
16 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
17 | + body := map[string]interface{}{ | ||
18 | + "pid": "int", | ||
19 | + "materialGroupName": "string", | ||
20 | + "materialGroupNumber": "string", | ||
21 | + } | ||
22 | + httpExpect.POST("/product-material-groups/"). | ||
23 | + WithJSON(body). | ||
24 | + Expect(). | ||
25 | + Status(http.StatusOK). | ||
26 | + JSON(). | ||
27 | + Object(). | ||
28 | + ContainsKey("code").ValueEqual("code", 0). | ||
29 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
30 | + ContainsKey("data").Value("data").Object(). | ||
31 | + ContainsKey("productMaterialGroupId").ValueNotEqual("productMaterialGroupId", BeZero()) | ||
32 | + }) | ||
33 | + }) | ||
34 | + }) | ||
35 | + AfterEach(func() { | ||
36 | + _, err := pG.DB.Exec("DELETE FROM product_material_groups WHERE true") | ||
37 | + Expect(err).NotTo(HaveOccurred()) | ||
38 | + }) | ||
39 | +}) |
1 | +package product_material_group | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/go-pg/pg/v10" | ||
5 | + "net/http" | ||
6 | + | ||
7 | + "github.com/gavv/httpexpect" | ||
8 | + . "github.com/onsi/ginkgo" | ||
9 | + . "github.com/onsi/gomega" | ||
10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
11 | +) | ||
12 | + | ||
13 | +var _ = Describe("返回物料分组服务", func() { | ||
14 | + var productMaterialGroupId int64 | ||
15 | + BeforeEach(func() { | ||
16 | + _, err := pG.DB.QueryOne( | ||
17 | + pg.Scan(&productMaterialGroupId), | ||
18 | + "INSERT INTO product_material_groups (product_material_group_id, company_id, org_id, pid, path, material_group_name, material_group_number, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_group_id", | ||
19 | + "testProductMaterialGroupId", "testCompanyId", "testOrgId", "testPid", "testPath", "testMaterialGroupName", "testMaterialGroupNumber", "testCreatedAt", "testUpdatedAt", "testDeletedAt") | ||
20 | + Expect(err).NotTo(HaveOccurred()) | ||
21 | + }) | ||
22 | + Describe("根据productMaterialGroupId参数返回物料分组", func() { | ||
23 | + Context("传入有效的productMaterialGroupId", func() { | ||
24 | + It("返回物料分组数据", func() { | ||
25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
26 | + httpExpect.GET("/product-material-groups/{productMaterialGroupId}"). | ||
27 | + Expect(). | ||
28 | + Status(http.StatusOK). | ||
29 | + JSON(). | ||
30 | + Object(). | ||
31 | + ContainsKey("code").ValueEqual("code", 0). | ||
32 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
33 | + ContainsKey("data").Value("data").Object() | ||
34 | + }) | ||
35 | + }) | ||
36 | + }) | ||
37 | + AfterEach(func() { | ||
38 | + _, err := pG.DB.Exec("DELETE FROM product_material_groups WHERE true") | ||
39 | + Expect(err).NotTo(HaveOccurred()) | ||
40 | + }) | ||
41 | +}) |
1 | +package product_material_group | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/go-pg/pg/v10" | ||
5 | + "net/http" | ||
6 | + | ||
7 | + "github.com/gavv/httpexpect" | ||
8 | + . "github.com/onsi/ginkgo" | ||
9 | + . "github.com/onsi/gomega" | ||
10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
11 | +) | ||
12 | + | ||
13 | +var _ = Describe("返回物料分组服务列表", func() { | ||
14 | + var productMaterialGroupId int64 | ||
15 | + BeforeEach(func() { | ||
16 | + _, err := pG.DB.QueryOne( | ||
17 | + pg.Scan(&productMaterialGroupId), | ||
18 | + "INSERT INTO product_material_groups (product_material_group_id, company_id, org_id, pid, path, material_group_name, material_group_number, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_group_id", | ||
19 | + "testProductMaterialGroupId", "testCompanyId", "testOrgId", "testPid", "testPath", "testMaterialGroupName", "testMaterialGroupNumber", "testCreatedAt", "testUpdatedAt", "testDeletedAt") | ||
20 | + Expect(err).NotTo(HaveOccurred()) | ||
21 | + }) | ||
22 | + Describe("根据参数返回物料分组列表", func() { | ||
23 | + Context("传入有效的参数", func() { | ||
24 | + It("返回物料分组数据列表", func() { | ||
25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
26 | + httpExpect.GET("/product-material-groups/"). | ||
27 | + WithQuery("offset", "int"). | ||
28 | + WithQuery("limit", "int"). | ||
29 | + Expect(). | ||
30 | + Status(http.StatusOK). | ||
31 | + JSON(). | ||
32 | + Object(). | ||
33 | + ContainsKey("code").ValueEqual("code", 0). | ||
34 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
35 | + ContainsKey("data").Value("data").Object(). | ||
36 | + ContainsKey("count").ValueEqual("count", 1). | ||
37 | + ContainsKey("productMaterialGroups").Value("productMaterialGroups").Array() | ||
38 | + }) | ||
39 | + }) | ||
40 | + }) | ||
41 | + AfterEach(func() { | ||
42 | + _, err := pG.DB.Exec("DELETE FROM product_material_groups WHERE true") | ||
43 | + Expect(err).NotTo(HaveOccurred()) | ||
44 | + }) | ||
45 | +}) |
1 | +package product_material_group | ||
2 | + | ||
3 | +import ( | ||
4 | + "net/http" | ||
5 | + "net/http/httptest" | ||
6 | + "testing" | ||
7 | + | ||
8 | + "github.com/beego/beego/v2/server/web" | ||
9 | + . "github.com/onsi/ginkgo" | ||
10 | + . "github.com/onsi/gomega" | ||
11 | + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application" | ||
12 | + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
13 | + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego" | ||
14 | +) | ||
15 | + | ||
16 | +func TestProductMaterialGroup(t *testing.T) { | ||
17 | + RegisterFailHandler(Fail) | ||
18 | + RunSpecs(t, "Beego Port ProductMaterialGroup Correlations Test Case Suite") | ||
19 | +} | ||
20 | + | ||
21 | +var handler http.Handler | ||
22 | +var server *httptest.Server | ||
23 | + | ||
24 | +var _ = BeforeSuite(func() { | ||
25 | + handler = web.BeeApp.Handlers | ||
26 | + server = httptest.NewServer(handler) | ||
27 | +}) | ||
28 | + | ||
29 | +var _ = AfterSuite(func() { | ||
30 | + server.Close() | ||
31 | +}) |
1 | +package product_material_group | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/go-pg/pg/v10" | ||
5 | + "net/http" | ||
6 | + | ||
7 | + "github.com/gavv/httpexpect" | ||
8 | + . "github.com/onsi/ginkgo" | ||
9 | + . "github.com/onsi/gomega" | ||
10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
11 | +) | ||
12 | + | ||
13 | +var _ = Describe("移除物料分组服务", func() { | ||
14 | + var productMaterialGroupId int64 | ||
15 | + BeforeEach(func() { | ||
16 | + _, err := pG.DB.QueryOne( | ||
17 | + pg.Scan(&productMaterialGroupId), | ||
18 | + "INSERT INTO product_material_groups (product_material_group_id, company_id, org_id, pid, path, material_group_name, material_group_number, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_group_id", | ||
19 | + "testProductMaterialGroupId", "testCompanyId", "testOrgId", "testPid", "testPath", "testMaterialGroupName", "testMaterialGroupNumber", "testCreatedAt", "testUpdatedAt", "testDeletedAt") | ||
20 | + Expect(err).NotTo(HaveOccurred()) | ||
21 | + }) | ||
22 | + Describe("根据参数移除物料分组服务", func() { | ||
23 | + Context("传入有效的productMaterialGroupId", func() { | ||
24 | + It("返回被移除物料分组的数据", func() { | ||
25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
26 | + httpExpect.DELETE("/product-material-groups/{productMaterialGroupId}"). | ||
27 | + Expect(). | ||
28 | + Status(http.StatusOK). | ||
29 | + JSON(). | ||
30 | + Object(). | ||
31 | + ContainsKey("code").ValueEqual("code", 0). | ||
32 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
33 | + ContainsKey("data").Value("data").Object() | ||
34 | + }) | ||
35 | + }) | ||
36 | + }) | ||
37 | + AfterEach(func() { | ||
38 | + _, err := pG.DB.Exec("DELETE FROM product_material_groups WHERE true") | ||
39 | + Expect(err).NotTo(HaveOccurred()) | ||
40 | + }) | ||
41 | +}) |
1 | +package product_material_group | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/go-pg/pg/v10" | ||
5 | + "net/http" | ||
6 | + | ||
7 | + "github.com/gavv/httpexpect" | ||
8 | + . "github.com/onsi/ginkgo" | ||
9 | + . "github.com/onsi/gomega" | ||
10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
11 | +) | ||
12 | + | ||
13 | +var _ = Describe("更新物料分组服务", func() { | ||
14 | + var productMaterialGroupId int64 | ||
15 | + BeforeEach(func() { | ||
16 | + _, err := pG.DB.QueryOne( | ||
17 | + pg.Scan(&productMaterialGroupId), | ||
18 | + "INSERT INTO product_material_groups (product_material_group_id, company_id, org_id, pid, path, material_group_name, material_group_number, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_group_id", | ||
19 | + "testProductMaterialGroupId", "testCompanyId", "testOrgId", "testPid", "testPath", "testMaterialGroupName", "testMaterialGroupNumber", "testCreatedAt", "testUpdatedAt", "testDeletedAt") | ||
20 | + Expect(err).NotTo(HaveOccurred()) | ||
21 | + }) | ||
22 | + Describe("提交数据更新物料分组服务", func() { | ||
23 | + Context("提交正确的物料分组数据", func() { | ||
24 | + It("返回更新后的物料分组数据", func() { | ||
25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
26 | + body := map[string]interface{}{ | ||
27 | + "materialGroupName": "string", | ||
28 | + "materialGroupNumber": "string", | ||
29 | + } | ||
30 | + httpExpect.PUT("/product-material-groups/{productMaterialGroupId}"). | ||
31 | + WithJSON(body). | ||
32 | + Expect(). | ||
33 | + Status(http.StatusOK). | ||
34 | + JSON(). | ||
35 | + Object(). | ||
36 | + ContainsKey("code").ValueEqual("code", 0). | ||
37 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
38 | + ContainsKey("data").Value("data").Object(). | ||
39 | + ContainsKey("productMaterialGroupId").ValueEqual("productMaterialGroupId", productMaterialGroupId) | ||
40 | + }) | ||
41 | + }) | ||
42 | + }) | ||
43 | + AfterEach(func() { | ||
44 | + _, err := pG.DB.Exec("DELETE FROM product_material_groups WHERE true") | ||
45 | + Expect(err).NotTo(HaveOccurred()) | ||
46 | + }) | ||
47 | +}) |
-
请 注册 或 登录 后发表评论