acquirer_mkt_qr_update_logic.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package acquirer_mkt_qr
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.i2edu.net/i2/i2-bill-api/model"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  12. "git.i2edu.net/i2/i2-bill-api/internal/types"
  13. "git.i2edu.net/i2/go-zero/core/logx"
  14. )
  15. type AcquirerMktQrUpdateLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewAcquirerMktQrUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) AcquirerMktQrUpdateLogic {
  21. return AcquirerMktQrUpdateLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (l *AcquirerMktQrUpdateLogic) AcquirerMktQrUpdate(r *http.Request) (*types.Response, error) {
  28. // todo: add your logic here and delete this line
  29. userId := l.svcCtx.GetUserIdByJwt(l.ctx)
  30. body, err := ioutil.ReadAll(r.Body)
  31. if err != nil {
  32. logx.Error(err.Error())
  33. return &types.Response{500, err.Error(), nil}, nil
  34. }
  35. bean := new(model.I2billAcquirerMktQrXorm)
  36. err = json.Unmarshal(body, bean)
  37. if err != nil {
  38. logx.Error(err.Error())
  39. return &types.Response{500, err.Error(), nil}, nil
  40. }
  41. userInfo, err := model.GetI2bilUserInfo(userId, l.svcCtx.DB)
  42. if err != nil {
  43. logx.Error(err.Error())
  44. return &types.Response{500, err.Error(), nil}, nil
  45. }
  46. //var ty = "part"
  47. //mk
  48. if userInfo.ErpId != "" {
  49. erpId, err := model.GetErpUser("", userInfo.ErpId, l.svcCtx.Transformer)
  50. if err != nil {
  51. logx.Error(err.Error())
  52. return &types.Response{500, err.Error(), nil}, nil
  53. }
  54. if erpId == "" {
  55. return &types.Response{500, "未找到mk用户", nil}, nil
  56. }
  57. //ty = "mk"
  58. } else {
  59. //兼职
  60. partUser, err := model.GetPartTimeXormByUserId(userId, l.svcCtx.DB)
  61. if err != nil {
  62. logx.Error(err.Error())
  63. return &types.Response{500, err.Error(), nil}, nil
  64. }
  65. if partUser.Id == 0 {
  66. return &types.Response{500, "请先申请成为兼职", nil}, nil
  67. }
  68. erpId, err := model.GetErpUser("", partUser.MkId, l.svcCtx.Transformer)
  69. if err != nil {
  70. logx.Error(err.Error())
  71. return &types.Response{500, err.Error(), nil}, nil
  72. }
  73. if erpId == "" {
  74. return &types.Response{500, "未找到mk用户", nil}, nil
  75. }
  76. bean.QudaoId = 1058
  77. bean.ActivityId = 0
  78. }
  79. acquirersQr := new(model.I2billAcquirerMktQrXorm)
  80. _, err = l.svcCtx.DB.Where("user_id = ? and del_flag = 0", userId).Get(acquirersQr)
  81. if err != nil {
  82. logx.Error(err.Error())
  83. return &types.Response{500, err.Error(), nil}, nil
  84. }
  85. if acquirersQr.Id != 0 {
  86. bean.LastUpdateTime = time.Now()
  87. bean.LastUpdateBy = userId
  88. _, err = l.svcCtx.DB.ID(acquirersQr.Id).AllCols().Update(bean)
  89. if err != nil {
  90. logx.Error(err.Error())
  91. return &types.Response{500, err.Error(), nil}, nil
  92. }
  93. return &types.Response{200, "", nil}, nil
  94. }
  95. bean.LastUpdateBy = userId
  96. bean.LastUpdateTime = time.Now()
  97. bean.CreateBy = userId
  98. bean.CreateTime = bean.LastUpdateTime
  99. bean.DelFlag = 0
  100. bean.UserId = userId
  101. //生成二维码
  102. //scene, _ := utils.AesEncrypt([]byte(fmt.Sprintf("%d", userId)), []byte(l.svcCtx.Config.AesSecret))
  103. sceneStr := fmt.Sprintf("%d", userId)
  104. atr, err := l.svcCtx.Wechat.GenQrCode(sceneStr, "pages/code/code")
  105. if err != nil {
  106. logx.Error(err.Error())
  107. return &types.Response{500, err.Error(), nil}, nil
  108. }
  109. if atr == nil || atr.Url == "" {
  110. return &types.Response{500, "二维码生成失败", nil}, nil
  111. }
  112. _, err = l.svcCtx.DB.Insert(atr)
  113. if err != nil {
  114. logx.Error(err.Error())
  115. return &types.Response{500, err.Error(), nil}, nil
  116. }
  117. bean.Qr = atr.Url
  118. _, err = l.svcCtx.DB.Insert(bean)
  119. bean.Qr = strings.TrimLeft(bean.Qr, "/")
  120. domain := strings.TrimRight(l.svcCtx.Config.AliYunOss.FileUrl, "/")
  121. bean.Qr = domain + "/" + bean.Qr
  122. if err != nil {
  123. return &types.Response{500, err.Error(), nil}, nil
  124. }
  125. return &types.Response{200, "", bean}, nil
  126. }