Dockerfile
This commit is contained in:
@@ -1,126 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cid/model/dto"
|
||||
"cid/model/entity"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
var AdPosition = &adPosition{}
|
||||
|
||||
type adPosition struct {
|
||||
}
|
||||
|
||||
// Insert 插入广告位
|
||||
func (d *adPosition) Insert(ctx context.Context, req *dto.AddAdPositionReq) (ids []any, err error) {
|
||||
var result entity.AdPosition
|
||||
if err = gconv.Struct(req, &result); err != nil {
|
||||
return
|
||||
}
|
||||
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, entity.AdPositionCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新广告位
|
||||
func (d *adPosition) Update(ctx context.Context, id *bson.ObjectID, updateData *entity.AdPosition) (err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
|
||||
if !g.IsEmpty(updateData) {
|
||||
bsonm, err := mongo.BuildUpdateData(ctx, updateData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
update := bson.M{"$set": bsonm}
|
||||
_, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus 更新广告位状态
|
||||
func (d *adPosition) UpdateStatus(ctx context.Context, id *bson.ObjectID, status string) (err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
update := bson.M{"$set": bson.M{"status": status}}
|
||||
|
||||
_, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个广告位
|
||||
func (d *adPosition) GetOne(ctx context.Context, id *bson.ObjectID) (adPosition *entity.AdPosition, err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
|
||||
adPosition = &entity.AdPosition{}
|
||||
err = mongo.DB().FindOne(ctx, filter, adPosition, entity.AdPositionCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除广告位
|
||||
func (d *adPosition) Delete(ctx context.Context, id *bson.ObjectID) (err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
_, err = mongo.DB().Delete(ctx, filter, entity.AdPositionCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *adPosition) buildListFilter(req *dto.ListAdPositionReq) bson.M {
|
||||
filter := bson.M{}
|
||||
|
||||
if !g.IsEmpty(req.Name) {
|
||||
filter["name"] = bson.M{"$regex": req.Name, "$options": "i"}
|
||||
}
|
||||
if !g.IsEmpty(req.PositionCode) {
|
||||
filter["positionCode"] = req.PositionCode
|
||||
}
|
||||
if !g.IsEmpty(req.PageName) {
|
||||
filter["page"] = req.PageName
|
||||
}
|
||||
if !g.IsEmpty(req.Section) {
|
||||
filter["section"] = req.Section
|
||||
}
|
||||
if !g.IsEmpty(req.Status) {
|
||||
filter["status"] = req.Status
|
||||
}
|
||||
if !g.IsEmpty(req.AdFormat) {
|
||||
filter["adFormat"] = req.AdFormat
|
||||
}
|
||||
|
||||
// 处理日期范围
|
||||
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
|
||||
}
|
||||
|
||||
// List 获取广告位列表
|
||||
func (d *adPosition) List(ctx context.Context, req *dto.ListAdPositionReq) (list []*entity.AdPosition, total int64, err error) {
|
||||
// 构建查询过滤条件
|
||||
filter := d.buildListFilter(req)
|
||||
|
||||
// 使用common/mongo的Find方法,自动处理分页、租户等
|
||||
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, req.Page, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// GetAvailableAdPositions 获取可用的广告位列表
|
||||
func (d *adPosition) GetAvailableAdPositions(ctx context.Context) (list []*entity.AdPosition, err error) {
|
||||
filter := bson.M{
|
||||
"status": "启用", // 只返回启用的广告位
|
||||
}
|
||||
|
||||
// 使用空的Page参数获取所有数据
|
||||
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
|
||||
_, err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, page, nil)
|
||||
return
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cid/consts"
|
||||
"cid/model/entity"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
var AdSource = &adSourceDao{}
|
||||
|
||||
type adSourceDao struct {
|
||||
}
|
||||
|
||||
// GetByName 根据名称获取广告源
|
||||
func (d *adSourceDao) GetByName(ctx context.Context, name string) (adSource *entity.AdSource, err error) {
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &adSource, consts.AdSourceCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetAvailableSources 获取可用的广告源
|
||||
func (d *adSourceDao) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
|
||||
// 使用空的Page参数获取所有数据
|
||||
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
|
||||
_, err = mongo.DB().Find(ctx, bson.M{"status": "active"}, &list, consts.AdSourceCollection, page, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// GetSourcesByProvider 根据提供商获取广告源
|
||||
func (d *adSourceDao) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
|
||||
// 使用空的Page参数获取所有数据
|
||||
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
|
||||
_, err = mongo.DB().Find(ctx, bson.M{"provider": provider, "status": "active"}, &list, consts.AdSourceCollection, page, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建广告源
|
||||
func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id string, err error) {
|
||||
ids, err := mongo.DB().Insert(ctx, []interface{}{adSource}, consts.AdSourceCollection)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
id = ids[0].(string)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新广告源
|
||||
func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (affected int64, err error) {
|
||||
result, err := mongo.DB().Update(ctx, bson.M{"_id": adSource.Id}, bson.M{"$set": adSource}, consts.AdSourceCollection)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Delete 删除广告源
|
||||
func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, err error) {
|
||||
count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, consts.AdSourceCollection)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取广告源
|
||||
func (d *adSourceDao) GetByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) {
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &adSource, consts.AdSourceCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateFields 更新广告源部分字段
|
||||
func (d *adSourceDao) UpdateFields(ctx context.Context, id string, data *entity.AdSource) (affected int64, err error) {
|
||||
result, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, consts.AdSourceCollection)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -1,235 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cid/model/dto"
|
||||
"cid/model/entity"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
var Advertisement = &advertisement{}
|
||||
|
||||
type advertisement struct {
|
||||
}
|
||||
|
||||
// Insert 插入广告
|
||||
func (d *advertisement) Insert(ctx context.Context, advertisement *entity.Advertisement) (err error) {
|
||||
// 获取stream消息
|
||||
redis := g.Redis()
|
||||
streamMsg, err := redis.Do(ctx, "XREAD", "STREAMS", "advertisement_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{}{advertisement}, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新广告
|
||||
func (d *advertisement) Update(ctx context.Context, req *dto.UpdateAdvertisementReq) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(req.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
// 构建动态更新字段
|
||||
updateFields := bson.M{}
|
||||
|
||||
// 广告基本信息
|
||||
if !g.IsEmpty(req.Title) {
|
||||
updateFields["title"] = req.Title
|
||||
}
|
||||
if !g.IsEmpty(req.Description) {
|
||||
updateFields["description"] = req.Description
|
||||
}
|
||||
if !g.IsEmpty(req.AdvertiserId) {
|
||||
updateFields["advertiserId"] = req.AdvertiserId
|
||||
}
|
||||
if !g.IsEmpty(req.AdPositionId) {
|
||||
updateFields["adPositionId"] = req.AdPositionId
|
||||
}
|
||||
if !g.IsEmpty(req.AdType) {
|
||||
updateFields["adType"] = req.AdType
|
||||
}
|
||||
if !g.IsEmpty(req.AdFormat) {
|
||||
updateFields["adFormat"] = req.AdFormat
|
||||
}
|
||||
if !g.IsEmpty(req.MaterialUrl) {
|
||||
updateFields["materialUrl"] = req.MaterialUrl
|
||||
}
|
||||
if !g.IsEmpty(req.TargetUrl) {
|
||||
updateFields["targetUrl"] = req.TargetUrl
|
||||
}
|
||||
|
||||
// 投放设置
|
||||
if req.StartDate != nil {
|
||||
updateFields["startDate"] = *req.StartDate
|
||||
}
|
||||
if req.EndDate != nil {
|
||||
updateFields["endDate"] = *req.EndDate
|
||||
}
|
||||
if req.Budget != nil {
|
||||
updateFields["budget"] = *req.Budget
|
||||
}
|
||||
if req.DailyBudget != nil {
|
||||
updateFields["dailyBudget"] = *req.DailyBudget
|
||||
}
|
||||
if req.BidAmount != nil {
|
||||
updateFields["bidAmount"] = *req.BidAmount
|
||||
}
|
||||
if !g.IsEmpty(req.BillingType) {
|
||||
updateFields["billingType"] = req.BillingType
|
||||
}
|
||||
|
||||
// 投放条件
|
||||
if req.Targeting != nil {
|
||||
updateFields["targeting"] = req.Targeting
|
||||
}
|
||||
|
||||
// 状态信息
|
||||
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.AdvertisementCollection)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus 更新广告状态
|
||||
func (d *advertisement) 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.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Audit 审核广告
|
||||
func (d *advertisement) 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.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatistics 更新广告统计数据
|
||||
func (d *advertisement) UpdateStatistics(ctx context.Context, id string, stats map[string]interface{}) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
update := bson.M{"$set": stats}
|
||||
|
||||
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个广告
|
||||
func (d *advertisement) GetOne(ctx context.Context, id string) (advertisement *entity.Advertisement, err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
advertisement = &entity.Advertisement{}
|
||||
err = mongo.DB().FindOne(ctx, filter, advertisement, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *advertisement) buildListFilter(req *dto.ListAdvertisementReq) bson.M {
|
||||
filter := bson.M{}
|
||||
|
||||
if !g.IsEmpty(req.AdvertiserId) {
|
||||
filter["advertiserId"] = req.AdvertiserId
|
||||
}
|
||||
if !g.IsEmpty(req.AdPositionId) {
|
||||
filter["adPositionId"] = req.AdPositionId
|
||||
}
|
||||
if !g.IsEmpty(req.AdType) {
|
||||
filter["adType"] = req.AdType
|
||||
}
|
||||
if !g.IsEmpty(req.Status) {
|
||||
filter["status"] = req.Status
|
||||
}
|
||||
if !g.IsEmpty(req.AuditStatus) {
|
||||
filter["auditStatus"] = req.AuditStatus
|
||||
}
|
||||
if !g.IsEmpty(req.Title) {
|
||||
filter["title"] = bson.M{"$regex": req.Title, "$options": "i"}
|
||||
}
|
||||
|
||||
// 处理日期范围
|
||||
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 *advertisement) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
||||
total, err = mongo.DB().Count(ctx, filter, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取广告列表
|
||||
func (d *advertisement) List(ctx context.Context, req *dto.ListAdvertisementReq) (list []*entity.Advertisement, 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.AdvertisementCollection, req.Page, nil)
|
||||
return
|
||||
}
|
||||
@@ -1,280 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cid/model/dto"
|
||||
"cid/model/entity"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/db/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
|
||||
}
|
||||
117
dao/app/application_dao.go
Normal file
117
dao/app/application_dao.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
consts "cid/consts/public"
|
||||
dto "cid/model/dto/app"
|
||||
entity "cid/model/entity/app"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var Application = new(applicationDao)
|
||||
|
||||
type applicationDao struct{}
|
||||
|
||||
// Insert 插入应用
|
||||
func (d *applicationDao) Insert(ctx context.Context, req *dto.CreateApplicationReq) (id int64, err error) {
|
||||
var res *entity.Application
|
||||
if err = gconv.Struct(req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).Data(&res).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// Update 更新应用
|
||||
func (d *applicationDao) Update(ctx context.Context, req *dto.UpdateApplicationReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).Data(&req).OmitEmpty().Where(entity.ApplicationCols.Id, req.Id).Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// Delete 删除应用
|
||||
func (d *applicationDao) Delete(ctx context.Context, req *dto.DeleteApplicationReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).Where(entity.ApplicationCols.Id, req.Id).Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetOne 获取单个应用
|
||||
func (d *applicationDao) GetOne(ctx context.Context, req *dto.GetApplicationReq) (res *entity.Application, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).Where(entity.ApplicationCols.Id, req.Id).One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 获取应用数量
|
||||
func (d *applicationDao) Count(ctx context.Context, req *dto.ListApplicationReq) (count int, err error) {
|
||||
return d.buildListFilter(ctx, req).Count()
|
||||
}
|
||||
|
||||
// List 获取应用列表
|
||||
func (d *applicationDao) List(ctx context.Context, req *dto.ListApplicationReq) (res []entity.Application, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req)
|
||||
model.OrderDesc(entity.ApplicationCols.CreatedAt)
|
||||
if req.Page != nil {
|
||||
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||||
}
|
||||
r, total, err := model.AllAndCount(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *applicationDao) buildListFilter(ctx context.Context, req *dto.ListApplicationReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).Model
|
||||
if !g.IsEmpty(req.Keyword) {
|
||||
model.WhereLike(entity.ApplicationCols.Name, "%"+req.Keyword+"%")
|
||||
model.WhereOrLike(entity.ApplicationCols.AppCode, "%"+req.Keyword+"%")
|
||||
}
|
||||
model.Where(entity.ApplicationCols.Name, req.Name)
|
||||
model.Where(entity.ApplicationCols.AppCode, req.AppCode)
|
||||
model.Where(entity.ApplicationCols.Type, req.Type)
|
||||
model.Where(entity.ApplicationCols.Status, req.Status)
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
|
||||
// UpdateStatus 更新应用状态
|
||||
func (d *applicationDao) UpdateStatus(ctx context.Context, id int64, status string) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).
|
||||
Data(map[string]interface{}{"status": status}).
|
||||
Where(entity.ApplicationCols.Id, id).
|
||||
Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetByAppCode 根据应用编码获取应用
|
||||
func (d *applicationDao) GetByAppCode(ctx context.Context, appCode string) (res *entity.Application, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).
|
||||
Where(entity.ApplicationCols.AppCode, appCode).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cid/model/entity"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
// applicationDao 应用DAO
|
||||
type applicationDao struct {
|
||||
}
|
||||
|
||||
var Application = &applicationDao{}
|
||||
|
||||
// Create 创建应用
|
||||
func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (string, error) {
|
||||
ids, err := mongo.DB().Insert(ctx, []interface{}{app}, "application")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
return ids[0].(string), nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取应用
|
||||
func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Application, error) {
|
||||
var app *entity.Application
|
||||
err := mongo.DB().FindOne(ctx, bson.M{"_id": id}, &app, "application")
|
||||
return app, err
|
||||
}
|
||||
|
||||
// GetByTenantID 根据租户ID获取应用列表
|
||||
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) {
|
||||
var apps []*entity.Application
|
||||
// 使用空的Page参数获取所有数据
|
||||
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
|
||||
_, err := mongo.DB().Find(ctx,
|
||||
bson.M{"tenantId": tenantID}, &apps, "application", page, nil)
|
||||
return apps, err
|
||||
}
|
||||
|
||||
// GetByAPIKey 根据API密钥获取应用
|
||||
func (d *applicationDao) GetByAPIKey(ctx context.Context, apiKey string) (*entity.Application, error) {
|
||||
var app *entity.Application
|
||||
err := mongo.DB().FindOne(ctx,
|
||||
bson.M{"appKey": apiKey}, &app, "application")
|
||||
return app, err
|
||||
}
|
||||
|
||||
// Update 更新应用
|
||||
func (d *applicationDao) Update(ctx context.Context, app *entity.Application) error {
|
||||
_, err := mongo.DB().Update(ctx, bson.M{"_id": app.Id}, bson.M{"$set": app}, "application")
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除应用
|
||||
func (d *applicationDao) Delete(ctx context.Context, id string) error {
|
||||
_, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "application")
|
||||
return err
|
||||
}
|
||||
|
||||
// List 应用列表
|
||||
func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSize int) ([]*entity.Application, int, error) {
|
||||
filter := bson.M{}
|
||||
if tenantID != "" {
|
||||
filter["tenantId"] = tenantID
|
||||
}
|
||||
|
||||
var apps []*entity.Application
|
||||
total, err := mongo.DB().Count(ctx, filter, "application")
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 使用common/mongo的Find方法,自动处理分页、租户等
|
||||
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(pageSize)}
|
||||
total, err = mongo.DB().Find(ctx, filter, &apps, "application", pageBean, nil)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return apps, int(total), nil
|
||||
}
|
||||
|
||||
// GetByName 根据名称获取应用
|
||||
func (d *applicationDao) GetByName(ctx context.Context, name string) (*entity.Application, error) {
|
||||
var app *entity.Application
|
||||
err := mongo.DB().FindOne(ctx, bson.M{"name": name}, &app, "application")
|
||||
return app, err
|
||||
}
|
||||
|
||||
// UpdateFields 更新应用部分字段
|
||||
func (d *applicationDao) UpdateFields(ctx context.Context, id string, data *entity.Application) error {
|
||||
_, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "application")
|
||||
return err
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cid/model/entity"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
var CIDRequest = &cidRequestDao{}
|
||||
|
||||
type cidRequestDao struct {
|
||||
}
|
||||
|
||||
// Create 创建CID请求记录
|
||||
func (d *cidRequestDao) Create(ctx context.Context, request *entity.CidRequest) (id string, err error) {
|
||||
ids, err := mongo.DB().Insert(ctx, []interface{}{request}, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
id = ids[0].(string)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetHistory 获取CID请求历史
|
||||
func (d *cidRequestDao) GetHistory(ctx context.Context, userId string, page, size int) (list []*entity.CidRequest, total int64, err error) {
|
||||
filter := bson.M{"userId": userId}
|
||||
|
||||
// 分页查询,使用common/mongo的Find方法,自动处理分页、租户等
|
||||
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(size)}
|
||||
total, err = mongo.DB().Find(ctx, filter, &list, entity.CidRequestCollection, pageBean, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// GetStatistics 获取统计信息
|
||||
func (d *cidRequestDao) GetStatistics(ctx context.Context, userId string) (stats map[string]interface{}, err error) {
|
||||
stats = make(map[string]interface{})
|
||||
|
||||
// 总请求数
|
||||
totalRequests, err := mongo.DB().Count(ctx, bson.M{"userId": userId}, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats["total_requests"] = totalRequests
|
||||
|
||||
// 成功请求数
|
||||
successfulRequests, err := mongo.DB().Count(ctx, bson.M{"userId": userId, "status": "completed"}, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats["successful_requests"] = successfulRequests
|
||||
|
||||
// 平均处理时间需要单独计算,MongoDB聚合查询
|
||||
// 这里简化处理,返回0
|
||||
stats["average_process_time"] = 0
|
||||
|
||||
return
|
||||
}
|
||||
118
dao/data/api_interface_dao.go
Normal file
118
dao/data/api_interface_dao.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
consts "cid/consts/public"
|
||||
dto "cid/model/dto/data"
|
||||
entity "cid/model/entity/data"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var ApiInterface = new(apiInterfaceDao)
|
||||
|
||||
type apiInterfaceDao struct{}
|
||||
|
||||
// Insert 插入接口
|
||||
func (d *apiInterfaceDao) Insert(ctx context.Context, req *dto.CreateApiInterfaceReq) (id int64, err error) {
|
||||
var res *entity.ApiInterface
|
||||
if err = gconv.Struct(req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Data(&res).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// Update 更新接口
|
||||
func (d *apiInterfaceDao) Update(ctx context.Context, req *dto.UpdateApiInterfaceReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Data(&req).OmitEmpty().Where(entity.ApiInterfaceCols.Id, req.Id).Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// Delete 删除接口
|
||||
func (d *apiInterfaceDao) Delete(ctx context.Context, req *dto.DeleteApiInterfaceReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Where(entity.ApiInterfaceCols.Id, req.Id).Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetOne 获取单个接口
|
||||
func (d *apiInterfaceDao) GetOne(ctx context.Context, req *dto.GetApiInterfaceReq) (res *entity.ApiInterface, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Where(entity.ApiInterfaceCols.Id, req.Id).One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 获取接口数量
|
||||
func (d *apiInterfaceDao) Count(ctx context.Context, req *dto.ListApiInterfaceReq) (count int, err error) {
|
||||
return d.buildListFilter(ctx, req).Count()
|
||||
}
|
||||
|
||||
// List 获取接口列表
|
||||
func (d *apiInterfaceDao) List(ctx context.Context, req *dto.ListApiInterfaceReq) (res []entity.ApiInterface, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req)
|
||||
model.OrderDesc(entity.ApiInterfaceCols.CreatedAt)
|
||||
if req.Page != nil {
|
||||
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||||
}
|
||||
r, total, err := model.AllAndCount(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *apiInterfaceDao) buildListFilter(ctx context.Context, req *dto.ListApiInterfaceReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Model
|
||||
if !g.IsEmpty(req.Keyword) {
|
||||
model.WhereLike(entity.ApiInterfaceCols.Name, "%"+req.Keyword+"%")
|
||||
model.WhereOrLike(entity.ApiInterfaceCols.Code, "%"+req.Keyword+"%")
|
||||
}
|
||||
model.Where(entity.ApiInterfaceCols.PlatformId, req.PlatformId)
|
||||
model.Where(entity.ApiInterfaceCols.Name, req.Name)
|
||||
model.Where(entity.ApiInterfaceCols.Code, req.Code)
|
||||
model.Where(entity.ApiInterfaceCols.Method, req.Method)
|
||||
model.Where(entity.ApiInterfaceCols.Status, req.Status)
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
|
||||
// UpdateStatus 更新接口状态
|
||||
func (d *apiInterfaceDao) UpdateStatus(ctx context.Context, id int64, status string) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).
|
||||
Data(map[string]interface{}{"status": status}).
|
||||
Where(entity.ApiInterfaceCols.Id, id).
|
||||
Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetByIds 根据ID列表获取接口列表
|
||||
func (d *apiInterfaceDao) GetByIds(ctx context.Context, ids []int64) (res []entity.ApiInterface, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).
|
||||
WhereIn(entity.ApiInterfaceCols.Id, ids).
|
||||
All()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
135
dao/data/data_fetch_log_dao.go
Normal file
135
dao/data/data_fetch_log_dao.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
consts "cid/consts/public"
|
||||
dto "cid/model/dto/data"
|
||||
entity "cid/model/entity/data"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
var DataFetchLog = new(dataFetchLogDao)
|
||||
|
||||
type dataFetchLogDao struct{}
|
||||
|
||||
// Insert 插入数据获取日志
|
||||
func (d *dataFetchLogDao) Insert(ctx context.Context, log *entity.DataFetchLog) (id int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).Data(&log).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// Update 更新数据获取日志
|
||||
func (d *dataFetchLogDao) Update(ctx context.Context, id int64, data map[string]interface{}) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).
|
||||
Data(data).
|
||||
OmitEmpty().
|
||||
Where(entity.DataFetchLogCols.Id, id).
|
||||
Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetOne 获取单个数据获取日志
|
||||
func (d *dataFetchLogDao) GetOne(ctx context.Context, req *dto.GetDataFetchLogReq) (res *entity.DataFetchLog, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).Where(entity.DataFetchLogCols.Id, req.Id).One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 获取数据获取日志数量
|
||||
func (d *dataFetchLogDao) Count(ctx context.Context, req *dto.ListDataFetchLogReq) (count int, err error) {
|
||||
return d.buildListFilter(ctx, req).Count()
|
||||
}
|
||||
|
||||
// List 获取数据获取日志列表
|
||||
func (d *dataFetchLogDao) List(ctx context.Context, req *dto.ListDataFetchLogReq) (res []entity.DataFetchLog, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req)
|
||||
model.OrderDesc(entity.DataFetchLogCols.CreatedAt)
|
||||
if req.Page != nil {
|
||||
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||||
}
|
||||
r, total, err := model.AllAndCount(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *dataFetchLogDao) buildListFilter(ctx context.Context, req *dto.ListDataFetchLogReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).Model
|
||||
model.Where(entity.DataFetchLogCols.PlatformId, req.PlatformId)
|
||||
model.Where(entity.DataFetchLogCols.InterfaceId, req.InterfaceId)
|
||||
model.Where(entity.DataFetchLogCols.RequestId, req.RequestId)
|
||||
model.Where(entity.DataFetchLogCols.Status, req.Status)
|
||||
if req.StartTime > 0 {
|
||||
model.WhereGTE(entity.DataFetchLogCols.StartTime, req.StartTime)
|
||||
}
|
||||
if req.EndTime > 0 {
|
||||
model.WhereLTE(entity.DataFetchLogCols.StartTime, req.EndTime)
|
||||
}
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
|
||||
// GetByRequestId 根据请求ID获取日志
|
||||
func (d *dataFetchLogDao) GetByRequestId(ctx context.Context, requestId string) (res *entity.DataFetchLog, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).
|
||||
Where(entity.DataFetchLogCols.RequestId, requestId).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus 更新日志状态
|
||||
func (d *dataFetchLogDao) UpdateStatus(ctx context.Context, id int64, status string, endTime int64, duration int, responseData, errorMessage string) (rows int64, err error) {
|
||||
updateData := map[string]interface{}{
|
||||
"status": status,
|
||||
}
|
||||
if endTime > 0 {
|
||||
updateData["end_time"] = endTime
|
||||
}
|
||||
if duration > 0 {
|
||||
updateData["duration"] = duration
|
||||
}
|
||||
if responseData != "" {
|
||||
updateData["response_data"] = responseData
|
||||
}
|
||||
if errorMessage != "" {
|
||||
updateData["error_message"] = errorMessage
|
||||
}
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).
|
||||
Data(updateData).
|
||||
Where(entity.DataFetchLogCols.Id, id).
|
||||
Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// IncrementRetryCount 增加重试次数
|
||||
func (d *dataFetchLogDao) IncrementRetryCount(ctx context.Context, id int64) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).
|
||||
Where(entity.DataFetchLogCols.Id, id).
|
||||
Data(gdb.Raw(entity.DataFetchLogCols.RetryCount + " + 1")).
|
||||
Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
115
dao/data/platform_dao.go
Normal file
115
dao/data/platform_dao.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
consts "cid/consts/public"
|
||||
dto "cid/model/dto/data"
|
||||
entity "cid/model/entity/data"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var Platform = new(platformDao)
|
||||
|
||||
type platformDao struct{}
|
||||
|
||||
// Insert 插入平台
|
||||
func (d *platformDao) Insert(ctx context.Context, req *dto.CreatePlatformReq) (id int64, err error) {
|
||||
var res *entity.Platform
|
||||
if err = gconv.Struct(req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).Data(&res).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// Update 更新平台
|
||||
func (d *platformDao) Update(ctx context.Context, req *dto.UpdatePlatformReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).Data(&req).OmitEmpty().Where(entity.PlatformCols.Id, req.Id).Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// Delete 删除平台
|
||||
func (d *platformDao) Delete(ctx context.Context, req *dto.DeletePlatformReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).Where(entity.PlatformCols.Id, req.Id).Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetOne 获取单个平台
|
||||
func (d *platformDao) GetOne(ctx context.Context, req *dto.GetPlatformReq) (res *entity.Platform, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).Where(entity.PlatformCols.Id, req.Id).One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 获取平台数量
|
||||
func (d *platformDao) Count(ctx context.Context, req *dto.ListPlatformReq) (count int, err error) {
|
||||
return d.buildListFilter(ctx, req).Count()
|
||||
}
|
||||
|
||||
// List 获取平台列表
|
||||
func (d *platformDao) List(ctx context.Context, req *dto.ListPlatformReq) (res []entity.Platform, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req)
|
||||
model.OrderDesc(entity.PlatformCols.CreatedAt)
|
||||
if req.Page != nil {
|
||||
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||||
}
|
||||
r, total, err := model.AllAndCount(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *platformDao) buildListFilter(ctx context.Context, req *dto.ListPlatformReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).Model
|
||||
if !g.IsEmpty(req.Keyword) {
|
||||
model.WhereLike(entity.PlatformCols.Name, "%"+req.Keyword+"%")
|
||||
}
|
||||
model.Where(entity.PlatformCols.Name, req.Name)
|
||||
model.Where(entity.PlatformCols.Type, req.Type)
|
||||
model.Where(entity.PlatformCols.Status, req.Status)
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
|
||||
// UpdateStatus 更新平台状态
|
||||
func (d *platformDao) UpdateStatus(ctx context.Context, id int64, status string) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).
|
||||
Data(map[string]interface{}{"status": status}).
|
||||
Where(entity.PlatformCols.Id, id).
|
||||
Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetByType 根据类型获取平台
|
||||
func (d *platformDao) GetByType(ctx context.Context, platformType string) (res *entity.Platform, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).
|
||||
Where(entity.PlatformCols.Type, platformType).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
138
dao/mapping/data_mapping_dao.go
Normal file
138
dao/mapping/data_mapping_dao.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package mapping
|
||||
|
||||
import (
|
||||
consts "cid/consts/public"
|
||||
dto "cid/model/dto/mapping"
|
||||
entity "cid/model/entity/mapping"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var DataMapping = new(dataMappingDao)
|
||||
|
||||
type dataMappingDao struct{}
|
||||
|
||||
// Insert 插入数据映射
|
||||
func (d *dataMappingDao) Insert(ctx context.Context, req *dto.CreateDataMappingReq) (id int64, err error) {
|
||||
var res *entity.DataMapping
|
||||
if err = gconv.Struct(req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Data(&res).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// BatchInsert 批量插入数据映射
|
||||
func (d *dataMappingDao) BatchInsert(ctx context.Context, mappings []entity.DataMapping) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Data(&mappings).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// Update 更新数据映射
|
||||
func (d *dataMappingDao) Update(ctx context.Context, req *dto.UpdateDataMappingReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Data(&req).OmitEmpty().Where(entity.DataMappingCols.Id, req.Id).Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// Delete 删除数据映射
|
||||
func (d *dataMappingDao) Delete(ctx context.Context, req *dto.DeleteDataMappingReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Where(entity.DataMappingCols.Id, req.Id).Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetOne 获取单个数据映射
|
||||
func (d *dataMappingDao) GetOne(ctx context.Context, req *dto.GetDataMappingReq) (res *entity.DataMapping, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Where(entity.DataMappingCols.Id, req.Id).One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 获取数据映射数量
|
||||
func (d *dataMappingDao) Count(ctx context.Context, req *dto.ListDataMappingReq) (count int, err error) {
|
||||
return d.buildListFilter(ctx, req).Count()
|
||||
}
|
||||
|
||||
// List 获取数据映射列表
|
||||
func (d *dataMappingDao) List(ctx context.Context, req *dto.ListDataMappingReq) (res []entity.DataMapping, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req)
|
||||
model.OrderAsc(entity.DataMappingCols.Priority)
|
||||
model.OrderDesc(entity.DataMappingCols.CreatedAt)
|
||||
if req.Page != nil {
|
||||
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||||
}
|
||||
r, total, err := model.AllAndCount(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表的过滤条件
|
||||
func (d *dataMappingDao) buildListFilter(ctx context.Context, req *dto.ListDataMappingReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Model
|
||||
model.Where(entity.DataMappingCols.PlatformId, req.PlatformId)
|
||||
model.Where(entity.DataMappingCols.InterfaceId, req.InterfaceId)
|
||||
model.Where(entity.DataMappingCols.SourceField, req.SourceField)
|
||||
model.Where(entity.DataMappingCols.TargetField, req.TargetField)
|
||||
model.Where(entity.DataMappingCols.Status, req.Status)
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
|
||||
// GetByInterfaceId 根据接口ID获取映射规则列表(按优先级排序)
|
||||
func (d *dataMappingDao) GetByInterfaceId(ctx context.Context, interfaceId int64) (res []entity.DataMapping, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).
|
||||
Where(entity.DataMappingCols.InterfaceId, interfaceId).
|
||||
Where("status", "active").
|
||||
OrderAsc(entity.DataMappingCols.Priority).
|
||||
All()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByInterfaceIdAndTargetField 根据接口ID和目标字段获取映射规则
|
||||
func (d *dataMappingDao) GetByInterfaceIdAndTargetField(ctx context.Context, interfaceId int64, targetField string) (res *entity.DataMapping, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).
|
||||
Where(entity.DataMappingCols.InterfaceId, interfaceId).
|
||||
Where(entity.DataMappingCols.TargetField, targetField).
|
||||
Where("status", "active").
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteByInterfaceId 根据接口ID删除所有映射规则
|
||||
func (d *dataMappingDao) DeleteByInterfaceId(ctx context.Context, interfaceId int64) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).
|
||||
Where(entity.DataMappingCols.InterfaceId, interfaceId).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cid/model/entity"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
var Strategy = &strategyDao{}
|
||||
|
||||
type strategyDao struct {
|
||||
}
|
||||
|
||||
// GetByName 根据名称获取策略
|
||||
func (d *strategyDao) GetByName(ctx context.Context, name string) (strategy *entity.Strategy, err error) {
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &strategy, "strategies")
|
||||
return
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取策略
|
||||
func (d *strategyDao) GetByID(ctx context.Context, id string) (strategy *entity.Strategy, err error) {
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &strategy, "strategies")
|
||||
return
|
||||
}
|
||||
|
||||
// GetByTenantLevel 根据租户级别获取策略
|
||||
func (d *strategyDao) GetByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) {
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies")
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建策略
|
||||
func (d *strategyDao) Create(ctx context.Context, strategy *entity.Strategy) (id string, err error) {
|
||||
ids, err := mongo.DB().Insert(ctx, []interface{}{strategy}, "strategies")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
id = ids[0].(string)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新策略
|
||||
func (d *strategyDao) Update(ctx context.Context, strategy *entity.Strategy) (affected int64, err error) {
|
||||
result, err := mongo.DB().Update(ctx, bson.M{"_id": strategy.Id}, bson.M{"$set": strategy}, "strategies")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Delete 删除策略
|
||||
func (d *strategyDao) Delete(ctx context.Context, id string) (affected int64, err error) {
|
||||
count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "strategies")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// GetList 获取策略列表
|
||||
func (d *strategyDao) GetList(ctx context.Context, page, size int, tenantLevel, status string) (list []*entity.Strategy, total int64, err error) {
|
||||
filter := bson.M{}
|
||||
|
||||
// 筛选条件
|
||||
if tenantLevel != "" {
|
||||
filter["tenantLevel"] = tenantLevel
|
||||
}
|
||||
if status != "" {
|
||||
filter["status"] = status
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
total, err = mongo.DB().Count(ctx, filter, "strategies")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 分页查询,使用common/mongo的Find方法,自动处理分页、租户等
|
||||
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(size)}
|
||||
total, err = mongo.DB().Find(ctx, filter, &list, "strategies", pageBean, nil)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user