change_block_list_logic.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package contact
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/zeromicro/go-zero/core/errorx"
  6. "wechat-api/ent/wx"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "wechat-api/internal/utils/dberrorhandler"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type ChangeBlockListLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewChangeBlockListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangeBlockListLogic {
  18. return &ChangeBlockListLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *ChangeBlockListLogic) ChangeBlockList(req *types.ChangeBlockListReq) (resp *types.BaseMsgResp, err error) {
  24. wxInfo, err := l.svcCtx.DB.Wx.Query().Where(wx.Wxid(req.OwnerWxid)).Only(l.ctx)
  25. if wxInfo == nil {
  26. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  27. }
  28. // 根据请求类型选择对应的黑名单列表
  29. blockList := wxInfo.BlockList
  30. if *req.Type != 1 {
  31. blockList = wxInfo.GroupBlockList
  32. }
  33. if contains(blockList, "ALL") {
  34. return nil, errorx.NewDefaultError("请先在账号管理中,取消所属账号的全部黑名单设置")
  35. }
  36. isInBlockList := contains(blockList, req.Wxid)
  37. shouldUpdateBlockList := (*req.Ai && isInBlockList) || (!*req.Ai && !isInBlockList)
  38. if shouldUpdateBlockList {
  39. // 更新黑名单: 如果是Ai,移除;如果不是Ai,添加
  40. if *req.Ai {
  41. blockList = removeElement(blockList, req.Wxid)
  42. } else {
  43. blockList = append(blockList, req.Wxid)
  44. }
  45. updateQuery := l.svcCtx.DB.Wx.Update().Where(wx.Wxid(req.OwnerWxid))
  46. if *req.Type == 1 {
  47. updateQuery.SetBlockList(blockList)
  48. } else {
  49. updateQuery.SetNotNilGroupBlockList(blockList)
  50. }
  51. err = updateQuery.Exec(l.ctx)
  52. if err != nil {
  53. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  54. }
  55. l.svcCtx.Rds.HDel(l.ctx, "wx_info", req.OwnerWxid)
  56. }
  57. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  58. }
  59. func contains(slice []string, element string) bool {
  60. for _, elem := range slice {
  61. if elem == element {
  62. return true
  63. }
  64. }
  65. return false
  66. }
  67. func removeElement(slice []string, element string) []string {
  68. for i, elem := range slice {
  69. if elem == element {
  70. return append(slice[:i], slice[i+1:]...)
  71. }
  72. }
  73. return slice
  74. }