onlineReturnMoney.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/consts"
  10. "jyOrderManager/internal/jyutil"
  11. "jyOrderManager/internal/logic/order"
  12. "jyOrderManager/internal/logic/product"
  13. "log"
  14. "math"
  15. "strings"
  16. "time"
  17. )
  18. // 线上回款-合并至回款表
  19. func OnlineReturnMoney() {
  20. var ctx = context.Background()
  21. 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 ")
  22. if !data.IsEmpty() {
  23. for _, m := range data.List() {
  24. var (
  25. rowId = gconv.String(m["id"])
  26. orderCode = gconv.String(m["order_code"])
  27. returnMoney = gconv.Int(m["return_money"])
  28. payTime = gconv.String(m["pay_time"])
  29. transactionId = gconv.String(m["transaction_id"])
  30. payWay = gconv.String(m["pay_way"])
  31. )
  32. orderData, _ := g.DB().GetOne(ctx, "SELECT * FROM dataexport_order WHERE order_code = ?", orderCode)
  33. if orderData.IsEmpty() {
  34. continue
  35. }
  36. var (
  37. money = gconv.Int(orderData.Map()["pay_money"])
  38. order_status = gconv.Int(orderData.Map()["order_status"])
  39. )
  40. //计算已回款金额
  41. var returned_money int
  42. 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() {
  43. returned_money = gconv.Int(calculation.Map()["returned_money"])
  44. }
  45. uData := make(map[string]interface{})
  46. var (
  47. entId, userPositionId int64
  48. err error
  49. )
  50. if returnMoney+returned_money == money && consts.PhoneRegex.MatchString(gconv.String(orderData["user_phone"])) {
  51. uData, entId, userPositionId, err = jyutil.GetCreateUserData(gconv.String(orderData.Map()["user_phone"]), gconv.String(orderData.Map()["company_name"]), gconv.Int(orderData.Map()["buy_subject"]) == 2)
  52. if err != nil {
  53. continue
  54. }
  55. }
  56. if err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  57. if returned_money == 0 { //首次回款业绩生效
  58. if err := order.CommonChange(ctx, orderCode, payTime, order.ReturnMoney); err != nil {
  59. log.Printf("%s 回款销售业绩生效异常 %v", orderCode, err)
  60. }
  61. }
  62. orderUpdate := map[string]interface{}{}
  63. if order_status != 1 {
  64. orderUpdate["order_status"] = 1
  65. }
  66. if returnMoney+returned_money == money { //全额回款
  67. orderUpdate["return_status"] = 1
  68. } else {
  69. orderUpdate["return_status"] = 2
  70. }
  71. if _, err := g.DB().Update(ctx, "dataexport_order", orderUpdate, map[string]interface{}{"order_code": orderCode}); err != nil {
  72. return err
  73. }
  74. var (
  75. procedures float64
  76. return_type int
  77. )
  78. if strings.Contains(payWay, "wx") {
  79. return_type = 1
  80. procedures = math.Round(gconv.Float64(returnMoney) * order.WxProceduresMoney / 100)
  81. } else {
  82. return_type = 2
  83. procedures = math.Round(gconv.Float64(returnMoney) * order.AliProceduresMoney / 100)
  84. }
  85. if _, err := g.DB().Update(ctx, "return_money_online", map[string]interface{}{
  86. "status": 2,
  87. }, map[string]interface{}{
  88. "id": rowId,
  89. }); err != nil {
  90. return err
  91. }
  92. if _, err := g.DB().Insert(ctx, "return_money_record", map[string]interface{}{
  93. "return_time": payTime,
  94. "return_money": returnMoney,
  95. "procedures_money": procedures, //手续费
  96. "return_type": return_type, //'回款方式,1-微信 2-支付宝 3-对公转账',
  97. "return_code": transactionId,
  98. "order_code": orderCode,
  99. "operate_person": "系统自动",
  100. "operate_time": time.Now().Format("2006-01-02 15:04:05"),
  101. "operate_type": 3,
  102. "flow_money": returnMoney,
  103. "state": 1,
  104. }); err != nil {
  105. return err
  106. }
  107. if orderUpdate["return_status"] == 1 && consts.PhoneRegex.MatchString(gconv.String(orderData["user_phone"])) {
  108. productDetail, _ := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and returned_open =1 and is_service_open = 0 and status =1`, orderCode))
  109. if !productDetail.IsEmpty() {
  110. //全额回款开通权益
  111. for _, o := range productDetail.List() {
  112. if !jyutil.IsServiceOpen(o) {
  113. continue
  114. }
  115. o["userMap"] = map[string]interface{}{
  116. "userData": uData, "entId": entId, "userPositionId": userPositionId,
  117. }
  118. //参数注入
  119. o["phone"] = orderData.Map()["user_phone"]
  120. o["order_code"] = orderCode
  121. o["amount"] = o["final_price"]
  122. o["reqCompanyName"] = orderData.Map()["company_name"]
  123. o["reqSubject"] = orderData.Map()["buy_subject"]
  124. o["linked_orderId"] = o["linked_detail_id"]
  125. o["linkedOrderId"] = o["linked_detail_id"]
  126. log.Println("order_code自动开启权益", o)
  127. productCode := gconv.String(o["product_code"])
  128. pFunc, err := product.JyProFunc.GetProductInitFuncByCode(productCode)
  129. if err != nil {
  130. continue
  131. }
  132. pObj, err := pFunc(o)
  133. if err != nil {
  134. gerror.Wrap(err, fmt.Sprintf("获取%s商品异常", productCode))
  135. continue
  136. }
  137. if err = pObj.OpenService(ctx, time.Now()); err != nil {
  138. log.Println("自动回款 OpenService err ", err)
  139. continue
  140. }
  141. }
  142. if gconv.Int(orderData.Map()["buy_subject"]) == 2 {
  143. uData["userId"] = userPositionId
  144. }
  145. if orderUserId := gconv.String(orderData.Map()["user_id"]); orderUserId == "" || orderUserId != gconv.String(uData["userId"]) || (gconv.Int(orderData.Map()["buy_subject"]) == 2 && gconv.Int64(orderData.Map()["ent_id"]) != entId) {
  146. log.Printf("同步更新订单用户身份:orderUserId:%s,userId:%v,entId:%d\n", orderUserId, uData["userId"], entId)
  147. upData := g.Map{
  148. "user_id": uData["userId"],
  149. }
  150. if entId > 0 { //企业服务
  151. upData["ent_id"] = entId
  152. if personPhone := gconv.String(orderData.Map()["personPhone"]); personPhone != "" {
  153. jyutil.EndAddUser(ctx, entId, gconv.String(orderData.Map()["user_phone"]), personPhone, gconv.String(orderData.Map()["personName"]))
  154. }
  155. }
  156. //更新订单
  157. _, err = g.DB().Update(ctx, consts.OrderListTableName, upData, "order_code=?", orderCode)
  158. if err != nil {
  159. return err
  160. }
  161. }
  162. }
  163. }
  164. return nil
  165. }); err != nil {
  166. log.Println("回款更新失败", orderCode)
  167. }
  168. }
  169. }
  170. //关闭过期订单
  171. g.DB().Update(ctx, "return_money_online", map[string]interface{}{"state": 1}, " state=0 AND expire_time<=? ", time.Now().Format("2006-01-02 15:04:05"))
  172. }