msgdistributorlogic.go 3.6 KB

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