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

@@ -19,92 +19,79 @@ type assetDao struct {
// Insert 插入资产
func (d *assetDao) Insert(ctx context.Context, req *dto.CreateAssetReq) (id int64, err error) {
var result entity.Asset
if err = gconv.Struct(req, &result); err != nil {
var res *entity.Asset
if err = gconv.Struct(req, &res); err != nil {
return
}
return gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Data(&result).InsertAndGetId()
}
// GetOne 获取单个资产
func (d *assetDao) GetOne(ctx context.Context, req *dto.GetAssetReq) (res *entity.Asset, err error) {
err = gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where("id", req.Id).Scan(&res)
return
r, err := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Data(&res).Insert()
if err != nil {
return
}
return r.LastInsertId()
}
// Update 更新资产
func (d *assetDao) Update(ctx context.Context, req *dto.UpdateAssetReq) (err error) {
data := g.Map{
"name": req.Name,
"description": req.Description,
"type": req.Type,
"category_id": req.CategoryId,
"image_url": req.ImageURL,
"images": req.Images,
"status": req.Status,
"online_time": req.OnlineTime,
"offline_time": req.OfflineTime,
"physical_asset_config": req.PhysicalAssetConfig,
"service_asset_config": req.ServiceAssetConfig,
"virtual_asset_config": req.VirtualAssetConfig,
"metadata": req.Metadata,
func (d *assetDao) Update(ctx context.Context, req *dto.UpdateAssetReq) (rows int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).OmitEmpty().Where(entity.AssetCol.Id, req.Id).Update()
if err != nil {
return
}
_, err = gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where("id", req.Id).Update(data)
return
return r.RowsAffected()
}
// DeleteFake 删除资产-根据id进行假删
func (d *assetDao) DeleteFake(ctx context.Context, req *dto.DeleteAssetReq) (err error) {
_, err = gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where("id", req.Id).Update(g.Map{
"is_deleted": true,
})
return
// Delete 删除资产-根据id进行假删
func (d *assetDao) Delete(ctx context.Context, req *dto.DeleteAssetReq) (rows int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Where(entity.AssetCol.Id, req.Id).Delete()
if err != nil {
return
}
return r.RowsAffected()
}
// GetOneById 通过ID获取单个资产内部使用uint64
func (d *assetDao) GetOneById(ctx context.Context, id uint64) (res *entity.Asset, err error) {
err = gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where("id", id).Scan(&res)
// GetOne 获取单个资产
func (d *assetDao) GetOne(ctx context.Context, req *dto.GetAssetReq, fields ...string) (res *entity.Asset, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where(entity.AssetCol.Id, req.Id).Fields(fields).One()
if err != nil {
return
}
err = r.Struct(&res)
return
}
// Count 获取资产数量
func (d *assetDao) Count(ctx context.Context, req *dto.ListAssetReq) (count int64, err error) {
m := d.buildListFilter(ctx, req)
c, err := m.Count()
return int64(c), err
func (d *assetDao) Count(ctx context.Context, req *dto.ListAssetReq) (count int, err error) {
return d.buildListFilter(ctx, req).Count()
}
// List 获取资产列表
func (d *assetDao) List(ctx context.Context, req *dto.ListAssetReq) (res []entity.Asset, total int, err error) {
m := d.buildListFilter(ctx, req)
func (d *assetDao) List(ctx context.Context, req *dto.ListAssetReq, fields ...string) (res []entity.Asset, total int, err error) {
model := d.buildListFilter(ctx, req)
model.Fields(fields)
model.OrderDesc(entity.AssetCol.CreatedAt)
if req.Page != nil {
m = m.Page(int(req.Page.PageNum), int(req.Page.PageSize))
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
}
err = m.ScanAndCount(&res, &total, false)
r, total, err := model.AllAndCount(false)
if err != nil {
return
}
err = r.Structs(&res)
return
}
// buildListFilter 构建列表查询的过滤条件
func (d *assetDao) buildListFilter(ctx context.Context, req *dto.ListAssetReq) *gdb.Model {
m := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Cache(ctx).Where("is_deleted", false)
if !g.IsEmpty(req.Name) {
m = m.Where("name", req.Name)
}
if !g.IsEmpty(req.Type) {
m = m.Where("type", req.Type)
}
if !g.IsEmpty(req.CategoryId) {
m = m.Where("category_id", req.CategoryId)
}
if !g.IsEmpty(req.Status) {
m = m.Where("status", req.Status)
model := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Model
if !g.IsEmpty(req.Keyword) {
model.WhereLike(entity.AssetCol.Name, "%"+req.Keyword+"%")
}
if !g.IsEmpty(req.CategoryPath) {
m = m.WhereLike("category_path", req.CategoryPath+"%")
model.WhereLike(entity.AssetCol.CategoryPath, req.CategoryPath+"%")
}
if !g.IsEmpty(req.Keyword) {
m = m.WhereLike("name", "%"+req.Keyword+"%")
}
return m
model.Where(entity.AssetCol.Name, req.Name)
model.Where(entity.AssetCol.Type, req.Type)
model.Where(entity.AssetCol.CategoryId, req.CategoryId)
model.Where(entity.AssetCol.Status, req.Status)
model.OmitEmptyWhere()
return model
}

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
}

View File

@@ -8,10 +8,8 @@ import (
"gitea.com/red-future/common/db/gfdb"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/guid"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
var Category = new(category)
@@ -20,63 +18,65 @@ type category struct {
}
// Insert 插入分类
func (d *category) Insert(ctx context.Context, req *dto.CreateCategoryReq) (res *entity.Category, err error) {
func (d *category) Insert(ctx context.Context, req *dto.CreateCategoryReq) (id int64, err error) {
var res *entity.Category
if err = gconv.Struct(req, &res); err != nil {
return
}
res.Bid = guid.S()
_, err = gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Insert(&res)
return res, nil
r, err := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Insert(&res)
if err != nil {
return
}
return r.LastInsertId()
}
// Update 更新分类
func (d *category) Update(ctx context.Context, req *dto.UpdateCategoryReq) (err error) {
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Data(gconv.Map(&req)).OmitEmpty()
if !g.IsEmpty(req.Bid) {
model.Where("bid", req.Bid)
func (d *category) Update(ctx context.Context, req *dto.UpdateCategoryReq) (rows int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Data(&req).OmitEmpty().Where(entity.CategoryCol.Id, req.Id).Update()
if err != nil {
return
}
if !g.IsEmpty(req.Id) {
model.Where("id", req.Id)
return r.RowsAffected()
}
// Delete 删除分类-根据id及父id进行假删
func (d *category) Delete(ctx context.Context, req *dto.DeleteCategoryReq) (rows int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Where(entity.CategoryCol.Id, req.Id).Delete()
if err != nil {
return
}
_, err = model.Update()
return
return r.RowsAffected()
}
// GetOne 获取单个分类
func (d *category) GetOne(ctx context.Context, req *dto.GetCategoryReq, fields ...string) (category *entity.Category, err error) {
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection)
if !g.IsEmpty(req.Bid) {
model.Where(entity.CategoryCol.Bid, req.Bid)
func (d *category) GetOne(ctx context.Context, req *dto.GetCategoryReq, fields ...string) (res *entity.Category, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Where(entity.CategoryCol.Id, req.Id).Fields(fields).One()
if err != nil {
return
}
if !g.IsEmpty(req.Id) {
model.Where(entity.CategoryCol.Id, req.Id)
}
res, err := model.Fields(fields).One()
err = res.Struct(&category)
return
}
// DeleteFake 删除分类-根据id及父id进行假删
func (d *category) DeleteFake(ctx context.Context, req *dto.DeleteCategoryReq) (err error) {
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection)
model.Where(entity.CategoryCol.Bid, req.Bid)
_, err = model.Delete()
err = r.Struct(&res)
return
}
// Count 根据条件统计分类数量
func (d *category) Count(ctx context.Context, req *dto.ListCategoryReq) (count int64, err error) {
m := d.buildListFilter(ctx, req)
c, err := m.Count()
return int64(c), err
func (d *category) Count(ctx context.Context, req *dto.ListCategoryReq) (count int, err error) {
return d.buildListFilter(ctx, req).Count()
}
// List 获取分类列表
func (d *category) List(ctx context.Context, req *dto.ListCategoryReq, fields ...string) (res []entity.Category, total int, err error) {
model := d.buildListFilter(ctx, req)
model.Fields(fields)
model.OrderAsc(entity.CategoryCol.Sort)
model.OrderDesc(entity.CategoryCol.CreatedAt)
if req.Page != nil {
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
}
r, total, err := model.AllAndCount(false)
err = gconv.Structs(r.List(), &res)
if err != nil {
return
}
err = r.Structs(&res)
return
}
@@ -88,8 +88,6 @@ func (d *category) buildListFilter(ctx context.Context, req *dto.ListCategoryReq
}
model.Where(entity.CategoryCol.ParentId, req.ParentId)
model.Where(entity.CategoryCol.Status, req.Status)
model.OrderAsc(entity.CategoryCol.Sort)
model.OrderDesc(entity.CategoryCol.CreatedAt)
model.OmitEmptyWhere()
return model
}

View File

@@ -39,7 +39,7 @@ func (d *stockDetails) DeleteManyByIds(ctx context.Context, allStockIds []*bson.
}
// GetStockCountBySkuId 获取库存数根据SKU ID
func (d *stockDetails) GetStockCountBySkuId(ctx context.Context, assetSkuId *bson.ObjectID) (total int64, err error) {
func (d *stockDetails) GetStockCountBySkuId(ctx context.Context, assetSkuId int64) (total int64, err error) {
// 构建查询过滤条件
filter := bson.M{}
filter["assetSkuId"] = assetSkuId