auto_create_drug.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "Xcx_New/utils"
  5. "fmt"
  6. "github.com/robfig/cron"
  7. "strconv"
  8. "time"
  9. )
  10. var createDrugCronJob *cron.Cron
  11. func init() {
  12. createDrugCronJob = cron.New()
  13. utils.InfoLog("开启自动更新药品库存定时任务")
  14. spec := "0 */1 * * * ?" // 每1分钟执行一次
  15. //spec := "0 55 23 * * ?" // 每天23点55执行一次
  16. //spec := "0 0 0 ? * Sun"
  17. createDrugCronJob.AddFunc(spec, func() {
  18. AutoCreateDrugJob()
  19. })
  20. }
  21. func BeginAutoCreateDrugJob() {
  22. createDrugCronJob.Start()
  23. }
  24. func AutoCreateDrugJob() {
  25. timeStr := time.Now().Format("2006-01-02")
  26. timeLayout := "2006-01-02 15:04:05"
  27. fmt.Println("timeStr:", timeStr)
  28. timeStringToTime, _ := utils.ParseTimeStringToTime(timeLayout, timeStr+" 00:00:00")
  29. timenow := timeStringToTime.Unix()
  30. fmt.Println("timenow是什么", timenow)
  31. //查询当天排班所有机构
  32. org, errs := GetAllOrgName(timenow)
  33. fmt.Print("错误是什么", errs)
  34. fmt.Println("查询有机构失败", err)
  35. var total int64
  36. for _, item := range org {
  37. fmt.Println("hhhh2h3h2323232323232323", item.UserOrgId)
  38. //查询该机构下的所有药品信息
  39. list, _ := FindAllDrugList(item.UserOrgId)
  40. for _, ite := range list {
  41. countList, _ := FindAllDrugTotalCount(ite.ID)
  42. for _, it := range countList {
  43. if ite.ID == it.DrugId {
  44. //转为拆零数量
  45. it.StockMaxNumber = it.StockMaxNumber * it.MinNumber
  46. total = it.StockMaxNumber + it.StockMinNumber
  47. fmt.Println(it.StockMaxNumber)
  48. fmt.Println(it.StockMinNumber)
  49. fmt.Println("total223232323232323232323", total)
  50. formatInt := strconv.FormatInt(total, 10)
  51. float, _ := strconv.ParseFloat(formatInt, 64)
  52. fmt.Println("min_number2323232323232", float)
  53. lib := models.BaseDrugLib{
  54. Total: float,
  55. }
  56. UpdateDrugTotal(it.DrugId, lib)
  57. }
  58. }
  59. }
  60. }
  61. }
  62. //查询当天有排班的所有机构
  63. func GetAllOrgName(time int64) (schedule []*models.XtSchedule, err error) {
  64. 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
  65. return schedule, err
  66. }
  67. //根据机构ID查询药品信息
  68. func FindAllDrugList(orgid int64) (lib []*models.BaseDrugLib, err error) {
  69. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&lib).Error
  70. return lib, err
  71. }
  72. //根据药品ID统计该药品的库存
  73. func FindAllDrugTotalCount(drug_id int64) (info []*models.DrugWarehouseInfoSix, err error) {
  74. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  75. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  76. fmt.Println(table)
  77. if drug_id > 0 {
  78. db = db.Where("x.drug_id = ?", drug_id)
  79. }
  80. 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
  81. return info, err
  82. }
  83. //更新药品库的total字段
  84. func UpdateDrugTotal(id int64, lib models.BaseDrugLib) error {
  85. err := XTWriteDB().Model(&lib).Where("id=? and status = 1", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  86. return err
  87. }