Files
customer-server/dao/customer_service_account_dao.go
2026-03-14 10:02:49 +08:00

198 lines
5.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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层已经设置了TenantIdcommonmongo.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->falsefalse->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
}