2025-12-06 09:10:24 +08:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-06 15:24:30 +08:00
|
|
|
|
"cidservice/model/dto"
|
|
|
|
|
|
"cidservice/model/entity"
|
2025-12-06 09:10:24 +08:00
|
|
|
|
"context"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"gitee.com/red-future---jilin-g/common/http"
|
|
|
|
|
|
"gitee.com/red-future---jilin-g/common/mongo"
|
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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.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.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.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.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.FindOne(ctx, filter, advertiser, entity.AdvertiserCollection)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新余额
|
|
|
|
|
|
newBalance := advertiser.AccountBalance + amount
|
|
|
|
|
|
update := bson.M{"$set": bson.M{"accountBalance": newBalance}}
|
|
|
|
|
|
|
|
|
|
|
|
_, err = mongo.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.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.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.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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 分页参数处理
|
|
|
|
|
|
pageNum := req.PageNum
|
|
|
|
|
|
if pageNum <= 0 {
|
|
|
|
|
|
pageNum = 1
|
|
|
|
|
|
}
|
|
|
|
|
|
pageSize := req.PageSize
|
|
|
|
|
|
if pageSize <= 0 {
|
|
|
|
|
|
pageSize = http.PageSize
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
limit := int64(pageSize)
|
|
|
|
|
|
skip := int64((pageNum - 1) * pageSize)
|
|
|
|
|
|
|
|
|
|
|
|
// 排序处理
|
|
|
|
|
|
sort := bson.M{"createdAt": -1}
|
|
|
|
|
|
|
|
|
|
|
|
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
|
|
|
|
|
|
|
|
|
|
|
|
err = mongo.Find(ctx, filter, &list, entity.AdvertiserCollection, opts)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|