2025-12-06 09:10:24 +08:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-09 16:10:45 +08:00
|
|
|
|
"cid/model/dto"
|
|
|
|
|
|
"cid/model/entity"
|
2025-12-06 09:10:24 +08:00
|
|
|
|
"context"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-02-24 17:17:10 +08:00
|
|
|
|
"gitea.com/red-future/common/db/mongo"
|
2025-12-06 09:10:24 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-30 10:09:18 +08:00
|
|
|
|
var Advertiser = &advertiser{}
|
2025-12-29 15:27:00 +08:00
|
|
|
|
|
|
|
|
|
|
type advertiser struct {
|
|
|
|
|
|
}
|
2025-12-06 09:10:24 +08:00
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 10:09:18 +08:00
|
|
|
|
_, err = mongo.DB().Insert(ctx, []interface{}{advertiser}, entity.AdvertiserCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
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}
|
2025-12-30 10:09:18 +08:00
|
|
|
|
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
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}}
|
|
|
|
|
|
|
2025-12-30 10:09:18 +08:00
|
|
|
|
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
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,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 10:09:18 +08:00
|
|
|
|
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
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{}
|
2025-12-30 10:09:18 +08:00
|
|
|
|
err = mongo.DB().FindOne(ctx, filter, advertiser, entity.AdvertiserCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新余额
|
|
|
|
|
|
newBalance := advertiser.AccountBalance + amount
|
|
|
|
|
|
update := bson.M{"$set": bson.M{"accountBalance": newBalance}}
|
|
|
|
|
|
|
2025-12-30 10:09:18 +08:00
|
|
|
|
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
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}}
|
|
|
|
|
|
|
2025-12-30 10:09:18 +08:00
|
|
|
|
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
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{}
|
2025-12-30 10:09:18 +08:00
|
|
|
|
err = mongo.DB().FindOne(ctx, filter, advertiser, entity.AdvertiserCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
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) {
|
2025-12-30 10:09:18 +08:00
|
|
|
|
total, err = mongo.DB().Count(ctx, filter, entity.AdvertiserCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 11:07:58 +08:00
|
|
|
|
// 使用common/mongo的Find方法,自动处理分页、租户等
|
|
|
|
|
|
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdvertiserCollection, req.Page, nil)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|