refactor: 重构资产模型与DAO层实现

This commit is contained in:
2026-03-19 17:45:06 +08:00
parent 5236c45a39
commit f30141679c
24 changed files with 570 additions and 600 deletions

View File

@@ -6,11 +6,10 @@ import (
"assets/model/entity/asset"
"context"
"gitea.com/red-future/common/beans"
"gitea.com/red-future/common/db/mongo"
"gitea.com/red-future/common/utils"
"gitea.com/red-future/common/db/gfdb"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"go.mongodb.org/mongo-driver/v2/bson"
"github.com/gogf/gf/v2/util/gconv"
)
var AssetSku = new(assetSku)
@@ -19,112 +18,94 @@ type assetSku struct {
}
// Insert 插入SKU
func (d *assetSku) Insert(ctx context.Context, req *dto.CreateAssetSkuReq) (ids []any, err error) {
var result *entity.AssetSku
if err = utils.Struct(req, &result); err != nil {
func (d *assetSku) Insert(ctx context.Context, req *dto.CreateAssetSkuReq) (id int64, err error) {
var res *entity.AssetSku
if err = gconv.Struct(req, &res); err != nil {
return
}
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, public.AssetSkuCollection)
return
}
// Update 更新SKU
func (d *assetSku) Update(ctx context.Context, req *dto.UpdateAssetSkuReq) (err error) {
buildUpdateData, err := mongo.BuildUpdateData(ctx, req)
r, err := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Ctx(ctx).Data(&res).Insert()
if err != nil {
return
}
filter := bson.M{"_id": req.Id}
update := bson.M{"$set": buildUpdateData}
if !g.IsEmpty(req.Stock) {
// 从$set中移除stock字段避免$set和$inc冲突
delete(buildUpdateData, "stock")
update = bson.M{
"$inc": bson.M{
"stock": req.Stock,
},
}
if len(buildUpdateData) > 0 {
update["$set"] = buildUpdateData
}
}
_, err = mongo.DB().Update(ctx, filter, update, public.AssetSkuCollection)
return
return r.LastInsertId()
}
// DeleteFake 删除SKU-根据id进行假删
func (d *assetSku) DeleteFake(ctx context.Context, req *dto.DeleteAssetSkuReq) (err error) {
filter := bson.M{"_id": req.Id}
_, err = mongo.DB().DeleteSoft(ctx, filter, public.AssetSkuCollection)
return
// Update 更新SKU
func (d *assetSku) Update(ctx context.Context, req *dto.UpdateAssetSkuReq) (rows int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Ctx(ctx).OmitEmpty().Where(entity.AssetCol.Id, req.Id).Update()
if err != nil {
return
}
return r.RowsAffected()
}
// Delete 删除SKU-根据id进行假删
func (d *assetSku) Delete(ctx context.Context, req *dto.DeleteAssetSkuReq) (rows int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Where(entity.AssetSkuCol.Id, req.Id).Delete()
if err != nil {
return
}
return r.RowsAffected()
}
// GetOne 获取单个SKU
func (d *assetSku) GetOne(ctx context.Context, req *dto.GetAssetSkuReq, noTenantId bool) (res *entity.AssetSku, err error) {
filter := bson.M{"_id": req.Id}
if noTenantId {
err = mongo.DB().NoTenantId().FindOne(ctx, filter, &res, public.AssetSkuCollection)
} else {
err = mongo.DB().FindOne(ctx, filter, &res, public.AssetSkuCollection)
func (d *assetSku) GetOne(ctx context.Context, req *dto.GetAssetSkuReq, fields ...string) (res *entity.AssetSku, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Ctx(ctx).Where(entity.AssetCol.Id, req.Id).Fields(fields).One()
if err != nil {
return
}
err = r.Struct(&res)
return
}
// GetListByAssetIdExcludeCurrentSku 根据资产ID获取SKU列表并且排除当前SKU
func (d *assetSku) GetListByAssetIdExcludeCurrentSku(ctx context.Context, assetId *bson.ObjectID, req *dto.ListAssetSkuReq) (res []entity.AssetSku, total int64, err error) {
filter := bson.M{"assetId": assetId, "_id": bson.M{"$ne": req.Id}}
total, err = mongo.DB().Find(ctx, filter, &res, public.AssetSkuCollection, req.Page, req.OrderBy)
func (d *assetSku) GetListByAssetIdExcludeCurrentSku(ctx context.Context, assetId int64, req *dto.ListAssetSkuReq, fields ...string) (res []entity.AssetSku, total int, err error) {
model := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection)
model.Fields(fields)
model.Where(entity.AssetSkuCol.AssetId, assetId)
model.WhereNot(entity.AssetSkuCol.Id, req.Id)
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
}
// List 获取SKU列表
func (d *assetSku) List(ctx context.Context, req *dto.ListAssetSkuReq, noTenantId bool) (res []entity.AssetSku, total int64, err error) {
// 构建查询过滤条件
filter, err := d.buildListFilter(ctx, req)
func (d *assetSku) List(ctx context.Context, req *dto.ListAssetSkuReq, fields ...string) (res []entity.AssetSku, total int, err error) {
model := d.buildListFilter(ctx, req)
model.Fields(fields)
model.OrderAsc(entity.AssetSkuCol.Sort)
model.OrderDesc(entity.AssetSkuCol.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
}
// 排序处理
req.OrderBy = []beans.OrderBy{
{Field: "sort", Order: beans.Asc},
{Field: "createdAt", Order: beans.Desc},
}
if noTenantId {
total, err = mongo.DB().NoTenantId().Find(ctx, filter, &res, public.AssetSkuCollection, req.Page, req.OrderBy)
} else {
total, err = mongo.DB().Find(ctx, filter, &res, public.AssetSkuCollection, req.Page, req.OrderBy)
}
err = r.Structs(&res)
return
}
// buildListFilter 构建列表查询的过滤条件
func (d *assetSku) buildListFilter(ctx context.Context, req *dto.ListAssetSkuReq) (filter bson.M, err error) {
_ = ctx
filter = bson.M{}
if !g.IsEmpty(req.AssetId) {
filter["assetId"] = req.AssetId
}
if !g.IsEmpty(req.Status) {
filter["status"] = req.Status
func (d *assetSku) buildListFilter(ctx context.Context, req *dto.ListAssetSkuReq) *gdb.Model {
model := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Model
if !g.IsEmpty(req.Keyword) {
model.WhereLike(entity.AssetCol.Name, "%"+req.Keyword+"%")
model.WhereOrLike(entity.AssetSkuCol.SkuName, "%"+req.Keyword+"%")
}
if !g.IsEmpty(req.CategoryPath) {
filter["categoryPath"] = bson.M{"$regex": "^" + req.CategoryPath, "$options": "i"}
model.WhereLike(entity.AssetSkuCol.CategoryPath, req.CategoryPath+"%")
}
if !g.IsEmpty(req.Keyword) {
orConditions := bson.A{
bson.M{"skuName": bson.M{"$regex": req.Keyword, "$options": "i"}},
bson.M{"assetName": bson.M{"$regex": req.Keyword, "$options": "i"}},
}
filter["$or"] = orConditions
}
if req.MinPrice > 0 {
filter["price"] = bson.M{"$gte": req.MinPrice}
}
if req.MaxPrice > 0 {
if filter["price"] == nil {
filter["price"] = bson.M{}
}
filter["price"].(bson.M)["$lte"] = req.MaxPrice
}
return
model.Where(entity.AssetSkuCol.Id, req.Id)
model.Where(entity.AssetSkuCol.Status, req.Status)
model.WhereGT(entity.AssetSkuCol.Price, req.MinPrice)
model.WhereLT(entity.AssetSkuCol.Price, req.MaxPrice)
model.OmitEmptyWhere()
return model
}