serviceOpen.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package order
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/gogf/gf/v2/database/gdb"
  7. "github.com/gogf/gf/v2/errors/gerror"
  8. "github.com/gogf/gf/v2/frame/g"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "jyOrderManager/internal/consts"
  11. "jyOrderManager/internal/jyutil"
  12. "jyOrderManager/internal/logic/product"
  13. "jyOrderManager/internal/model"
  14. "log"
  15. "time"
  16. )
  17. func ServiceOpen(ctx context.Context, param model.OrderOpenServiceParams) error {
  18. //查询订单
  19. if !consts.PhoneRegex.MatchString(param.Phone) {
  20. return errors.New("手机号格式异常")
  21. }
  22. orderData, _ := g.DB().GetOne(ctx, fmt.Sprintf("select * from dataexport_order where order_code ='%s' and audit_status =3", param.OrderCode))
  23. if orderData.IsEmpty() {
  24. return errors.New("为查询到订单信息")
  25. }
  26. if gconv.String(orderData.Map()["user_phone"]) != param.Phone {
  27. g.DB().Update(ctx, "dataexport_order", map[string]interface{}{
  28. "user_phone": param.Phone,
  29. }, map[string]interface{}{
  30. "order_code": param.OrderCode,
  31. })
  32. }
  33. productDetail, err := g.DB().Ctx(ctx).Query(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE order_code ='%s' and is_service_open = 0 and status =1`, param.OrderCode))
  34. if err != nil || productDetail.IsEmpty() {
  35. return err
  36. }
  37. uData, entId, userPositionId, err := jyutil.GetCreateUserData(param.Phone, gconv.String(orderData.Map()["company_name"]), gconv.Int(orderData.Map()["buy_subject"]) == 2)
  38. if err != nil {
  39. return errors.New("用户创建失败")
  40. }
  41. if err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
  42. // 产品服务开通
  43. for _, m := range productDetail.List() {
  44. if !jyutil.IsServiceOpen(m) {
  45. continue
  46. }
  47. var openTime time.Time
  48. if gconv.Time(param.StartTime).Unix() > time.Now().Unix() {
  49. openTime = gconv.Time(param.StartTime)
  50. } else {
  51. openTime = time.Now()
  52. }
  53. //参数注入
  54. m["userMap"] = map[string]interface{}{
  55. "userData": uData, "entId": entId, "userPositionId": userPositionId,
  56. }
  57. m["phone"] = param.Phone
  58. m["order_code"] = param.OrderCode
  59. m["reqCompanyName"] = orderData.Map()["company_name"]
  60. m["amount"] = m["final_price"]
  61. m["reqSubject"] = orderData.Map()["buy_subject"]
  62. m["linked_orderId"] = m["linked_detail_id"]
  63. productCode := gconv.String(m["product_code"])
  64. pFunc, err := product.JyProFunc.GetProductInitFuncByCode(productCode)
  65. if err != nil {
  66. return err
  67. }
  68. pObj, err := pFunc(m)
  69. if err != nil {
  70. return gerror.Wrap(err, fmt.Sprintf("获取%s商品异常", productCode))
  71. }
  72. if err := pObj.OpenService(ctx, openTime); err != nil {
  73. return err
  74. }
  75. }
  76. if orderUserId := gconv.String(orderData.Map()["user_id"]); orderUserId == "" || orderUserId != gconv.String(uData["userId"]) {
  77. log.Printf("同步更新订单用户身份:orderUserId:%s,userId:%s,entId:%d\n", orderUserId, uData["userId"], entId)
  78. upData := g.Map{
  79. "user_id": uData["userId"],
  80. }
  81. if entId > 0 { //企业服务
  82. upData["ent_id"] = entId
  83. if personPhone := gconv.String(orderData.Map()["personPhone"]); personPhone != "" {
  84. jyutil.EndAddUser(ctx, entId, gconv.String(orderData.Map()["user_phone"]), personPhone, gconv.String(orderData.Map()["personName"]))
  85. }
  86. }
  87. //更新订单
  88. _, err = g.DB().Update(ctx, consts.OrderListTableName, upData, "order_code=?", param.OrderCode)
  89. if err != nil {
  90. return err
  91. }
  92. }
  93. return nil
  94. }); err != nil {
  95. log.Println(err)
  96. return err
  97. }
  98. return nil
  99. }