作者 yangfu

feat: 单位换算增加物料表关联

@@ -439,6 +439,32 @@ func FastProductMaterialGroup(transactionContext application.TransactionContext, @@ -439,6 +439,32 @@ func FastProductMaterialGroup(transactionContext application.TransactionContext,
439 return rep, mod, err 439 return rep, mod, err
440 } 440 }
441 441
  442 +// FastProductMaterialGroup 快速返回物料
  443 +//
  444 +// transactionContext 事务
  445 +// id 对象唯一标识
  446 +func FastProductMaterial(transactionContext application.TransactionContext, id int, options ...option) (domain.ProductMaterialRepository, *domain.ProductMaterial, error) {
  447 + var rep domain.ProductMaterialRepository
  448 + var mod *domain.ProductMaterial
  449 + var err error
  450 + if value, err := CreateProductMaterialRepository(map[string]interface{}{
  451 + "transactionContext": transactionContext,
  452 + }); err != nil {
  453 + return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  454 + } else {
  455 + rep = value
  456 + }
  457 + if id > 0 {
  458 + if mod, err = rep.FindOne(map[string]interface{}{"productMaterialGroupId": id}); err != nil {
  459 + if err == domain.ErrorNotFound {
  460 + return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该物料分组不存在")
  461 + }
  462 + return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  463 + }
  464 + }
  465 + return rep, mod, err
  466 +}
  467 +
442 /***** 2.配置 *****/ 468 /***** 2.配置 *****/
443 469
444 type FastOptions struct { 470 type FastOptions struct {
@@ -13,10 +13,13 @@ type MaterialUnitConversionDto struct { @@ -13,10 +13,13 @@ type MaterialUnitConversionDto struct {
13 ToUnitQuantity *domain.UnitQuantity `json:"toUnitQuantity,omitempty"` 13 ToUnitQuantity *domain.UnitQuantity `json:"toUnitQuantity,omitempty"`
14 } 14 }
15 15
16 -func (d *MaterialUnitConversionDto) LoadDto(m *domain.UnitConversion) *MaterialUnitConversionDto { 16 +func (d *MaterialUnitConversionDto) LoadDto(m *domain.UnitConversion, material *domain.ProductMaterial) *MaterialUnitConversionDto {
17 d.UnitConversionId = m.UnitConversionId 17 d.UnitConversionId = m.UnitConversionId
18 d.MaterialName = m.Material.MaterialName 18 d.MaterialName = m.Material.MaterialName
19 d.FromUnitQuantity = m.FromUnitQuantity 19 d.FromUnitQuantity = m.FromUnitQuantity
20 d.ToUnitQuantity = m.ToUnitQuantity 20 d.ToUnitQuantity = m.ToUnitQuantity
  21 + if material != nil {
  22 + d.MaterialName = material.MaterialName
  23 + }
21 return d 24 return d
22 } 25 }
@@ -25,7 +25,7 @@ type UnitConversionDto struct { @@ -25,7 +25,7 @@ type UnitConversionDto struct {
25 AuthFlag bool `json:"authFlag"` 25 AuthFlag bool `json:"authFlag"`
26 } 26 }
27 27
28 -func (d *UnitConversionDto) LoadDto(m *domain.UnitConversion, orgId int) *UnitConversionDto { 28 +func (d *UnitConversionDto) LoadDto(m *domain.UnitConversion, orgId int, productMaterial *domain.ProductMaterial) *UnitConversionDto {
29 d.UnitConversionId = m.UnitConversionId 29 d.UnitConversionId = m.UnitConversionId
30 d.WorkStation = m.WorkStation 30 d.WorkStation = m.WorkStation
31 d.Material = m.Material 31 d.Material = m.Material
@@ -36,5 +36,8 @@ func (d *UnitConversionDto) LoadDto(m *domain.UnitConversion, orgId int) *UnitCo @@ -36,5 +36,8 @@ func (d *UnitConversionDto) LoadDto(m *domain.UnitConversion, orgId int) *UnitCo
36 if m.Ext != nil { 36 if m.Ext != nil {
37 d.OrgName = m.Ext.OrgName 37 d.OrgName = m.Ext.OrgName
38 } 38 }
  39 + if productMaterial != nil {
  40 + d.Material.MaterialName = productMaterial.MaterialName
  41 + }
39 return d 42 return d
40 } 43 }
@@ -91,12 +91,21 @@ func (unitConversionService *UnitConversionService) GetUnitConversion(getUnitCon @@ -91,12 +91,21 @@ func (unitConversionService *UnitConversionService) GetUnitConversion(getUnitCon
91 //var unitConversionRepository domain.UnitConversionRepository 91 //var unitConversionRepository domain.UnitConversionRepository
92 var unitConversion *domain.UnitConversion 92 var unitConversion *domain.UnitConversion
93 _, unitConversion, err = factory.FastPgUnitConversion(transactionContext, getUnitConversionQuery.UnitConversionId) 93 _, unitConversion, err = factory.FastPgUnitConversion(transactionContext, getUnitConversionQuery.UnitConversionId)
94 - 94 + if err != nil {
  95 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  96 + }
  97 + var productMaterial *domain.ProductMaterial
  98 + if unitConversion.Material.ProductMaterialId > 0 {
  99 + _, productMaterial, err = factory.FastProductMaterial(transactionContext, unitConversion.Material.ProductMaterialId)
  100 + if err != nil {
  101 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  102 + }
  103 + }
95 if err := transactionContext.CommitTransaction(); err != nil { 104 if err := transactionContext.CommitTransaction(); err != nil {
96 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 105 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
97 } 106 }
98 result := &dto.UnitConversionDto{} 107 result := &dto.UnitConversionDto{}
99 - result.LoadDto(unitConversion, 0) 108 + result.LoadDto(unitConversion, 0, productMaterial)
100 return result, nil 109 return result, nil
101 110
102 } 111 }
@@ -293,17 +302,29 @@ func (unitConversionService *UnitConversionService) SearchUnitConversion(operate @@ -293,17 +302,29 @@ func (unitConversionService *UnitConversionService) SearchUnitConversion(operate
293 if err != nil { 302 if err != nil {
294 return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 303 return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
295 } 304 }
296 - if err := transactionContext.CommitTransaction(); err != nil {  
297 - return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 305 +
  306 + var mapProductMaterial = make(map[int]*domain.ProductMaterial, 0)
  307 + productMaterialIds := domain.UnitConversions(unitConversions).ProductMaterialIds()
  308 + if len(productMaterialIds) > 0 {
  309 + productMaterialRepository, _, _ := factory.FastProductMaterial(transactionContext, 0)
  310 + _, productMaterials, err := productMaterialRepository.Find(map[string]interface{}{"productMaterialIds": productMaterialIds})
  311 + if err != nil {
  312 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  313 + }
  314 + mapProductMaterial = domain.ProductMaterials(productMaterials).ToMapById()
298 } 315 }
299 316
300 var result = make([]*dto.UnitConversionDto, 0) 317 var result = make([]*dto.UnitConversionDto, 0)
301 for i := range unitConversions { 318 for i := range unitConversions {
302 item := unitConversions[i] 319 item := unitConversions[i]
303 newItem := &dto.UnitConversionDto{} 320 newItem := &dto.UnitConversionDto{}
304 - newItem.LoadDto(item, operateInfo.OrgId) 321 + newItem.LoadDto(item, operateInfo.OrgId, mapProductMaterial[item.Material.ProductMaterialId])
305 result = append(result, newItem) 322 result = append(result, newItem)
306 } 323 }
  324 +
  325 + if err := transactionContext.CommitTransaction(); err != nil {
  326 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  327 + }
307 return count, result, nil 328 return count, result, nil
308 } 329 }
309 330
@@ -331,11 +352,22 @@ func (unitConversionService *UnitConversionService) SearchMaterial(operateInfo * @@ -331,11 +352,22 @@ func (unitConversionService *UnitConversionService) SearchMaterial(operateInfo *
331 return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 352 return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
332 } 353 }
333 354
  355 + var mapProductMaterial = make(map[int]*domain.ProductMaterial, 0)
  356 + productMaterialIds := domain.UnitConversions(unitConversions).ProductMaterialIds()
  357 + if len(productMaterialIds) > 0 {
  358 + productMaterialRepository, _, _ := factory.FastProductMaterial(transactionContext, 0)
  359 + _, productMaterials, err := productMaterialRepository.Find(map[string]interface{}{"productMaterialIds": productMaterialIds})
  360 + if err != nil {
  361 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  362 + }
  363 + mapProductMaterial = domain.ProductMaterials(productMaterials).ToMapById()
  364 + }
  365 +
334 var result = make([]*dto.MaterialUnitConversionDto, 0) 366 var result = make([]*dto.MaterialUnitConversionDto, 0)
335 for i := range unitConversions { 367 for i := range unitConversions {
336 item := unitConversions[i] 368 item := unitConversions[i]
337 newItem := &dto.MaterialUnitConversionDto{} 369 newItem := &dto.MaterialUnitConversionDto{}
338 - newItem.LoadDto(item) 370 + newItem.LoadDto(item, mapProductMaterial[item.Material.ProductMaterialId])
339 result = append(result, newItem) 371 result = append(result, newItem)
340 } 372 }
341 return count, map[string]interface{}{ 373 return count, map[string]interface{}{
@@ -2,9 +2,11 @@ package domain @@ -2,9 +2,11 @@ package domain
2 2
3 // 物料 3 // 物料
4 type Material struct { 4 type Material struct {
5 - // 物料名称 5 + // 物料ID
  6 + ProductMaterialId int `json:"productMaterialId"`
  7 + // 物料名称 (最新数据需要查询物料表)
6 MaterialName string `json:"materialName,omitempty"` 8 MaterialName string `json:"materialName,omitempty"`
7 - // 物料类别 9 + // 物料类别 ‘南瓜饼串’、‘ 包装袋(空)’、‘包装袋(件数)’、装箱(件数)
8 MaterialCategory string `json:"materialCategory,omitempty"` 10 MaterialCategory string `json:"materialCategory,omitempty"`
9 } 11 }
10 12
@@ -49,3 +49,13 @@ func (productMaterial *ProductMaterial) Identify() interface{} { @@ -49,3 +49,13 @@ func (productMaterial *ProductMaterial) Identify() interface{} {
49 func (productMaterial *ProductMaterial) Update(data map[string]interface{}) error { 49 func (productMaterial *ProductMaterial) Update(data map[string]interface{}) error {
50 return nil 50 return nil
51 } 51 }
  52 +
  53 +type ProductMaterials []*ProductMaterial
  54 +
  55 +func (productMaterials ProductMaterials) ToMapById() map[int]*ProductMaterial {
  56 + var mapProductMaterial = make(map[int]*ProductMaterial, 0)
  57 + for _, v := range productMaterials {
  58 + mapProductMaterial[v.ProductMaterialId] = v
  59 + }
  60 + return mapProductMaterial
  61 +}
1 package domain 1 package domain
2 2
3 -import "time" 3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
  5 + "time"
  6 +)
4 7
5 // 单位换算 8 // 单位换算
6 type UnitConversion struct { 9 type UnitConversion struct {
@@ -57,3 +60,16 @@ func (unitConversion *UnitConversion) Update(data map[string]interface{}) error @@ -57,3 +60,16 @@ func (unitConversion *UnitConversion) Update(data map[string]interface{}) error
57 unitConversion.UpdatedAt = time.Now() 60 unitConversion.UpdatedAt = time.Now()
58 return nil 61 return nil
59 } 62 }
  63 +
  64 +type UnitConversions []*UnitConversion
  65 +
  66 +func (unitConversions UnitConversions) ProductMaterialIds() []int {
  67 + set := utils.NewSet()
  68 + for i := range unitConversions {
  69 + if unitConversions[i].Material.ProductMaterialId == 0 {
  70 + continue
  71 + }
  72 + set.Add(unitConversions[i].Material.ProductMaterialId)
  73 + }
  74 + return set.KeysInt()
  75 +}
@@ -153,7 +153,11 @@ func (repository *ProductMaterialRepository) Find(queryOptions map[string]interf @@ -153,7 +153,11 @@ func (repository *ProductMaterialRepository) Find(queryOptions map[string]interf
153 var productMaterialModels []*models.ProductMaterial 153 var productMaterialModels []*models.ProductMaterial
154 productMaterials := make([]*domain.ProductMaterial, 0) 154 productMaterials := make([]*domain.ProductMaterial, 0)
155 query := sqlbuilder.BuildQuery(tx.Model(&productMaterialModels), queryOptions) 155 query := sqlbuilder.BuildQuery(tx.Model(&productMaterialModels), queryOptions)
156 - query.SetOffsetAndLimit(20) 156 + query.SetWhereByQueryOption("company_id = ?", "companyId")
  157 + if v, ok := queryOptions["productMaterialIds"]; ok && len(v.([]int)) > 0 {
  158 + query.Where("product_material_id in (?)", pg.In(v))
  159 + }
  160 + query.SetOffsetAndLimit(domain.MaxQueryRow)
157 query.SetOrderDirect("product_material_id", "DESC") 161 query.SetOrderDirect("product_material_id", "DESC")
158 if count, err := query.SelectAndCount(); err != nil { 162 if count, err := query.SelectAndCount(); err != nil {
159 return 0, productMaterials, err 163 return 0, productMaterials, err
  1 +package utils
  2 +
  3 +const (
  4 + unmanaged = iota
  5 + untyped
  6 + intType
  7 + int64Type
  8 + uintType
  9 + uint64Type
  10 + stringType
  11 +)
  12 +
  13 +type PlaceholderType = struct{}
  14 +
  15 +// Set is not thread-safe, for concurrent use, make sure to use it with synchronization.
  16 +type Set struct {
  17 + data map[interface{}]PlaceholderType
  18 + tp int
  19 +}
  20 +
  21 +// NewSet returns a managed Set, can only put the values with the same type.
  22 +func NewSet() *Set {
  23 + return &Set{
  24 + data: make(map[interface{}]PlaceholderType),
  25 + tp: untyped,
  26 + }
  27 +}
  28 +
  29 +// NewUnmanagedSet returns a unmanaged Set, which can put values with different types.
  30 +func NewUnmanagedSet() *Set {
  31 + return &Set{
  32 + data: make(map[interface{}]PlaceholderType),
  33 + tp: unmanaged,
  34 + }
  35 +}
  36 +
  37 +// Add adds i into s.
  38 +func (s *Set) Add(i ...interface{}) {
  39 + for _, each := range i {
  40 + s.add(each)
  41 + }
  42 +}
  43 +
  44 +// AddInt adds int values ii into s.
  45 +func (s *Set) AddInt(ii ...int) {
  46 + for _, each := range ii {
  47 + s.add(each)
  48 + }
  49 +}
  50 +
  51 +// AddInt64 adds int64 values ii into s.
  52 +func (s *Set) AddInt64(ii ...int64) {
  53 + for _, each := range ii {
  54 + s.add(each)
  55 + }
  56 +}
  57 +
  58 +// AddUint adds uint values ii into s.
  59 +func (s *Set) AddUint(ii ...uint) {
  60 + for _, each := range ii {
  61 + s.add(each)
  62 + }
  63 +}
  64 +
  65 +// AddUint64 adds uint64 values ii into s.
  66 +func (s *Set) AddUint64(ii ...uint64) {
  67 + for _, each := range ii {
  68 + s.add(each)
  69 + }
  70 +}
  71 +
  72 +// AddStr adds string values ss into s.
  73 +func (s *Set) AddStr(ss ...string) {
  74 + for _, each := range ss {
  75 + s.add(each)
  76 + }
  77 +}
  78 +
  79 +// Contains checks if i is in s.
  80 +func (s *Set) Contains(i interface{}) bool {
  81 + if len(s.data) == 0 {
  82 + return false
  83 + }
  84 +
  85 + s.validate(i)
  86 + _, ok := s.data[i]
  87 + return ok
  88 +}
  89 +
  90 +// Keys returns the keys in s.
  91 +func (s *Set) Keys() []interface{} {
  92 + var keys []interface{}
  93 +
  94 + for key := range s.data {
  95 + keys = append(keys, key)
  96 + }
  97 +
  98 + return keys
  99 +}
  100 +
  101 +// KeysInt returns the int keys in s.
  102 +func (s *Set) KeysInt() []int {
  103 + var keys []int
  104 +
  105 + for key := range s.data {
  106 + if intKey, ok := key.(int); ok {
  107 + keys = append(keys, intKey)
  108 + }
  109 + }
  110 +
  111 + return keys
  112 +}
  113 +
  114 +// KeysInt64 returns int64 keys in s.
  115 +func (s *Set) KeysInt64() []int64 {
  116 + var keys []int64
  117 +
  118 + for key := range s.data {
  119 + if intKey, ok := key.(int64); ok {
  120 + keys = append(keys, intKey)
  121 + }
  122 + }
  123 +
  124 + return keys
  125 +}
  126 +
  127 +// KeysUint returns uint keys in s.
  128 +func (s *Set) KeysUint() []uint {
  129 + var keys []uint
  130 +
  131 + for key := range s.data {
  132 + if intKey, ok := key.(uint); ok {
  133 + keys = append(keys, intKey)
  134 + }
  135 + }
  136 +
  137 + return keys
  138 +}
  139 +
  140 +// KeysUint64 returns uint64 keys in s.
  141 +func (s *Set) KeysUint64() []uint64 {
  142 + var keys []uint64
  143 +
  144 + for key := range s.data {
  145 + if intKey, ok := key.(uint64); ok {
  146 + keys = append(keys, intKey)
  147 + }
  148 + }
  149 +
  150 + return keys
  151 +}
  152 +
  153 +// KeysStr returns string keys in s.
  154 +func (s *Set) KeysStr() []string {
  155 + var keys []string
  156 +
  157 + for key := range s.data {
  158 + if strKey, ok := key.(string); ok {
  159 + keys = append(keys, strKey)
  160 + }
  161 + }
  162 +
  163 + return keys
  164 +}
  165 +
  166 +// Remove removes i from s.
  167 +func (s *Set) Remove(i interface{}) {
  168 + s.validate(i)
  169 + delete(s.data, i)
  170 +}
  171 +
  172 +// Count returns the number of items in s.
  173 +func (s *Set) Count() int {
  174 + return len(s.data)
  175 +}
  176 +
  177 +func (s *Set) add(i interface{}) {
  178 + switch s.tp {
  179 + case unmanaged:
  180 + // do nothing
  181 + case untyped:
  182 + s.setType(i)
  183 + default:
  184 + s.validate(i)
  185 + }
  186 + s.data[i] = PlaceholderType{}
  187 +}
  188 +
  189 +func (s *Set) setType(i interface{}) {
  190 + // s.tp can only be untyped here
  191 + switch i.(type) {
  192 + case int:
  193 + s.tp = intType
  194 + case int64:
  195 + s.tp = int64Type
  196 + case uint:
  197 + s.tp = uintType
  198 + case uint64:
  199 + s.tp = uint64Type
  200 + case string:
  201 + s.tp = stringType
  202 + }
  203 +}
  204 +
  205 +func (s *Set) validate(i interface{}) {
  206 + if s.tp == unmanaged {
  207 + return
  208 + }
  209 +
  210 + switch i.(type) {
  211 + case int:
  212 + if s.tp != intType {
  213 + //logx.Errorf("Error: element is int, but set contains elements with type %d", s.tp)
  214 + }
  215 + case int64:
  216 + if s.tp != int64Type {
  217 + //logx.Errorf("Error: element is int64, but set contains elements with type %d", s.tp)
  218 + }
  219 + case uint:
  220 + if s.tp != uintType {
  221 + //logx.Errorf("Error: element is uint, but set contains elements with type %d", s.tp)
  222 + }
  223 + case uint64:
  224 + if s.tp != uint64Type {
  225 + //logx.Errorf("Error: element is uint64, but set contains elements with type %d", s.tp)
  226 + }
  227 + case string:
  228 + if s.tp != stringType {
  229 + //logx.Errorf("Error: element is string, but set contains elements with type %d", s.tp)
  230 + }
  231 + }
  232 +}