Files
cid/dao/advertiser_dao.go
2026-02-24 16:24:47 +08:00

281 lines
7.1 KiB
Go
Raw 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 (
"cid/model/dto"
"cid/model/entity"
"context"
"time"
"gitea.com/red-future/common/mongo"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"go.mongodb.org/mongo-driver/v2/bson"
)
var Advertiser = &advertiser{}
type advertiser struct {
}
// Insert 插入广告主
func (d *advertiser) Insert(ctx context.Context, advertiser *entity.Advertiser) (err error) {
// 获取stream消息
redis := g.Redis()
streamMsg, err := redis.Do(ctx, "XREAD", "STREAMS", "advertiser_stream", "$")
if err != nil {
g.Log().Errorf(ctx, "获取stream消息失败: %v", err)
} else {
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
}
_, err = mongo.DB().Insert(ctx, []interface{}{advertiser}, entity.AdvertiserCollection)
return
}
// Update 更新广告主
func (d *advertiser) Update(ctx context.Context, req *dto.UpdateAdvertiserReq) (err error) {
objectId, err := bson.ObjectIDFromHex(req.Id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
// 构建动态更新字段
updateFields := bson.M{}
// 基本信息
if !g.IsEmpty(req.Name) {
updateFields["name"] = req.Name
}
if !g.IsEmpty(req.ContactName) {
updateFields["contactName"] = req.ContactName
}
if !g.IsEmpty(req.ContactPhone) {
updateFields["contactPhone"] = req.ContactPhone
}
if !g.IsEmpty(req.ContactEmail) {
updateFields["contactEmail"] = req.ContactEmail
}
if !g.IsEmpty(req.Company) {
updateFields["company"] = req.Company
}
if !g.IsEmpty(req.Industry) {
updateFields["industry"] = req.Industry
}
if !g.IsEmpty(req.Scale) {
updateFields["scale"] = req.Scale
}
// 证件信息
if !g.IsEmpty(req.BusinessLicenseUrl) {
updateFields["businessLicenseUrl"] = req.BusinessLicenseUrl
}
if !g.IsEmpty(req.ICPLicenseUrl) {
updateFields["icpLicenseUrl"] = req.ICPLicenseUrl
}
if req.OtherLicenseUrls != nil {
updateFields["otherLicenseUrls"] = req.OtherLicenseUrls
}
// 财务信息
if !g.IsEmpty(req.BankName) {
updateFields["bankName"] = req.BankName
}
if !g.IsEmpty(req.BankAccount) {
updateFields["bankAccount"] = req.BankAccount
}
if !g.IsEmpty(req.AccountName) {
updateFields["accountName"] = req.AccountName
}
// 合同信息
if !g.IsEmpty(req.ContractId) {
updateFields["contractId"] = req.ContractId
}
if !g.IsEmpty(req.ContractType) {
updateFields["contractType"] = req.ContractType
}
if !g.IsEmpty(req.ContractUrl) {
updateFields["contractUrl"] = req.ContractUrl
}
if req.SignDate != nil {
updateFields["signDate"] = *req.SignDate
}
if req.ExpireDate != nil {
updateFields["expireDate"] = *req.ExpireDate
}
// 系统信息
if req.AccountBalance != nil {
updateFields["accountBalance"] = *req.AccountBalance
}
if req.CreditLimit != nil {
updateFields["creditLimit"] = *req.CreditLimit
}
if !g.IsEmpty(req.Remark) {
updateFields["remark"] = req.Remark
}
// 状态信息
if req.Status != nil {
updateFields["status"] = *req.Status
}
if req.AuditStatus != nil {
updateFields["auditStatus"] = *req.AuditStatus
}
if req.AuditReason != nil {
updateFields["auditReason"] = *req.AuditReason
}
if len(updateFields) > 0 {
update := bson.M{"$set": updateFields}
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
}
return
}
// UpdateStatus 更新广告主状态
func (d *advertiser) UpdateStatus(ctx context.Context, id, status string) (err error) {
objectId, err := bson.ObjectIDFromHex(id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
update := bson.M{"$set": bson.M{"status": status}}
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
return
}
// Audit 审核广告主
func (d *advertiser) Audit(ctx context.Context, id, auditStatus, auditReason string) (err error) {
objectId, err := bson.ObjectIDFromHex(id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
// 获取当前用户ID实际项目中应从上下文获取
auditBy := "system"
auditTime := time.Now().Unix()
update := bson.M{
"$set": bson.M{
"auditStatus": auditStatus,
"auditReason": auditReason,
"auditTime": auditTime,
"auditBy": auditBy,
},
}
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
return
}
// Recharge 充值
func (d *advertiser) Recharge(ctx context.Context, id string, amount int64, remark string) (err error) {
objectId, err := bson.ObjectIDFromHex(id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
// 先获取当前余额
advertiser := &entity.Advertiser{}
err = mongo.DB().FindOne(ctx, filter, advertiser, entity.AdvertiserCollection)
if err != nil {
return
}
// 更新余额
newBalance := advertiser.AccountBalance + amount
update := bson.M{"$set": bson.M{"accountBalance": newBalance}}
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
return
}
// UpdateCreditLimit 更新授信额度
func (d *advertiser) UpdateCreditLimit(ctx context.Context, id string, creditLimit int64) (err error) {
objectId, err := bson.ObjectIDFromHex(id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
update := bson.M{"$set": bson.M{"creditLimit": creditLimit}}
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
return
}
// GetOne 获取单个广告主
func (d *advertiser) GetOne(ctx context.Context, id string) (advertiser *entity.Advertiser, err error) {
objectId, err := bson.ObjectIDFromHex(id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
advertiser = &entity.Advertiser{}
err = mongo.DB().FindOne(ctx, filter, advertiser, entity.AdvertiserCollection)
return
}
// buildListFilter 构建列表查询的过滤条件
func (d *advertiser) buildListFilter(req *dto.ListAdvertiserReq) bson.M {
filter := bson.M{}
if !g.IsEmpty(req.Name) {
filter["name"] = bson.M{"$regex": req.Name, "$options": "i"}
}
if !g.IsEmpty(req.ContactName) {
filter["contactName"] = bson.M{"$regex": req.ContactName, "$options": "i"}
}
if !g.IsEmpty(req.Company) {
filter["company"] = bson.M{"$regex": req.Company, "$options": "i"}
}
if !g.IsEmpty(req.Industry) {
filter["industry"] = req.Industry
}
if !g.IsEmpty(req.Status) {
filter["status"] = req.Status
}
if !g.IsEmpty(req.AuditStatus) {
filter["auditStatus"] = req.AuditStatus
}
// 处理日期范围
if len(req.DateRange) == 2 {
startTime := gconv.Int64(req.DateRange[0])
endTime := gconv.Int64(req.DateRange[1])
filter["createdAt"] = bson.M{
"$gte": startTime,
"$lte": endTime,
}
}
return filter
}
// checkTotalCount 检查总数
func (d *advertiser) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
total, err = mongo.DB().Count(ctx, filter, entity.AdvertiserCollection)
return
}
// List 获取广告主列表
func (d *advertiser) List(ctx context.Context, req *dto.ListAdvertiserReq) (list []*entity.Advertiser, total int64, err error) {
// 构建查询过滤条件
filter := d.buildListFilter(req)
// 检查总数
total, err = d.checkTotalCount(ctx, filter)
if err != nil {
return
}
// 使用common/mongo的Find方法自动处理分页、租户等
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdvertiserCollection, req.Page, nil)
return
}