作者 tangxuhui

拉取 生产订单数据

... ... @@ -18,18 +18,24 @@ func NewCrontabService(options map[string]interface{}) *CrontabService {
}
func (crontabService *CrontabService) initTask() {
//PullDataK3Cloud 晚上0时10分执行
//拉取物料数据 晚上0时10分执行
pullMaterialK3cloud := task.NewTask("pullMaterialK3cloud", "0 10 0 * * *", func(ctx context.Context) error {
srv := syncdata.PullDataK3CloudService{}
return srv.PullMaterialNewest()
})
task.AddTask("pullMaterialK3cloud", pullMaterialK3cloud)
//PullDataK3Cloud 晚上0时10分执行
//拉取物料分组数据 晚上0时10分执行
pullMaterialGroupK3cloud := task.NewTask("pullMaterialGroupK3cloud", "0 10 0 * * *", func(ctx context.Context) error {
srv := syncdata.PullDataK3CloudService{}
return srv.PullMaterialGroup()
})
task.AddTask("pullMaterialGroupK3cloud", pullMaterialGroupK3cloud)
//拉取订生产订单数据 晚上0时10分执行
PullPrdMoK3cloud := task.NewTask("PullPrdMoK3cloud", "0 10 0 * * *", func(ctx context.Context) error {
srv := syncdata.PullDataK3CloudService{}
return srv.PullPrdMoNewest()
})
task.AddTask("PullPrdMoK3cloud", PullPrdMoK3cloud)
}
func (crontabService *CrontabService) StartCrontabTask() {
... ...
... ... @@ -276,3 +276,156 @@ func (srv *PullDataK3CloudService) PullMaterialGroup() error {
}
return nil
}
//PullPrdMoNewest 拉取生产订单数据
func (srv *PullDataK3CloudService) PullPrdMoNewest() error {
var (
err error
prdMoDao *dao.PrdMoK3cloudDao
)
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()
}()
prdMoDao, err = dao.NewPrdMoK3cloudDao(transactionContext.(*pgTransaction.TransactionContext))
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
version, err := prdMoDao.GetLastVersion()
if err != nil {
return application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if err = transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var timeFilter time.Time
if version > 0 {
timeFilter = time.Unix(version, 0)
}
err = srv.PullPrdMo(timeFilter)
if err != nil {
return err
}
return err
}
//PullPrdMo 拉取生产订单数据
func (srv *PullDataK3CloudService) PullPrdMo(timeFilter time.Time) error {
//拉取数据
var filterString []string
if !timeFilter.IsZero() {
str := timeFilter.Format("2006-01-02T15:04:05")
filterString = append(filterString, "FModifyDate>='"+str+"'")
}
client, err := newK3cloudClient()
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
fieldKeys := []string{
"FID", "FBillNo", "FWorkShopID", "FWorkShopID.FName", "FMaterialId", "FMaterialId.FName",
"FMaterialId.FNumber", "FPlanStartDate", "FPlanFinishDate", "FDate", "FQty",
"FCreateDate", "FModifyDate", "FPrdOrgId", "FPrdOrgId.FName",
}
var (
startRow int
allResult []map[string]string
queryErr error
)
for {
result, err := client.ExecuteBillQuery(k3cloud.RequestExecuteBillQuery{
FormId: "PRD_MO",
Data: k3cloud.ExecuteBillQueryData{
FormId: "PRD_MO",
FieldKeys: strings.Join(fieldKeys, ","), //查询的字段
StartRow: startRow,
Limit: 1000,
FilterString: strings.Join(filterString, " and "),
},
})
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 (
prdMoModels []models.PrdMoK3cloud
prdMoTemp models.PrdMoK3cloud
)
nowTime := time.Now()
for _, item := range allResult {
id, err := strconv.Atoi(item["FID"])
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
workShopID, _ := strconv.Atoi(item["WorkShopID"])
materialId, _ := strconv.Atoi(item["MaterialId"])
qty, _ := strconv.ParseFloat(item["Qty"], 64)
fPrdOrgId, _ := strconv.Atoi(item["FPrdOrgId"])
planStartDate, _ := time.Parse("2006-01-02T15:04:05", item["FPlanStartDate"])
planFinishDate, _ := time.Parse("2006-01-02T15:04:05", item["FPlanFinishDate"])
fDate, _ := time.Parse("2006-01-02T15:04:05", item["FDate"])
fCreateDate, _ := time.Parse("2006-01-02T15:04:05.999", item["FCreateDate"])
fModifyDate, _ := time.Parse("2006-01-02T15:04:05.999", item["FModifyDate"])
prdMoTemp = models.PrdMoK3cloud{
Id: id,
WorkShopID: workShopID,
MaterialId: materialId,
Qty: qty,
PlanStartDate: planStartDate,
PlanFinishDate: planFinishDate,
FDate: fDate,
CreateDate: fCreateDate,
ModifyDate: fModifyDate,
BillNo: item["FBillNo"],
WorkShopName: item["FWorkShopID.FName"],
MaterialName: item["FMaterialId.FName"],
MaterialNumber: item["FMaterialId.FNumber"],
DataVersion: nowTime.Unix(),
PrdOrgId: fPrdOrgId,
PrdOrgName: item["FPrdOrgId.FName"],
}
prdMoModels = append(prdMoModels, prdMoTemp)
}
var (
prdMoDao *dao.PrdMoK3cloudDao
)
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()
}()
prdMoDao, err = dao.NewPrdMoK3cloudDao(transactionContext.(*pgTransaction.TransactionContext))
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
//同步PrdMoK3cloud表数据
err = prdMoDao.SyncDataPrdMoK3cloud(prdMoModels)
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
}
... ...
... ... @@ -25,30 +25,6 @@ func NewMaterialK3cloudDao(transactionContext *pgTransaction.TransactionContext)
//SyncDataMaterialK3cloud 同步MaterialK3cloud表数据
func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3cloud) error {
// -- 插入或者更新
// 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" )
// VALUES
// (
// '1',2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
// NULL,NULL,NULL,NULL,nextval( 'manufacture.manufacture_seq_id_seq' ),NULL
// ) 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" )
sqlValues := []string{}
var strTemp []string
for i := range data {
... ...
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 PrdMoK3cloudDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewPrdMoK3cloudDao(transactionContext *pgTransaction.TransactionContext) (*PrdMoK3cloudDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PrdMoK3cloudDao{
transactionContext: transactionContext,
}, nil
}
}
//SyncDataPrdMoK3cloud 同步PrdMoK3cloud表数据
func (d *PrdMoK3cloudDao) SyncDataPrdMoK3cloud(data []models.PrdMoK3cloud) error {
//插入或者更新
sqlValues := []string{}
var strTemp []string
for i := range data {
strTemp = make([]string, 0, 5)
strTemp = append(strTemp, strconv.Itoa(data[i].Id))
strTemp = append(strTemp, `'`+data[i].BillNo+`'`)
strTemp = append(strTemp, strconv.Itoa(data[i].WorkShopID))
strTemp = append(strTemp, `'`+data[i].WorkShopName+`'`)
strTemp = append(strTemp, strconv.Itoa(data[i].MaterialId))
strTemp = append(strTemp, `'`+data[i].MaterialName+`'`)
strTemp = append(strTemp, `'`+data[i].MaterialNumber+`'`)
strTemp = append(strTemp, `'`+data[i].PlanStartDate.Format("2006-01-02 15:04:05.999")+`'`)
strTemp = append(strTemp, `'`+data[i].PlanFinishDate.Format("2006-01-02 15:04:05.999")+`'`)
strTemp = append(strTemp, `'`+data[i].FDate.Format("2006-01-02 15:04:05.999")+`'`)
strTemp = append(strTemp, fmt.Sprintf("%.10f", data[i].Qty)) //15.5000000000
strTemp = append(strTemp, `'`+data[i].CreateDate.Format("2006-01-02 15:04:05.999")+`'`)
strTemp = append(strTemp, `'`+data[i].ModifyDate.Format("2006-01-02 15:04:05.999")+`'`)
strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
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 : i+100]
} else {
valueTemp = sqlValues[i:]
}
sql := `INSERT INTO "manufacture"."prd_mo_k3cloud" (
"id","bill_no","work_shop_id","work_shop_name",
"material_id","material_name","material_number",
"plan_start_date","plan_finish_date","f_date",
"qty","create_date","modify_date","data_version" )
VALUES ` +
strings.Join(valueTemp, ",") +
` ON conflict ( "id" ) DO
UPDATE
SET (
"bill_no","work_shop_id","work_shop_name",
"material_id","material_name","material_number",
"plan_start_date","plan_finish_date","f_date",
"qty","create_date","modify_date","data_version") = (
EXCLUDED."bill_no",EXCLUDED."work_shop_id",
EXCLUDED."work_shop_name",EXCLUDED."material_id",
EXCLUDED."material_name",EXCLUDED."material_number",
EXCLUDED."plan_start_date",EXCLUDED."plan_finish_date",
EXCLUDED."f_date",EXCLUDED."qty",EXCLUDED."create_date",
EXCLUDED."modify_date",EXCLUDED."data_version"
)`
_, err := d.transactionContext.PgTx.Exec(sql)
if err != nil {
return err
}
}
return nil
}
func (d *PrdMoK3cloudDao) GetLastVersion() (int64, error) {
var prdMoData []models.PrdMoK3cloud
err := d.transactionContext.PgTx.Model(&prdMoData).
Order("data_version DESC").
Limit(1).
Select()
if err != nil {
return 0, err
}
if len(prdMoData) == 0 {
return 0, nil
}
return prdMoData[0].DataVersion, nil
}
... ...
... ... @@ -38,6 +38,7 @@ func init() {
(*models.Workshop)(nil),
(*models.MaterialK3cloud)(nil),
(*models.MaterialGroupK3cloud)(nil),
(*models.PrdMoK3cloud)(nil),
} {
err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
Temp: false,
... ...
... ... @@ -13,10 +13,10 @@ type MaterialK3cloud struct {
ErpClsId int `comment:"物料属性" pg:"erp_cls_id"`
BaseUnitId int `comment:"基本单位" pg:"base_unit_id"`
BaseUnitName string `comment:"基本单位名称" pg:"base_unit_name"`
CreateDate time.Time `comment:"创建时间" pg:"create_date,type:timestamp"`
ModifyDate time.Time `comment:"修改时间" pg:"modify_date,type:timestamp"`
ForbidDate time.Time `comment:"禁用时间" pg:"forbid_date,type:timestamp"`
ApproveDate time.Time `comment:"审核时间" pg:"approve_date,type:timestamp"`
CreateDate time.Time `comment:"创建时间" pg:"create_date"`
ModifyDate time.Time `comment:"修改时间" pg:"modify_date"`
ForbidDate time.Time `comment:"禁用时间" pg:"forbid_date"`
ApproveDate time.Time `comment:"审核时间" pg:"approve_date"`
MaterialGroup int `comment:"物料分组" pg:"material_group"`
MaterialGroupNumber string `comment:"物料分组编码" pg:"material_group_number"`
MaterialGroupName string `comment:"物料分组名称" pg:"material_group_name"`
... ...
package models
import "time"
//计划订单
//PrdMoK3cloud ..
type PrdMoK3cloud struct {
tableName string `comment:"采集自金蝶k3cloud计划订单" pg:"manufacture.prd_mo_k3cloud,alias:prd_mo_k3cloud"`
Id int `pg:",pk"`
BillNo string `comment:"订单编号" pg:"bill_no"`
WorkShopID int `comment:"生产车间" pg:"work_shop_id"`
WorkShopName string `comment:"生产车间名称" pg:"work_shop_name"`
MaterialId int `comment:"物料" pg:"material_id"`
MaterialName string `comment:"物料名称" pg:"material_name"`
MaterialNumber string `comment:"物料名称" pg:"material_number"`
PlanStartDate time.Time `comment:"计划开工时间" pg:"plan_start_date"`
PlanFinishDate time.Time `comment:"计划完工时间" pg:"plan_finish_date"`
FDate time.Time `comment:"单据日期" pg:"f_date"`
Qty float64 `comment:"数量" pg:"qty"`
CreateDate time.Time `comment:"创建时间" pg:"create_date"`
ModifyDate time.Time `comment:"修改时间" pg:"modify_date"`
DataVersion int64 `comment:"数据版本" pg:"data_version"`
PrdOrgId int `comment:"生产组织" pg:"data_version"`
PrdOrgName string `comment:"生产组织名称" pg:"data_version"`
}
... ...
... ... @@ -56,5 +56,31 @@ func TestExecuteBillQuery(t *testing.T) {
t.Error(err)
return
}
// t.Logf("tomap===>%v \n", result.ToMapString())
}
func TestExecuteBillQuery2(t *testing.T) {
client, err := NewClient(hostUrl, acctID, username, password)
if err != nil {
t.Error(err)
return
}
fieldKeys := []string{
"FID", "FBillNo", "FWorkShopID", "FWorkShopID.FName", "FMaterialId", "FMaterialId.FName",
"FMaterialId.FNumber", "FPlanStartDate", "FPlanFinishDate", "FDate", "FQty",
"FCreateDate", "FModifyDate", "FPrdOrgId", "FPrdOrgId.FName",
}
result, err := client.ExecuteBillQuery(RequestExecuteBillQuery{
FormId: "PRD_MO",
Data: ExecuteBillQueryData{
FormId: "PRD_MO",
FieldKeys: strings.Join(fieldKeys, ","), //查询的字段
TopRowCount: 2,
FilterString: "",
},
})
t.Logf("result buf===> %s \n", string(result.Buf))
if err != nil {
t.Error(err)
return
}
}
... ...
... ... @@ -14,6 +14,7 @@
| :---------------- | --------------------------- | ---- |
| BD_MATERIAL | 基础管理-基础资料-物料 | |
| SAL_MATERIALGROUP | 基础管理-基础资料-物料-分组 | |
| PRD_MO | 生产制造-生产订单 | |
| | | |
## 物料字段描述(部分)
... ... @@ -46,4 +47,20 @@
| FNumber | 分组编码 | 字符串 |
| FName | 分组名称 | 字符串 |
| FParentId | 上级分组 | 字符串 |
| | | |
### 生产订单字段描述(部分)
| 字段名 | 备注 | 类型 |
| ------------------- | ------------------------------------- | ------ |
| FBillNo | 订单编号 | 字符串 |
| FWorkShopID | 生产车间 | 数字 |
| FWorkShopID.FName | 生产车间 | 字符串 |
| FMaterialId | 物料 | 数字 |
| FPlanStartDate | 计划开工时间,如"2021-12-20T00:00:00" | 字符串 |
| FPlanFinishDate | 计划完工时间,如"2021-12-20T00:00:00" | 字符串 |
| FMaterialId.FName | 物料名称 | 字符串 |
| FMaterialId.FNumber | 物料编码 | 字符串 |
| FDate | 单据日期,如"2021-12-20T00:00:00" | 字符串 |
| FQty | 数量,如 15.5000000000 | 小数 |
| FID | id | 数字 |
| FPrdOrgId | 生产组织 | 数字 |
... ...
... ... @@ -20,3 +20,9 @@ func (c *TestController) InvokPullMaterialGroup() {
err := srv.PullMaterialGroup()
c.Response(nil, err)
}
func (c *TestController) InvokPullPrdMoNewest() {
srv := syncdata.PullDataK3CloudService{}
err := srv.PullPrdMoNewest()
c.Response(nil, err)
}
... ...
... ... @@ -8,4 +8,5 @@ import (
func init() {
web.Router("/TestController/PullMaterialNewest", &controllers.TestController{}, "Get:InvokPullMaterialNewest")
web.Router("/TestController/PullMaterialGroup", &controllers.TestController{}, "Get:InvokPullMaterialGroup")
web.Router("/TestController/InvokPullPrdMoNewest", &controllers.TestController{}, "Get:InvokPullPrdMoNewest")
}
... ...