198 lines
5.9 KiB
Go
198 lines
5.9 KiB
Go
|
|
package dao
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"customer-server/model/dto"
|
|||
|
|
"customer-server/model/entity"
|
|||
|
|
"fmt"
|
|||
|
|
|
|||
|
|
"gitea.com/red-future/common/beans"
|
|||
|
|
"gitea.com/red-future/common/db/mongo"
|
|||
|
|
"gitea.com/red-future/common/redis"
|
|||
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|||
|
|
"github.com/gogf/gf/v2/frame/g"
|
|||
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|||
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
var CustomerServiceAccount = new(customerServiceAccount)
|
|||
|
|
|
|||
|
|
type customerServiceAccount struct{}
|
|||
|
|
|
|||
|
|
// FindByAccountName 根据accountName查询
|
|||
|
|
// 使用 MongoDAO(不需要token验证)
|
|||
|
|
func (d *customerServiceAccount) FindByAccountName(ctx context.Context, accountName string) (account *entity.CustomerServiceAccount, err error) {
|
|||
|
|
filter := bson.M{"accountName": accountName, "isDeleted": false}
|
|||
|
|
|
|||
|
|
var result entity.CustomerServiceAccount
|
|||
|
|
err = MongoDAO.FindOne(ctx, filter, &result, entity.CustomerServiceAccountCollection)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 如果未找到记录,result 是零值
|
|||
|
|
if result.Id.IsZero() {
|
|||
|
|
return nil, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
account = &result
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Insert 插入客服账号
|
|||
|
|
func (d *customerServiceAccount) Insert(ctx context.Context, data *entity.CustomerServiceAccount) (err error) {
|
|||
|
|
// 统一使用commonmongo.DB().Insert,自动清除缓存
|
|||
|
|
// service层已经设置了TenantId,commonmongo.DB().Insert不会覆盖已有值
|
|||
|
|
ids, err := mongo.DB().Insert(ctx, []interface{}{data}, entity.CustomerServiceAccountCollection)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if len(ids) > 0 {
|
|||
|
|
if oid, ok := ids[0].(bson.ObjectID); ok {
|
|||
|
|
data.Id = &oid // 取地址赋值给指针类型
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Update 更新客服账号
|
|||
|
|
func (d *customerServiceAccount) Update(ctx context.Context, req *dto.UpdateCustomerServiceAccountReq) (err error) {
|
|||
|
|
objectId, err := bson.ObjectIDFromHex(req.Id)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
filter := bson.M{"_id": objectId}
|
|||
|
|
|
|||
|
|
// 如果accountName变更,需要清理旧的缓存
|
|||
|
|
if !g.IsEmpty(req.AccountName) {
|
|||
|
|
// 先查出旧的accountName
|
|||
|
|
var oldAccount entity.CustomerServiceAccount
|
|||
|
|
if findErr := MongoDAO.FindOne(ctx, filter, &oldAccount, entity.CustomerServiceAccountCollection); findErr == nil {
|
|||
|
|
// 清理旧accountName的缓存
|
|||
|
|
oldCacheKey := fmt.Sprintf("tenant:account:%s", oldAccount.AccountName)
|
|||
|
|
redis.RedisClient().Del(ctx, oldCacheKey)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
updateFields := bson.M{}
|
|||
|
|
if !g.IsEmpty(req.AccountName) {
|
|||
|
|
updateFields["accountName"] = req.AccountName
|
|||
|
|
}
|
|||
|
|
if !g.IsEmpty(req.Platform) {
|
|||
|
|
updateFields["platform"] = req.Platform
|
|||
|
|
}
|
|||
|
|
if req.SelfIdentity != nil {
|
|||
|
|
updateFields["selfIdentity"] = *req.SelfIdentity
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 如果有字段需要更新,则执行 MongoDB 更新操作
|
|||
|
|
if len(updateFields) > 0 {
|
|||
|
|
_, err = mongo.DB().Update(ctx, filter, bson.M{"$set": updateFields}, entity.CustomerServiceAccountCollection)
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Delete 软删除客服账号
|
|||
|
|
func (d *customerServiceAccount) Delete(ctx context.Context, id string) (err error) {
|
|||
|
|
objectId, err := bson.ObjectIDFromHex(id)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
filter := bson.M{"_id": objectId, "isDeleted": false}
|
|||
|
|
|
|||
|
|
// 删除前先查出accountName,清理缓存
|
|||
|
|
var account entity.CustomerServiceAccount
|
|||
|
|
if findErr := MongoDAO.FindOne(ctx, filter, &account, entity.CustomerServiceAccountCollection); findErr == nil {
|
|||
|
|
cacheKey := fmt.Sprintf("tenant:account:%s", account.AccountName)
|
|||
|
|
redis.RedisClient().Del(ctx, cacheKey)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
update := bson.M{"$set": bson.M{"isDeleted": true, "updatedAt": gtime.Now().Time}}
|
|||
|
|
_, err = mongo.DB().Update(ctx, filter, update, entity.CustomerServiceAccountCollection)
|
|||
|
|
if err != nil {
|
|||
|
|
return gerror.Wrap(err, "删除客服账号失败")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ToggleStatus 切换客服账号状态(1 启用,0 禁用)
|
|||
|
|
func (d *customerServiceAccount) ToggleStatus(ctx context.Context, req *dto.ToggleCustomerServiceAccountStatusReq) (err error) {
|
|||
|
|
objectId, err := bson.ObjectIDFromHex(req.Id)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
filter := bson.M{"_id": objectId}
|
|||
|
|
|
|||
|
|
// 先查出当前状态
|
|||
|
|
var account entity.CustomerServiceAccount
|
|||
|
|
if err = MongoDAO.FindOne(ctx, filter, &account, entity.CustomerServiceAccountCollection); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 计算新的状态:true->false,false->true(切换禁用状态)
|
|||
|
|
newIsDisabled := true
|
|||
|
|
if !account.Id.IsZero() && account.IsDisabled {
|
|||
|
|
newIsDisabled = false
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_, err = mongo.DB().Update(ctx, filter, bson.M{"$set": bson.M{"isDisabled": newIsDisabled}}, entity.CustomerServiceAccountCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// buildListFilter 构建列表查询的过滤条件
|
|||
|
|
func (d *customerServiceAccount) buildListFilter(req *dto.ListCustomerServiceAccountReq) bson.M {
|
|||
|
|
filter := bson.M{}
|
|||
|
|
if !g.IsEmpty(req.AccountName) {
|
|||
|
|
filter["accountName"] = bson.M{"$regex": req.AccountName, "$options": "i"} // $regex模糊查询,忽略大小写
|
|||
|
|
}
|
|||
|
|
if req.IsDisabled != nil {
|
|||
|
|
filter["isDisabled"] = *req.IsDisabled
|
|||
|
|
}
|
|||
|
|
if !g.IsEmpty(req.Platform) {
|
|||
|
|
filter["platform"] = req.Platform
|
|||
|
|
}
|
|||
|
|
return filter
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// checkTotalCount 检查总数
|
|||
|
|
func (d *customerServiceAccount) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
|||
|
|
total, err = mongo.DB().Count(ctx, filter, entity.CustomerServiceAccountCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// List 获取客服账号列表(包含所有账号,含已禁用账号)
|
|||
|
|
func (d *customerServiceAccount) List(ctx context.Context, req *dto.ListCustomerServiceAccountReq) (list []*entity.CustomerServiceAccount, total int64, err error) {
|
|||
|
|
// 构建查询过滤条件
|
|||
|
|
filter := d.buildListFilter(req)
|
|||
|
|
|
|||
|
|
// 检查总数
|
|||
|
|
total, err = d.checkTotalCount(ctx, filter)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 分页参数处理
|
|||
|
|
pageNum := req.PageNum
|
|||
|
|
if pageNum <= 0 {
|
|||
|
|
pageNum = 1
|
|||
|
|
}
|
|||
|
|
pageSize := req.PageSize
|
|||
|
|
if pageSize <= 0 {
|
|||
|
|
pageSize = 20
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 使用统一的mongo.DB().Find方法(支持分页和排序)
|
|||
|
|
page := &beans.Page{
|
|||
|
|
PageNum: int64(pageNum),
|
|||
|
|
PageSize: int64(pageSize),
|
|||
|
|
}
|
|||
|
|
orderBy := []beans.OrderBy{
|
|||
|
|
{Field: "createdAt", Order: beans.Desc}, // 按创建时间倒序
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_, err = mongo.DB().Find(ctx, filter, &list, entity.CustomerServiceAccountCollection, page, orderBy)
|
|||
|
|
return
|
|||
|
|
}
|