DeductionNotice.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package TimeMonitor
  2. import (
  3. "Cold_DeductionNotice/WarningNotice"
  4. "Cold_DeductionNotice/logs"
  5. "Cold_DeductionNotice/models/Account"
  6. "Cold_DeductionNotice/models/Company"
  7. "Cold_DeductionNotice/models/Warning"
  8. "fmt"
  9. "github.com/robfig/cron/v3"
  10. "strconv"
  11. "time"
  12. )
  13. func DeductionNoticeInit() {
  14. //go DeductionNoticeStatus()
  15. crontab := cron.New(cron.WithSeconds())
  16. _, err := crontab.AddFunc("0 0 1 * * *", DeductionNoticeStatus)
  17. if err != nil {
  18. fmt.Printf("err: %v\n", err)
  19. fmt.Println("初始化成功")
  20. }
  21. crontab.Start()
  22. defer crontab.Stop()
  23. select {}
  24. }
  25. func DeductionNoticeStatus() {
  26. fmt.Println("==============定时 通知扣费================")
  27. now := time.Now()
  28. d, _ := time.ParseDuration("-24h")
  29. currentTime := now.Add(d)
  30. logs.Println(fmt.Sprintf("%d-%02d-%02d", currentTime.Year(), currentTime.Month(), currentTime.Day()))
  31. TABLE_name := fmt.Sprintf("warning_sand_%d%02d", currentTime.Year(), currentTime.Month())
  32. logs.Println("TABLE_name:", TABLE_name)
  33. StartTine := fmt.Sprintf("%d-%02d-%02d 00:00:00", currentTime.Year(), currentTime.Month(), currentTime.Day())
  34. EndTime := fmt.Sprintf("%d-%02d-%02d 23:59:59", currentTime.Year(), currentTime.Month(), currentTime.Day())
  35. for _,v := range Account.Read_Company_All(){
  36. logs.Println("=======",v.Id,v.T_name)
  37. var Money float32
  38. // 微信计费
  39. Ntype_0 := Warning.Get_WarningSandNum(TABLE_name, v.Id, 0, StartTine, EndTime)
  40. logs.Println("微信条数:",Ntype_0," 金额:",Ntype_0 * 0)
  41. // 短信计费
  42. Ntype_1 := Warning.Get_WarningSandNum(TABLE_name, v.Id, 1, StartTine, EndTime)
  43. logs.Println("短信条数:",Ntype_1," 金额:",float32(Ntype_1) * 0.1)
  44. Money += float32(Ntype_1) * 0.12
  45. // 电话计费
  46. Ntype_2 := Warning.Get_WarningSandNum(TABLE_name, v.Id, 2, StartTine, EndTime)
  47. logs.Println("电话条数:",Ntype_2," 金额:",float32(Ntype_2) * 0.1)
  48. Money += float32(Ntype_2) * 0.15
  49. if Money == 0 {
  50. // 没有费用,跳过
  51. continue
  52. }
  53. // 更新费用
  54. _, Company_r := Account.Read_Company_id(v.Id)
  55. logs.Println(Company_r.T_name," 扣费记录 -"," 当期余额:",Company_r.T_money," ",fmt.Sprintf("%d-%02d-%02d", currentTime.Year(), currentTime.Month(), currentTime.Day()),"扣除:",Money)
  56. Company_r.T_money = Company_r.T_money - Money
  57. Account.Update_Company(Company_r,"T_money")
  58. // 插入账单
  59. CompanyBill_r := Company.CompanyBill{
  60. T_pid : Company_r.Id,
  61. T_type : "扣费",
  62. T_project : "通知计费(天)",
  63. T_bill : fmt.Sprintf("%d-%02d-%02d", currentTime.Year(), currentTime.Month(), currentTime.Day())+"通知计费(天),短信条数:"+strconv.Itoa(Ntype_1)+"条,电话条数:"+strconv.Itoa(Ntype_2)+"条",
  64. T_charging : Money,
  65. T_balance : Company_r.T_money,
  66. }
  67. Company.Add_CompanyBill(CompanyBill_r)
  68. var Warning_r Warning.Warning
  69. Warning_r.T_pid = Company_r.Id // 报警类型
  70. Warning_r.T_tp = 1002 // 报警类型
  71. Warning_r.T_sn = ""
  72. Warning_r.T_id = 0
  73. Warning_r.T_Msid = 0
  74. Warning_r.T_D_name = Company_r.T_name
  75. Warning_r.T_DS_name = Company_r.T_name
  76. Warning_r.T_Ut = time.Now()
  77. Warning_r.T_State = 1
  78. // 余额预警
  79. if Company_r.T_money < 10{
  80. Warning_r.T_Remark = "您当前余额不足10元,请及时充值"
  81. }
  82. if Company_r.T_money <= 0{
  83. Warning_r.T_Remark = "您当前余额"+fmt.Sprintf("%.2f", Company_r.T_money)+"元,请及时充值"
  84. }
  85. // 发送给用户
  86. WarningNotice.WarningToUser(&Warning_r,Warning_r.T_pid)
  87. Warning.Add_Warning(Warning_r) // 添加报警
  88. }
  89. //
  90. }