msgdistributorlogic.go 3.3 KB

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