package service import ( "XT_New/models" "XT_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() { 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) //查询入库数量 warehouseInfo, _ := FindAllGoodWarehouseInfoCount(ite.ID) //查询自动出库数量 autoWarehouseOut, _ := FindAutoGoodWarehouseInfoCount(ite.ID) //查询手动出库 warehouseOut, _ := FindGoodWarehouseOutCount(ite.ID) var total int64 total = warehouseInfo.WarehousingCount - autoWarehouseOut.Count - warehouseOut.Count formatInt := strconv.FormatInt(total, 10) totalfloat, _ := strconv.ParseFloat(formatInt, 64) info := models.GoodInfo{ Total: totalfloat, } UpdateStockTotal(ite.ID, info) //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 } func FindAllGoodWarehouseInfoCount(good_id int64) (models.WarehousingInfo, error) { info := models.WarehousingInfo{} db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1") if good_id > 0 { db = db.Where("x.good_id = ? and x.warehousing_count <> 0", good_id) } err = db.Select("x.good_id,sum(x.warehousing_count) as warehousing_count").Find(&info).Error return info, err } func FindAutoGoodWarehouseInfoCount(good_id int64) (models.AutomaticReduceDetail, error) { detail := models.AutomaticReduceDetail{} db := XTReadDB().Table("xt_automatic_reduce_detail as x").Where("x.status = 1") if good_id > 0 { db = db.Where("x.good_id = ? and x.count<> 0", good_id) } err := db.Select("x.good_id,sum(x.count) as count").Find(&detail).Error return detail, err } func FindGoodWarehouseOutCount(good_id int64) (models.WarehouseOutInfo, error) { out := models.WarehouseOutInfo{} db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1") if good_id > 0 { db = db.Where("x.good_id = ? and x.is_sys = 0 and x.count<> 0", good_id) } err := db.Select("x.good_id,sum(x.count) as count").Find(&out).Error return out, err }