广告平台(总站长使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

82 lines
2.4 KiB

  1. package svc
  2. import (
  3. "applet/app/utils/cache"
  4. "applet/app/utils/logx"
  5. model2 "code.fnuoos.com/zhimeng/model.git/src/super/model"
  6. "errors"
  7. "fmt"
  8. "time"
  9. "xorm.io/xorm"
  10. )
  11. const MediumSettlementStateLockKey = "medium_settlement_state_lock_key"
  12. const MediumSettlementStateLockKeyValue = "running"
  13. func MediumSettlementState(eg *xorm.Engine) {
  14. now := time.Now()
  15. if now.Hour() > 1 && now.Hour() > 8 {
  16. //TODO::只在凌晨 1 点 ~ 凌晨 8 点运行
  17. fmt.Println("err>>>>>>>", errors.New("非运行时间"))
  18. logx.Warn(errors.New("非运行时间"))
  19. return
  20. }
  21. //TODO::增加“悲观锁”防止串行
  22. getString, _ := cache.GetString(MediumSettlementStateLockKey)
  23. //if err != nil {
  24. // return err
  25. //}
  26. if getString == MediumSettlementStateLockKeyValue {
  27. fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
  28. logx.Warn(errors.New("上一次结算未执行完"))
  29. return
  30. }
  31. cache.SetEx(MediumSettlementStateLockKey, MediumSettlementStateLockKeyValue, 3600*8) //8小时
  32. fmt.Println("calc master data statistics...")
  33. defer func() {
  34. if err := recover(); err != nil {
  35. _ = logx.Error(err)
  36. }
  37. }()
  38. today := now.Format("2006-01-02")
  39. //1、查询"日结"结算单 medium_settlement 数据
  40. _, err := eg.Where("kind = ?", 1).And("start_date != ?", today).Update(&model2.MediumSettlement{
  41. State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订)
  42. EndDate: now.AddDate(0, 0, -1).Format("2006-01-02"),
  43. })
  44. if err != nil {
  45. fmt.Println("err>>>>>>>", err.Error())
  46. logx.Warn(err)
  47. }
  48. //2、查询"周结"结算单 medium_settlement 数据
  49. weekday := now.Weekday()
  50. if weekday == time.Monday {
  51. _, err = eg.Where("kind = ?", 2).And("start_date != ?", today).Update(&model2.MediumSettlement{
  52. State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订)
  53. EndDate: now.AddDate(0, 0, -1).Format("2006-01-02"),
  54. })
  55. if err != nil {
  56. fmt.Println("err>>>>>>>", err.Error())
  57. logx.Warn(err)
  58. }
  59. }
  60. //3、查询"月结"结算单 medium_settlement 数据
  61. if now.Day() == 1 {
  62. _, err = eg.Where("kind = ?", 3).And("start_date != ?", today).Update(&model2.MediumSettlement{
  63. State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订)
  64. EndDate: now.AddDate(0, 0, -1).Format("2006-01-02"),
  65. })
  66. if err != nil {
  67. fmt.Println("err>>>>>>>", err.Error())
  68. logx.Warn(err)
  69. }
  70. }
  71. return
  72. }