package service import ( "XT_New/models" "XT_New/utils" "fmt" "github.com/robfig/cron" "strconv" "time" ) var createDrugCronJob *cron.Cron func init() { createDrugCronJob = cron.New() utils.InfoLog("开启自动更新药品库存定时任务") spec := "0 */1 * * * ?" // 每1分钟执行一次 //spec := "0 55 23 * * ?" // 每天23点55执行一次 //spec := "0 0 0 ? * Sun" createDrugCronJob.AddFunc(spec, func() { AutoCreateDrugJob() }) } func BeginAutoCreateDrugJob() { createDrugCronJob.Start() } func AutoCreateDrugJob() { 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) var total int64 for _, item := range org { //查询该机构下的所有药品信息 list, _ := FindAllDrugList(item.UserOrgId) for _, ite := range list { countList, _ := FindAllDrugTotalCount(ite.ID) for _, it := range countList { if ite.ID == it.DrugId { //转为拆零数量 it.StockMaxNumber = it.StockMaxNumber * it.MinNumber total = it.StockMaxNumber + it.StockMinNumber fmt.Println(it.StockMaxNumber) fmt.Println(it.StockMinNumber) formatInt := strconv.FormatInt(total, 10) float, _ := strconv.ParseFloat(formatInt, 64) lib := models.BaseDrugLib{ Total: float, } UpdateDrugTotal(it.DrugId, lib) } } } } } //查询当天有排班的所有机构 func GetAllOrgName(time int64) (schedule []*models.XtSchedule, err error) { err = XTReadDB().Raw("select user_org_id,id,partition_id,bed_id,patient_id,schedule_date,schedule_type,schedule_week,mode_id from xt_schedule where schedule_date = ? group by user_org_id", time).Scan(&schedule).Error return schedule, err } //根据机构ID查询药品信息 func FindAllDrugList(orgid int64) (lib []*models.BaseDrugLib, err error) { err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&lib).Error return lib, err } //根据药品ID统计该药品的库存 func FindAllDrugTotalCount(drug_id int64) (info []*models.DrugWarehouseInfoSix, err error) { db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1") table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1") fmt.Println(table) if drug_id > 0 { db = db.Where("x.drug_id = ?", drug_id) } err = db.Select("x.drug_id,sum(x.stock_max_number) as stock_max_number,sum(x.stock_min_number) as stock_min_number,b.min_number").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&info).Error return info, err } //更新药品库的total字段 func UpdateDrugTotal(id int64, lib models.BaseDrugLib) error { err := XTWriteDB().Model(&lib).Where("id=? and status = 1", id).Updates(map[string]interface{}{"total": lib.Total}).Error return err } func FindAllDrugWarehouseInfo(drug_id int64) (models.DrugWarehouseInfoSix, error) { info := models.DrugWarehouseInfoSix{} db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1") table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1") fmt.Println(table) if drug_id > 0 { db = db.Where("x.drug_id = ?", drug_id) } err = db.Select("x.drug_id,sum(x.warehousing_count) as warehousing_count,b.min_number").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&info).Error return info, err }