123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package add_friend
- import (
- "context"
- "github.com/suyuan32/simple-admin-common/msg/errormsg"
- "github.com/zeromicro/go-zero/core/errorx"
- "github.com/zeromicro/go-zero/core/logx"
- "wechat-api/ent/addwechatfriendlog"
- "wechat-api/ent/predicate"
- "wechat-api/ent/wx"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "wechat-api/internal/utils"
- )
- type AddFriendListLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewAddFriendListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddFriendListLogic {
- return &AddFriendListLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *AddFriendListLogic) AddFriendList(req *types.AddFriendListReq) (resp *types.AddFriendListResp, err error) {
- //获取上下文的组织id
- organizationId := l.ctx.Value("organizationId").(uint64)
- //通过organizationId去wx表拿微信机器人的数
- query := l.svcCtx.DB.Wx.Query().Where(wx.OrganizationIDEQ(organizationId), wx.DeletedAtIsNil())
- // 动态添加条件示例:假设 req.OwnerWxId 存在时添加额外的 Where 条件
- if utils.IsNonEmptyString(req.OwnerWxId) {
- query = query.Where(wx.WxidEQ(*req.OwnerWxId))
- }
- wxIds, err := query.Select(wx.FieldWxid).Strings(l.ctx)
- if err != nil {
- //数据库错误,不直接返回错误,防止暴露数据库错误
- return nil, errorx.NewInvalidArgumentError("请联系管理员")
- }
- //机器人列表为空,直接返回
- if len(wxIds) == 0 {
- return nil, errorx.NewInvalidArgumentError("当前组织架构下,没有wx机器人")
- }
- var predicates []predicate.AddWechatFriendLog
- predicates = append(predicates, addwechatfriendlog.OwnerWxIDIn(wxIds...))
- ownerWxType := req.OwnerWxType
- //查询企业微信机器人加的好友
- if ownerWxType != nil && *ownerWxType > 0 {
- predicates = append(predicates, addwechatfriendlog.OwnerWxTypeEQ(*ownerWxType))
- }
- //查找添加好友
- if utils.IsNonEmptyString(req.FindContent) {
- predicates = append(predicates, addwechatfriendlog.FindContent(*req.FindContent))
- }
- var IsCanAdd []int
- if req.Status != nil {
- switch *req.Status {
- case 1:
- IsCanAdd = append(IsCanAdd, 0, 1)
- case 2:
- IsCanAdd = append(IsCanAdd, 3, 4, 6)
- case 3:
- IsCanAdd = append(IsCanAdd, 2)
- default:
- IsCanAdd = append(IsCanAdd, 0, 1, 2, 3, 4, 5, 6)
- }
- if len(IsCanAdd) < 7 {
- predicates = append(predicates, addwechatfriendlog.IsCanAddIn(IsCanAdd...))
- }
- }
- data, err := l.svcCtx.DB.AddWechatFriendLog.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
- logx.Info("数据", data)
- if err != nil {
- return nil, errorx.NewInvalidArgumentError("请联系管理员")
- }
- resp = &types.AddFriendListResp{}
- resp.Msg = errormsg.Success
- resp.Data.Total = data.PageDetails.Total
- for _, v := range data.List {
- var wxString string
- if v.OwnerWxType == 1 {
- wxString = "个微"
- } else {
- wxString = "企微"
- }
- resp.Data.Data = append(resp.Data.Data,
- types.FriendList{
- Id: v.ID,
- OwnerWxId: &v.OwnerWxID,
- OwnerWxType: &wxString,
- FindContent: &v.FindContent,
- Status: func() *string {
- statusMap := map[int]string{
- 0: "数据准备中",
- 1: "待执行",
- 2: "成功邀请",
- 3: "timeout及其他错误",
- 4: "用户不存在",
- 5: "后台取消",
- 6: "已是好友",
- }
- statusStr := statusMap[v.IsCanAdd]
- return &statusStr
- }(),
- IsShow: func() *int {
- statusMap := map[int]int{
- 0: 1,
- 1: 1,
- 2: 0,
- 3: 0,
- 4: 0,
- 5: 1,
- 6: 0,
- }
- isShow := statusMap[v.IsCanAdd]
- return &isShow
- }(),
- TaskCount: &v.TaskCount,
- Message: &v.Message,
- CreateTime: utils.UnixTimeToBeijing(v.CreatedAt),
- UpdateTime: utils.UnixTimeToBeijing(v.UpdatedAt),
- },
- )
- }
- return resp, nil
- }
|