1
This commit is contained in:
167
dao/data_dao.go
Normal file
167
dao/data_dao.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"customer-server/model/dto"
|
||||
"customer-server/model/entity"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"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 Data = new(data)
|
||||
|
||||
type data struct{}
|
||||
|
||||
// Insert 插入数据
|
||||
func (d *data) Insert(ctx context.Context, data *entity.Data) (err error) {
|
||||
_, err = mongo.DB().Insert(ctx, []interface{}{data}, entity.DataCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新数据
|
||||
func (d *data) Update(ctx context.Context, req *dto.UpdateDataReq) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(req.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
// 构建动态更新字段
|
||||
updateFields := bson.M{}
|
||||
if !g.IsEmpty(req.CustomerId) {
|
||||
updateFields["customerId"] = req.CustomerId
|
||||
}
|
||||
if !g.IsEmpty(req.AccountName) {
|
||||
updateFields["accountName"] = req.AccountName
|
||||
}
|
||||
if req.IsInbound != nil {
|
||||
updateFields["isInbound"] = *req.IsInbound
|
||||
}
|
||||
if req.IsActive != nil {
|
||||
updateFields["isActive"] = *req.IsActive
|
||||
}
|
||||
if req.IsServed != nil {
|
||||
updateFields["isServed"] = *req.IsServed
|
||||
}
|
||||
if req.HasSentContactCard != nil {
|
||||
updateFields["hasSentContactCard"] = *req.HasSentContactCard
|
||||
}
|
||||
if req.HasSentNameCard != nil {
|
||||
updateFields["hasSentNameCard"] = *req.HasSentNameCard
|
||||
}
|
||||
if req.HasLeftContactInfo != nil {
|
||||
updateFields["hasLeftContactInfo"] = *req.HasLeftContactInfo
|
||||
}
|
||||
|
||||
if len(updateFields) > 0 {
|
||||
_, err = mongo.DB().Update(ctx, filter, bson.M{"$set": updateFields}, entity.DataCollection)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// // Delete 删除数据
|
||||
// func (d *data) Delete(ctx context.Context, req *dto.DeleteDataReq) (err error) {
|
||||
// objectId, err := bson.ObjectIDFromHex(req.Id)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// filter := bson.M{"_id": objectId}
|
||||
// _, err = mongo.DB().Delete(ctx, filter, d.collection)
|
||||
// return
|
||||
// }
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *data) buildListFilter(req *dto.ListDataReq) bson.M {
|
||||
filter := bson.M{}
|
||||
if !g.IsEmpty(req.CustomerId) {
|
||||
filter["customerId"] = req.CustomerId
|
||||
}
|
||||
if !g.IsEmpty(req.AccountName) {
|
||||
filter["accountName"] = *req.AccountName
|
||||
}
|
||||
|
||||
// 处理时间范围筛选
|
||||
if !g.IsEmpty(req.StartDate) || !g.IsEmpty(req.EndDate) {
|
||||
timeFilter := bson.M{}
|
||||
|
||||
// 开始日期:大于等于当天 00:00:00(时间戳秒)
|
||||
if !g.IsEmpty(req.StartDate) {
|
||||
// 将日期字符串转换为时间戳(秒)
|
||||
startTime, err := parseDate(req.StartDate)
|
||||
if err == nil {
|
||||
timeFilter["$gte"] = startTime
|
||||
}
|
||||
}
|
||||
|
||||
// 结束日期:小于等于当天 23:59:59(时间戳秒)
|
||||
if !g.IsEmpty(req.EndDate) {
|
||||
// 将日期字符串转换为时间戳(秒)+ 86399(一天的最后一秒)
|
||||
endTime, err := parseDate(req.EndDate)
|
||||
if err == nil {
|
||||
timeFilter["$lte"] = endTime + 86399 // 加上一天的秒数 - 1
|
||||
}
|
||||
}
|
||||
|
||||
if len(timeFilter) > 0 {
|
||||
filter["sessionStartTime"] = timeFilter
|
||||
}
|
||||
}
|
||||
|
||||
return filter
|
||||
}
|
||||
|
||||
// checkTotalCount 检查总数
|
||||
func (d *data) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
||||
total, err = mongo.DB().Count(ctx, filter, entity.DataCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取数据列表
|
||||
func (d *data) List(ctx context.Context, req *dto.ListDataReq) (list []*entity.Data, 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: "sessionStartTime", Order: beans.Desc}, // 按会话开始时间倒序
|
||||
}
|
||||
|
||||
_, err = mongo.DB().Find(ctx, filter, &list, entity.DataCollection, page, orderBy)
|
||||
return
|
||||
}
|
||||
|
||||
// parseDate 将日期字符串(YYYY-MM-DD)转换为时间戳(秒)
|
||||
func parseDate(dateStr string) (int64, error) {
|
||||
// 使用 gtime 解析日期字符串
|
||||
t := gtime.NewFromStr(dateStr)
|
||||
if t == nil {
|
||||
return 0, gerror.New("日期格式错误")
|
||||
}
|
||||
// 返回时间戳(秒)
|
||||
return t.Timestamp(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user