onlineReturnMoney.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package timedTask
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/database/gdb"
  6. "github.com/gogf/gf/v2/errors/gerror"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "jyOrderManager/internal/jyutil"
  10. "jyOrderManager/internal/logic/order"
  11. "jyOrderManager/internal/logic/product"
  12. "log"
  13. "math"
  14. "strings"
  15. "time"
  16. )
  17. // 线上回款-合并至回款表
  18. func OnlineReturnMoney() {
  19. var ctx = context.Background()
  20. data, _ := g.DB().Query(ctx, "SELECT r.id,r.order_code,r.return_money,p.pay_way,p.transaction_id,p.pay_time FROM return_money_online r inner join return_money_online_pay p on (r.id=p.return_id) WHERE r.status=1 and p.status =1 ")
  21. if !data.IsEmpty() {
  22. for _, m := range data.List() {
  23. var (
  24. rowId = gconv.String(m["id"])
  25. orderCode = gconv.String(m["order_code"])
  26. returnMoney = gconv.Int(m["return_money"])
  27. payTime = gconv.String(m["pay_time"])
  28. transactionId = gconv.String(m["transaction_id"])
  29. payWay = gconv.String(m["pay_way"])
  30. )
  31. orderData, _ := g.DB().GetOne(ctx, "dataexport_order", map[string]interface{}{
  32. "order_code": orderCode,
  33. }, "", "")
  34. if orderData.IsEmpty() {
  35. continue
  36. }
  37. var (
  38. money = gconv.Int(orderData.Map()["pay_money"])
  39. order_status = gconv.Int(orderData.Map()["order_status"])
  40. )
  41. //计算已回款金额
  42. var returned_money int
  43. if calculation, _ := g.DB().GetOne(ctx, "SELECT SUM(return_money) as returned_money FROM return_money_record WHERE order_code=? AND state=1", orderCode); !calculation.IsEmpty() {
  44. returned_money = gconv.Int(calculation.Map()["returned_money"])
  45. }
  46. if err := g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  47. if returned_money == 0 { //首次回款业绩生效
  48. if err := order.CommonChange(ctx, orderCode, payTime, order.ReturnMoney); err != nil {
  49. log.Printf("%s 回款销售业绩生效异常 %v", orderCode, err)
  50. }
  51. }
  52. orderUpdate := map[string]interface{}{}
  53. if order_status != 1 {
  54. orderUpdate["order_status"] = 1
  55. }
  56. if returnMoney+returned_money == money { //全额回款
  57. orderUpdate["return_status"] = 1
  58. } else {
  59. orderUpdate["return_status"] = 2
  60. }
  61. if _, err := g.DB().Update(ctx, "dataexport_order", orderUpdate, map[string]interface{}{"order_code": orderCode}); err != nil {
  62. return err
  63. }
  64. var (
  65. procedures float64
  66. return_type int
  67. )
  68. if strings.Contains(payWay, "wx") {
  69. return_type = 1
  70. procedures = math.Round(gconv.Float64(returnMoney) * order.WxProceduresMoney / 100)
  71. } else {
  72. return_type = 2
  73. procedures = math.Round(gconv.Float64(returnMoney) * order.AliProceduresMoney / 100)
  74. }
  75. if _, err := g.DB().Update(ctx, "return_money_online", map[string]interface{}{
  76. "status": 2,
  77. }, map[string]interface{}{
  78. "id": rowId,
  79. }); err != nil {
  80. return err
  81. }
  82. if _, err := g.DB().Insert(ctx, "return_money_record", map[string]interface{}{
  83. "return_time": payTime,
  84. "return_money": returnMoney,
  85. "procedures_money": procedures, //手续费
  86. "return_type": return_type, //'回款方式,1-微信 2-支付宝 3-对公转账',
  87. "return_code": transactionId,
  88. "order_code": orderCode,
  89. "operate_person": "系统自动",
  90. "operate_time": time.Now().Format("2006-01-02 15:04:05"),
  91. "operate_type": 3,
  92. "flow_money": returnMoney,
  93. "state": 1,
  94. }); err != nil {
  95. return err
  96. }
  97. if orderUpdate["return_status"] == 1 {
  98. productDetail, _ := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and returned_open =1 and returned_open = 0 and is_service_open = 1`, orderCode))
  99. if !productDetail.IsEmpty() {
  100. //全额回款开通权益
  101. for _, o := range productDetail.List() {
  102. if !jyutil.IsServiceOpen(m) {
  103. continue
  104. }
  105. //参数注入
  106. m["phone"] = o["user_phone"]
  107. m["order_code"] = orderCode
  108. m["amount"] = m["final_price"]
  109. m["reqCompanyName"] = o["company_name"]
  110. m["reqSubject"] = o["buy_subject"]
  111. productCode := gconv.String(m["product_code"])
  112. pFunc, err := product.JyProFunc.GetProductInitFuncByCode(productCode)
  113. if err != nil {
  114. continue
  115. }
  116. pObj, err := pFunc(m)
  117. if err != nil {
  118. gerror.Wrap(err, fmt.Sprintf("获取%s商品异常", productCode))
  119. continue
  120. }
  121. if err = pObj.OpenService(ctx, time.Now()); err != nil {
  122. log.Println("自动回款 OpenService err ", err)
  123. continue
  124. }
  125. }
  126. }
  127. }
  128. return nil
  129. }); err != nil {
  130. log.Println("回款更新失败", orderCode)
  131. }
  132. }
  133. }
  134. //关闭过期订单
  135. g.DB().Update(ctx, "return_money_online", map[string]interface{}{"state": 1}, " state=0 AND expire_time<=? ", time.Now().Format(time.DateTime))
  136. }