作者 yangfu

refactor: 生产计划同步优化

1 package syncdata 1 package syncdata
2 2
3 import ( 3 import (
4 - "fmt"  
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 4 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
9 "strconv" 9 "strconv"
10 "strings" 10 "strings"
11 "time" 11 "time"
@@ -526,18 +526,13 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa @@ -526,18 +526,13 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa
526 // 计划信息 526 // 计划信息
527 plan, err = productPlanRepository.FindOne(map[string]interface{}{"companyId": cid, "orgId": oid, "erpBillNo": v.BillNo, "productCode": v.MaterialNumber}) 527 plan, err = productPlanRepository.FindOne(map[string]interface{}{"companyId": cid, "orgId": oid, "erpBillNo": v.BillNo, "productCode": v.MaterialNumber})
528 if err == nil && plan != nil { 528 if err == nil && plan != nil {
529 - //plan.UpdatedAt = time.Now()  
530 continue 529 continue
531 } 530 }
532 - count, _, errFindPlan := productPlanRepository.Find(map[string]interface{}{"companyId": cid, "orgId": oid, "erpBillNo": v.BillNo, "productCode": v.MaterialNumber})  
533 - if errFindPlan != nil { 531 + batchNumber, genBatchNumberErr := redis.GenCode(redis.NewBillNoCodeCache(cid, v.BillNo))
  532 + if genBatchNumberErr != nil {
  533 + log.Logger.Error(err.Error(), map[string]interface{}{"id": v.RowId, "err": "gen batch number error"})
534 continue 534 continue
535 } 535 }
536 - /* ERP系统BillNO + MaterialNumber 唯一 会重复,需要做自增格式化处理*/  
537 - batchNumber := fmt.Sprintf("%v-1", v.BillNo)  
538 - if count >= 1 {  
539 - batchNumber = fmt.Sprintf("%v-%v", v.BillNo, count)  
540 - }  
541 if err == domain.ErrorNotFound { 536 if err == domain.ErrorNotFound {
542 plan = &domain.ProductPlan{ 537 plan = &domain.ProductPlan{
543 CompanyId: cid, 538 CompanyId: cid,
@@ -556,7 +551,7 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa @@ -556,7 +551,7 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa
556 // 更新数据 551 // 更新数据
557 plan.PlanDevoted = &domain.UnitQuantity{ 552 plan.PlanDevoted = &domain.UnitQuantity{
558 Unit: product.ProductSpec.Unit, 553 Unit: product.ProductSpec.Unit,
559 - Quantity: 0, 554 + Quantity: v.Qty,
560 Weight: product.ProductWeigh(0), 555 Weight: product.ProductWeigh(0),
561 UnitWeight: product.ProductSpec.UnitWeight, 556 UnitWeight: product.ProductSpec.UnitWeight,
562 } 557 }
@@ -86,3 +86,25 @@ func NewPlanBatchCodeCache(id int) PlanBatchCodeCache { @@ -86,3 +86,25 @@ func NewPlanBatchCodeCache(id int) PlanBatchCodeCache {
86 Time: time.Now(), 86 Time: time.Now(),
87 } 87 }
88 } 88 }
  89 +
  90 +type BillNoCodeCache struct {
  91 + CompanyId int
  92 + BillNo string
  93 +}
  94 +
  95 +func (ca BillNoCodeCache) CacheKey() string {
  96 + str := fmt.Sprintf("%v:bill-no:%v:%v", constant.CACHE_PREFIX, ca.CompanyId, ca.BillNo)
  97 + return str
  98 +}
  99 +func (ca BillNoCodeCache) Format(index int) string {
  100 + return fmt.Sprintf("%v-%v", ca.BillNo, index)
  101 +}
  102 +func (ca BillNoCodeCache) Expire() time.Duration {
  103 + return time.Hour * 24 * 5
  104 +}
  105 +func NewBillNoCodeCache(id int, billNo string) BillNoCodeCache {
  106 + return BillNoCodeCache{
  107 + CompanyId: id,
  108 + BillNo: billNo,
  109 + }
  110 +}