auto_create_stock.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. 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. for _, it := range countList {
  40. if ite.ID == it.GoodId {
  41. formatInt := strconv.FormatInt(it.StockCount, 10)
  42. total, _ := strconv.ParseFloat(formatInt, 64)
  43. info := models.GoodInfo{
  44. Total: total,
  45. }
  46. UpdateStockTotal(it.GoodId, info)
  47. }
  48. }
  49. }
  50. }
  51. }
  52. //查询所有的耗材信息
  53. func FindAlLGoodInformation(orgid int64) (info []*models.GoodInfo, err error) {
  54. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&info).Error
  55. return info, err
  56. }
  57. func FindAllGoodInformationTotalCount(good_id int64) (info []*models.WarehousingInfo, err error) {
  58. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status =1")
  59. if good_id > 0 {
  60. db = db.Where("x.good_id = ?", good_id)
  61. }
  62. err = db.Select("x.good_id,sum(x.stock_count) as stock_count,x.warehousing_count").Find(&info).Error
  63. return info, err
  64. }
  65. //更新耗材库的total字段
  66. func UpdateStockTotal(id int64, lib models.GoodInfo) error {
  67. err := XTWriteDB().Model(&lib).Where("id=? and status = 1", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  68. return err
  69. }