msgdistributorlogic.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. "app.yhyue.com/moapp/jybase/redis"
  6. "context"
  7. "database/sql"
  8. "fmt"
  9. IC "jyBXSubscribe/rpc/init"
  10. "jyBXSubscribe/rpc/model"
  11. "strings"
  12. "time"
  13. "jyBXSubscribe/rpc/internal/svc"
  14. "jyBXSubscribe/rpc/type/bxsubscribe"
  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, infoIds, staffs []string
  37. staffs = strings.Split(in.Staffs, ",")
  38. var pushCas []*model.PushCa
  39. for _, eid := range strings.Split(in.MessageId, ",") {
  40. if eid != "" {
  41. array := encrypt.DecodeArticleId2ByCheck(eid)
  42. if len(array) == 1 && array[0] != "" {
  43. infoIds = append(infoIds, array[0])
  44. pushCas = append(pushCas, &model.PushCa{InfoId: array[0]})
  45. }
  46. }
  47. }
  48. if len(infoIds) == 0 || in.Staffs == "" {
  49. return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "缺少参数"}, nil
  50. }
  51. infoList := model.NewSubscribePush().GetInfoByIds(IC.MgoBidding, IC.DB.Mongo.Bidding.Collection, IC.DB.Mongo.Bidding.CollectionBack, pushCas)
  52. for _, info := range infoList {
  53. if info.Area != "" && info.Area != "全国" {
  54. regin = append(regin, info.Area)
  55. }
  56. }
  57. //区域人员校验
  58. userArr := model.Distributor(regin, common.IntAll(in.EntId), common.IntAll(in.EntUserId))
  59. for _, uid := range staffs {
  60. check := false
  61. for _, user := range userArr {
  62. if user.Id == common.IntAll(uid) {
  63. check = true
  64. break
  65. }
  66. }
  67. if !check {
  68. return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "选择人员异常"}, nil
  69. }
  70. }
  71. //查询分发信息内容
  72. msgRes := IC.BaseServiceMysql.SelectBySql(fmt.Sprintf("SELECT p.* FROM pushentniche p,(SELECT infoid,MIN(id) as id FROM pushentniche WHERE infoid in ('%s') and entid =? and source = 2 GROUP BY infoid) b WHERE p.id=b.id", strings.Join(infoIds, "','")), in.EntId)
  73. if msgRes == nil || len(*msgRes) == 0 {
  74. return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "获取分发信息内容异常"}, nil
  75. }
  76. //分发数据库修改
  77. ok := IC.BaseServiceMysql.ExecTx("分发数据库修改", func(tx *sql.Tx) bool {
  78. insertRow := []string{"entid", "deptid", "infoid", "matchkeys", "type", "product", "matchways", "matchitems", "disid", "source", "date", "userid", "isvisit", "visittime"}
  79. msgItems := []string{"entid", "deptid", "infoid", "matchkeys", "type", "product", "matchways", "matchitems", "disid"}
  80. dateNew := time.Now().Unix()
  81. for _, m := range *msgRes {
  82. var msgValues, insertValue []interface{}
  83. for _, key := range msgItems {
  84. msgValues = append(msgValues, m[key])
  85. }
  86. for _, uid := range staffs {
  87. insertValue = append(insertValue, msgValues...)
  88. insertValue = append(insertValue, []interface{}{3, dateNew, uid, nil, nil}...)
  89. }
  90. affected, _ := IC.BaseServiceMysql.InsertBatchByTx(tx, "pushentniche", insertRow, insertValue)
  91. if affected == 0 {
  92. return false
  93. }
  94. }
  95. return true
  96. })
  97. //清除推送列表缓存
  98. for _, staff := range staffs {
  99. //today
  100. redis.Del("pushcache_2_b", fmt.Sprintf("entnichepush_%s", staff))
  101. //all
  102. redis.Del("pushcache_2_a", fmt.Sprintf("all_entnichepush_vip_%s", staff))
  103. redis.Del("pushcache_2_a", fmt.Sprintf("all_entnichepush_member_%s", staff))
  104. }
  105. if !ok {
  106. return &bxsubscribe.StatusResp{ErrorCode: -1, ErrorMsg: "数据分发异常"}, nil
  107. }
  108. return &bxsubscribe.StatusResp{Status: 1}, nil
  109. }