acquirer_mkt_qr_update_logic.go 4.6 KB

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