msgdistributorlogic.go 4.1 KB

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