作者 yangfu

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/allied-creation/allied-creat…

…ion-manufacture into dev
@@ -2,6 +2,7 @@ package main @@ -2,6 +2,7 @@ package main
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 +
5 "github.com/beego/beego/v2/server/web" 6 "github.com/beego/beego/v2/server/web"
6 "github.com/linmadan/egglib-go/log/logrus" 7 "github.com/linmadan/egglib-go/log/logrus"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
@@ -9,6 +10,7 @@ import ( @@ -9,6 +10,7 @@ import (
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/mqtt" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/mqtt"
11 12
  13 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/crontab"
12 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 14 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
13 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" 15 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
14 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" 16 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
@@ -32,6 +34,9 @@ func main() { @@ -32,6 +34,9 @@ func main() {
32 log.Logger.AddHook(bw) 34 log.Logger.AddHook(bw)
33 redis.InitRedis() 35 redis.InitRedis()
34 go mqtt.Start() 36 go mqtt.Start()
  37 + cron := crontab.NewCrontabService(nil)
  38 + cron.StartCrontabTask()
  39 + defer cron.StopCrontabTask()
35 log.Logger.Info("server start!") 40 log.Logger.Info("server start!")
36 web.Run() 41 web.Run()
37 } 42 }
  1 +package crontab
  2 +
  3 +import (
  4 + "context"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
  7 +
  8 + "github.com/beego/beego/v2/task"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/syncdata"
  10 +)
  11 +
  12 +type CrontabService struct {
  13 +}
  14 +
  15 +func NewCrontabService(options map[string]interface{}) *CrontabService {
  16 + newCrontabService := &CrontabService{}
  17 + return newCrontabService
  18 +}
  19 +
  20 +func (crontabService *CrontabService) initTask() {
  21 + //PullDataK3Cloud 晚上0时10分执行
  22 + pullMaterialK3cloud := task.NewTask("pullMaterialK3cloud", "0 10 0 * * *", func(ctx context.Context) error {
  23 + srv := syncdata.PullDataK3CloudService{}
  24 + return srv.PullMaterialNewest()
  25 + })
  26 + task.AddTask("pullMaterialK3cloud", pullMaterialK3cloud)
  27 + //PullDataK3Cloud 晚上0时10分执行
  28 + pullMaterialGroupK3cloud := task.NewTask("pullMaterialGroupK3cloud", "0 10 0 * * *", func(ctx context.Context) error {
  29 + srv := syncdata.PullDataK3CloudService{}
  30 + return srv.PullMaterialGroup()
  31 + })
  32 + task.AddTask("pullMaterialGroupK3cloud", pullMaterialGroupK3cloud)
  33 +}
  34 +
  35 +func (crontabService *CrontabService) StartCrontabTask() {
  36 + crontabService.initTask()
  37 + task.StartTask()
  38 + log.Logger.Info("crontab start!")
  39 +}
  40 +
  41 +func (crontabService *CrontabService) StopCrontabTask() {
  42 + task.StopTask()
  43 + log.Logger.Info("crontab stop!")
  44 +}
  1 +package syncdata
  2 +
  3 +import (
  4 + "strconv"
  5 + "strings"
  6 + "time"
  7 +
  8 + "github.com/linmadan/egglib-go/core/application"
  9 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
  11 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/dao"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
  13 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils/k3cloud"
  14 +)
  15 +
  16 +//拉取金蝶k3cloud的数据,并更新本地数据库
  17 +
  18 +type PullDataK3CloudService struct {
  19 +}
  20 +
  21 +func newK3cloudClient() (*k3cloud.Client, error) {
  22 + // TODO 使用配置方式传入
  23 + var (
  24 + acctID = "20211118121754866"
  25 + username = "18559023318"
  26 + password = "stx@123456"
  27 + hostUrl = "https://tianlian.test.ik3cloud.com/k3cloud"
  28 + )
  29 + client, err := k3cloud.NewClient(hostUrl, acctID, username, password)
  30 + return client, err
  31 +}
  32 +
  33 +func (srv *PullDataK3CloudService) PullMaterialNewest() error {
  34 + var (
  35 + err error
  36 + materialDao *dao.MaterialK3cloudDao
  37 + )
  38 + transactionContext, err := factory.CreateTransactionContext(nil)
  39 + if err != nil {
  40 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  41 + }
  42 + if err := transactionContext.StartTransaction(); err != nil {
  43 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  44 + }
  45 + defer func() {
  46 + transactionContext.RollbackTransaction()
  47 + }()
  48 + materialDao, err = dao.NewMaterialK3cloudDao(transactionContext.(*pgTransaction.TransactionContext))
  49 + if err != nil {
  50 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  51 + }
  52 + version, err := materialDao.GetLastVersion()
  53 + if err != nil {
  54 + return application.ThrowError(application.BUSINESS_ERROR, err.Error())
  55 + }
  56 + if err = transactionContext.CommitTransaction(); err != nil {
  57 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  58 + }
  59 + var timeFilter time.Time
  60 + if version > 0 {
  61 + timeFilter = time.Unix(version, 0)
  62 + }
  63 + err = srv.PullMaterial(timeFilter)
  64 + if err != nil {
  65 + return err
  66 + }
  67 + return err
  68 +}
  69 +
  70 +//PullMaterial 拉取物料数据
  71 +func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error {
  72 + //拉取数据
  73 + var filterString []string
  74 + if !timeFilter.IsZero() {
  75 + str := timeFilter.Format("2006-01-02T15:04:05")
  76 + filterString = append(filterString, "FModifyDate>='"+str+"'")
  77 + }
  78 + client, err := newK3cloudClient()
  79 + if err != nil {
  80 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  81 + }
  82 + fieldKeys := []string{
  83 + "FMATERIALID", "FSpecification", "FName", "FNumber",
  84 + "FDocumentStatus", "FForbidStatus", "FErpClsID",
  85 + "FBaseUnitId", "FBaseUnitId.FName", "FCreateDate", "FModifyDate",
  86 + "FForbidDate", "FApproveDate", "FMaterialGroup", "FMaterialGroup.FName",
  87 + "FRefStatus", "FMaterialGroup.FNumber", "FUseOrgId", "FUseOrgId.FName",
  88 + }
  89 + var (
  90 + startRow int
  91 + allResult []map[string]string
  92 + queryErr error
  93 + )
  94 + for {
  95 + result, err := client.ExecuteBillQuery(k3cloud.RequestExecuteBillQuery{
  96 + FormId: "BD_MATERIAL",
  97 + Data: k3cloud.ExecuteBillQueryData{
  98 + FormId: "BD_MATERIAL",
  99 + FieldKeys: strings.Join(fieldKeys, ","), //查询的字段
  100 + StartRow: startRow,
  101 + Limit: 1000,
  102 + FilterString: strings.Join(filterString, " and "),
  103 + },
  104 + })
  105 + if err != nil {
  106 + queryErr = err
  107 + break
  108 + }
  109 + mp := result.ToMapString()
  110 + if len(mp) == 0 {
  111 + break
  112 + }
  113 + allResult = append(allResult, mp...)
  114 + startRow += 1000
  115 + }
  116 + if queryErr != nil {
  117 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  118 + }
  119 + var (
  120 + materialModels []models.MaterialK3cloud
  121 + materialTemp models.MaterialK3cloud
  122 + )
  123 + nowTime := time.Now()
  124 + for _, item := range allResult {
  125 + materialId, err := strconv.Atoi(item["FMATERIALID"])
  126 + if err != nil {
  127 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  128 + }
  129 + fErpClsID, _ := strconv.Atoi(item["FErpClsID"])
  130 + fBaseUnitId, _ := strconv.Atoi(item["FBaseUnitId"])
  131 + materialGroup, _ := strconv.Atoi(item["MaterialGroup"])
  132 + fUseOrgId, _ := strconv.Atoi(item["FUseOrgId"])
  133 + refStatus, _ := strconv.Atoi(item["RefStatus"])
  134 + fCreateDate, _ := time.Parse("2006-01-02T15:04:05.999", item["FCreateDate"])
  135 + fModifyDate, _ := time.Parse("2006-01-02T15:04:05.999", item["FModifyDate"])
  136 + fForbidDate, _ := time.Parse("2006-01-02T15:04:05.999", item["FForbidDate"])
  137 + fApproveDate, _ := time.Parse("2006-01-02T15:04:05.999", item["FApproveDate"])
  138 + materialTemp = models.MaterialK3cloud{
  139 + MaterialId: materialId,
  140 + Name: item["FName"],
  141 + Number: item["FNumber"],
  142 + Specification: item["FSpecification"],
  143 + ForbidStatus: item["FForbidStatus"],
  144 + ErpClsId: fErpClsID,
  145 + BaseUnitId: fBaseUnitId,
  146 + BaseUnitName: item["FBaseUnitId.FName"],
  147 + CreateDate: fCreateDate,
  148 + ModifyDate: fModifyDate,
  149 + ForbidDate: fForbidDate,
  150 + ApproveDate: fApproveDate,
  151 + MaterialGroup: materialGroup,
  152 + MaterialGroupNumber: item["FMaterialGroup.FNumber"],
  153 + MaterialGroupName: item["FMaterialGroup.FName"],
  154 + RefStatus: refStatus,
  155 + UseOrgId: fUseOrgId,
  156 + UseOrgName: item["FUseOrgId.FName"],
  157 + JoinProductId: 0,
  158 + DataVersion: nowTime.Unix(),
  159 + }
  160 + materialModels = append(materialModels, materialTemp)
  161 + }
  162 + var (
  163 + materialDao *dao.MaterialK3cloudDao
  164 + )
  165 + transactionContext, err := factory.CreateTransactionContext(nil)
  166 + if err != nil {
  167 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  168 + }
  169 + if err := transactionContext.StartTransaction(); err != nil {
  170 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  171 + }
  172 + defer func() {
  173 + transactionContext.RollbackTransaction()
  174 + }()
  175 + materialDao, err = dao.NewMaterialK3cloudDao(transactionContext.(*pgTransaction.TransactionContext))
  176 + if err != nil {
  177 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  178 + }
  179 + //同步MaterialK3cloud表数据
  180 + err = materialDao.SyncDataMaterialK3cloud(materialModels)
  181 + if err != nil {
  182 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  183 + }
  184 + //MaterialK3cloud表数据到Proudct表
  185 + err = materialDao.SyncDataProudct(nowTime.Unix())
  186 + if err != nil {
  187 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  188 + }
  189 + if err = transactionContext.CommitTransaction(); err != nil {
  190 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  191 + }
  192 + return nil
  193 +}
  194 +
  195 +//PullMaterialGroup 拉取物料分组
  196 +func (srv *PullDataK3CloudService) PullMaterialGroup() error {
  197 + client, err := newK3cloudClient()
  198 + if err != nil {
  199 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  200 + }
  201 + fieldKeys := []string{
  202 + "FID", "FNumber", "FName", "FParentId",
  203 + }
  204 + var (
  205 + startRow int
  206 + allResult []map[string]string
  207 + queryErr error
  208 + )
  209 + for {
  210 + result, err := client.ExecuteBillQuery(k3cloud.RequestExecuteBillQuery{
  211 + FormId: "SAL_MATERIALGROUP",
  212 + Data: k3cloud.ExecuteBillQueryData{
  213 + FormId: "SAL_MATERIALGROUP",
  214 + FieldKeys: strings.Join(fieldKeys, ","), //查询的字段
  215 + StartRow: startRow,
  216 + Limit: 1000,
  217 + },
  218 + })
  219 + if err != nil {
  220 + queryErr = err
  221 + break
  222 + }
  223 + mp := result.ToMapString()
  224 + if len(mp) == 0 {
  225 + break
  226 + }
  227 + allResult = append(allResult, mp...)
  228 + startRow += 1000
  229 + }
  230 + if queryErr != nil {
  231 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  232 + }
  233 +
  234 + var (
  235 + materialGroups []models.MaterialGroupK3cloud
  236 + materialGroupTemp models.MaterialGroupK3cloud
  237 + )
  238 + nowTime := time.Now()
  239 + for _, item := range allResult {
  240 + id, _ := strconv.Atoi(item["FID"])
  241 + fParentId, _ := strconv.Atoi(item["FParentId"])
  242 + materialGroupTemp = models.MaterialGroupK3cloud{
  243 + Id: id,
  244 + ParentId: fParentId,
  245 + Number: item["FNumber"],
  246 + Name: item["FName"],
  247 + DataVersion: nowTime.Unix(),
  248 + }
  249 + materialGroups = append(materialGroups, materialGroupTemp)
  250 + }
  251 +
  252 + var (
  253 + materialGroupDao *dao.MaterialGroupK3cloudDao
  254 + )
  255 + transactionContext, err := factory.CreateTransactionContext(nil)
  256 + if err != nil {
  257 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  258 + }
  259 + if err := transactionContext.StartTransaction(); err != nil {
  260 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  261 + }
  262 + defer func() {
  263 + transactionContext.RollbackTransaction()
  264 + }()
  265 + materialGroupDao, err = dao.NewMaterialGroupK3cloudDao(transactionContext.(*pgTransaction.TransactionContext))
  266 + if err != nil {
  267 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  268 + }
  269 + //记录到MaterialGroupK3cloud表
  270 + err = materialGroupDao.SyncDataMaterialGroupK3cloud(materialGroups)
  271 + if err != nil {
  272 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  273 + }
  274 + if err = transactionContext.CommitTransaction(); err != nil {
  275 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  276 + }
  277 + return nil
  278 +}
  1 +/postgresql_local.go
  1 +//go:build !local
  2 +
1 package constant 3 package constant
2 4
3 import ( 5 import (
  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 +//SyncDataMaterialGroupK3cloud 同步MaterialGroupK3cloud表 物料分组数据
  27 +func (d *MaterialGroupK3cloudDao) SyncDataMaterialGroupK3cloud(data []models.MaterialGroupK3cloud) error {
  28 + sqlValues := []string{}
  29 + var strTemp []string
  30 + for i := range data {
  31 + strTemp = make([]string, 0, 5)
  32 + strTemp = append(strTemp, strconv.Itoa(data[i].Id))
  33 + strTemp = append(strTemp, strconv.Itoa(data[i].ParentId))
  34 + strTemp = append(strTemp, `'`+data[i].Number+`'`)
  35 + strTemp = append(strTemp, `'`+data[i].Name+`'`)
  36 + strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
  37 + sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")")
  38 + }
  39 + var valueTemp []string
  40 + for i := 0; i < len(sqlValues); i += 100 {
  41 + if i <= len(sqlValues)-100 {
  42 + valueTemp = sqlValues[i : i+100]
  43 + } else {
  44 + valueTemp = sqlValues[i:]
  45 + }
  46 + sql := `INSERT INTO "manufacture"."material_group_k3cloud" (
  47 + "id","parent_id","number","name","data_version")
  48 + VALUES ` + strings.Join(valueTemp, ",") + `
  49 + ON conflict ( "id" ) DO
  50 + UPDATE
  51 + SET (
  52 + "parent_id","number","name","data_version") = (
  53 + EXCLUDED."parent_id",EXCLUDED."number",
  54 + EXCLUDED."name",EXCLUDED."data_version" )`
  55 + _, err := d.transactionContext.PgTx.Exec(sql)
  56 + if err != nil {
  57 + return err
  58 + }
  59 + }
  60 +
  61 + return nil
  62 +}
1 package dao 1 package dao
2 2
3 import ( 3 import (
  4 + "fmt"
  5 + "strconv"
  6 + "strings"
  7 +
4 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 8 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
6 ) 10 )
@@ -9,6 +13,16 @@ type MaterialK3cloudDao struct { @@ -9,6 +13,16 @@ type MaterialK3cloudDao struct {
9 transactionContext *pgTransaction.TransactionContext 13 transactionContext *pgTransaction.TransactionContext
10 } 14 }
11 15
  16 +func NewMaterialK3cloudDao(transactionContext *pgTransaction.TransactionContext) (*MaterialK3cloudDao, error) {
  17 + if transactionContext == nil {
  18 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  19 + } else {
  20 + return &MaterialK3cloudDao{
  21 + transactionContext: transactionContext,
  22 + }, nil
  23 + }
  24 +}
  25 +
12 //SyncDataMaterialK3cloud 同步MaterialK3cloud表数据 26 //SyncDataMaterialK3cloud 同步MaterialK3cloud表数据
13 func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3cloud) error { 27 func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3cloud) error {
14 // -- 插入或者更新 28 // -- 插入或者更新
@@ -35,25 +49,119 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo @@ -35,25 +49,119 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo
35 // EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group", 49 // EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group",
36 // EXCLUDED."material_group_number",EXCLUDED."material_group_name", 50 // EXCLUDED."material_group_number",EXCLUDED."material_group_name",
37 // EXCLUDED."ref_status ",EXCLUDED."data_version" ) 51 // EXCLUDED."ref_status ",EXCLUDED."data_version" )
  52 + sqlValues := []string{}
  53 + var strTemp []string
  54 + for i := range data {
  55 + strTemp = make([]string, 0, 18)
  56 + strTemp = append(strTemp, strconv.Itoa(data[i].MaterialId))
  57 + strTemp = append(strTemp, `'`+data[i].Name+`'`)
  58 + strTemp = append(strTemp, `'`+data[i].Number+`'`)
  59 + strTemp = append(strTemp, `'`+data[i].Specification+`'`)
  60 + strTemp = append(strTemp, `'`+data[i].ForbidStatus+`'`)
  61 + strTemp = append(strTemp, strconv.Itoa(data[i].ErpClsId))
  62 + strTemp = append(strTemp, strconv.Itoa(data[i].BaseUnitId))
  63 + strTemp = append(strTemp, `'`+data[i].BaseUnitName+`'`)
  64 + if data[i].CreateDate.IsZero() {
  65 + strTemp = append(strTemp, `NULL`)
  66 + } else {
  67 + strTemp = append(strTemp, `'`+data[i].CreateDate.Format("2006-01-02 15:04:05.999")+`'`)
  68 + }
  69 + if data[i].ModifyDate.IsZero() {
  70 + strTemp = append(strTemp, `NULL`)
  71 + } else {
  72 + strTemp = append(strTemp, `'`+data[i].ModifyDate.Format("2006-01-02 15:04:05.999")+`'`)
  73 + }
  74 + if data[i].ForbidDate.IsZero() {
  75 + strTemp = append(strTemp, `NULL`)
  76 + } else {
  77 + strTemp = append(strTemp, `'`+data[i].ForbidDate.Format("2006-01-02 15:04:05.999")+`'`)
  78 + }
  79 + if data[i].ApproveDate.IsZero() {
  80 + strTemp = append(strTemp, `NULL`)
  81 + } else {
  82 + strTemp = append(strTemp, `'`+data[i].ApproveDate.Format("2006-01-02 15:04:05.999")+`'`)
  83 + }
  84 + strTemp = append(strTemp, strconv.Itoa(data[i].MaterialGroup))
  85 + strTemp = append(strTemp, `'`+data[i].MaterialGroupNumber+`'`)
  86 + strTemp = append(strTemp, `'`+data[i].MaterialGroupName+`'`)
  87 + strTemp = append(strTemp, strconv.Itoa(data[i].RefStatus))
  88 + //关联的产品表id ,使用 product 产品表的自增序列表
  89 + strTemp = append(strTemp, "nextval('manufacture.product_product_id_seq'::regclass)")
  90 + strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
  91 + strTemp = append(strTemp, strconv.Itoa(data[i].UseOrgId))
  92 + strTemp = append(strTemp, `'`+data[i].UseOrgName+`'`)
  93 + sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")")
  94 +
  95 + }
  96 + var valueTemp []string
  97 + for i := 0; i < len(sqlValues); i += 100 {
  98 + if i <= len(sqlValues)-100 {
  99 + valueTemp = sqlValues[i : i+100]
  100 + } else {
  101 + valueTemp = sqlValues[i:]
  102 + }
  103 + sql := `INSERT INTO "manufacture"."material_k3cloud" (
  104 + "material_id","name","number","specification","forbid_status",
  105 + "erp_cls_id","base_unit_id","base_unit_name","create_date",
  106 + "modify_date","forbid_date","approve_date","material_group",
  107 + "material_group_number","material_group_name","ref_status",
  108 + "join_product_id","data_version","use_org_id","use_org_name" )
  109 + VALUES ` + strings.Join(valueTemp, ",") +
  110 + ` ON conflict ( material_id ) DO
  111 + UPDATE
  112 + SET (
  113 + "name","number","specification","forbid_status","erp_cls_id",
  114 + "base_unit_id","base_unit_name","create_date","modify_date",
  115 + "forbid_date","approve_date","material_group","material_group_number",
  116 + "material_group_name","ref_status","data_version","use_org_id","use_org_name" ) = (
  117 + EXCLUDED."name",EXCLUDED."number",EXCLUDED."specification",
  118 + EXCLUDED."forbid_status",EXCLUDED."erp_cls_id",EXCLUDED."base_unit_id",
  119 + EXCLUDED."base_unit_name",EXCLUDED."create_date",EXCLUDED."modify_date",
  120 + EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group",
  121 + EXCLUDED."material_group_number",EXCLUDED."material_group_name",
  122 + EXCLUDED."ref_status",EXCLUDED."data_version",EXCLUDED."use_org_id",EXCLUDED."use_org_name")`
  123 + _, err := d.transactionContext.PgTx.Exec(sql)
  124 + if err != nil {
  125 + return err
  126 + }
  127 + }
  128 +
38 return nil 129 return nil
39 } 130 }
40 131
41 -//SyncDataProudct 同步Proudct表数据  
42 -func (d *MaterialK3cloudDao) SyncDataProudct(version int) error { 132 +//SyncDataProudct 同步MaterialK3cloud表数据到Proudct表
  133 +func (d *MaterialK3cloudDao) SyncDataProudct(version int64) error {
43 // -- 插入或者更新 134 // -- 插入或者更新
44 - // INSERT INTO "manufacture"."product"(  
45 - // "company_id", "org_id", "product_id", "product_code", "product_name",  
46 - // "product_category", "product_spec", "created_at", "updated_at"  
47 - // )  
48 - // SELECT 0,0,"join_product_id","number","name","material_group_name",'{}',now(),now()  
49 - // FROM "manufacture"."material_k3cloud" WHERE "data_version"=0000  
50 - // ON conflict ( product_id ) DO  
51 - // UPDATE  
52 - // SET (  
53 - // "company_id", "org_id", "product_id", "product_code", "product_name",  
54 - // "product_category", "product_spec", "created_at", "updated_at")=(  
55 - // EXCLUDED."company_id", EXCLUDED."org_id",EXCLUDED."product_id",  
56 - // EXCLUDED."product_code",EXCLUDED."product_name",EXCLUDED."product_category",  
57 - // EXCLUDED."product_spec", EXCLUDED."created_at", EXCLUDED."updated_at")  
58 - return nil 135 + sql := `INSERT INTO "manufacture"."product"(
  136 + "company_id", "org_id", "product_id", "product_code", "product_name",
  137 + "product_category", "product_spec", "created_at", "updated_at"
  138 + )
  139 + SELECT 0,use_org_id,"join_product_id","number","name","material_group_name",
  140 + json_build_object('unit',specification),now(),now()
  141 + FROM "manufacture"."material_k3cloud" WHERE "data_version"=? AND "material_group_number" LIKE '05%'
  142 + ON conflict ( product_id ) DO
  143 + UPDATE
  144 + SET (
  145 + "company_id", "org_id", "product_id", "product_code", "product_name",
  146 + "product_category", "product_spec", "updated_at")=(
  147 + EXCLUDED."company_id", EXCLUDED."org_id",EXCLUDED."product_id",
  148 + EXCLUDED."product_code",EXCLUDED."product_name",EXCLUDED."product_category",
  149 + EXCLUDED."product_spec", EXCLUDED."updated_at") `
  150 + _, err := d.transactionContext.PgTx.Exec(sql, version)
  151 + return err
  152 +}
  153 +
  154 +func (d *MaterialK3cloudDao) GetLastVersion() (int64, error) {
  155 + var materialData []models.MaterialK3cloud
  156 + err := d.transactionContext.PgTx.Model(&materialData).
  157 + Order("data_version DESC").
  158 + Limit(1).
  159 + Select()
  160 + if err != nil {
  161 + return 0, err
  162 + }
  163 + if len(materialData) == 0 {
  164 + return 0, nil
  165 + }
  166 + return materialData[0].DataVersion, nil
59 } 167 }
@@ -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,8 @@ func init() { @@ -35,6 +36,8 @@ 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),
  40 + (*models.MaterialGroupK3cloud)(nil),
38 } { 41 } {
39 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ 42 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
40 Temp: false, 43 Temp: false,
@@ -8,7 +8,7 @@ type DataLogK3cloud struct { @@ -8,7 +8,7 @@ type DataLogK3cloud struct {
8 FormId string `comment:"formId" pg:"form_id"` 8 FormId string `comment:"formId" pg:"form_id"`
9 RequestParam string `comment:"请求参数" pg:"request_param"` 9 RequestParam string `comment:"请求参数" pg:"request_param"`
10 BeginAt int64 `comment:"开始时间" pg:"begin_at"` 10 BeginAt int64 `comment:"开始时间" pg:"begin_at"`
11 - EndAt int64 `comment:"结束时间" pg:"begin_at"` 11 + EndAt int64 `comment:"结束时间" pg:"end_at"`
12 DataVersion int64 `comment:"数据版本" pg:"data_version"` 12 DataVersion int64 `comment:"数据版本" pg:"data_version"`
13 IsSuccess int `comment:"是否成功" pg:"is_success"` 13 IsSuccess int `comment:"是否成功" pg:"is_success"`
14 Error string `comment:"错误信息" pg:"error"` 14 Error string `comment:"错误信息" pg:"error"`
@@ -3,9 +3,9 @@ package models @@ -3,9 +3,9 @@ package models
3 //MaterialGroupK3cloud 采集自金蝶k3cloud物料分组数据 3 //MaterialGroupK3cloud 采集自金蝶k3cloud物料分组数据
4 type MaterialGroupK3cloud struct { 4 type MaterialGroupK3cloud struct {
5 tableName string `comment:"采集自金蝶k3cloud物料数据" pg:"manufacture.material_group_k3cloud,alias:material_group_k3cloud"` 5 tableName string `comment:"采集自金蝶k3cloud物料数据" pg:"manufacture.material_group_k3cloud,alias:material_group_k3cloud"`
6 - Id int64 `pg:",pk"` 6 + Id int `pg:",pk"`
7 Number string `comment:"分组编码" pg:"number"` 7 Number string `comment:"分组编码" pg:"number"`
8 Name string `comment:"分组名称" pg:"name"` 8 Name string `comment:"分组名称" pg:"name"`
9 - ParentId int64 `comment:"父级id" pg:"parent_id"` 9 + ParentId int `comment:"父级id" pg:"parent_id"`
10 DataVersion int64 `comment:"数据版本" pg:"data_version"` 10 DataVersion int64 `comment:"数据版本" pg:"data_version"`
11 } 11 }
1 package models 1 package models
2 2
  3 +import "time"
  4 +
3 //MaterialK3cloud 采集自金蝶k3cloud物料数据 5 //MaterialK3cloud 采集自金蝶k3cloud物料数据
4 type MaterialK3cloud struct { 6 type MaterialK3cloud struct {
5 tableName string `comment:"采集自金蝶k3cloud物料数据" pg:"manufacture.material_k3cloud,alias:material_k3cloud"` 7 tableName string `comment:"采集自金蝶k3cloud物料数据" pg:"manufacture.material_k3cloud,alias:material_k3cloud"`
6 - MaterialId int64 `pg:",pk"` 8 + MaterialId int `pg:",pk"`
7 Name string `comment:"物料名称" pg:"name"` 9 Name string `comment:"物料名称" pg:"name"`
8 Number string `comment:"物料编码" pg:"number"` 10 Number string `comment:"物料编码" pg:"number"`
9 Specification string `comment:"规格型号" pg:"specification"` 11 Specification string `comment:"规格型号" pg:"specification"`
@@ -11,14 +13,16 @@ type MaterialK3cloud struct { @@ -11,14 +13,16 @@ type MaterialK3cloud struct {
11 ErpClsId int `comment:"物料属性" pg:"erp_cls_id"` 13 ErpClsId int `comment:"物料属性" pg:"erp_cls_id"`
12 BaseUnitId int `comment:"基本单位" pg:"base_unit_id"` 14 BaseUnitId int `comment:"基本单位" pg:"base_unit_id"`
13 BaseUnitName string `comment:"基本单位名称" pg:"base_unit_name"` 15 BaseUnitName string `comment:"基本单位名称" pg:"base_unit_name"`
14 - CreateDate string `comment:"创建时间" pg:"create_date"`  
15 - ModifyDate string `comment:"修改时间" pg:"modify_date"`  
16 - ForbidDate string `comment:"禁用时间" pg:"forbid_date"`  
17 - ApproveDate string `comment:"审核时间" pg:"approve_date"` 16 + CreateDate time.Time `comment:"创建时间" pg:"create_date,type:timestamp"`
  17 + ModifyDate time.Time `comment:"修改时间" pg:"modify_date,type:timestamp"`
  18 + ForbidDate time.Time `comment:"禁用时间" pg:"forbid_date,type:timestamp"`
  19 + ApproveDate time.Time `comment:"审核时间" pg:"approve_date,type:timestamp"`
18 MaterialGroup int `comment:"物料分组" pg:"material_group"` 20 MaterialGroup int `comment:"物料分组" pg:"material_group"`
19 MaterialGroupNumber string `comment:"物料分组编码" pg:"material_group_number"` 21 MaterialGroupNumber string `comment:"物料分组编码" pg:"material_group_number"`
20 MaterialGroupName string `comment:"物料分组名称" pg:"material_group_name"` 22 MaterialGroupName string `comment:"物料分组名称" pg:"material_group_name"`
21 RefStatus int `comment:"是否使用" pg:"ref_status"` 23 RefStatus int `comment:"是否使用" pg:"ref_status"`
  24 + UseOrgId int `comment:"使用组织" pg:"use_org_id"`
  25 + UseOrgName string `comment:"使用组织" pg:"use_org_name"`
22 JoinProductId int64 `comment:"关联的product表id" pg:"join_product_id"` 26 JoinProductId int64 `comment:"关联的product表id" pg:"join_product_id"`
23 DataVersion int64 `comment:"数据版本" pg:"data_version"` 27 DataVersion int64 `comment:"数据版本" pg:"data_version"`
24 } 28 }
1 package k3cloud 1 package k3cloud
2 2
3 import ( 3 import (
  4 + "strings"
4 "testing" 5 "testing"
5 ) 6 )
6 7
@@ -34,13 +35,20 @@ func TestExecuteBillQuery(t *testing.T) { @@ -34,13 +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", "已使用"}}
  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 + }
37 result, err := client.ExecuteBillQuery(RequestExecuteBillQuery{ 45 result, err := client.ExecuteBillQuery(RequestExecuteBillQuery{
38 FormId: "BD_MATERIAL", 46 FormId: "BD_MATERIAL",
39 Data: ExecuteBillQueryData{ 47 Data: ExecuteBillQueryData{
40 FormId: "BD_MATERIAL", 48 FormId: "BD_MATERIAL",
41 - FieldKeys: "FMATERIALID,FSpecification,FName,FNumber,FModifyDate,FBaseUnitId.FName,FUseOrgId.FName", //查询的字段 49 + FieldKeys: strings.Join(fieldKeys, ","), //查询的字段
42 TopRowCount: 5, 50 TopRowCount: 5,
43 - FilterString: `FMaterialGroup.FNumber like '05%' and FModifyDate<'2022-01-08T19:36:06'`, 51 + FilterString: "",
44 }, 52 },
45 }) 53 })
46 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 +}
  17 +
  18 +func (c *TestController) InvokPullMaterialGroup() {
  19 + srv := syncdata.PullDataK3CloudService{}
  20 + err := srv.PullMaterialGroup()
  21 + c.Response(nil, err)
  22 +}
  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 + web.Router("/TestController/PullMaterialGroup", &controllers.TestController{}, "Get:InvokPullMaterialGroup")
  11 +}