auto_create_drug.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_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. //查询该机构下的所有药品信息
  38. list, _ := FindAllDrugList(item.UserOrgId)
  39. for _, ite := range list {
  40. countList, _ := FindAllDrugTotalCount(ite.ID)
  41. for _, it := range countList {
  42. if ite.ID == it.DrugId {
  43. //转为拆零数量
  44. it.StockMaxNumber = it.StockMaxNumber * it.MinNumber
  45. total = it.StockMaxNumber + it.StockMinNumber
  46. fmt.Println(it.StockMaxNumber)
  47. fmt.Println(it.StockMinNumber)
  48. formatInt := strconv.FormatInt(total, 10)
  49. float, _ := strconv.ParseFloat(formatInt, 64)
  50. lib := models.BaseDrugLib{
  51. Total: float,
  52. }
  53. UpdateDrugTotal(it.DrugId, lib)
  54. }
  55. }
  56. }
  57. }
  58. }
  59. //查询当天有排班的所有机构
  60. func GetAllOrgName(time int64) (schedule []*models.XtSchedule, err error) {
  61. 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
  62. return schedule, err
  63. }
  64. //根据机构ID查询药品信息
  65. func FindAllDrugList(orgid int64) (lib []*models.BaseDrugLib, err error) {
  66. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&lib).Error
  67. return lib, err
  68. }
  69. //根据药品ID统计该药品的库存
  70. func FindAllDrugTotalCount(drug_id int64) (info []*models.DrugWarehouseInfoSix, err error) {
  71. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  72. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  73. fmt.Println(table)
  74. if drug_id > 0 {
  75. db = db.Where("x.drug_id = ?", drug_id)
  76. }
  77. 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
  78. return info, err
  79. }
  80. //更新药品库的total字段
  81. func UpdateDrugTotal(id int64, lib models.BaseDrugLib) error {
  82. err := XTWriteDB().Model(&lib).Where("id=? and status = 1", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  83. return err
  84. }
  85. func FindAllDrugWarehouseInfo(drug_id int64) (models.DrugWarehouseInfoSix, error) {
  86. info := models.DrugWarehouseInfoSix{}
  87. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  88. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  89. fmt.Println(table)
  90. if drug_id > 0 {
  91. db = db.Where("x.drug_id = ?", drug_id)
  92. }
  93. 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
  94. return info, err
  95. }