作者 tangxuhui

拉取物料数据

... ... @@ -24,6 +24,12 @@ func (crontabService *CrontabService) initTask() {
return srv.PullMaterialNewest()
})
task.AddTask("pullMaterialK3cloud", pullMaterialK3cloud)
//PullDataK3Cloud 晚上0时10分执行
pullMaterialGroupK3cloud := task.NewTask("pullMaterialGroupK3cloud", "0 10 0 * * *", func(ctx context.Context) error {
srv := syncdata.PullDataK3CloudService{}
return srv.PullMaterialGroup()
})
task.AddTask("pullMaterialGroupK3cloud", pullMaterialGroupK3cloud)
}
func (crontabService *CrontabService) StartCrontabTask() {
... ... @@ -34,4 +40,5 @@ func (crontabService *CrontabService) StartCrontabTask() {
func (crontabService *CrontabService) StopCrontabTask() {
task.StopTask()
log.Logger.Info("crontab stop!")
}
... ...
... ... @@ -191,3 +191,88 @@ func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error {
}
return nil
}
//PullMaterialGroup 拉取物料分组
func (srv *PullDataK3CloudService) PullMaterialGroup() error {
client, err := newK3cloudClient()
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
fieldKeys := []string{
"FID", "FNumber", "FName", "FParentId",
}
var (
startRow int
allResult []map[string]string
queryErr error
)
for {
result, err := client.ExecuteBillQuery(k3cloud.RequestExecuteBillQuery{
FormId: "SAL_MATERIALGROUP",
Data: k3cloud.ExecuteBillQueryData{
FormId: "SAL_MATERIALGROUP",
FieldKeys: strings.Join(fieldKeys, ","), //查询的字段
StartRow: startRow,
Limit: 1000,
},
})
if err != nil {
queryErr = err
break
}
mp := result.ToMapString()
if len(mp) == 0 {
break
}
allResult = append(allResult, mp...)
startRow += 1000
}
if queryErr != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var (
materialGroups []models.MaterialGroupK3cloud
materialGroupTemp models.MaterialGroupK3cloud
)
nowTime := time.Now()
for _, item := range allResult {
id, _ := strconv.Atoi(item["FID"])
fParentId, _ := strconv.Atoi(item["FParentId"])
materialGroupTemp = models.MaterialGroupK3cloud{
Id: id,
ParentId: fParentId,
Number: item["FNumber"],
Name: item["FName"],
DataVersion: nowTime.Unix(),
}
materialGroups = append(materialGroups, materialGroupTemp)
}
var (
materialGroupDao *dao.MaterialGroupK3cloudDao
)
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
materialGroupDao, err = dao.NewMaterialGroupK3cloudDao(transactionContext.(*pgTransaction.TransactionContext))
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
//记录到MaterialGroupK3cloud表
err = materialGroupDao.SyncDataMaterialGroupK3cloud(materialGroups)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err = transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
... ...
... ... @@ -23,93 +23,35 @@ func NewMaterialGroupK3cloudDao(transactionContext *pgTransaction.TransactionCon
}
}
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 {
//SyncDataMaterialGroupK3cloud 同步MaterialGroupK3cloud表 物料分组数据
func (d *MaterialGroupK3cloudDao) SyncDataMaterialGroupK3cloud(data []models.MaterialGroupK3cloud) 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 = make([]string, 0, 5)
strTemp = append(strTemp, strconv.Itoa(data[i].Id))
strTemp = append(strTemp, strconv.Itoa(data[i].ParentId))
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, `'`+data[i].Name+`'`)
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]
if i <= len(sqlValues)-100 {
valueTemp = sqlValues[i : 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
sql := `INSERT INTO "manufacture"."material_group_k3cloud" (
"id","parent_id","number","name","data_version")
VALUES ` + strings.Join(valueTemp, ",") + `
ON conflict ( "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" )`
"parent_id","number","name","data_version") = (
EXCLUDED."parent_id",EXCLUDED."number",
EXCLUDED."name",EXCLUDED."data_version" )`
_, err := d.transactionContext.PgTx.Exec(sql)
if err != nil {
return err
... ...
... ... @@ -94,12 +94,11 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo
}
var valueTemp []string
fmt.Println("总数", len(data))
for i := 0; i < len(sqlValues); i += 100 {
if i <= len(sqlValues)-100 {
valueTemp = sqlValues[i : i+100]
} else {
valueTemp = sqlValues[i-100:]
valueTemp = sqlValues[i:]
}
sql := `INSERT INTO "manufacture"."material_k3cloud" (
"material_id","name","number","specification","forbid_status",
... ... @@ -107,7 +106,7 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo
"modify_date","forbid_date","approve_date","material_group",
"material_group_number","material_group_name","ref_status",
"join_product_id","data_version","use_org_id","use_org_name" )
VALUES ` + strings.Join(valueTemp, ",") +
VALUES ` + strings.Join(valueTemp, ",") +
` ON conflict ( material_id ) DO
UPDATE
SET (
... ...
... ... @@ -37,6 +37,7 @@ func init() {
(*models.UnitConversion)(nil),
(*models.Workshop)(nil),
(*models.MaterialK3cloud)(nil),
(*models.MaterialGroupK3cloud)(nil),
} {
err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
Temp: false,
... ...
... ... @@ -3,9 +3,9 @@ package models
//MaterialGroupK3cloud 采集自金蝶k3cloud物料分组数据
type MaterialGroupK3cloud struct {
tableName string `comment:"采集自金蝶k3cloud物料数据" pg:"manufacture.material_group_k3cloud,alias:material_group_k3cloud"`
Id int64 `pg:",pk"`
Id int `pg:",pk"`
Number string `comment:"分组编码" pg:"number"`
Name string `comment:"分组名称" pg:"name"`
ParentId int64 `comment:"父级id" pg:"parent_id"`
ParentId int `comment:"父级id" pg:"parent_id"`
DataVersion int64 `comment:"数据版本" pg:"data_version"`
}
... ...
... ... @@ -14,3 +14,9 @@ func (c *TestController) InvokPullMaterialNewest() {
err := srv.PullMaterialNewest()
c.Response(nil, err)
}
func (c *TestController) InvokPullMaterialGroup() {
srv := syncdata.PullDataK3CloudService{}
err := srv.PullMaterialGroup()
c.Response(nil, err)
}
... ...
... ... @@ -7,4 +7,5 @@ import (
func init() {
web.Router("/TestController/PullMaterialNewest", &controllers.TestController{}, "Get:InvokPullMaterialNewest")
web.Router("/TestController/PullMaterialGroup", &controllers.TestController{}, "Get:InvokPullMaterialGroup")
}
... ...