auto_create_stock.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 createStockCronJob *cron.Cron
  11. func init() {
  12. utils.InfoLog("开启自动更新耗材库存定时任务")
  13. createStockCronJob = cron.New()
  14. //spec := "0 */5 * * * ?" // 每1分钟执行一次
  15. spec := "0 55 23 * * ?" // 每天23点55执行一次
  16. //spec := "0 0 0 ? * Sun"
  17. createStockCronJob.AddFunc(spec, func() {
  18. AutoCreateStockJob()
  19. })
  20. }
  21. func BeginAutoCreateStockJob() {
  22. createStockCronJob.Start()
  23. }
  24. func AutoCreateStockJob() {
  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. for _, item := range org {
  36. information, _ := FindAlLGoodInformation(item.UserOrgId)
  37. for _, ite := range information {
  38. //countList, _ := FindAllGoodInformationTotalCount(ite.ID)
  39. //查询入库数量
  40. warehouseInfo, _ := FindAllGoodWarehouseInfoCount(ite.ID)
  41. //查询自动出库数量
  42. autoWarehouseOut, _ := FindAutoGoodWarehouseInfoCount(ite.ID)
  43. //查询手动出库
  44. warehouseOut, _ := FindGoodWarehouseOutCount(ite.ID)
  45. var total int64
  46. total = warehouseInfo.WarehousingCount - autoWarehouseOut.Count - warehouseOut.Count
  47. formatInt := strconv.FormatInt(total, 10)
  48. totalfloat, _ := strconv.ParseFloat(formatInt, 64)
  49. info := models.GoodInfo{
  50. Total: totalfloat,
  51. }
  52. UpdateStockTotal(ite.ID, info)
  53. //for _, it := range countList {
  54. // if ite.ID == it.GoodId {
  55. // formatInt := strconv.FormatInt(it.StockCount, 10)
  56. // total, _ := strconv.ParseFloat(formatInt, 64)
  57. // info := models.GoodInfo{
  58. // Total: total,
  59. // }
  60. // UpdateStockTotal(it.GoodId, info)
  61. // }
  62. //}
  63. }
  64. }
  65. }
  66. //查询所有的耗材信息
  67. func FindAlLGoodInformation(orgid int64) (info []*models.GoodInfo, err error) {
  68. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&info).Error
  69. return info, err
  70. }
  71. func FindAllGoodInformationTotalCount(good_id int64) (info []*models.WarehousingInfo, err error) {
  72. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status =1")
  73. if good_id > 0 {
  74. db = db.Where("x.good_id = ?", good_id)
  75. }
  76. err = db.Select("x.good_id,sum(x.stock_count) as stock_count,x.warehousing_count").Find(&info).Error
  77. return info, err
  78. }
  79. //更新耗材库的total字段
  80. func UpdateStockTotal(id int64, lib models.GoodInfo) error {
  81. err := XTWriteDB().Model(&lib).Where("id=? and status = 1", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  82. return err
  83. }
  84. func FindAllGoodWarehouseInfoCount(good_id int64) (models.WarehousingInfo, error) {
  85. info := models.WarehousingInfo{}
  86. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  87. if good_id > 0 {
  88. db = db.Where("x.good_id = ? and x.warehousing_count <> 0", good_id)
  89. }
  90. err = db.Select("x.good_id,sum(x.warehousing_count) as warehousing_count").Find(&info).Error
  91. return info, err
  92. }
  93. func FindAutoGoodWarehouseInfoCount(good_id int64) (models.AutomaticReduceDetail, error) {
  94. detail := models.AutomaticReduceDetail{}
  95. db := XTReadDB().Table("xt_automatic_reduce_detail as x").Where("x.status = 1")
  96. if good_id > 0 {
  97. db = db.Where("x.good_id = ? and x.count<> 0", good_id)
  98. }
  99. err := db.Select("x.good_id,sum(x.count) as count").Find(&detail).Error
  100. return detail, err
  101. }
  102. func FindGoodWarehouseOutCount(good_id int64) (models.WarehouseOutInfo, error) {
  103. out := models.WarehouseOutInfo{}
  104. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  105. if good_id > 0 {
  106. db = db.Where("x.good_id = ? and x.is_sys = 0 and x.count<> 0", good_id)
  107. }
  108. err := db.Select("x.good_id,sum(x.count) as count").Find(&out).Error
  109. return out, err
  110. }