作者 tangxuhui

调试、修复错误

... ... @@ -84,7 +84,7 @@ func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error {
"FDocumentStatus", "FForbidStatus", "FErpClsID",
"FBaseUnitId", "FBaseUnitId.FName", "FCreateDate", "FModifyDate",
"FForbidDate", "FApproveDate", "FMaterialGroup", "FMaterialGroup.FName",
"FRefStatus", "FMaterialGroup.FNumber", "FUseOrgId",
"FRefStatus", "FMaterialGroup.FNumber", "FUseOrgId", "FUseOrgId.FName",
}
var (
startRow int
... ... @@ -153,6 +153,7 @@ func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error {
MaterialGroupName: item["FMaterialGroup.FName"],
RefStatus: refStatus,
UseOrgId: fUseOrgId,
UseOrgName: item["FUseOrgId.FName"],
JoinProductId: 0,
DataVersion: nowTime.Unix(),
}
... ...
package dao
import (
"fmt"
"strconv"
"strings"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
type MaterialGroupK3cloudDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewMaterialGroupK3cloudDao(transactionContext *pgTransaction.TransactionContext) (*MaterialGroupK3cloudDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &MaterialGroupK3cloudDao{
transactionContext: transactionContext,
}, nil
}
}
func (d *MaterialGroupK3cloudDao) GetLastVersion() (int64, error) {
var materialData []models.MaterialK3cloud
err := d.transactionContext.PgTx.Model(&materialData).
Order("data_version DESC").
Limit(1).
Select()
if err != nil {
return 0, err
}
if len(materialData) == 0 {
return 0, nil
}
return materialData[0].DataVersion, nil
}
//SyncDataMaterialK3cloud 同步MaterialK3cloud表数据
func (d *MaterialGroupK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3cloud) error {
sqlValues := []string{}
var strTemp []string
for i := range data {
strTemp = make([]string, 0, 18)
strTemp = append(strTemp, strconv.Itoa(data[i].MaterialId))
strTemp = append(strTemp, `'`+data[i].Name+`'`)
strTemp = append(strTemp, `'`+data[i].Number+`'`)
strTemp = append(strTemp, `'`+data[i].Specification+`'`)
strTemp = append(strTemp, `'`+data[i].ForbidStatus+`'`)
strTemp = append(strTemp, strconv.Itoa(data[i].ErpClsId))
strTemp = append(strTemp, strconv.Itoa(data[i].BaseUnitId))
strTemp = append(strTemp, `'`+data[i].BaseUnitName+`'`)
if data[i].CreateDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].CreateDate.String()+`'`)
}
if data[i].ModifyDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].ModifyDate.String()+`'`)
}
if data[i].ForbidDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].ForbidDate.String()+`'`)
}
if data[i].ApproveDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].ApproveDate.String()+`'`)
}
strTemp = append(strTemp, strconv.Itoa(data[i].MaterialGroup))
strTemp = append(strTemp, `'`+data[i].MaterialGroupNumber+`'`)
strTemp = append(strTemp, `'`+data[i].MaterialGroupName+`'`)
strTemp = append(strTemp, strconv.Itoa(data[i].RefStatus))
//关联的产品表id ,使用 product 产品表的自增序列表
strTemp = append(strTemp, "nextval('manufacture.product_product_id_seq'::regclass)")
strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
strTemp = append(strTemp, strconv.Itoa(data[i].UseOrgId))
sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")")
}
var valueTemp []string
for i := 0; i < len(sqlValues); i += 100 {
if i < len(sqlValues)-100 {
valueTemp = sqlValues[i:100]
} else {
valueTemp = sqlValues[i:]
}
sql := `INSERT INTO "manufacture"."material_k3cloud" (
"material_id","name","number","specification","forbid_status",
"erp_cls_id","base_unit_id","base_unit_name","create_date",
"modify_date","forbid_date","approve_date","material_group",
"material_group_number","material_group_name","ref_status",
"join_product_id","data_version","use_org_id" )
VALUES ` + strings.Join(valueTemp, ",") +
` ON conflict ( material_id ) DO
UPDATE
SET (
"name","number","specification","forbid_status","erp_cls_id",
"base_unit_id","base_unit_name","create_date","modify_date",
"forbid_date","approve_date","material_group","material_group_number",
"material_group_name","ref_status","data_version" ) = (
EXCLUDED."name",EXCLUDED."number",EXCLUDED."specification",
EXCLUDED."forbid_status",EXCLUDED."erp_cls_id",EXCLUDED."base_unit_id",
EXCLUDED."base_unit_name",EXCLUDED."create_date",EXCLUDED."modify_date",
EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group",
EXCLUDED."material_group_number",EXCLUDED."material_group_name",
EXCLUDED."ref_status",EXCLUDED."data_version" )`
_, err := d.transactionContext.PgTx.Exec(sql)
if err != nil {
return err
}
}
return nil
}
... ...
... ... @@ -64,22 +64,22 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo
if data[i].CreateDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].CreateDate.String()+`'`)
strTemp = append(strTemp, `'`+data[i].CreateDate.Format("2006-01-02 15:04:05.999")+`'`)
}
if data[i].ModifyDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].ModifyDate.String()+`'`)
strTemp = append(strTemp, `'`+data[i].ModifyDate.Format("2006-01-02 15:04:05.999")+`'`)
}
if data[i].ForbidDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].ForbidDate.String()+`'`)
strTemp = append(strTemp, `'`+data[i].ForbidDate.Format("2006-01-02 15:04:05.999")+`'`)
}
if data[i].ApproveDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].ApproveDate.String()+`'`)
strTemp = append(strTemp, `'`+data[i].ApproveDate.Format("2006-01-02 15:04:05.999")+`'`)
}
strTemp = append(strTemp, strconv.Itoa(data[i].MaterialGroup))
strTemp = append(strTemp, `'`+data[i].MaterialGroupNumber+`'`)
... ... @@ -89,22 +89,24 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo
strTemp = append(strTemp, "nextval('manufacture.product_product_id_seq'::regclass)")
strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
strTemp = append(strTemp, strconv.Itoa(data[i].UseOrgId))
strTemp = append(strTemp, `'`+data[i].UseOrgName+`'`)
sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")")
}
var valueTemp []string
fmt.Println("总数", len(data))
for i := 0; i < len(sqlValues); i += 100 {
if i < len(sqlValues)-100 {
valueTemp = sqlValues[i:100]
if i <= len(sqlValues)-100 {
valueTemp = sqlValues[i : i+100]
} else {
valueTemp = sqlValues[i:]
valueTemp = sqlValues[i-100:]
}
sql := `INSERT INTO "manufacture"."material_k3cloud" (
"material_id","name","number","specification","forbid_status",
"erp_cls_id","base_unit_id","base_unit_name","create_date",
"modify_date","forbid_date","approve_date","material_group",
"material_group_number","material_group_name","ref_status ",
"join_product_id","data_version","use_org_id" )
"material_group_number","material_group_name","ref_status",
"join_product_id","data_version","use_org_id","use_org_name" )
VALUES ` + strings.Join(valueTemp, ",") +
` ON conflict ( material_id ) DO
UPDATE
... ... @@ -112,13 +114,13 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo
"name","number","specification","forbid_status","erp_cls_id",
"base_unit_id","base_unit_name","create_date","modify_date",
"forbid_date","approve_date","material_group","material_group_number",
"material_group_name","ref_status ","data_version" ) = (
"material_group_name","ref_status","data_version","use_org_id","use_org_name" ) = (
EXCLUDED."name",EXCLUDED."number",EXCLUDED."specification",
EXCLUDED."forbid_status",EXCLUDED."erp_cls_id",EXCLUDED."base_unit_id",
EXCLUDED."base_unit_name",EXCLUDED."create_date",EXCLUDED."modify_date",
EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group",
EXCLUDED."material_group_number",EXCLUDED."material_group_name",
EXCLUDED."ref_status ",EXCLUDED."data_version" )`
EXCLUDED."ref_status",EXCLUDED."data_version",EXCLUDED."use_org_id",EXCLUDED."use_org_name")`
_, err := d.transactionContext.PgTx.Exec(sql)
if err != nil {
return err
... ... @@ -137,12 +139,12 @@ func (d *MaterialK3cloudDao) SyncDataProudct(version int64) error {
)
SELECT 0,use_org_id,"join_product_id","number","name","material_group_name",
json_build_object('unit',specification),now(),now()
FROM "manufacture"."material_k3cloud" WHERE "data_version"=?
FROM "manufacture"."material_k3cloud" WHERE "data_version"=? AND "material_group_number" LIKE '05%'
ON conflict ( product_id ) DO
UPDATE
SET (
"company_id", "org_id", "product_id", "product_code", "product_name",
"product_category", "product_spec", "created_at", "updated_at")=(
"product_category", "product_spec", "updated_at")=(
EXCLUDED."company_id", EXCLUDED."org_id",EXCLUDED."product_id",
EXCLUDED."product_code",EXCLUDED."product_name",EXCLUDED."product_category",
EXCLUDED."product_spec", EXCLUDED."updated_at") `
... ...
... ... @@ -2,11 +2,12 @@ package pg
import (
"fmt"
"reflect"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
"reflect"
"github.com/linmadan/egglib-go/persistent/pg/hooks"
)
... ... @@ -35,6 +36,7 @@ func init() {
(*models.ProductRecord)(nil),
(*models.UnitConversion)(nil),
(*models.Workshop)(nil),
(*models.MaterialK3cloud)(nil),
} {
err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
Temp: false,
... ...
... ... @@ -22,6 +22,7 @@ type MaterialK3cloud struct {
MaterialGroupName string `comment:"物料分组名称" pg:"material_group_name"`
RefStatus int `comment:"是否使用" pg:"ref_status"`
UseOrgId int `comment:"使用组织" pg:"use_org_id"`
UseOrgName string `comment:"使用组织" pg:"use_org_name"`
JoinProductId int64 `comment:"关联的product表id" pg:"join_product_id"`
DataVersion int64 `comment:"数据版本" pg:"data_version"`
}
... ...
package k3cloud
import (
"strings"
"testing"
)
... ... @@ -34,14 +35,20 @@ func TestExecuteBillQuery(t *testing.T) {
// {"FApproveDate", "审核日期"}, {"FOldNumber", "旧物料编码"}, {"FMaterialGroup", "物料分组"}, {"FPLMMaterialId", "PLM物料内码"}, {"FMaterialSRC", "物料来源"},
// {"FIsSalseByNet", "是否网销"}, {"FIsAutoAllocate", "自动分配"}, {"FSPUID", "SPU信息"}, {"FPinYin", "拼音"}, {"FDSMatchByLot", "按批号匹配供需"},
// {"FForbidReson", "禁用原因"}, {"FRefStatus", "已使用"}}
//FMATERIALID,FSpecification,FName,FNumber,FModifyDate,FBaseUnitId.FName,FUseOrgId.FName
fieldKeys := []string{
"FMATERIALID", "FSpecification", "FName", "FNumber",
"FDocumentStatus", "FForbidStatus", "FErpClsID",
"FBaseUnitId", "FBaseUnitId.FName", "FCreateDate", "FModifyDate",
"FForbidDate", "FApproveDate", "FMaterialGroup", "FMaterialGroup.FName",
"FRefStatus", "FMaterialGroup.FNumber", "FUseOrgId", "FUseOrgId.FName",
}
result, err := client.ExecuteBillQuery(RequestExecuteBillQuery{
FormId: "BD_MATERIAL",
Data: ExecuteBillQueryData{
FormId: "BD_MATERIAL",
FieldKeys: "FMATERIALID,FSpecification,FName,FNumber,FModifyDate,FBaseUnitId.FName,FUseOrgId,FUseOrgId.FName", //查询的字段
TopRowCount: 0,
FilterString: `FMaterialGroup.FNumber like '05%' and FModifyDate<'2022-01-08T19:36:06'`,
FieldKeys: strings.Join(fieldKeys, ","), //查询的字段
TopRowCount: 5,
FilterString: "",
},
})
t.Logf("result buf===> %s \n", string(result.Buf))
... ...
package controllers
import (
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/syncdata"
)
type TestController struct {
beego.BaseController
}
func (c *TestController) InvokPullMaterialNewest() {
srv := syncdata.PullDataK3CloudService{}
err := srv.PullMaterialNewest()
c.Response(nil, err)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego/controllers"
)
func init() {
web.Router("/TestController/PullMaterialNewest", &controllers.TestController{}, "Get:InvokPullMaterialNewest")
}
... ...