crontab_sync_material.go 1.5 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"
	"runtime/debug"
	"time"
)

// SyncMaterial 定时同步物料
func SyncMaterial(ctx context.Context) error {
	defer func() {
		if r := recover(); r != nil {
			log.Logger.Error(fmt.Sprintf("%v", r), map[string]interface{}{"task": "定时同步物料", "stack": string(debug.Stack())})
		}
	}()
	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()
	}()
	var pullType string = ""
	if val := ctx.Value("pullType"); val != nil {
		pullType = val.(string)
	}
	log.Logger.Debug("【定时同步物料】 启动")
	pullK3CloudService := syncdata.PullDataK3CloudService{}
	if err := pullK3CloudService.SyncDataMaterial(transactionContext.(*pg.TransactionContext), pullType, time.Time{}); err != nil {
		log.Logger.Error(err.Error(), map[string]interface{}{"task": "定时同步物料"})
		return nil
	}
	if err = transactionContext.CommitTransaction(); err != nil {
		return err
	}
	return nil
}