crontab_sync_product_plan.go 1.4 KB
package crontab

import (
	"context"
	"fmt"
	"github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/syncdata"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
	"time"
)

// 定时同步生产计划
func SyncProductPlan(ctx context.Context) error {
	defer func() {
		if r := recover(); r != nil {
			log.Logger.Error(fmt.Sprintf("%v", r))
		}
	}()
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		if err != nil {
			log.Logger.Error("【定时同步车间计划】 失败:" + err.Error())
		}
		transactionContext.RollbackTransaction()
	}()

	log.Logger.Debug("【定时同步车间计划】 启动")
	pullK3CloudService := syncdata.PullDataK3CloudService{}
	t := ctx.Value("fromTime")
	var fromTime time.Time
	if t != nil {
		if v, ok := t.(time.Time); ok {
			fromTime = v
		}
	}
	if err := pullK3CloudService.SyncDataProductPlan(transactionContext.(*pg.TransactionContext), fromTime); err != nil {
		log.Logger.Error(err.Error())
		return nil
	}
	if err = transactionContext.CommitTransaction(); err != nil {
		return err
	}
	return nil
}