作者 yangfu

feat:1.增加金蝶生产计划定时同步

@@ -54,6 +54,9 @@ func (crontabService *CrontabService) initTask() { @@ -54,6 +54,9 @@ func (crontabService *CrontabService) initTask() {
54 54
55 autoTodayWorkshopPlanCompletionRecord := task.NewTask("定时刷新当天车间计划完成纪录", "0 0 1-23/3 * * *", AutoTodayWorkshopPlanCompletionRecord) // 1:00, 4:00, 每三个小时运行一次 55 autoTodayWorkshopPlanCompletionRecord := task.NewTask("定时刷新当天车间计划完成纪录", "0 0 1-23/3 * * *", AutoTodayWorkshopPlanCompletionRecord) // 1:00, 4:00, 每三个小时运行一次
56 task.AddTask("autoTodayWorkshopPlanCompletionRecord", autoTodayWorkshopPlanCompletionRecord) 56 task.AddTask("autoTodayWorkshopPlanCompletionRecord", autoTodayWorkshopPlanCompletionRecord)
  57 +
  58 + syncProductPlan := task.NewTask("定时同步车间计划", "0 */5 * * * *", SyncProductPlan)
  59 + task.AddTask("SyncProductPlan", syncProductPlan)
57 } 60 }
58 61
59 func (crontabService *CrontabService) StartCrontabTask() { 62 func (crontabService *CrontabService) StartCrontabTask() {
  1 +package crontab
  2 +
  3 +import (
  4 + "context"
  5 + "fmt"
  6 + "github.com/linmadan/egglib-go/transaction/pg"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/syncdata"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
  10 +)
  11 +
  12 +func SyncProductPlan(ctx context.Context) error {
  13 + defer func() {
  14 + if r := recover(); r != nil {
  15 + log.Logger.Error(fmt.Sprintf("%v", r))
  16 + }
  17 + }()
  18 + transactionContext, err := factory.CreateTransactionContext(nil)
  19 + if err != nil {
  20 + return err
  21 + }
  22 + if err := transactionContext.StartTransaction(); err != nil {
  23 + return err
  24 + }
  25 + defer func() {
  26 + if err != nil {
  27 + log.Logger.Error("【定时同步车间计划】 失败:" + err.Error())
  28 + }
  29 + transactionContext.RollbackTransaction()
  30 + }()
  31 +
  32 + log.Logger.Debug("【定时同步车间计划】 启动")
  33 + pullK3CloudService := syncdata.PullDataK3CloudService{}
  34 + if err := pullK3CloudService.SyncDataProductPlan(transactionContext.(*pg.TransactionContext)); err != nil {
  35 + log.Logger.Error(err.Error())
  36 + return nil
  37 + }
  38 + if err = transactionContext.CommitTransaction(); err != nil {
  39 + return err
  40 + }
  41 + return nil
  42 +}
@@ -2,7 +2,9 @@ package syncdata @@ -2,7 +2,9 @@ package syncdata
2 2
3 import ( 3 import (
4 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 4 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
6 "strconv" 8 "strconv"
7 "strings" 9 "strings"
8 "time" 10 "time"
@@ -437,19 +439,110 @@ func (srv *PullDataK3CloudService) PullPrdMo(timeFilter time.Time) error { @@ -437,19 +439,110 @@ func (srv *PullDataK3CloudService) PullPrdMo(timeFilter time.Time) error {
437 return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 439 return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
438 } 440 }
439 441
  442 + //var userService = domainService.NewUserService()
  443 + //org, err := userService.Organization(constant.MANUFACTURE_DEFAULT_ORGID)
  444 + //if err != nil {
  445 + // return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  446 + //}
  447 +
  448 + //同步ProductPlan 表数据
  449 + //err = prdMoDao.SyncDataProductPlan(version, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID, org.OrgName)
  450 + //if err != nil {
  451 + // return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  452 + //}
  453 + if err = transactionContext.CommitTransaction(); err != nil {
  454 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  455 + }
  456 + return nil
  457 +}
  458 +
  459 +// 同步生产计划
  460 +func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.TransactionContext) error {
  461 + /*
  462 + 1.获取更新时间
  463 + 2.获取prd_mo_k3cloud从更新时间开始的有变化的数据
  464 + 3.查询是否有重复的批次号
  465 + 4.有进行更新,其他的插入
  466 + */
  467 + lastTime, err := redis.GetLastK3CloudFetchTime()
  468 + if err != nil {
  469 + return err
  470 + }
  471 + prdMoK3cloudDao, _ := dao.NewPrdMoK3cloudDao(ptr)
  472 + records, err := prdMoK3cloudDao.GetLatestData(lastTime)
  473 + if err != nil {
  474 + return err
  475 + }
440 var userService = domainService.NewUserService() 476 var userService = domainService.NewUserService()
441 org, err := userService.Organization(constant.MANUFACTURE_DEFAULT_ORGID) 477 org, err := userService.Organization(constant.MANUFACTURE_DEFAULT_ORGID)
442 if err != nil { 478 if err != nil {
443 return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 479 return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
444 } 480 }
  481 + var (
  482 + cid = constant.MANUFACTURE_DEFAULT_COMPANYID
  483 + oid = constant.MANUFACTURE_DEFAULT_ORGID
  484 + workshop *domain.Workshop
  485 + exists bool
  486 + )
445 487
446 - //同步ProductPlan 表数据  
447 - err = prdMoDao.SyncDataProductPlan(version, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID, org.OrgName)  
448 - if err != nil {  
449 - return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 488 + workshops, _ := factory.FastPgWorkshops(ptr, cid)
  489 +
  490 + productPlanRepository, _, _ := factory.FastPgProductPlan(ptr, 0)
  491 + productRepository, _, _ := factory.FastPgProduct(ptr, 0)
  492 + for _, v := range records {
  493 + if workshop, exists = workshops.ExistsWorkshops(v.WorkShopName); !exists {
  494 + continue
  495 + }
  496 + var plan *domain.ProductPlan
  497 + var product *domain.Product
  498 +
  499 + // 产品信息
  500 + product, err = productRepository.FindOne(map[string]interface{}{"companyId": cid, "orgId": oid, "productCode": v.MaterialNumber})
  501 + if err != nil || err == domain.ErrorNotFound {
  502 + continue
  503 + }
  504 + // 计划信息
  505 + plan, err = productPlanRepository.FindOne(map[string]interface{}{"companyId": cid, "orgId": oid, "batchNumber": v.BillNo})
  506 + if err == nil && plan != nil {
  507 + plan.UpdatedAt = time.Now()
  508 + continue
  509 + }
  510 + if err == domain.ErrorNotFound {
  511 + plan = &domain.ProductPlan{
  512 + CompanyId: cid,
  513 + OrgId: oid,
  514 + BatchNumber: v.BillNo,
  515 + ProductDate: v.PlanStartDate,
  516 + Workshop: workshop.CloneSample(),
  517 + WorkOn: 0,
  518 + Machine: "",
  519 + PlanStatus: domain.PlanOffline,
  520 + WorkStation: &domain.WorkStation{},
  521 + CreatedAt: time.Now(),
  522 + UpdatedAt: time.Now(),
  523 + }
  524 + }
  525 + // 更新数据
  526 + plan.PlanDevoted = &domain.UnitQuantity{
  527 + Unit: product.ProductSpec.Unit,
  528 + Quantity: 0,
  529 + Weight: product.ProductWeigh(0),
  530 + UnitWeight: product.ProductSpec.UnitWeight,
  531 + }
  532 + plan.Workshop = workshop.CloneSample()
  533 + plan.UpdatedAt = time.Now()
  534 + plan.Ext = domain.NewExt(org.OrgName).WithProductPlanExt(&domain.ProductPlanExt{
  535 + ProductId: product.ProductId,
  536 + ProductCode: product.ProductCode,
  537 + ProductName: product.ProductName,
  538 + DevotedUnit: "份",
  539 + })
  540 + plan.PlanProductName = product.ProductName
  541 + plan.Remark = v.Description
  542 +
  543 + if plan, err = productPlanRepository.Save(plan); err != nil {
  544 + return err
450 } 545 }
451 - if err = transactionContext.CommitTransaction(); err != nil {  
452 - return application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
453 } 546 }
454 return nil 547 return nil
455 } 548 }
@@ -44,6 +44,19 @@ func (m Workshops) FindWorkshopsByName(workshopName string) []int { @@ -44,6 +44,19 @@ func (m Workshops) FindWorkshopsByName(workshopName string) []int {
44 return result 44 return result
45 } 45 }
46 46
  47 +func (m Workshops) ExistsWorkshops(workshopName string) (*Workshop, bool) {
  48 + if len(workshopName) == 0 {
  49 + return nil, false
  50 + }
  51 + for i := range m {
  52 + item := m[i]
  53 + if strings.EqualFold(item.WorkshopName, workshopName) {
  54 + return item, true
  55 + }
  56 + }
  57 + return nil, false
  58 +}
  59 +
47 func (m Workshops) FindProductLinesByName(lineName string) []int { 60 func (m Workshops) FindProductLinesByName(lineName string) []int {
48 result := make([]int, 0) 61 result := make([]int, 0)
49 if len(lineName) == 0 { 62 if len(lineName) == 0 {
@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "fmt" 4 "fmt"
5 "strconv" 5 "strconv"
6 "strings" 6 "strings"
  7 + "time"
7 8
8 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 9 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
9 "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"
@@ -156,3 +157,16 @@ func (d *PrdMoK3cloudDao) SyncDataProductPlan(version int64, companyId, orgId in @@ -156,3 +157,16 @@ func (d *PrdMoK3cloudDao) SyncDataProductPlan(version int64, companyId, orgId in
156 _, err := d.transactionContext.PgTx.Exec(sql, companyId, orgId, version) 157 _, err := d.transactionContext.PgTx.Exec(sql, companyId, orgId, version)
157 return err 158 return err
158 } 159 }
  160 +
  161 +// 增量获取数据
  162 +func (d *PrdMoK3cloudDao) GetLatestData(fromTime time.Time) ([]*models.PrdMoK3cloud, error) {
  163 + m := new(models.PrdMoK3cloud)
  164 + result := make([]*models.PrdMoK3cloud, 0)
  165 + query := d.transactionContext.PgTx.Model(m)
  166 + query.Where("modify_date>=?", fromTime)
  167 + err := query.Select(&result)
  168 + if err != nil {
  169 + return nil, err
  170 + }
  171 + return result, nil
  172 +}
  1 +package redis
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/go-redis/redis"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
  7 + "time"
  8 +)
  9 +
  10 +// 获取每日设备运行数据
  11 +func GetLastK3CloudFetchTime() (time.Time, error) {
  12 + client := GetRedis()
  13 + key := K3CloudFetchTimeKey()
  14 + result := client.Get(key)
  15 + t, err := result.Int()
  16 + if err == redis.Nil {
  17 + if _, err := client.Set(key, time.Now().Unix(), 0).Result(); err != nil {
  18 + return time.Time{}, err
  19 + }
  20 + }
  21 + fetchTime := time.Unix(int64(t), 0)
  22 + if fetchTime.IsZero() {
  23 + return time.Time{}, fmt.Errorf("zerotime retry")
  24 + }
  25 + if _, err := client.Set(key, time.Now().Unix(), 0).Result(); err != nil {
  26 + return time.Time{}, err
  27 + }
  28 + return fetchTime, nil
  29 +}
  30 +
  31 +func K3CloudFetchTimeKey() string {
  32 + str := fmt.Sprintf("%v:k3clound-data:last-fetch:%v-%v", constant.CACHE_PREFIX, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID)
  33 + return str
  34 +}