returnMoneyTask.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package task
  2. import (
  3. "config"
  4. "fmt"
  5. "github.com/cron"
  6. "log"
  7. "math"
  8. "order"
  9. qu "qfw/util"
  10. "qfw/util/redis"
  11. "time"
  12. "util"
  13. )
  14. // TimeTask1 自动回款定时任务
  15. func AutoReturnMoneyTask() {
  16. StartTask1()
  17. c := cron.New()
  18. taskTime := config.SysConfigs.Tasktime
  19. log.Println(taskTime)
  20. cronStr := "0 */" + fmt.Sprint(taskTime) + " * * * ?"
  21. // cronstr := "0 0 */" + fmt.Sprint(taskTime) + " * * ?" //每TaskTime小时执行一次
  22. err := c.AddFunc(cronStr, func() {
  23. StartTask1()
  24. })
  25. if err != nil {
  26. fmt.Println("定时任务启动失败:", err)
  27. return
  28. }
  29. c.Start()
  30. }
  31. // StartTask1 自动回款任务
  32. func StartTask1() {
  33. log.Println("自动回款匹配。。。开始")
  34. query := "select * from transaction where ISRELATION = 0"
  35. log.Println("query: ", query)
  36. data := util.CbsDB.SelectBySql(query)
  37. if data != nil && *data != nil && len(*data) > 0 {
  38. orderData := util.JysqlDB.Find("dataexport_order", map[string]interface{}{"return_status": 0, "audit_status": 3}, "order_code,pay_money,company_name,commission,procedures_money,filter", "", -1, -1)
  39. for _, v := range *data {
  40. returnMoney := int(math.Floor(float64(float64(qu.Float64All(v["TRSBAL"]))*100) + 0.5))
  41. companyName := qu.ObjToString(v["OTHNAM"])
  42. remark := qu.ObjToString(v["TXTDSM"])
  43. remarks := qu.ObjToString(v["NUSAGE"])
  44. id := qu.IntAll(v["id"])
  45. returnTime := qu.ObjToString(v["BNKTIM"])
  46. BNKFLW := qu.ObjToString(v["BNKFLW"])
  47. BNKNAM := qu.ObjToString(v["BNKNAM"])
  48. if orderData != nil && len(*orderData) > 0 {
  49. for _, o := range *orderData {
  50. money := qu.IntAll(o["pay_money"]) - qu.IntAll(o["commission"]) - qu.IntAll(o["procedures_money"])
  51. companyNames := qu.ObjToString(o["company_name"])
  52. orderCode := qu.ObjToString(o["order_code"])
  53. if money == returnMoney && (companyNames == companyName || remark == orderCode || remarks == orderCode) {
  54. isExists, err := redis.Exists("qmx_filter", "qmx_auto_return_"+fmt.Sprint(id))
  55. if !isExists || err != nil {
  56. log.Println("自动回款匹配成功", id, orderCode)
  57. updateData := map[string]interface{}{"return_status": 1, "order_status": 1}
  58. contract := util.JysqlDB.FindOne("contract", map[string]interface{}{"order_code": orderCode}, "", "")
  59. count := util.JysqlDB.Count("return_money_record", map[string]interface{}{"order_code": orderCode})
  60. if count < 1 && contract != nil && qu.IntAll((*contract)["contract_archive_status"]) != 1 {
  61. updateData["sale_time"] = returnTime
  62. }
  63. returnId := util.JysqlDB.Insert("return_money_record", map[string]interface{}{
  64. "order_code": orderCode,
  65. "return_time": returnTime,
  66. "return_money": money,
  67. "return_type": 3,
  68. "return_invoice_status": 0,
  69. "operate_time": time.Now().Format("2006-01-02 15:04:05"),
  70. "flow_money": money,
  71. "bank_name": BNKNAM,
  72. "bank_flow": BNKFLW,
  73. "operate_type": 2,
  74. "state": 1,
  75. "pay_account_name": companyName,
  76. })
  77. redis.Put("qmx_filter", "qmx_auto_return_"+fmt.Sprint(id), 1, 3600)
  78. if returnId > 0 {
  79. log.Println("自动回款创建回款记录成功", id, orderCode)
  80. util.JysqlDB.Update("dataexport_order", map[string]interface{}{"order_code": orderCode}, updateData)
  81. util.CbsDB.Update("transaction", map[string]interface{}{"id": id}, map[string]interface{}{"ISRELATION": 1, "return_id": fmt.Sprint(returnId)})
  82. filterMap := qu.ObjToMap(o["filter"])
  83. paybackOpenServer := false
  84. if v, ok := ((*filterMap)["paybackOpenServer"]).(bool); ok {
  85. paybackOpenServer = v
  86. }
  87. if qu.IntAll(o["return_status"]) == 1 && paybackOpenServer {
  88. err := order.OpenUserService(nil, orderCode, o)
  89. if err != nil {
  90. log.Println("自动回款开通权益失败", id, orderCode)
  91. }
  92. }
  93. } else {
  94. log.Println("自动回款创建回款记录失败", id, orderCode)
  95. }
  96. } else {
  97. log.Println("自动回款重复匹配过滤", id, orderCode)
  98. }
  99. }
  100. }
  101. }
  102. }
  103. } else {
  104. log.Println("回款信息查询为空")
  105. }
  106. log.Println("自动回款匹配。。。结束")
  107. }