acquirer_mkt_qr_update_logic.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.GetAcquirePermInfo(userId, l.svcCtx.Transformer, l.svcCtx.DB)
  42. if err != nil {
  43. logx.Error(err.Error())
  44. return &types.Response{500, err.Error(), nil}, nil
  45. }
  46. if userInfo == nil || userInfo.UserId == "" {
  47. return &types.Response{500, "请先申请成为兼职", nil}, nil
  48. }
  49. //var ty = "part"
  50. //mk
  51. if userInfo.Role == 2 {
  52. bean.Role = 2
  53. //ty = "mk"
  54. } else {
  55. //兼职
  56. partUser, err := model.GetPartTimeXormByUserId(userId, l.svcCtx.DB)
  57. if err != nil {
  58. logx.Error(err.Error())
  59. return &types.Response{500, err.Error(), nil}, nil
  60. }
  61. if partUser.Id == 0 {
  62. return &types.Response{500, "请先申请成为兼职", nil}, nil
  63. }
  64. if partUser.MkId == "" {
  65. return &types.Response{500, "请联系管理员,分配mk", nil}, nil
  66. }
  67. erpUser, err := model.GetErpUser("", partUser.MkId, l.svcCtx.Transformer)
  68. if err != nil {
  69. logx.Error(err.Error())
  70. return &types.Response{500, err.Error(), nil}, nil
  71. }
  72. if erpUser == nil || erpUser.UserId == "" {
  73. return &types.Response{500, "未找到分配有效的mk", nil}, nil
  74. }
  75. bean.QuaoYji = 1001
  76. bean.CallType = 1007
  77. bean.MaType = 1445
  78. bean.NetworkDetailId = 1058
  79. bean.ActivityId = 0
  80. bean.Role = 1
  81. }
  82. acquirersQr := new(model.I2billAcquirerMktQrXorm)
  83. _, err = l.svcCtx.DB.Where("user_id = ? and del_flag = 0", userId).Get(acquirersQr)
  84. if err != nil {
  85. logx.Error(err.Error())
  86. return &types.Response{500, err.Error(), nil}, nil
  87. }
  88. if acquirersQr.Id != 0 {
  89. acquirersQr.LastUpdateTime = time.Now()
  90. acquirersQr.LastUpdateBy = userId
  91. acquirersQr.SchoolId = bean.SchoolId
  92. acquirersQr.ActivityId = bean.ActivityId
  93. acquirersQr.QuaoYji = bean.QuaoYji
  94. acquirersQr.CallType = bean.CallType
  95. acquirersQr.MaType = bean.MaType
  96. acquirersQr.NetworkDetailId = bean.NetworkDetailId
  97. acquirersQr.Role = bean.Role
  98. _, 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", "role").Update(acquirersQr)
  99. if err != nil {
  100. logx.Error(err.Error())
  101. return &types.Response{500, err.Error(), nil}, nil
  102. }
  103. acquirersQr.Qr = strings.TrimLeft(acquirersQr.Qr, "/")
  104. domain := strings.TrimRight(l.svcCtx.Config.AliYunOss.FileUrl, "/")
  105. acquirersQr.Qr = domain + "/" + bean.Qr
  106. return &types.Response{200, "", bean}, nil
  107. }
  108. bean.LastUpdateBy = userId
  109. bean.LastUpdateTime = time.Now()
  110. bean.CreateBy = userId
  111. bean.CreateTime = bean.LastUpdateTime
  112. bean.DelFlag = 0
  113. bean.UserId = userId
  114. //生成二维码
  115. //scene, _ := utils.AesEncrypt([]byte(fmt.Sprintf("%d", userId)), []byte(l.svcCtx.Config.AesSecret))
  116. sceneStr := fmt.Sprintf("%d", userId)
  117. atr, err := l.svcCtx.Wechat.GenQrCode(sceneStr, l.svcCtx.Config.Weixin.SharePage)
  118. if err != nil {
  119. logx.Error(err.Error())
  120. return &types.Response{500, err.Error(), nil}, nil
  121. }
  122. if atr == nil || atr.Url == "" {
  123. return &types.Response{500, "二维码生成失败", nil}, nil
  124. }
  125. _, err = l.svcCtx.DB.Insert(atr)
  126. if err != nil {
  127. logx.Error(err.Error())
  128. return &types.Response{500, err.Error(), nil}, nil
  129. }
  130. bean.Qr = atr.Url
  131. _, err = l.svcCtx.DB.Insert(bean)
  132. bean.Qr = strings.TrimLeft(bean.Qr, "/")
  133. domain := strings.TrimRight(l.svcCtx.Config.AliYunOss.FileUrl, "/")
  134. bean.Qr = domain + "/" + bean.Qr
  135. if err != nil {
  136. return &types.Response{500, err.Error(), nil}, nil
  137. }
  138. return &types.Response{200, "", bean}, nil
  139. }