作者 yangfu

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,deleted_at);
5 5
6 -- 表product_material 增加唯一索引 idx_unq_product_material_company_id_material_number 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);  
  7 +create UNIQUE INDEX idx_unq_product_material_company_id_material_number on manufacture.product_material using btree(company_id,material_number,deleted_at);
@@ -6,11 +6,8 @@ import ( @@ -6,11 +6,8 @@ import (
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
7 "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" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
9 - "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/mqtt"  
10 - "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/task"  
11 "time" 9 "time"
12 10
13 - "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/crontab"  
14 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 11 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
15 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" 12 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
16 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" 13 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
@@ -30,11 +27,11 @@ func main() { @@ -30,11 +27,11 @@ func main() {
30 log.Logger.Info("server start ....") 27 log.Logger.Info("server start ....")
31 log.Logger.Debug(fmt.Sprintf("ENABLE_KAFKA_LOG:%v", constant.ENABLE_KAFKA_LOG)) 28 log.Logger.Debug(fmt.Sprintf("ENABLE_KAFKA_LOG:%v", constant.ENABLE_KAFKA_LOG))
32 29
33 - go mqtt.Start(log.Logger)  
34 - go task.Run()  
35 - cron := crontab.NewCrontabService(nil)  
36 - cron.StartCrontabTask()  
37 - defer cron.StopCrontabTask() 30 + //go mqtt.Start(log.Logger)
  31 + //go task.Run()
  32 + //cron := crontab.NewCrontabService(nil)
  33 + //cron.StartCrontabTask()
  34 + //defer cron.StopCrontabTask()
38 time.Sleep(time.Second) 35 time.Sleep(time.Second)
39 log.Logger.Info("server start!") 36 log.Logger.Info("server start!")
40 web.Run() 37 web.Run()
@@ -846,6 +846,8 @@ func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.Transacti @@ -846,6 +846,8 @@ func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.Transacti
846 return err 846 return err
847 } 847 }
848 log.Logger.Debug(fmt.Sprintf("【同步物料数据】查询数据条数:%v", len(records))) 848 log.Logger.Debug(fmt.Sprintf("【同步物料数据】查询数据条数:%v", len(records)))
  849 + _, materialsFromDb, _ := productMaterialRepository.Find(map[string]interface{}{"companyId": cid, "limit": 20000})
  850 + mapMaterialsFromDb := domain.ProductMaterials(materialsFromDb).ToMapByNumber()
849 for index, item := range records { 851 for index, item := range records {
850 var ( 852 var (
851 material *domain.ProductMaterial 853 material *domain.ProductMaterial
@@ -854,14 +856,13 @@ func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.Transacti @@ -854,14 +856,13 @@ func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.Transacti
854 attribute string //物料属性 856 attribute string //物料属性
855 category string //物料类别 857 category string //物料类别
856 ) 858 )
857 - if materialGroup, ok = mapMaterialGroup[item.MaterialGroupNumber]; !ok { 859 + if item.UseOrgName != "素天下" {
858 continue 860 continue
859 } 861 }
860 - material, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": cid, "materialNumber": item.Number})  
861 - if err == nil && material != nil { 862 + if materialGroup, ok = mapMaterialGroup[item.MaterialGroupNumber]; !ok {
862 continue 863 continue
863 } 864 }
864 - if err != domain.ErrorNotFound { 865 + if _, ok := mapMaterialsFromDb[item.Number]; ok {
865 continue 866 continue
866 } 867 }
867 if v, ok := mapMaterialCategory[item.FCategoryID]; ok { 868 if v, ok := mapMaterialCategory[item.FCategoryID]; ok {
@@ -889,7 +890,8 @@ func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.Transacti @@ -889,7 +890,8 @@ func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.Transacti
889 Ext: domain.NewExt(org.OrgName), 890 Ext: domain.NewExt(org.OrgName),
890 } 891 }
891 if material, err = productMaterialRepository.Save(material); err != nil { 892 if material, err = productMaterialRepository.Save(material); err != nil {
892 - return err 893 + log.Logger.Debug(fmt.Sprintf("同步失败 序号:%v 物料数据 id:%v number:%v name:%v 错误:%v", index, material.ProductMaterialId, material.MaterialNumber, material.MaterialName, err.Error()))
  894 + continue
893 } 895 }
894 log.Logger.Debug(fmt.Sprintf("同步 序号:%v 物料数据 id:%v number:%v name:%v", index, material.ProductMaterialId, material.MaterialNumber, material.MaterialName)) 896 log.Logger.Debug(fmt.Sprintf("同步 序号:%v 物料数据 id:%v number:%v name:%v", index, material.ProductMaterialId, material.MaterialNumber, material.MaterialName))
895 } 897 }