作者 13655977079

feat:增加物料管理

@@ -2,17 +2,16 @@ package command @@ -2,17 +2,16 @@ package command
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "github.com/beego/beego/v2/core/validation"
5 "reflect" 6 "reflect"
6 "strings" 7 "strings"
7 -  
8 - "github.com/beego/beego/v2/core/validation"  
9 ) 8 )
10 9
11 type CreateProductMaterialCommand struct { 10 type CreateProductMaterialCommand struct {
12 // 企业id 11 // 企业id
13 - CompanyId int `cname:"企业id" json:"companyId" valid:"Required"` 12 + CompanyId int `cname:"企业id" json:"companyId"`
14 // 组织ID 13 // 组织ID
15 - OrgId int `cname:"组织ID" json:"orgId" valid:"Required"` 14 + OrgId int `cname:"组织ID" json:"orgId"`
16 // 物料分组ID 15 // 物料分组ID
17 ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"` 16 ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"`
18 // 物料编码 17 // 物料编码
@@ -24,9 +23,9 @@ type CreateProductMaterialCommand struct { @@ -24,9 +23,9 @@ type CreateProductMaterialCommand struct {
24 // 物料类别 23 // 物料类别
25 MaterialCategory string `cname:"物料类别" json:"materialCategory" valid:"Required"` 24 MaterialCategory string `cname:"物料类别" json:"materialCategory" valid:"Required"`
26 // 规格 25 // 规格
27 - Specification string `cname:"规格" json:"specification"` 26 + Specification string `cname:"规格" json:"specification" valid:"Required"`
28 // 单位 27 // 单位
29 - Unit string `cname:"单位" json:"unit"` 28 + Unit string `cname:"单位" json:"unit" valid:"Required"`
30 // 保质期 单位:天 29 // 保质期 单位:天
31 ExpiredDay int `cname:"保质期 单位:天" json:"expiredDay"` 30 ExpiredDay int `cname:"保质期 单位:天" json:"expiredDay"`
32 // 备注 31 // 备注
@@ -10,9 +10,9 @@ import ( @@ -10,9 +10,9 @@ import (
10 10
11 type UpdateProductMaterialCommand struct { 11 type UpdateProductMaterialCommand struct {
12 // 企业id 12 // 企业id
13 - CompanyId int `cname:"企业id" json:"companyId"` 13 + //CompanyId int `cname:"企业id" json:"companyId"`
14 // 组织ID 14 // 组织ID
15 - OrgId int `cname:"组织ID" json:"orgId"` 15 + //OrgId int `cname:"组织ID" json:"orgId"`
16 // 物料ID 16 // 物料ID
17 ProductMaterialId int `cname:"物料ID" json:"productMaterialId" valid:"Required"` 17 ProductMaterialId int `cname:"物料ID" json:"productMaterialId" valid:"Required"`
18 // 物料分组ID 18 // 物料分组ID
1 package dto 1 package dto
2 2
3 -import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"  
4 -  
5 type DtoMaterial struct { 3 type DtoMaterial struct {
6 ProductMaterialId int `json:"productMaterialId"` // 物料ID 4 ProductMaterialId int `json:"productMaterialId"` // 物料ID
7 MaterialNumber string `json:"materialNumber"` //物料编码 5 MaterialNumber string `json:"materialNumber"` //物料编码
@@ -13,5 +11,5 @@ type DtoMaterial struct { @@ -13,5 +11,5 @@ type DtoMaterial struct {
13 ProductMaterialGroupId int `json:"productMaterialGroupId"` //物料分组 string/string/string 11 ProductMaterialGroupId int `json:"productMaterialGroupId"` //物料分组 string/string/string
14 ExpiredDay int `json:"expiredDay"` //保质期 12 ExpiredDay int `json:"expiredDay"` //保质期
15 Remark string `json:"remark"` //备注 13 Remark string `json:"remark"` //备注
16 - ProductMaterialGroupIdName []*domain.ProductMaterialGroup 14 + ProductMaterialGroups []int `json:"productMaterialGroups"`
17 } 15 }
  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 SearchProductMaterialQuery struct {
  12 + PageNumber int64 `json:"pageNumber"`
  13 + PageSize int64 `json:"pageSize"`
  14 + // 物料分组ID
  15 + ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId"`
  16 + // 物料分组ID数组
  17 + ProductMaterialGroupIds []int
  18 + // 物料名称
  19 + MaterialName string `cname:"物料名称" json:"materialName"`
  20 + // 物料类别
  21 + MaterialCategory string `cname:"物料类别" json:"materialCategory"`
  22 + CompanyId int
  23 +}
  24 +
  25 +func (listProductMaterialQuery *SearchProductMaterialQuery) Valid(validation *validation.Validation) {
  26 + //validation.SetError("CustomValid", "未实现的自定义认证")
  27 +}
  28 +
  29 +func (listProductMaterialQuery *SearchProductMaterialQuery) ValidateQuery() error {
  30 + valid := validation.Validation{}
  31 + b, err := valid.Valid(listProductMaterialQuery)
  32 + if err != nil {
  33 + return err
  34 + }
  35 + if !b {
  36 + elem := reflect.TypeOf(listProductMaterialQuery).Elem()
  37 + for _, validErr := range valid.Errors {
  38 + field, isExist := elem.FieldByName(validErr.Field)
  39 + if isExist {
  40 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  41 + } else {
  42 + return fmt.Errorf(validErr.Message)
  43 + }
  44 + }
  45 + }
  46 + return nil
  47 +}
@@ -11,6 +11,7 @@ import ( @@ -11,6 +11,7 @@ import (
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/query" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/query"
12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
13 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService" 13 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
  14 + "time"
14 ) 15 )
15 16
16 // 生产物料服务 17 // 生产物料服务
@@ -90,15 +91,19 @@ func (productMaterialService *ProductMaterialService) GetProductMaterial(operate @@ -90,15 +91,19 @@ func (productMaterialService *ProductMaterialService) GetProductMaterial(operate
90 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductMaterialQuery.ProductMaterialId))) 91 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductMaterialQuery.ProductMaterialId)))
91 } else { 92 } else {
92 materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext)) 93 materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
93 - ProductMaterialGroupIdName, _, err := materialService.AllMaterialGroupParent(operateInfo, productMaterial.ProductMaterialGroupId) 94 + productMaterialGroupIdNames, _, err := materialService.AllMaterialGroupParent(operateInfo, productMaterial.ProductMaterialGroupId)
94 if err != nil { 95 if err != nil {
95 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 96 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
96 } 97 }
97 - if len(ProductMaterialGroupIdName) == 0 { 98 + if len(productMaterialGroupIdNames) == 0 {
98 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 99 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
99 } 100 }
  101 + var productMaterialGroups []int
  102 + for _, productMaterialGroupIdName := range productMaterialGroupIdNames {
  103 + productMaterialGroups = append(productMaterialGroups, productMaterialGroupIdName.ProductMaterialGroupId)
  104 + }
100 one := dto.DtoProductMaterial(productMaterial) 105 one := dto.DtoProductMaterial(productMaterial)
101 - one.ProductMaterialGroupIdName = ProductMaterialGroupIdName 106 + one.ProductMaterialGroups = productMaterialGroups
102 if err := transactionContext.CommitTransaction(); err != nil { 107 if err := transactionContext.CommitTransaction(); err != nil {
103 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 108 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
104 } 109 }
@@ -156,7 +161,7 @@ func (productMaterialService *ProductMaterialService) ListProductMaterial(operat @@ -156,7 +161,7 @@ func (productMaterialService *ProductMaterialService) ListProductMaterial(operat
156 } 161 }
157 162
158 // 返回生产物料服务列表 163 // 返回生产物料服务列表
159 -func (productMaterialService *ProductMaterialService) SearchProductMaterial(operateInfo *domain.OperateInfo, listProductMaterialQuery *query.ListProductMaterialQuery) (int64, []*dto.DtoMaterial, error) { 164 +func (productMaterialService *ProductMaterialService) SearchProductMaterial(operateInfo *domain.OperateInfo, listProductMaterialQuery *query.SearchProductMaterialQuery) (int64, []*dto.DtoMaterial, error) {
160 if err := listProductMaterialQuery.ValidateQuery(); err != nil { 165 if err := listProductMaterialQuery.ValidateQuery(); err != nil {
161 return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error()) 166 return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
162 } 167 }
@@ -178,7 +183,7 @@ func (productMaterialService *ProductMaterialService) SearchProductMaterial(oper @@ -178,7 +183,7 @@ func (productMaterialService *ProductMaterialService) SearchProductMaterial(oper
178 } else { 183 } else {
179 productMaterialRepository = value 184 productMaterialRepository = value
180 } 185 }
181 - var results []*dto.DtoMaterial 186 + results := make([]*dto.DtoMaterial, 0)
182 if listProductMaterialQuery.ProductMaterialGroupId != 0 { 187 if listProductMaterialQuery.ProductMaterialGroupId != 0 {
183 materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext)) 188 materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
184 _, ProductMaterialGroupIds, err := materialService.AllMaterialGroupChild(operateInfo, listProductMaterialQuery.ProductMaterialGroupId) 189 _, ProductMaterialGroupIds, err := materialService.AllMaterialGroupChild(operateInfo, listProductMaterialQuery.ProductMaterialGroupId)
@@ -256,45 +261,30 @@ func (productMaterialService *ProductMaterialService) UpdateProductMaterial(oper @@ -256,45 +261,30 @@ func (productMaterialService *ProductMaterialService) UpdateProductMaterial(oper
256 defer func() { 261 defer func() {
257 transactionContext.RollbackTransaction() 262 transactionContext.RollbackTransaction()
258 }() 263 }()
259 - var productMaterialRepository domain.ProductMaterialRepository  
260 - if value, err := factory.CreateProductMaterialRepository(map[string]interface{}{  
261 - "transactionContext": transactionContext,  
262 - }); err != nil {  
263 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
264 - } else {  
265 - productMaterialRepository = value  
266 - }  
267 - productMaterial, err := productMaterialRepository.FindOne(map[string]interface{}{"productMaterialId": cmd.ProductMaterialId}) 264 + productMaterialRepository, productMaterial, err := factory.FastProductMaterial(transactionContext, cmd.ProductMaterialId)
268 if err != nil { 265 if err != nil {
269 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 266 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
270 } 267 }
271 - if productMaterial == nil {  
272 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(cmd.ProductMaterialId)))  
273 - }  
274 - newProductMaterial := &domain.ProductMaterial{ //赋值  
275 - ProductMaterialId: cmd.ProductMaterialId,  
276 - ProductMaterialGroupId: cmd.ProductMaterialGroupId,  
277 - MaterialName: cmd.MaterialName,  
278 - MaterialNumber: productMaterial.MaterialNumber,  
279 - MaterialAttribute: &domain.MaterialAttribute{Attribute: cmd.MaterialAttribute},  
280 - MaterialCategory: &domain.MaterialCategory{Category: cmd.MaterialCategory},  
281 - ProductMaterialExt: &domain.MaterialExt{ 268 +
  269 + productMaterial.ProductMaterialGroupId = cmd.ProductMaterialGroupId
  270 + productMaterial.MaterialName = cmd.MaterialName
  271 + productMaterial.MaterialAttribute.Attribute = cmd.MaterialAttribute
  272 + productMaterial.MaterialCategory.Category = cmd.MaterialCategory
  273 + productMaterial.ProductMaterialExt = &domain.MaterialExt{
282 Specification: cmd.Specification, 274 Specification: cmd.Specification,
283 Unit: cmd.Unit, 275 Unit: cmd.Unit,
284 ExpiredDay: cmd.ExpiredDay, 276 ExpiredDay: cmd.ExpiredDay,
285 Remark: cmd.Remark, 277 Remark: cmd.Remark,
286 - },  
287 - CreatedAt: productMaterial.CreatedAt,  
288 } 278 }
289 - materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))  
290 - data, err := materialService.UpdateMaterial(operateInfo, newProductMaterial)  
291 - if err != nil { 279 + productMaterial.UpdatedAt = time.Now()
  280 +
  281 + if _, err := productMaterialRepository.Save(productMaterial); err != nil {
292 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 282 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
293 } 283 }
294 if err := transactionContext.CommitTransaction(); err != nil { 284 if err := transactionContext.CommitTransaction(); err != nil {
295 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 285 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
296 } 286 }
297 - return data, nil 287 + return struct{}{}, nil
298 } 288 }
299 289
300 // 批量移除生产物料服务 290 // 批量移除生产物料服务
@@ -56,39 +56,6 @@ func (ptr *PGMaterialService) AddMaterial(opt *domain.OperateInfo, item *domain. @@ -56,39 +56,6 @@ func (ptr *PGMaterialService) AddMaterial(opt *domain.OperateInfo, item *domain.
56 return aaa, err 56 return aaa, err
57 } 57 }
58 58
59 -func (ptr *PGMaterialService) UpdateMaterial(opt *domain.OperateInfo, item *domain.ProductMaterial) (*domain.ProductMaterial, error) {  
60 - var (  
61 - user *domain.User  
62 - org *domain.Org  
63 - err error  
64 - newProductMaterial *domain.ProductMaterial  
65 - productMaterialRepository, _ = repository.NewProductMaterialRepository(ptr.transactionContext)  
66 - //productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)  
67 - )  
68 - if user, err = NewUserService().User(opt.UserId); err != nil {  
69 - return nil, err  
70 - }  
71 - if org, err = NewUserService().Organization(opt.OrgId); err != nil {  
72 - return nil, err  
73 - }  
74 - newProductMaterial = &domain.ProductMaterial{  
75 - CompanyId: opt.CompanyId,  
76 - OrgId: opt.OrgId,  
77 - ProductMaterialId: item.ProductMaterialId,  
78 - ProductMaterialGroupId: item.ProductMaterialGroupId,  
79 - MaterialName: item.MaterialName,  
80 - MaterialNumber: item.MaterialNumber,  
81 - MaterialAttribute: item.MaterialAttribute,  
82 - MaterialCategory: item.MaterialCategory,  
83 - ProductMaterialExt: item.ProductMaterialExt,  
84 - CreatedAt: item.CreatedAt,  
85 - UpdatedAt: time.Now(),  
86 - Ext: domain.NewExt(org.OrgName).WithOperator(user),  
87 - }  
88 - aaa, err := productMaterialRepository.Save(newProductMaterial)  
89 - return aaa, err  
90 -}  
91 -  
92 // 物料分组 59 // 物料分组
93 60
94 func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) { 61 func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
@@ -164,7 +164,7 @@ func (repository *ProductMaterialRepository) Find(queryOptions map[string]interf @@ -164,7 +164,7 @@ func (repository *ProductMaterialRepository) Find(queryOptions map[string]interf
164 query.Where("material_name like ?", fmt.Sprintf("%%%v%%", v)) 164 query.Where("material_name like ?", fmt.Sprintf("%%%v%%", v))
165 } 165 }
166 if v, ok := queryOptions["materialCategory"]; ok && v != "" { 166 if v, ok := queryOptions["materialCategory"]; ok && v != "" {
167 - query.Where("material_category like ?", fmt.Sprintf("%%%v%%", v)) 167 + query.Where(fmt.Sprintf(`material_category->>'category' like '%%%v%%'`, v))
168 } 168 }
169 query.SetOffsetAndLimit(domain.MaxQueryRow) 169 query.SetOffsetAndLimit(domain.MaxQueryRow)
170 query.SetOrderDirect("product_material_id", "DESC") 170 query.SetOrderDirect("product_material_id", "DESC")
@@ -29,8 +29,8 @@ func (controller *ProductMaterialController) UpdateProductMaterial() { @@ -29,8 +29,8 @@ func (controller *ProductMaterialController) UpdateProductMaterial() {
29 productMaterialId, _ := controller.GetInt(":productMaterialId") 29 productMaterialId, _ := controller.GetInt(":productMaterialId")
30 updateProductMaterialCommand.ProductMaterialId = productMaterialId 30 updateProductMaterialCommand.ProductMaterialId = productMaterialId
31 operateInfo := ParseOperateInfo(controller.BaseController) 31 operateInfo := ParseOperateInfo(controller.BaseController)
32 - updateProductMaterialCommand.CompanyId = operateInfo.CompanyId  
33 - updateProductMaterialCommand.OrgId = operateInfo.OrgId 32 + //updateProductMaterialCommand.CompanyId = operateInfo.CompanyId
  33 + //updateProductMaterialCommand.OrgId = operateInfo.OrgId
34 data, err := productMaterialService.UpdateProductMaterial(operateInfo, updateProductMaterialCommand) 34 data, err := productMaterialService.UpdateProductMaterial(operateInfo, updateProductMaterialCommand)
35 controller.Response(data, err) 35 controller.Response(data, err)
36 } 36 }
@@ -67,7 +67,7 @@ func (controller *ProductMaterialController) ListProductMaterial() { @@ -67,7 +67,7 @@ func (controller *ProductMaterialController) ListProductMaterial() {
67 67
68 func (controller *ProductMaterialController) SearchProductMaterial() { 68 func (controller *ProductMaterialController) SearchProductMaterial() {
69 productMaterialService := service.NewProductMaterialService(nil) 69 productMaterialService := service.NewProductMaterialService(nil)
70 - listProductMaterialQuery := &query.ListProductMaterialQuery{} 70 + listProductMaterialQuery := &query.SearchProductMaterialQuery{}
71 controller.Unmarshal(listProductMaterialQuery) 71 controller.Unmarshal(listProductMaterialQuery)
72 operateInfo := ParseOperateInfo(controller.BaseController) 72 operateInfo := ParseOperateInfo(controller.BaseController)
73 total, data, err := productMaterialService.SearchProductMaterial(operateInfo, listProductMaterialQuery) 73 total, data, err := productMaterialService.SearchProductMaterial(operateInfo, listProductMaterialQuery)