auto_create_stock.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 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. utils.InfoLog("开启自动更新耗材库存定时任务1111111111111")
  19. AutoCreateStockJob()
  20. })
  21. }
  22. func BeginAutoCreateStockJob() {
  23. createStockCronJob.Start()
  24. }
  25. func AutoCreateStockJob() {
  26. timeStr := time.Now().Format("2006-01-02")
  27. timeLayout := "2006-01-02 15:04:05"
  28. fmt.Println("timeStr:", timeStr)
  29. timeStringToTime, _ := utils.ParseTimeStringToTime(timeLayout, timeStr+" 00:00:00")
  30. timenow := timeStringToTime.Unix()
  31. fmt.Println("timenow是什么", timenow)
  32. //查询当天排班所有机构
  33. org, errs := GetAllOrgName(timenow)
  34. fmt.Print("错误是什么", errs)
  35. fmt.Println("查询有机构失败", err)
  36. for _, item := range org {
  37. information, _ := FindAlLGoodInformation(item.UserOrgId)
  38. for _, ite := range information {
  39. countList, _ := FindAllGoodInformationTotalCount(ite.ID)
  40. for _, it := range countList {
  41. if ite.ID == it.GoodId {
  42. formatInt := strconv.FormatInt(it.StockCount, 10)
  43. total, _ := strconv.ParseFloat(formatInt, 64)
  44. info := models.GoodInfo{
  45. Total: total,
  46. }
  47. UpdateStockTotal(it.GoodId, info)
  48. }
  49. }
  50. }
  51. }
  52. }
  53. //查询所有的耗材信息
  54. func FindAlLGoodInformation(orgid int64) (info []*models.GoodInfo, err error) {
  55. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&info).Error
  56. return info, err
  57. }
  58. func FindAllGoodInformationTotalCount(good_id int64) (info []*models.WarehousingInfo, err error) {
  59. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status =1")
  60. if good_id > 0 {
  61. db = db.Where("x.good_id = ?", good_id)
  62. }
  63. err = db.Select("x.good_id,sum(x.stock_count) as stock_count,x.warehousing_count").Find(&info).Error
  64. return info, err
  65. }
  66. //更新耗材库的total字段
  67. func UpdateStockTotal(id int64, lib models.GoodInfo) error {
  68. err := XTWriteDB().Model(&lib).Where("id=? and status = 1", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  69. return err
  70. }