package service import ( "Xcx_New/models" "Xcx_New/utils" "fmt" "github.com/robfig/cron" "strconv" "time" ) var createStockCronJob *cron.Cron func init() { utils.InfoLog("开启自动更新耗材库存定时任务") createStockCronJob = cron.New() spec := "0 */5 * * * ?" // 每1分钟执行一次 //spec := "0 55 23 * * ?" // 每天23点55执行一次 //spec := "0 0 0 ? * Sun" createStockCronJob.AddFunc(spec, func() { utils.InfoLog("开启自动更新耗材库存定时任务1111111111111") AutoCreateStockJob() }) } func BeginAutoCreateStockJob() { createStockCronJob.Start() } func AutoCreateStockJob() { timeStr := time.Now().Format("2006-01-02") timeLayout := "2006-01-02 15:04:05" fmt.Println("timeStr:", timeStr) timeStringToTime, _ := utils.ParseTimeStringToTime(timeLayout, timeStr+" 00:00:00") timenow := timeStringToTime.Unix() fmt.Println("timenow是什么", timenow) //查询当天排班所有机构 org, errs := GetAllOrgName(timenow) fmt.Print("错误是什么", errs) fmt.Println("查询有机构失败", err) for _, item := range org { information, _ := FindAlLGoodInformation(item.UserOrgId) for _, ite := range information { countList, _ := FindAllGoodInformationTotalCount(ite.ID) for _, it := range countList { if ite.ID == it.GoodId { formatInt := strconv.FormatInt(it.StockCount, 10) total, _ := strconv.ParseFloat(formatInt, 64) info := models.GoodInfo{ Total: total, } UpdateStockTotal(it.GoodId, info) } } } } } //查询所有的耗材信息 func FindAlLGoodInformation(orgid int64) (info []*models.GoodInfo, err error) { err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&info).Error return info, err } func FindAllGoodInformationTotalCount(good_id int64) (info []*models.WarehousingInfo, err error) { db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status =1") if good_id > 0 { db = db.Where("x.good_id = ?", good_id) } err = db.Select("x.good_id,sum(x.stock_count) as stock_count,x.warehousing_count").Find(&info).Error return info, err } //更新耗材库的total字段 func UpdateStockTotal(id int64, lib models.GoodInfo) error { err := XTWriteDB().Model(&lib).Where("id=? and status = 1", id).Updates(map[string]interface{}{"total": lib.Total}).Error return err }