acquirer_mkt_qr_update_logic.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. erpUser, 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 erpUser == nil || erpUser.UserId == "" {
  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. if partUser.MkId == "" {
  69. return &types.Response{500, "请联系管理员,分配mk", nil}, nil
  70. }
  71. erpUser, err := model.GetErpUser("", partUser.MkId, l.svcCtx.Transformer)
  72. if err != nil {
  73. logx.Error(err.Error())
  74. return &types.Response{500, err.Error(), nil}, nil
  75. }
  76. if erpUser == nil || erpUser.UserId == "" {
  77. return &types.Response{500, "未找到mk用户", nil}, nil
  78. }
  79. bean.QuaoYji = 1001
  80. bean.CallType = 1007
  81. bean.MaType = 1445
  82. bean.NetworkDetailId = 1058
  83. }
  84. acquirersQr := new(model.I2billAcquirerMktQrXorm)
  85. _, err = l.svcCtx.DB.Where("user_id = ? and del_flag = 0", userId).Get(acquirersQr)
  86. if err != nil {
  87. logx.Error(err.Error())
  88. return &types.Response{500, err.Error(), nil}, nil
  89. }
  90. if acquirersQr.Id != 0 {
  91. acquirersQr.LastUpdateTime = time.Now()
  92. acquirersQr.LastUpdateBy = userId
  93. acquirersQr.SchoolId = bean.SchoolId
  94. acquirersQr.ActivityId = bean.ActivityId
  95. acquirersQr.QuaoYji = bean.QuaoYji
  96. acquirersQr.CallType = bean.CallType
  97. acquirersQr.MaType = bean.MaType
  98. acquirersQr.NetworkDetailId = bean.NetworkDetailId
  99. _, err = l.svcCtx.DB.ID(acquirersQr.Id).Cols("school_id", "activity_id", "quao_yji", "call_type", "ma_type", "network_detail_id", "last_update_time", "last_update_by").Update(acquirersQr)
  100. if err != nil {
  101. logx.Error(err.Error())
  102. return &types.Response{500, err.Error(), nil}, nil
  103. }
  104. acquirersQr.Qr = strings.TrimLeft(acquirersQr.Qr, "/")
  105. domain := strings.TrimRight(l.svcCtx.Config.AliYunOss.FileUrl, "/")
  106. acquirersQr.Qr = domain + "/" + bean.Qr
  107. return &types.Response{200, "", bean}, nil
  108. }
  109. bean.LastUpdateBy = userId
  110. bean.LastUpdateTime = time.Now()
  111. bean.CreateBy = userId
  112. bean.CreateTime = bean.LastUpdateTime
  113. bean.DelFlag = 0
  114. bean.UserId = userId
  115. //生成二维码
  116. //scene, _ := utils.AesEncrypt([]byte(fmt.Sprintf("%d", userId)), []byte(l.svcCtx.Config.AesSecret))
  117. sceneStr := fmt.Sprintf("%d", userId)
  118. atr, err := l.svcCtx.Wechat.GenQrCode(sceneStr, l.svcCtx.Config.Weixin.SharePage)
  119. if err != nil {
  120. logx.Error(err.Error())
  121. return &types.Response{500, err.Error(), nil}, nil
  122. }
  123. if atr == nil || atr.Url == "" {
  124. return &types.Response{500, "二维码生成失败", nil}, nil
  125. }
  126. _, err = l.svcCtx.DB.Insert(atr)
  127. if err != nil {
  128. logx.Error(err.Error())
  129. return &types.Response{500, err.Error(), nil}, nil
  130. }
  131. bean.Qr = atr.Url
  132. _, err = l.svcCtx.DB.Insert(bean)
  133. bean.Qr = strings.TrimLeft(bean.Qr, "/")
  134. domain := strings.TrimRight(l.svcCtx.Config.AliYunOss.FileUrl, "/")
  135. bean.Qr = domain + "/" + bean.Qr
  136. if err != nil {
  137. return &types.Response{500, err.Error(), nil}, nil
  138. }
  139. return &types.Response{200, "", bean}, nil
  140. }