作者 tangxuhui

调试、修复错误

@@ -84,7 +84,7 @@ func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error { @@ -84,7 +84,7 @@ func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error {
84 "FDocumentStatus", "FForbidStatus", "FErpClsID", 84 "FDocumentStatus", "FForbidStatus", "FErpClsID",
85 "FBaseUnitId", "FBaseUnitId.FName", "FCreateDate", "FModifyDate", 85 "FBaseUnitId", "FBaseUnitId.FName", "FCreateDate", "FModifyDate",
86 "FForbidDate", "FApproveDate", "FMaterialGroup", "FMaterialGroup.FName", 86 "FForbidDate", "FApproveDate", "FMaterialGroup", "FMaterialGroup.FName",
87 - "FRefStatus", "FMaterialGroup.FNumber", "FUseOrgId", 87 + "FRefStatus", "FMaterialGroup.FNumber", "FUseOrgId", "FUseOrgId.FName",
88 } 88 }
89 var ( 89 var (
90 startRow int 90 startRow int
@@ -153,6 +153,7 @@ func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error { @@ -153,6 +153,7 @@ func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error {
153 MaterialGroupName: item["FMaterialGroup.FName"], 153 MaterialGroupName: item["FMaterialGroup.FName"],
154 RefStatus: refStatus, 154 RefStatus: refStatus,
155 UseOrgId: fUseOrgId, 155 UseOrgId: fUseOrgId,
  156 + UseOrgName: item["FUseOrgId.FName"],
156 JoinProductId: 0, 157 JoinProductId: 0,
157 DataVersion: nowTime.Unix(), 158 DataVersion: nowTime.Unix(),
158 } 159 }
  1 +package dao
  2 +
  3 +import (
  4 + "fmt"
  5 + "strconv"
  6 + "strings"
  7 +
  8 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
  10 +)
  11 +
  12 +type MaterialGroupK3cloudDao struct {
  13 + transactionContext *pgTransaction.TransactionContext
  14 +}
  15 +
  16 +func NewMaterialGroupK3cloudDao(transactionContext *pgTransaction.TransactionContext) (*MaterialGroupK3cloudDao, error) {
  17 + if transactionContext == nil {
  18 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  19 + } else {
  20 + return &MaterialGroupK3cloudDao{
  21 + transactionContext: transactionContext,
  22 + }, nil
  23 + }
  24 +}
  25 +
  26 +func (d *MaterialGroupK3cloudDao) GetLastVersion() (int64, error) {
  27 + var materialData []models.MaterialK3cloud
  28 + err := d.transactionContext.PgTx.Model(&materialData).
  29 + Order("data_version DESC").
  30 + Limit(1).
  31 + Select()
  32 + if err != nil {
  33 + return 0, err
  34 + }
  35 + if len(materialData) == 0 {
  36 + return 0, nil
  37 + }
  38 + return materialData[0].DataVersion, nil
  39 +}
  40 +
  41 +//SyncDataMaterialK3cloud 同步MaterialK3cloud表数据
  42 +func (d *MaterialGroupK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3cloud) error {
  43 + sqlValues := []string{}
  44 + var strTemp []string
  45 + for i := range data {
  46 + strTemp = make([]string, 0, 18)
  47 + strTemp = append(strTemp, strconv.Itoa(data[i].MaterialId))
  48 + strTemp = append(strTemp, `'`+data[i].Name+`'`)
  49 + strTemp = append(strTemp, `'`+data[i].Number+`'`)
  50 + strTemp = append(strTemp, `'`+data[i].Specification+`'`)
  51 + strTemp = append(strTemp, `'`+data[i].ForbidStatus+`'`)
  52 + strTemp = append(strTemp, strconv.Itoa(data[i].ErpClsId))
  53 + strTemp = append(strTemp, strconv.Itoa(data[i].BaseUnitId))
  54 + strTemp = append(strTemp, `'`+data[i].BaseUnitName+`'`)
  55 + if data[i].CreateDate.IsZero() {
  56 + strTemp = append(strTemp, `NULL`)
  57 + } else {
  58 + strTemp = append(strTemp, `'`+data[i].CreateDate.String()+`'`)
  59 + }
  60 + if data[i].ModifyDate.IsZero() {
  61 + strTemp = append(strTemp, `NULL`)
  62 + } else {
  63 + strTemp = append(strTemp, `'`+data[i].ModifyDate.String()+`'`)
  64 + }
  65 + if data[i].ForbidDate.IsZero() {
  66 + strTemp = append(strTemp, `NULL`)
  67 + } else {
  68 + strTemp = append(strTemp, `'`+data[i].ForbidDate.String()+`'`)
  69 + }
  70 + if data[i].ApproveDate.IsZero() {
  71 + strTemp = append(strTemp, `NULL`)
  72 + } else {
  73 + strTemp = append(strTemp, `'`+data[i].ApproveDate.String()+`'`)
  74 + }
  75 + strTemp = append(strTemp, strconv.Itoa(data[i].MaterialGroup))
  76 + strTemp = append(strTemp, `'`+data[i].MaterialGroupNumber+`'`)
  77 + strTemp = append(strTemp, `'`+data[i].MaterialGroupName+`'`)
  78 + strTemp = append(strTemp, strconv.Itoa(data[i].RefStatus))
  79 + //关联的产品表id ,使用 product 产品表的自增序列表
  80 + strTemp = append(strTemp, "nextval('manufacture.product_product_id_seq'::regclass)")
  81 + strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
  82 + strTemp = append(strTemp, strconv.Itoa(data[i].UseOrgId))
  83 + sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")")
  84 +
  85 + }
  86 + var valueTemp []string
  87 + for i := 0; i < len(sqlValues); i += 100 {
  88 + if i < len(sqlValues)-100 {
  89 + valueTemp = sqlValues[i:100]
  90 + } else {
  91 + valueTemp = sqlValues[i:]
  92 + }
  93 + sql := `INSERT INTO "manufacture"."material_k3cloud" (
  94 + "material_id","name","number","specification","forbid_status",
  95 + "erp_cls_id","base_unit_id","base_unit_name","create_date",
  96 + "modify_date","forbid_date","approve_date","material_group",
  97 + "material_group_number","material_group_name","ref_status",
  98 + "join_product_id","data_version","use_org_id" )
  99 + VALUES ` + strings.Join(valueTemp, ",") +
  100 + ` ON conflict ( material_id ) DO
  101 + UPDATE
  102 + SET (
  103 + "name","number","specification","forbid_status","erp_cls_id",
  104 + "base_unit_id","base_unit_name","create_date","modify_date",
  105 + "forbid_date","approve_date","material_group","material_group_number",
  106 + "material_group_name","ref_status","data_version" ) = (
  107 + EXCLUDED."name",EXCLUDED."number",EXCLUDED."specification",
  108 + EXCLUDED."forbid_status",EXCLUDED."erp_cls_id",EXCLUDED."base_unit_id",
  109 + EXCLUDED."base_unit_name",EXCLUDED."create_date",EXCLUDED."modify_date",
  110 + EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group",
  111 + EXCLUDED."material_group_number",EXCLUDED."material_group_name",
  112 + EXCLUDED."ref_status",EXCLUDED."data_version" )`
  113 + _, err := d.transactionContext.PgTx.Exec(sql)
  114 + if err != nil {
  115 + return err
  116 + }
  117 + }
  118 +
  119 + return nil
  120 +}
@@ -64,22 +64,22 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo @@ -64,22 +64,22 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo
64 if data[i].CreateDate.IsZero() { 64 if data[i].CreateDate.IsZero() {
65 strTemp = append(strTemp, `NULL`) 65 strTemp = append(strTemp, `NULL`)
66 } else { 66 } else {
67 - strTemp = append(strTemp, `'`+data[i].CreateDate.String()+`'`) 67 + strTemp = append(strTemp, `'`+data[i].CreateDate.Format("2006-01-02 15:04:05.999")+`'`)
68 } 68 }
69 if data[i].ModifyDate.IsZero() { 69 if data[i].ModifyDate.IsZero() {
70 strTemp = append(strTemp, `NULL`) 70 strTemp = append(strTemp, `NULL`)
71 } else { 71 } else {
72 - strTemp = append(strTemp, `'`+data[i].ModifyDate.String()+`'`) 72 + strTemp = append(strTemp, `'`+data[i].ModifyDate.Format("2006-01-02 15:04:05.999")+`'`)
73 } 73 }
74 if data[i].ForbidDate.IsZero() { 74 if data[i].ForbidDate.IsZero() {
75 strTemp = append(strTemp, `NULL`) 75 strTemp = append(strTemp, `NULL`)
76 } else { 76 } else {
77 - strTemp = append(strTemp, `'`+data[i].ForbidDate.String()+`'`) 77 + strTemp = append(strTemp, `'`+data[i].ForbidDate.Format("2006-01-02 15:04:05.999")+`'`)
78 } 78 }
79 if data[i].ApproveDate.IsZero() { 79 if data[i].ApproveDate.IsZero() {
80 strTemp = append(strTemp, `NULL`) 80 strTemp = append(strTemp, `NULL`)
81 } else { 81 } else {
82 - strTemp = append(strTemp, `'`+data[i].ApproveDate.String()+`'`) 82 + strTemp = append(strTemp, `'`+data[i].ApproveDate.Format("2006-01-02 15:04:05.999")+`'`)
83 } 83 }
84 strTemp = append(strTemp, strconv.Itoa(data[i].MaterialGroup)) 84 strTemp = append(strTemp, strconv.Itoa(data[i].MaterialGroup))
85 strTemp = append(strTemp, `'`+data[i].MaterialGroupNumber+`'`) 85 strTemp = append(strTemp, `'`+data[i].MaterialGroupNumber+`'`)
@@ -89,22 +89,24 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo @@ -89,22 +89,24 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo
89 strTemp = append(strTemp, "nextval('manufacture.product_product_id_seq'::regclass)") 89 strTemp = append(strTemp, "nextval('manufacture.product_product_id_seq'::regclass)")
90 strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion))) 90 strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
91 strTemp = append(strTemp, strconv.Itoa(data[i].UseOrgId)) 91 strTemp = append(strTemp, strconv.Itoa(data[i].UseOrgId))
  92 + strTemp = append(strTemp, `'`+data[i].UseOrgName+`'`)
92 sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")") 93 sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")")
93 94
94 } 95 }
95 var valueTemp []string 96 var valueTemp []string
  97 + fmt.Println("总数", len(data))
96 for i := 0; i < len(sqlValues); i += 100 { 98 for i := 0; i < len(sqlValues); i += 100 {
97 - if i < len(sqlValues)-100 {  
98 - valueTemp = sqlValues[i:100] 99 + if i <= len(sqlValues)-100 {
  100 + valueTemp = sqlValues[i : i+100]
99 } else { 101 } else {
100 - valueTemp = sqlValues[i:] 102 + valueTemp = sqlValues[i-100:]
101 } 103 }
102 sql := `INSERT INTO "manufacture"."material_k3cloud" ( 104 sql := `INSERT INTO "manufacture"."material_k3cloud" (
103 "material_id","name","number","specification","forbid_status", 105 "material_id","name","number","specification","forbid_status",
104 "erp_cls_id","base_unit_id","base_unit_name","create_date", 106 "erp_cls_id","base_unit_id","base_unit_name","create_date",
105 "modify_date","forbid_date","approve_date","material_group", 107 "modify_date","forbid_date","approve_date","material_group",
106 - "material_group_number","material_group_name","ref_status ",  
107 - "join_product_id","data_version","use_org_id" ) 108 + "material_group_number","material_group_name","ref_status",
  109 + "join_product_id","data_version","use_org_id","use_org_name" )
108 VALUES ` + strings.Join(valueTemp, ",") + 110 VALUES ` + strings.Join(valueTemp, ",") +
109 ` ON conflict ( material_id ) DO 111 ` ON conflict ( material_id ) DO
110 UPDATE 112 UPDATE
@@ -112,13 +114,13 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo @@ -112,13 +114,13 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo
112 "name","number","specification","forbid_status","erp_cls_id", 114 "name","number","specification","forbid_status","erp_cls_id",
113 "base_unit_id","base_unit_name","create_date","modify_date", 115 "base_unit_id","base_unit_name","create_date","modify_date",
114 "forbid_date","approve_date","material_group","material_group_number", 116 "forbid_date","approve_date","material_group","material_group_number",
115 - "material_group_name","ref_status ","data_version" ) = ( 117 + "material_group_name","ref_status","data_version","use_org_id","use_org_name" ) = (
116 EXCLUDED."name",EXCLUDED."number",EXCLUDED."specification", 118 EXCLUDED."name",EXCLUDED."number",EXCLUDED."specification",
117 EXCLUDED."forbid_status",EXCLUDED."erp_cls_id",EXCLUDED."base_unit_id", 119 EXCLUDED."forbid_status",EXCLUDED."erp_cls_id",EXCLUDED."base_unit_id",
118 EXCLUDED."base_unit_name",EXCLUDED."create_date",EXCLUDED."modify_date", 120 EXCLUDED."base_unit_name",EXCLUDED."create_date",EXCLUDED."modify_date",
119 EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group", 121 EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group",
120 EXCLUDED."material_group_number",EXCLUDED."material_group_name", 122 EXCLUDED."material_group_number",EXCLUDED."material_group_name",
121 - EXCLUDED."ref_status ",EXCLUDED."data_version" )` 123 + EXCLUDED."ref_status",EXCLUDED."data_version",EXCLUDED."use_org_id",EXCLUDED."use_org_name")`
122 _, err := d.transactionContext.PgTx.Exec(sql) 124 _, err := d.transactionContext.PgTx.Exec(sql)
123 if err != nil { 125 if err != nil {
124 return err 126 return err
@@ -137,12 +139,12 @@ func (d *MaterialK3cloudDao) SyncDataProudct(version int64) error { @@ -137,12 +139,12 @@ func (d *MaterialK3cloudDao) SyncDataProudct(version int64) error {
137 ) 139 )
138 SELECT 0,use_org_id,"join_product_id","number","name","material_group_name", 140 SELECT 0,use_org_id,"join_product_id","number","name","material_group_name",
139 json_build_object('unit',specification),now(),now() 141 json_build_object('unit',specification),now(),now()
140 - FROM "manufacture"."material_k3cloud" WHERE "data_version"=? 142 + FROM "manufacture"."material_k3cloud" WHERE "data_version"=? AND "material_group_number" LIKE '05%'
141 ON conflict ( product_id ) DO 143 ON conflict ( product_id ) DO
142 UPDATE 144 UPDATE
143 SET ( 145 SET (
144 "company_id", "org_id", "product_id", "product_code", "product_name", 146 "company_id", "org_id", "product_id", "product_code", "product_name",
145 - "product_category", "product_spec", "created_at", "updated_at")=( 147 + "product_category", "product_spec", "updated_at")=(
146 EXCLUDED."company_id", EXCLUDED."org_id",EXCLUDED."product_id", 148 EXCLUDED."company_id", EXCLUDED."org_id",EXCLUDED."product_id",
147 EXCLUDED."product_code",EXCLUDED."product_name",EXCLUDED."product_category", 149 EXCLUDED."product_code",EXCLUDED."product_name",EXCLUDED."product_category",
148 EXCLUDED."product_spec", EXCLUDED."updated_at") ` 150 EXCLUDED."product_spec", EXCLUDED."updated_at") `
@@ -2,11 +2,12 @@ package pg @@ -2,11 +2,12 @@ package pg
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "reflect"
  6 +
5 "github.com/go-pg/pg/v10" 7 "github.com/go-pg/pg/v10"
6 "github.com/go-pg/pg/v10/orm" 8 "github.com/go-pg/pg/v10/orm"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
9 - "reflect"  
10 11
11 "github.com/linmadan/egglib-go/persistent/pg/hooks" 12 "github.com/linmadan/egglib-go/persistent/pg/hooks"
12 ) 13 )
@@ -35,6 +36,7 @@ func init() { @@ -35,6 +36,7 @@ func init() {
35 (*models.ProductRecord)(nil), 36 (*models.ProductRecord)(nil),
36 (*models.UnitConversion)(nil), 37 (*models.UnitConversion)(nil),
37 (*models.Workshop)(nil), 38 (*models.Workshop)(nil),
  39 + (*models.MaterialK3cloud)(nil),
38 } { 40 } {
39 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ 41 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
40 Temp: false, 42 Temp: false,
@@ -22,6 +22,7 @@ type MaterialK3cloud struct { @@ -22,6 +22,7 @@ type MaterialK3cloud struct {
22 MaterialGroupName string `comment:"物料分组名称" pg:"material_group_name"` 22 MaterialGroupName string `comment:"物料分组名称" pg:"material_group_name"`
23 RefStatus int `comment:"是否使用" pg:"ref_status"` 23 RefStatus int `comment:"是否使用" pg:"ref_status"`
24 UseOrgId int `comment:"使用组织" pg:"use_org_id"` 24 UseOrgId int `comment:"使用组织" pg:"use_org_id"`
  25 + UseOrgName string `comment:"使用组织" pg:"use_org_name"`
25 JoinProductId int64 `comment:"关联的product表id" pg:"join_product_id"` 26 JoinProductId int64 `comment:"关联的product表id" pg:"join_product_id"`
26 DataVersion int64 `comment:"数据版本" pg:"data_version"` 27 DataVersion int64 `comment:"数据版本" pg:"data_version"`
27 } 28 }
1 package k3cloud 1 package k3cloud
2 2
3 import ( 3 import (
  4 + "strings"
4 "testing" 5 "testing"
5 ) 6 )
6 7
@@ -34,14 +35,20 @@ func TestExecuteBillQuery(t *testing.T) { @@ -34,14 +35,20 @@ func TestExecuteBillQuery(t *testing.T) {
34 // {"FApproveDate", "审核日期"}, {"FOldNumber", "旧物料编码"}, {"FMaterialGroup", "物料分组"}, {"FPLMMaterialId", "PLM物料内码"}, {"FMaterialSRC", "物料来源"}, 35 // {"FApproveDate", "审核日期"}, {"FOldNumber", "旧物料编码"}, {"FMaterialGroup", "物料分组"}, {"FPLMMaterialId", "PLM物料内码"}, {"FMaterialSRC", "物料来源"},
35 // {"FIsSalseByNet", "是否网销"}, {"FIsAutoAllocate", "自动分配"}, {"FSPUID", "SPU信息"}, {"FPinYin", "拼音"}, {"FDSMatchByLot", "按批号匹配供需"}, 36 // {"FIsSalseByNet", "是否网销"}, {"FIsAutoAllocate", "自动分配"}, {"FSPUID", "SPU信息"}, {"FPinYin", "拼音"}, {"FDSMatchByLot", "按批号匹配供需"},
36 // {"FForbidReson", "禁用原因"}, {"FRefStatus", "已使用"}} 37 // {"FForbidReson", "禁用原因"}, {"FRefStatus", "已使用"}}
37 - //FMATERIALID,FSpecification,FName,FNumber,FModifyDate,FBaseUnitId.FName,FUseOrgId.FName 38 + fieldKeys := []string{
  39 + "FMATERIALID", "FSpecification", "FName", "FNumber",
  40 + "FDocumentStatus", "FForbidStatus", "FErpClsID",
  41 + "FBaseUnitId", "FBaseUnitId.FName", "FCreateDate", "FModifyDate",
  42 + "FForbidDate", "FApproveDate", "FMaterialGroup", "FMaterialGroup.FName",
  43 + "FRefStatus", "FMaterialGroup.FNumber", "FUseOrgId", "FUseOrgId.FName",
  44 + }
38 result, err := client.ExecuteBillQuery(RequestExecuteBillQuery{ 45 result, err := client.ExecuteBillQuery(RequestExecuteBillQuery{
39 FormId: "BD_MATERIAL", 46 FormId: "BD_MATERIAL",
40 Data: ExecuteBillQueryData{ 47 Data: ExecuteBillQueryData{
41 FormId: "BD_MATERIAL", 48 FormId: "BD_MATERIAL",
42 - FieldKeys: "FMATERIALID,FSpecification,FName,FNumber,FModifyDate,FBaseUnitId.FName,FUseOrgId,FUseOrgId.FName", //查询的字段  
43 - TopRowCount: 0,  
44 - FilterString: `FMaterialGroup.FNumber like '05%' and FModifyDate<'2022-01-08T19:36:06'`, 49 + FieldKeys: strings.Join(fieldKeys, ","), //查询的字段
  50 + TopRowCount: 5,
  51 + FilterString: "",
45 }, 52 },
46 }) 53 })
47 t.Logf("result buf===> %s \n", string(result.Buf)) 54 t.Logf("result buf===> %s \n", string(result.Buf))
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/web/beego"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/syncdata"
  6 +)
  7 +
  8 +type TestController struct {
  9 + beego.BaseController
  10 +}
  11 +
  12 +func (c *TestController) InvokPullMaterialNewest() {
  13 + srv := syncdata.PullDataK3CloudService{}
  14 + err := srv.PullMaterialNewest()
  15 + c.Response(nil, err)
  16 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + web.Router("/TestController/PullMaterialNewest", &controllers.TestController{}, "Get:InvokPullMaterialNewest")
  10 +}