作者 13655977079

feat:增加物料管理

1 1
2 -- 表product_material_group 增加唯一索引 idx_unq_product_material_group_company_id_org_id_material_group_number 2 -- 表product_material_group 增加唯一索引 idx_unq_product_material_group_company_id_org_id_material_group_number
3 3
4 -create UNIQUE INDEX idx_unq_product_material_group_company_id_org_id_material_group_number on manufacture.product_material_group using btree(company_id,org_id,material_group_number);  
  4 +create UNIQUE INDEX idx_unq_product_material_group_company_id_org_id_material_group_number on manufacture.product_material_group using btree(company_id,org_id,material_group_number);
  5 +
  6 +-- 表product_material 增加唯一索引 idx_unq_product_material_company_id_material_number
  7 +create UNIQUE INDEX idx_unq_product_material_company_id_material_number on manufacture.product_material using btree(company_id,material_number);
@@ -14,19 +14,15 @@ type CreateProductCommand struct { @@ -14,19 +14,15 @@ type CreateProductCommand struct {
14 // 组织ID 14 // 组织ID
15 OrgId int `cname:"组织ID" json:"orgId" valid:"Required"` 15 OrgId int `cname:"组织ID" json:"orgId" valid:"Required"`
16 // 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001 16 // 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001
17 - ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode"` 17 + ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode" valid:"Required"`
18 // 产品名称 18 // 产品名称
19 - ProductName string `cname:"产品名称" json:"productName" valid:"Required"` 19 + // ProductName string `cname:"产品名称" json:"productName" valid:"Required"`
20 // 产品类别 20 // 产品类别
21 - ProductCategory string `cname:"产品类别" json:"productCategory" valid:"Required"`  
22 - // 数量(保留两位小数)  
23 - //Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"` 21 + // ProductCategory string `cname:"产品类别" json:"productCategory" valid:"Required"`
24 // 单位 22 // 单位
25 - Unit string `cname:"单位" json:"unit" valid:"Required"` 23 + // Unit string `cname:"单位" json:"unit" valid:"Required"`
26 // 单份重量(原材料) 24 // 单份重量(原材料)
27 UnitWeight float64 `cname:"单份重量(原材料)" json:"unitWeight" valid:"Required"` 25 UnitWeight float64 `cname:"单份重量(原材料)" json:"unitWeight" valid:"Required"`
28 - // 重量  
29 - //Weight float64 `cname:"重量" json:"weight" valid:"Required"`  
30 } 26 }
31 27
32 func (createProductCommand *CreateProductCommand) Valid(validation *validation.Validation) { 28 func (createProductCommand *CreateProductCommand) Valid(validation *validation.Validation) {
@@ -12,19 +12,15 @@ type UpdateProductCommand struct { @@ -12,19 +12,15 @@ type UpdateProductCommand struct {
12 // 产品ID 12 // 产品ID
13 ProductId int `cname:"产品ID" json:"productId" valid:"Required"` 13 ProductId int `cname:"产品ID" json:"productId" valid:"Required"`
14 // 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001 14 // 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001
15 - ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode"` 15 + //ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode"`
16 // 产品名称 16 // 产品名称
17 - ProductName string `cname:"产品名称" json:"productName" valid:"Required"` 17 + ProductName string `cname:"产品名称" json:"productName" `
18 // 产品类别 18 // 产品类别
19 - ProductCategory string `cname:"产品类别" json:"productCategory" valid:"Required"`  
20 - // 数量(保留两位小数)  
21 - //Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"` 19 + ProductCategory string `cname:"产品类别" json:"productCategory"`
22 // 单位 20 // 单位
23 - Unit string `cname:"单位" json:"unit" valid:"Required"` 21 + Unit string `cname:"单位" json:"unit"`
24 // 单份重量(原材料) 22 // 单份重量(原材料)
25 UnitWeight float64 `cname:"单份重量(原材料)" json:"unitWeight" valid:"Required"` 23 UnitWeight float64 `cname:"单份重量(原材料)" json:"unitWeight" valid:"Required"`
26 - // 重量  
27 - //Weight float64 `cname:"重量" json:"weight" valid:"Required"`  
28 } 24 }
29 25
30 func (updateProductCommand *UpdateProductCommand) Valid(validation *validation.Validation) { 26 func (updateProductCommand *UpdateProductCommand) Valid(validation *validation.Validation) {
@@ -14,7 +14,6 @@ import ( @@ -14,7 +14,6 @@ import (
14 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" 14 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
15 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils" 15 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
16 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" 16 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
17 - "time"  
18 ) 17 )
19 18
20 // 产品服务 19 // 产品服务
@@ -22,7 +21,7 @@ type ProductService struct { @@ -22,7 +21,7 @@ type ProductService struct {
22 } 21 }
23 22
24 // 创建产品服务 23 // 创建产品服务
25 -func (productService *ProductService) CreateProduct(createProductCommand *command.CreateProductCommand) (interface{}, error) { 24 +func (productService *ProductService) CreateProduct(opt *domain.OperateInfo, createProductCommand *command.CreateProductCommand) (interface{}, error) {
26 if err := createProductCommand.ValidateCommand(); err != nil { 25 if err := createProductCommand.ValidateCommand(); err != nil {
27 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 26 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
28 } 27 }
@@ -37,50 +36,23 @@ func (productService *ProductService) CreateProduct(createProductCommand *comman @@ -37,50 +36,23 @@ func (productService *ProductService) CreateProduct(createProductCommand *comman
37 transactionContext.RollbackTransaction() 36 transactionContext.RollbackTransaction()
38 }() 37 }()
39 38
40 - if len(createProductCommand.ProductCode) == 0 {  
41 - generator := redis.NewProductCodeCache(createProductCommand.CompanyId)  
42 - code, err := redis.GenCode(generator)  
43 - if err != nil {  
44 - log.Logger.Error(err.Error())  
45 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")  
46 - }  
47 - createProductCommand.ProductCode = code 39 + batchAddProductService, _ := domainService.NewPGBatchAddProductService(transactionContext.(*pgTransaction.TransactionContext))
  40 + item := &domain.ImportProductItem{
  41 + ProductCode: createProductCommand.ProductCode,
  42 + UnitWeight: utils.AssertString(createProductCommand.UnitWeight),
48 } 43 }
49 - var userService = domainService.NewUserService()  
50 - var org *domain.Org  
51 - org, err = userService.Organization(createProductCommand.OrgId)  
52 - if err != nil {  
53 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 44 + if _, err = batchAddProductService.BatchAddProduct(opt, []*domain.ImportProductItem{
  45 + item,
  46 + }, false); err != nil {
  47 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
54 } 48 }
55 -  
56 - newProduct := &domain.Product{  
57 - CompanyId: createProductCommand.CompanyId,  
58 - OrgId: createProductCommand.OrgId,  
59 - ProductCode: createProductCommand.ProductCode,  
60 - ProductName: createProductCommand.ProductName,  
61 - ProductCategory: createProductCommand.ProductCategory,  
62 - ProductSpec: &domain.UnitQuantity{  
63 - Unit: createProductCommand.Unit,  
64 - UnitWeight: createProductCommand.UnitWeight,  
65 - },  
66 - CreatedAt: time.Now(),  
67 - UpdatedAt: time.Now(),  
68 - Ext: domain.NewExt(org.OrgName),  
69 - }  
70 - productRepository, _, _ := factory.FastPgProduct(transactionContext, 0)  
71 -  
72 - if item, err := productRepository.FindOne(map[string]interface{}{"companyId": createProductCommand.CompanyId, "productCode": createProductCommand.ProductCode}); err == nil && item != nil {  
73 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "产品编号重复,请重新提交") 49 + if len(item.FailReason) > 0 {
  50 + return nil, application.ThrowError(application.TRANSACTION_ERROR, item.FailReason)
74 } 51 }
75 -  
76 - if product, err := productRepository.Save(newProduct); err != nil {  
77 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
78 - } else {  
79 - if err := transactionContext.CommitTransaction(); err != nil {  
80 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
81 - }  
82 - return product, nil 52 + if err := transactionContext.CommitTransaction(); err != nil {
  53 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
83 } 54 }
  55 + return struct{}{}, nil
84 } 56 }
85 57
86 // 返回产品服务 58 // 返回产品服务
@@ -242,7 +214,7 @@ func (productService *ProductService) BatchRemoveProduct(cmd *command.BatchRemov @@ -242,7 +214,7 @@ func (productService *ProductService) BatchRemoveProduct(cmd *command.BatchRemov
242 } 214 }
243 215
244 // 更新产品服务 216 // 更新产品服务
245 -func (productService *ProductService) UpdateProduct(updateProductCommand *command.UpdateProductCommand) (interface{}, error) { 217 +func (productService *ProductService) UpdateProduct(opt *domain.OperateInfo, updateProductCommand *command.UpdateProductCommand) (interface{}, error) {
246 if err := updateProductCommand.ValidateCommand(); err != nil { 218 if err := updateProductCommand.ValidateCommand(); err != nil {
247 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 219 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
248 } 220 }
@@ -260,19 +232,11 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman @@ -260,19 +232,11 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman
260 if err != nil { 232 if err != nil {
261 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 233 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
262 } 234 }
263 - if len(updateProductCommand.ProductCode) == 0 {  
264 - generator := redis.NewProductCodeCache(product.CompanyId)  
265 - code, err := redis.GenCode(generator)  
266 - if err != nil {  
267 - log.Logger.Error(err.Error())  
268 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")  
269 - }  
270 - updateProductCommand.ProductCode = code  
271 - }  
272 - if updateProductCommand.ProductCode != product.ProductCode {  
273 - if item, err := productRepository.FindOne(map[string]interface{}{"companyId": product.CompanyId, "productCode": updateProductCommand.ProductCode}); err == nil && item != nil {  
274 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "产品编号重复,请重新提交")  
275 - } 235 +
  236 + var material *domain.ProductMaterial
  237 + productMaterialRepository, _, _ := factory.FastProductMaterial(transactionContext, 0)
  238 + if material, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": product.CompanyId, "materialNumber": product.ProductCode}); err != nil || material == nil {
  239 + return nil, application.ThrowError(application.BUSINESS_ERROR, "物料不存在")
276 } 240 }
277 241
278 var userService = domainService.NewUserService() 242 var userService = domainService.NewUserService()
@@ -281,8 +245,13 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman @@ -281,8 +245,13 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman
281 if err != nil { 245 if err != nil {
282 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 246 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
283 } 247 }
  248 +
  249 + updateProductCommand.ProductName = material.MaterialName
  250 + updateProductCommand.Unit = material.ProductMaterialExt.Unit
  251 + updateProductCommand.ProductCategory = material.MaterialCategory.Category
284 data := tool_funs.SimpleStructToMap(updateProductCommand) 252 data := tool_funs.SimpleStructToMap(updateProductCommand)
285 data["orgName"] = org.OrgName 253 data["orgName"] = org.OrgName
  254 +
286 if err := product.Update(data); err != nil { 255 if err := product.Update(data); err != nil {
287 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 256 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
288 } 257 }
1 package dto 1 package dto
2 2
3 -import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  6 +)
4 7
5 type MaterialGroupDto struct { 8 type MaterialGroupDto struct {
6 // 物料分组ID 9 // 物料分组ID
@@ -26,3 +29,30 @@ func (d *MaterialGroupDto) LoadDto(m *domain.ProductMaterialGroup, orgId int) *M @@ -26,3 +29,30 @@ func (d *MaterialGroupDto) LoadDto(m *domain.ProductMaterialGroup, orgId int) *M
26 d.MaterialGroupNumber = m.MaterialGroupNumber 29 d.MaterialGroupNumber = m.MaterialGroupNumber
27 return d 30 return d
28 } 31 }
  32 +
  33 +func (productMaterialGroup *MaterialGroupDto) PID() string {
  34 + return fmt.Sprintf("%d", productMaterialGroup.Pid)
  35 +}
  36 +func (productMaterialGroup *MaterialGroupDto) ID() string {
  37 + return fmt.Sprintf("%d", productMaterialGroup.ProductMaterialGroupId)
  38 +}
  39 +
  40 +type MaterialGroupDtos []*MaterialGroupDto
  41 +
  42 +func (tree MaterialGroupDtos) Len() int {
  43 + return len(tree)
  44 +}
  45 +
  46 +func (tree MaterialGroupDtos) Less(i, j int) bool {
  47 + if tree[i].Pid < tree[j].Pid {
  48 + return true
  49 + }
  50 + if tree[i].Pid == tree[j].Pid {
  51 + return tree[i].ProductMaterialGroupId < tree[j].ProductMaterialGroupId
  52 + }
  53 + return false
  54 +}
  55 +
  56 +func (tree MaterialGroupDtos) Swap(i, j int) {
  57 + tree[i], tree[j] = tree[j], tree[i]
  58 +}
  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 SelectorProductMaterialGroupQuery struct {
  12 + // 查询偏离量
  13 + //Offset int `cname:"查询偏离量" json:"offset"`
  14 + // 查询限制
  15 + //Limit int `cname:"查询限制" json:"limit"`
  16 + // options 'tree' 'list' default tree
  17 + Style string `json:"style"`
  18 + // 当前公司
  19 + CompanyId int `cname:"当前公司" json:"companyId,omitempty" valid:"Required"`
  20 + // 页码
  21 + //PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
  22 + // 页数
  23 + //PageSize int `cname:"页数" json:"pageSize,omitempty"`
  24 +}
  25 +
  26 +func (cmd *SelectorProductMaterialGroupQuery) Valid(validation *validation.Validation) {
  27 + //cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize)
  28 + cmd.Style = "tree"
  29 +}
  30 +
  31 +func (cmd *SelectorProductMaterialGroupQuery) ValidateQuery() error {
  32 + valid := validation.Validation{}
  33 + b, err := valid.Valid(cmd)
  34 + if err != nil {
  35 + return err
  36 + }
  37 + if !b {
  38 + elem := reflect.TypeOf(cmd).Elem()
  39 + for _, validErr := range valid.Errors {
  40 + field, isExist := elem.FieldByName(validErr.Field)
  41 + if isExist {
  42 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  43 + } else {
  44 + return fmt.Errorf(validErr.Message)
  45 + }
  46 + }
  47 + }
  48 + return nil
  49 +}
@@ -12,6 +12,7 @@ import ( @@ -12,6 +12,7 @@ import (
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 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils" 14 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
  15 + "sort"
15 ) 16 )
16 17
17 // 物料分组服务 18 // 物料分组服务
@@ -38,6 +39,7 @@ func (productMaterialGroupService *ProductMaterialGroupService) CreateProductMat @@ -38,6 +39,7 @@ func (productMaterialGroupService *ProductMaterialGroupService) CreateProductMat
38 MaterialGroupName: createProductMaterialGroupCommand.MaterialGroupName, 39 MaterialGroupName: createProductMaterialGroupCommand.MaterialGroupName,
39 MaterialGroupNumber: createProductMaterialGroupCommand.MaterialGroupNumber, 40 MaterialGroupNumber: createProductMaterialGroupCommand.MaterialGroupNumber,
40 } 41 }
  42 +
41 var productMaterialGroup *domain.ProductMaterialGroup 43 var productMaterialGroup *domain.ProductMaterialGroup
42 materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext)) 44 materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
43 if productMaterialGroup, err = materialService.AddMaterialGroup(operateInfo, newProductMaterialGroup); err != nil { 45 if productMaterialGroup, err = materialService.AddMaterialGroup(operateInfo, newProductMaterialGroup); err != nil {
@@ -185,7 +187,7 @@ func (productMaterialGroupService *ProductMaterialGroupService) UpdateProductMat @@ -185,7 +187,7 @@ func (productMaterialGroupService *ProductMaterialGroupService) UpdateProductMat
185 productMaterialGroup := &domain.ProductMaterialGroup{ 187 productMaterialGroup := &domain.ProductMaterialGroup{
186 ProductMaterialGroupId: cmd.ProductMaterialGroupId, 188 ProductMaterialGroupId: cmd.ProductMaterialGroupId,
187 MaterialGroupName: cmd.MaterialGroupName, 189 MaterialGroupName: cmd.MaterialGroupName,
188 - MaterialGroupNumber: cmd.MaterialGroupNumber, 190 + //MaterialGroupNumber: cmd.MaterialGroupNumber,
189 } 191 }
190 materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext)) 192 materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
191 if productMaterialGroup, err := materialService.UpdateMaterialGroup(operateInfo, productMaterialGroup); err != nil { 193 if productMaterialGroup, err := materialService.UpdateMaterialGroup(operateInfo, productMaterialGroup); err != nil {
@@ -233,6 +235,48 @@ func (productMaterialGroupService *ProductMaterialGroupService) SearchProductMat @@ -233,6 +235,48 @@ func (productMaterialGroupService *ProductMaterialGroupService) SearchProductMat
233 return count, results, nil 235 return count, results, nil
234 } 236 }
235 237
  238 +func (productMaterialGroupService *ProductMaterialGroupService) SelectorProductMaterialGroup(operateInfo *domain.OperateInfo, cmd *query.SelectorProductMaterialGroupQuery) (int64, interface{}, error) {
  239 + if err := cmd.ValidateQuery(); err != nil {
  240 + return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
  241 + }
  242 + transactionContext, err := factory.CreateTransactionContext(nil)
  243 + if err != nil {
  244 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  245 + }
  246 + if err := transactionContext.StartTransaction(); err != nil {
  247 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  248 + }
  249 + defer func() {
  250 + transactionContext.RollbackTransaction()
  251 + }()
  252 + var productMaterialGroupRepository domain.ProductMaterialGroupRepository
  253 + productMaterialGroupRepository, _, _ = factory.FastProductMaterialGroup(transactionContext, 0)
  254 + queryOptions := utils.ObjectToMap(cmd)
  255 + count, productGroups, err := productMaterialGroupRepository.Find(queryOptions)
  256 + if err != nil {
  257 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  258 + }
  259 + var results = make([]*dto.MaterialGroupDto, 0)
  260 + for i := range productGroups {
  261 + newItem := &dto.MaterialGroupDto{}
  262 + newItem.LoadDto(productGroups[i], operateInfo.OrgId)
  263 + results = append(results, newItem)
  264 + }
  265 + if err := transactionContext.CommitTransaction(); err != nil {
  266 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  267 + }
  268 + if cmd.Style == "tree" {
  269 + sort.Stable(dto.MaterialGroupDtos(results))
  270 + nodes := make([]utils.TreeNode, 0)
  271 + for i := range results {
  272 + nodes = append(nodes, results[i])
  273 + }
  274 + tree := utils.NewTree(nodes)
  275 + return count, tree.Nodes, nil
  276 + }
  277 + return count, results, nil
  278 +}
  279 +
236 func NewProductMaterialGroupService(options map[string]interface{}) *ProductMaterialGroupService { 280 func NewProductMaterialGroupService(options map[string]interface{}) *ProductMaterialGroupService {
237 newProductMaterialGroupService := &ProductMaterialGroupService{} 281 newProductMaterialGroupService := &ProductMaterialGroupService{}
238 return newProductMaterialGroupService 282 return newProductMaterialGroupService
@@ -2,6 +2,7 @@ package domain @@ -2,6 +2,7 @@ package domain
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
5 "strconv" 6 "strconv"
6 "time" 7 "time"
7 ) 8 )
@@ -99,15 +100,18 @@ type ImportProductItem struct { @@ -99,15 +100,18 @@ type ImportProductItem struct {
99 } 100 }
100 101
101 func (item *ImportProductItem) Valid() error { 102 func (item *ImportProductItem) Valid() error {
102 - if len(item.ProductName) == 0 {  
103 - return fmt.Errorf("品名不能为空")  
104 - }  
105 - if len(item.Unit) == 0 {  
106 - return fmt.Errorf("规格不能为空")  
107 - }  
108 - if len(item.ProductCategory) == 0 {  
109 - return fmt.Errorf("类别不能为空") 103 + if len(item.ProductCode) == 0 {
  104 + return fmt.Errorf("产品编号不能为空")
110 } 105 }
  106 + //if len(item.ProductName) == 0 {
  107 + // return fmt.Errorf("品名不能为空")
  108 + //}
  109 + //if len(item.Unit) == 0 {
  110 + // return fmt.Errorf("规格不能为空")
  111 + //}
  112 + //if len(item.ProductCategory) == 0 {
  113 + // return fmt.Errorf("类别不能为空")
  114 + //}
111 if len(item.UnitWeight) == 0 { 115 if len(item.UnitWeight) == 0 {
112 item.UnitWeight = "0" 116 item.UnitWeight = "0"
113 } 117 }
@@ -116,3 +120,13 @@ func (item *ImportProductItem) Valid() error { @@ -116,3 +120,13 @@ func (item *ImportProductItem) Valid() error {
116 } 120 }
117 return nil 121 return nil
118 } 122 }
  123 +
  124 +type Products []*Product
  125 +
  126 +func (products Products) ProductCodes() []string {
  127 + var result = utils.NewSet()
  128 + for _, v := range products {
  129 + result.Add(v.ProductCode)
  130 + }
  131 + return result.KeysStr()
  132 +}
@@ -59,3 +59,11 @@ func (productMaterials ProductMaterials) ToMapById() map[int]*ProductMaterial { @@ -59,3 +59,11 @@ func (productMaterials ProductMaterials) ToMapById() map[int]*ProductMaterial {
59 } 59 }
60 return mapProductMaterial 60 return mapProductMaterial
61 } 61 }
  62 +
  63 +func (productMaterials ProductMaterials) ToMapByNumber() map[string]*ProductMaterial {
  64 + var mapProductMaterial = make(map[string]*ProductMaterial, 0)
  65 + for _, v := range productMaterials {
  66 + mapProductMaterial[v.MaterialNumber] = v
  67 + }
  68 + return mapProductMaterial
  69 +}
@@ -2,6 +2,7 @@ package domain @@ -2,6 +2,7 @@ package domain
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "strconv"
5 "time" 6 "time"
6 ) 7 )
7 8
@@ -49,6 +50,27 @@ func (productMaterialGroup *ProductMaterialGroup) Update(data map[string]interfa @@ -49,6 +50,27 @@ func (productMaterialGroup *ProductMaterialGroup) Update(data map[string]interfa
49 return nil 50 return nil
50 } 51 }
51 52
  53 +func (productMaterialGroup *ProductMaterialGroup) GroupNumberComposeUp(parent *ProductMaterialGroup, latestNumber string, total int) string {
  54 + iNumber, err := strconv.ParseInt(latestNumber, 10, 64)
  55 +
  56 + if err == nil {
  57 + iNumber += 1
  58 + return formatFunc(iNumber)
  59 + }
  60 + if parent != nil {
  61 + return parent.MaterialGroupNumber + formatFunc(int64(total+1))
  62 + }
  63 + return formatFunc(int64(total + 1))
  64 +}
  65 +
  66 +func formatFunc(iNumber int64) string {
  67 + sNumber := fmt.Sprintf("%d", iNumber)
  68 + if len(sNumber)%2 == 1 {
  69 + return "0" + sNumber
  70 + }
  71 + return sNumber
  72 +}
  73 +
52 /***** 1.实现树 *****/ 74 /***** 1.实现树 *****/
53 75
54 func (productMaterialGroup *ProductMaterialGroup) PID() string { 76 func (productMaterialGroup *ProductMaterialGroup) PID() string {
  1 +package dao
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
  7 +)
  8 +
  9 +type ProductMaterialGroupDao struct {
  10 + transactionContext *pgTransaction.TransactionContext
  11 +}
  12 +
  13 +func (dao *ProductMaterialGroupDao) CurrentGroupLatestNumber(companyId int, productMaterialGroupId int) (string, int, error) {
  14 + m := new(models.ProductMaterialGroup)
  15 + var result string
  16 + var total int
  17 + query := dao.transactionContext.PgTx.Model(m).Where("company_id = ?", companyId)
  18 + query.Where("pid = ?", productMaterialGroupId)
  19 + query.ColumnExpr("max(material_group_number) number")
  20 + query.ColumnExpr("count(material_group_number) total")
  21 + query.AllWithDeleted()
  22 + err := query.Select(&result, &total)
  23 + return result, total, err
  24 +}
  25 +
  26 +func NewProductMaterialGroupDao(transactionContext *pgTransaction.TransactionContext) (*ProductMaterialGroupDao, error) {
  27 + if transactionContext == nil {
  28 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  29 + } else {
  30 + return &ProductMaterialGroupDao{
  31 + transactionContext: transactionContext,
  32 + }, nil
  33 + }
  34 +}
@@ -6,8 +6,8 @@ import ( @@ -6,8 +6,8 @@ import (
6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
7 "github.com/linmadan/egglib-go/utils/tool_funs" 7 "github.com/linmadan/egglib-go/utils/tool_funs"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
9 - "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"  
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
12 "strconv" 12 "strconv"
13 "time" 13 "time"
@@ -36,10 +36,22 @@ func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, li @@ -36,10 +36,22 @@ func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, li
36 if err != nil { 36 if err != nil {
37 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 37 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
38 } 38 }
39 -  
40 - var generator = redis.NewProductCodeCache(opt.CompanyId) 39 + var materials domain.ProductMaterials
  40 + if materials, err = ptr.GetProductMaterialByImportItems(opt.CompanyId, list); err != nil {
  41 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  42 + }
  43 + mapMaterials := materials.ToMapByNumber()
41 for i := range list { 44 for i := range list {
42 item := list[i] 45 item := list[i]
  46 + if v, ok := mapMaterials[item.ProductCode]; !ok {
  47 + item.FailReason = "导入的产品编号不存在"
  48 + failRows = append(failRows, item)
  49 + continue
  50 + } else {
  51 + item.Unit = v.ProductMaterialExt.Unit
  52 + item.ProductName = v.MaterialName
  53 + item.ProductCategory = v.MaterialCategory.Category
  54 + }
43 if err := item.Valid(); err != nil { 55 if err := item.Valid(); err != nil {
44 item.FailReason = err.Error() 56 item.FailReason = err.Error()
45 failRows = append(failRows, item) 57 failRows = append(failRows, item)
@@ -69,14 +81,6 @@ func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, li @@ -69,14 +81,6 @@ func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, li
69 continue 81 continue
70 } 82 }
71 } 83 }
72 - if len(newItem.ProductCode) == 0 {  
73 - code, err := redis.GenCode(generator)  
74 - if err != nil {  
75 - log.Logger.Error(err.Error())  
76 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")  
77 - }  
78 - newItem.ProductCode = code  
79 - }  
80 84
81 if _, ok := mapProduct[newItem.ProductCode]; !ok { 85 if _, ok := mapProduct[newItem.ProductCode]; !ok {
82 mapProduct[newItem.ProductCode] = newItem 86 mapProduct[newItem.ProductCode] = newItem
@@ -105,6 +109,28 @@ func (ptr *PGBatchAddProductService) updateProduct(opt *domain.OperateInfo, item @@ -105,6 +109,28 @@ func (ptr *PGBatchAddProductService) updateProduct(opt *domain.OperateInfo, item
105 return nil 109 return nil
106 } 110 }
107 111
  112 +func (ptr *PGBatchAddProductService) GetProductMaterialByProductCodes(companyId int, productCodes []string) ([]*domain.ProductMaterial, error) {
  113 + productMaterialRepository, _ := repository.NewProductMaterialRepository(ptr.transactionContext)
  114 + _, productMaterials, err := productMaterialRepository.Find(map[string]interface{}{"companyId": companyId, "materialNumbers": productCodes})
  115 + return productMaterials, err
  116 +}
  117 +
  118 +func (ptr *PGBatchAddProductService) GetProductMaterialByImportItems(companyId int, list []*domain.ImportProductItem) ([]*domain.ProductMaterial, error) {
  119 + productCodes := ptr.GetProductCodesByImportItems(list)
  120 + if len(productCodes) == 0 {
  121 + return []*domain.ProductMaterial{}, nil
  122 + }
  123 + return ptr.GetProductMaterialByProductCodes(companyId, productCodes)
  124 +}
  125 +
  126 +func (ptr *PGBatchAddProductService) GetProductCodesByImportItems(list []*domain.ImportProductItem) []string {
  127 + var result = utils.NewSet()
  128 + for _, v := range list {
  129 + result.Add(v.ProductCode)
  130 + }
  131 + return result.KeysStr()
  132 +}
  133 +
108 func NewPGBatchAddProductService(transactionContext *pgTransaction.TransactionContext) (*PGBatchAddProductService, error) { 134 func NewPGBatchAddProductService(transactionContext *pgTransaction.TransactionContext) (*PGBatchAddProductService, error) {
109 if transactionContext == nil { 135 if transactionContext == nil {
110 return nil, fmt.Errorf("transactionContext参数不能为nil") 136 return nil, fmt.Errorf("transactionContext参数不能为nil")
@@ -123,10 +123,10 @@ func (ptr *PGCommonStatisticsService) HourProductiveStatistics(queryOptions map[ @@ -123,10 +123,10 @@ func (ptr *PGCommonStatisticsService) HourProductiveStatistics(queryOptions map[
123 } 123 }
124 124
125 type HourProductiveStatisticsRequest struct { 125 type HourProductiveStatisticsRequest struct {
126 - CompanyId int `json:"companyId" valid:"Required"`  
127 - OrgId int `json:"orgId" valid:"Required"`  
128 - WorkshopId int `json:"workshopId" valid:"Required"`  
129 - Date time.Time `json:"date"` 126 + CompanyId int `json:"companyId" valid:"Required"`
  127 + OrgId int `json:"orgId" valid:"Required"`
  128 + WorkshopId int `json:"workshopId" valid:"Required"`
  129 + Date utils.TimeString `json:"date"`
130 } 130 }
131 131
132 func NewXYData(xData []string, values interface{}) interface{} { 132 func NewXYData(xData []string, values interface{}) interface{} {
@@ -154,8 +154,8 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map @@ -154,8 +154,8 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map
154 Total float64 `json:"total"` 154 Total float64 `json:"total"`
155 } 155 }
156 var date = time.Now() 156 var date = time.Now()
157 - if !xtime.IsZero(request.Date) {  
158 - date = request.Date 157 + if !xtime.IsZero(time.Time(request.Date)) {
  158 + date = time.Time(request.Date)
159 } 159 }
160 workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": request.WorkshopId}) 160 workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": request.WorkshopId})
161 if err != nil || workshop == nil { 161 if err != nil || workshop == nil {
@@ -213,8 +213,8 @@ func (ptr *PGCommonStatisticsService) ProportionOfSecondLevelStatistics(queryOpt @@ -213,8 +213,8 @@ func (ptr *PGCommonStatisticsService) ProportionOfSecondLevelStatistics(queryOpt
213 } 213 }
214 productRecordDao, _ := dao.NewProductRecordDao(ptr.transactionContext) 214 productRecordDao, _ := dao.NewProductRecordDao(ptr.transactionContext)
215 var date = time.Now() 215 var date = time.Now()
216 - if !xtime.IsZero(request.Date) {  
217 - date = request.Date 216 + if !xtime.IsZero(time.Time(request.Date)) {
  217 + date = time.Time(request.Date)
218 } 218 }
219 var input = []struct { 219 var input = []struct {
220 name string 220 name string
@@ -262,8 +262,8 @@ func (ptr *PGCommonStatisticsService) WorkshopProductionEfficiencyStatistics(que @@ -262,8 +262,8 @@ func (ptr *PGCommonStatisticsService) WorkshopProductionEfficiencyStatistics(que
262 } 262 }
263 productRecordDao, _ := dao.NewDeviceDailyRunningRecordDao(ptr.transactionContext) 263 productRecordDao, _ := dao.NewDeviceDailyRunningRecordDao(ptr.transactionContext)
264 var date = time.Now() 264 var date = time.Now()
265 - if !xtime.IsZero(request.Date) {  
266 - date = request.Date 265 + if !xtime.IsZero(time.Time(request.Date)) {
  266 + date = time.Time(request.Date)
267 } 267 }
268 var input = []struct { 268 var input = []struct {
269 name string 269 name string
@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "fmt" 4 "fmt"
5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/dao"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
9 "sort" 10 "sort"
@@ -66,6 +67,10 @@ func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *do @@ -66,6 +67,10 @@ func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *do
66 parent *domain.ProductMaterialGroup 67 parent *domain.ProductMaterialGroup
67 newProductMaterialGroup *domain.ProductMaterialGroup 68 newProductMaterialGroup *domain.ProductMaterialGroup
68 productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext) 69 productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
  70 +
  71 + productMaterialGroupDao *dao.ProductMaterialGroupDao
  72 + latestNumber string
  73 + total int
69 ) 74 )
70 if user, err = NewUserService().User(opt.UserId); err != nil { 75 if user, err = NewUserService().User(opt.UserId); err != nil {
71 return nil, err 76 return nil, err
@@ -81,6 +86,12 @@ func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *do @@ -81,6 +86,12 @@ func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *do
81 if newProductMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || newProductMaterialGroup != nil { 86 if newProductMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || newProductMaterialGroup != nil {
82 return nil, fmt.Errorf("物料分组编码已存在") 87 return nil, fmt.Errorf("物料分组编码已存在")
83 } 88 }
  89 + productMaterialGroupDao, _ = dao.NewProductMaterialGroupDao(ptr.transactionContext)
  90 + latestNumber, total, err = productMaterialGroupDao.CurrentGroupLatestNumber(opt.CompanyId, item.Pid)
  91 + if err != nil {
  92 + return nil, err
  93 + }
  94 +
84 newProductMaterialGroup = &domain.ProductMaterialGroup{ 95 newProductMaterialGroup = &domain.ProductMaterialGroup{
85 CompanyId: opt.CompanyId, 96 CompanyId: opt.CompanyId,
86 OrgId: opt.OrgId, 97 OrgId: opt.OrgId,
@@ -92,16 +103,17 @@ func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *do @@ -92,16 +103,17 @@ func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *do
92 UpdatedAt: time.Now(), 103 UpdatedAt: time.Now(),
93 Ext: domain.NewExt(org.OrgName).WithOperator(user), 104 Ext: domain.NewExt(org.OrgName).WithOperator(user),
94 } 105 }
  106 + newProductMaterialGroup.MaterialGroupNumber = newProductMaterialGroup.GroupNumberComposeUp(parent, latestNumber, total)
95 newProductMaterialGroup, err = productMaterialGroupRepository.Save(newProductMaterialGroup) 107 newProductMaterialGroup, err = productMaterialGroupRepository.Save(newProductMaterialGroup)
96 return newProductMaterialGroup, err 108 return newProductMaterialGroup, err
97 } 109 }
98 110
99 func (ptr *PGMaterialService) UpdateMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) { 111 func (ptr *PGMaterialService) UpdateMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
100 var ( 112 var (
101 - user *domain.User  
102 - org *domain.Org  
103 - err error  
104 - parent *domain.ProductMaterialGroup 113 + user *domain.User
  114 + org *domain.Org
  115 + err error
  116 + //parent *domain.ProductMaterialGroup
105 productMaterialGroup *domain.ProductMaterialGroup 117 productMaterialGroup *domain.ProductMaterialGroup
106 productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext) 118 productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
107 ) 119 )
@@ -111,24 +123,25 @@ func (ptr *PGMaterialService) UpdateMaterialGroup(opt *domain.OperateInfo, item @@ -111,24 +123,25 @@ func (ptr *PGMaterialService) UpdateMaterialGroup(opt *domain.OperateInfo, item
111 if org, err = NewUserService().Organization(opt.OrgId); err != nil { 123 if org, err = NewUserService().Organization(opt.OrgId); err != nil {
112 return nil, err 124 return nil, err
113 } 125 }
114 - if item.ProductMaterialGroupId == item.Pid {  
115 - return nil, fmt.Errorf("当前物料分组不能做为自己上级")  
116 - } 126 + //if item.ProductMaterialGroupId == item.Pid {
  127 + // return nil, fmt.Errorf("当前物料分组不能做为自己上级")
  128 + //}
117 if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.ProductMaterialGroupId}); err != nil || productMaterialGroup == nil { 129 if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.ProductMaterialGroupId}); err != nil || productMaterialGroup == nil {
118 return nil, fmt.Errorf("物料分组不存在") 130 return nil, fmt.Errorf("物料分组不存在")
119 } 131 }
120 - if item.Pid != productMaterialGroup.ProductMaterialGroupId && item.Pid != 0 {  
121 - if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil {  
122 - return nil, fmt.Errorf("上级物料分组不存在")  
123 - }  
124 - }  
125 - if productMaterialGroup.MaterialGroupNumber != item.MaterialGroupNumber {  
126 - if group, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || group != nil {  
127 - return nil, fmt.Errorf("物料分组编码已存在")  
128 - }  
129 - }  
130 132
131 - productMaterialGroup.MaterialGroupNumber = item.MaterialGroupNumber 133 + //if item.Pid != productMaterialGroup.ProductMaterialGroupId && item.Pid != 0 {
  134 + // if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil {
  135 + // return nil, fmt.Errorf("上级物料分组不存在")
  136 + // }
  137 + //}
  138 + //if productMaterialGroup.MaterialGroupNumber != item.MaterialGroupNumber {
  139 + // if group, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || group != nil {
  140 + // return nil, fmt.Errorf("物料分组编码已存在")
  141 + // }
  142 + //}
  143 + //productMaterialGroup.MaterialGroupNumber = item.MaterialGroupNumber
  144 +
132 productMaterialGroup.MaterialGroupName = item.MaterialGroupName 145 productMaterialGroup.MaterialGroupName = item.MaterialGroupName
133 productMaterialGroup.UpdatedAt = item.UpdatedAt 146 productMaterialGroup.UpdatedAt = item.UpdatedAt
134 productMaterialGroup.Ext = domain.NewExt(org.OrgName).WithOperator(user) 147 productMaterialGroup.Ext = domain.NewExt(org.OrgName).WithOperator(user)
@@ -166,6 +166,9 @@ func (repository *ProductMaterialRepository) Find(queryOptions map[string]interf @@ -166,6 +166,9 @@ func (repository *ProductMaterialRepository) Find(queryOptions map[string]interf
166 if v, ok := queryOptions["materialCategory"]; ok && v != "" { 166 if v, ok := queryOptions["materialCategory"]; ok && v != "" {
167 query.Where(fmt.Sprintf(`material_category->>'category' like '%%%v%%'`, v)) 167 query.Where(fmt.Sprintf(`material_category->>'category' like '%%%v%%'`, v))
168 } 168 }
  169 + if v, ok := queryOptions["materialNumbers"]; ok && len(v.([]string)) > 0 {
  170 + query.Where("material_number in (?)", pg.In(v))
  171 + }
169 query.SetOffsetAndLimit(domain.MaxQueryRow) 172 query.SetOffsetAndLimit(domain.MaxQueryRow)
170 query.SetOrderDirect("product_material_id", "DESC") 173 query.SetOrderDirect("product_material_id", "DESC")
171 if count, err := query.SelectAndCount(); err != nil { 174 if count, err := query.SelectAndCount(); err != nil {
1 package utils 1 package utils
2 2
3 import ( 3 import (
  4 + "github.com/linmadan/egglib-go/utils/xtime"
4 "strconv" 5 "strconv"
5 "time" 6 "time"
6 ) 7 )
@@ -103,3 +104,32 @@ func TimeBeforeEqual(t1, t2 time.Time) bool { @@ -103,3 +104,32 @@ func TimeBeforeEqual(t1, t2 time.Time) bool {
103 } 104 }
104 return false 105 return false
105 } 106 }
  107 +
  108 +type TimeString time.Time
  109 +
  110 +func (timeString *TimeString) UnmarshalJSON(data []byte) error {
  111 + if len(data) == 2 {
  112 + return nil
  113 + }
  114 + t, err := xtime.Parse(string(data[1 : len(data)-1]))
  115 + if err != nil {
  116 + return err
  117 + }
  118 + *timeString = TimeString(t)
  119 + return nil
  120 +}
  121 +
  122 +func (timeString TimeString) MarshalJSON() ([]byte, error) {
  123 + if xtime.IsZero(time.Time(timeString)) {
  124 + return []byte(`""`), nil
  125 + }
  126 + b := make([]byte, 0)
  127 + b = append(b, '"')
  128 + b = time.Time(timeString).AppendFormat(b, time.RFC3339)
  129 + b = append(b, '"')
  130 + return b, nil
  131 +}
  132 +
  133 +func (timeString TimeString) Time() time.Time {
  134 + return time.Time(timeString)
  135 +}
@@ -19,7 +19,7 @@ func (controller *ProductController) CreateProduct() { @@ -19,7 +19,7 @@ func (controller *ProductController) CreateProduct() {
19 op := ParseOperateInfo(controller.BaseController) 19 op := ParseOperateInfo(controller.BaseController)
20 createProductCommand.CompanyId = op.CompanyId 20 createProductCommand.CompanyId = op.CompanyId
21 createProductCommand.OrgId = op.OrgId 21 createProductCommand.OrgId = op.OrgId
22 - data, err := productService.CreateProduct(createProductCommand) 22 + data, err := productService.CreateProduct(ParseOperateInfo(controller.BaseController), createProductCommand)
23 controller.Response(data, err) 23 controller.Response(data, err)
24 } 24 }
25 25
@@ -29,7 +29,7 @@ func (controller *ProductController) UpdateProduct() { @@ -29,7 +29,7 @@ func (controller *ProductController) UpdateProduct() {
29 Must(controller.Unmarshal(updateProductCommand)) 29 Must(controller.Unmarshal(updateProductCommand))
30 productId, _ := controller.GetInt(":productId") 30 productId, _ := controller.GetInt(":productId")
31 updateProductCommand.ProductId = productId 31 updateProductCommand.ProductId = productId
32 - data, err := productService.UpdateProduct(updateProductCommand) 32 + data, err := productService.UpdateProduct(ParseOperateInfo(controller.BaseController), updateProductCommand)
33 controller.Response(data, err) 33 controller.Response(data, err)
34 } 34 }
35 35
@@ -78,14 +78,10 @@ func (controller *ProductMaterialGroupController) SearchProductMaterialGroup() { @@ -78,14 +78,10 @@ func (controller *ProductMaterialGroupController) SearchProductMaterialGroup() {
78 78
79 func (controller *ProductMaterialGroupController) SelectorProductMaterialGroup() { 79 func (controller *ProductMaterialGroupController) SelectorProductMaterialGroup() {
80 productMaterialGroupService := service.NewProductMaterialGroupService(nil) 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 81 + cmd := &query.SelectorProductMaterialGroupQuery{}
86 operateInfo := ParseOperateInfo(controller.BaseController) 82 operateInfo := ParseOperateInfo(controller.BaseController)
87 cmd.CompanyId = operateInfo.CompanyId 83 cmd.CompanyId = operateInfo.CompanyId
88 - count, data, err := productMaterialGroupService.SearchProductMaterialGroup(ParseOperateInfo(controller.BaseController), cmd) 84 + count, data, err := productMaterialGroupService.SelectorProductMaterialGroup(ParseOperateInfo(controller.BaseController), cmd)
89 controller.Response(map[string]interface{}{ 85 controller.Response(map[string]interface{}{
90 "groups": data, 86 "groups": data,
91 "count": count, 87 "count": count,