msgdistributorlogic.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package logic
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. IC "jyBXSubscribe/rpc/init"
  7. "jyBXSubscribe/rpc/model"
  8. "strings"
  9. "time"
  10. "jyBXSubscribe/rpc/internal/svc"
  11. "jyBXSubscribe/rpc/type/bxsubscribe"
  12. "app.yhyue.com/moapp/jybase/common"
  13. "app.yhyue.com/moapp/jybase/date"
  14. "app.yhyue.com/moapp/jybase/redis"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type MsgDistributorLogic struct {
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. logx.Logger
  21. }
  22. func NewMsgDistributorLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MsgDistributorLogic {
  23. return &MsgDistributorLogic{
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. Logger: logx.WithContext(ctx),
  27. }
  28. }
  29. // 信息分发
  30. func (l *MsgDistributorLogic) MsgDistributor(in *bxsubscribe.MsgDistributorReq) (*bxsubscribe.StatusResp, error) {
  31. userEnt := model.EntInfo(common.IntAll(in.EntId), common.IntAll(in.EntUserId))
  32. if !(userEnt.Role_admin_system || userEnt.Role_admin_department) {
  33. return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "无权限"}, nil
  34. }
  35. //根据信息查询区域列表
  36. var regin, pushIds, staffs []string
  37. staffs = strings.Split(in.Staffs, ",")
  38. var pushCas []*model.PushCa
  39. for _, id := range strings.Split(in.MessageId, ",") {
  40. if id != "" {
  41. pushIds = append(pushIds, id)
  42. }
  43. }
  44. if len(pushIds) == 0 || in.Staffs == "" {
  45. return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "缺少参数"}, nil
  46. }
  47. //获取分发信息的地区
  48. infoRes := IC.BaseServiceMysql.SelectBySql(fmt.Sprintf("SELECT infoid FROM push.pushentniche WHERE id in('%s')", strings.Join(pushIds, "','")))
  49. if infoRes == nil || len(*infoRes) == 0 {
  50. return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "获取分发地区异常"}, nil
  51. }
  52. for _, m := range *infoRes {
  53. if infoId, _ := m["infoid"].(string); infoId != "" {
  54. pushCas = append(pushCas, &model.PushCa{InfoId: infoId})
  55. }
  56. }
  57. infoList := model.NewSubscribePush().GetInfoByIds(IC.MgoBidding, IC.DB.Mongo.Bidding.Collection, IC.DB.Mongo.Bidding.CollectionBack, pushCas, false)
  58. for _, info := range infoList {
  59. if info.Area != "" && info.Area != "全国" {
  60. regin = append(regin, info.Area)
  61. }
  62. }
  63. //区域人员校验
  64. userArr := model.Distributor(regin, common.IntAll(in.EntId), common.IntAll(in.EntUserId))
  65. for _, uid := range staffs {
  66. check := false
  67. for _, user := range userArr {
  68. if user.Id == common.IntAll(uid) {
  69. check = true
  70. break
  71. }
  72. }
  73. if !check {
  74. return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "选择人员异常"}, nil
  75. }
  76. }
  77. //查询分发信息内容
  78. msgRes := IC.BaseServiceMysql.SelectBySql(fmt.Sprintf("SELECT * FROM push.pushentniche WHERE id in ('%s') and entid =? ", strings.Join(pushIds, "','")), in.EntId)
  79. if msgRes == nil || len(*msgRes) == 0 {
  80. return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "获取分发信息内容异常"}, nil
  81. }
  82. //分发数据库修改
  83. ok := IC.BaseServiceMysql.ExecTx("分发数据库修改", func(tx *sql.Tx) bool {
  84. insertRow := []string{"entid", "deptid", "infoid", "matchkeys", "type", "product", "matchways", "matchitems", "disid", "area", "city", "buyerclass", "toptype", "subtype", "subscopeclass", "budget", "bidamount", "attachment_count", "source", "date", "userid", "isvisit", "visittime", "pid"}
  85. msgItems := []string{"entid", "deptid", "infoid", "matchkeys", "type", "product", "matchways", "matchitems", "disid", "area", "city", "buyerclass", "toptype", "subtype", "subscopeclass", "budget", "bidamount", "attachment_count"}
  86. row := []string{"userid", "entid", "infoid", "createtime"}
  87. values := []interface{}{}
  88. dateNew := time.Now().Unix()
  89. for _, m := range *msgRes {
  90. var msgValues, insertValue []interface{}
  91. for _, key := range msgItems {
  92. msgValues = append(msgValues, m[key])
  93. }
  94. for _, uid := range staffs {
  95. insertValue = append(insertValue, msgValues...)
  96. insertValue = append(insertValue, []interface{}{3, dateNew, uid, nil, nil, m["id"]}...)
  97. values = append(values, uid, m["entid"], m["infoid"], date.NowFormat(date.Date_Full_Layout))
  98. }
  99. affected, _ := IC.BaseServiceMysql.InsertBatchByTx(tx, "push.pushentniche", insertRow, insertValue)
  100. if affected == 0 {
  101. return false
  102. }
  103. }
  104. IC.BaseServiceMysql.InsertBatchByTx(tx, "base_service.ent_bid_distribute", row, values)
  105. return true
  106. })
  107. //清除推送列表缓存
  108. for _, staff := range staffs {
  109. //today
  110. redis.Del("pushcache_2_b", fmt.Sprintf("entnichepush_%s", staff))
  111. //all
  112. redis.Del("pushcache_2_a", fmt.Sprintf("all_entnichepush_%s", staff))
  113. redis.Del("pushcache_2_a", fmt.Sprintf("all_entnichepush_vip_%s", staff))
  114. redis.Del("pushcache_2_a", fmt.Sprintf("all_entnichepush_member_%s", staff))
  115. }
  116. if !ok {
  117. return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "数据分发异常"}, nil
  118. }
  119. return &bxsubscribe.StatusResp{Status: 1}, nil
  120. }