acquirer_mkt_qr_update_logic.go 4.6 KB

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