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

@@ -13,7 +13,6 @@ import (
"gitea.com/red-future/common/utils"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"go.mongodb.org/mongo-driver/v2/bson"
)
type assetSku struct{}
@@ -28,7 +27,7 @@ func (s *assetSku) CreateAssetSku(ctx context.Context, req *dto.CreateAssetSkuRe
return nil, errors.New("关联资产不存在")
}
// 根据资产ID查询SKU列表用于下面验证自定义属性和sku名称重不重复
skusList, _, err := dao.AssetSku.List(ctx, &dto.ListAssetSkuReq{AssetId: req.AssetId}, false)
skusList, _, err := dao.AssetSku.List(ctx, &dto.ListAssetSkuReq{AssetId: assetEntity.Id})
if err != nil {
return
}
@@ -39,26 +38,23 @@ func (s *assetSku) CreateAssetSku(ctx context.Context, req *dto.CreateAssetSkuRe
}
req.UnlimitedStock = assetEntity.UnlimitedStock
req.StockMode = assetEntity.StockMode
// TODO: 类型不匹配 uint64 vs *bson.ObjectID
// req.CategoryId = assetEntity.CategoryId
req.CategoryId = assetEntity.CategoryId
req.CategoryPath = assetEntity.CategoryPath
// TODO: 类型不匹配 string vs beans.TenantModuleType
// req.TenantModuleType = assetEntity.TenantModuleType
req.TenantModuleType = assetEntity.TenantModuleType
// 插入数据库
ids, err := dao.AssetSku.Insert(ctx, req)
id, err := dao.AssetSku.Insert(ctx, req)
if err != nil {
return
}
id := ids[0].(bson.ObjectID)
res = &dto.CreateAssetSkuRes{
Id: &id,
Id: id,
}
return
}
// UpdateAssetSku 更新SKU
func (s *assetSku) UpdateAssetSku(ctx context.Context, req *dto.UpdateAssetSkuReq) error {
getOne, err := dao.AssetSku.GetOne(ctx, &dto.GetAssetSkuReq{Id: req.Id}, false)
func (s *assetSku) UpdateAssetSku(ctx context.Context, req *dto.UpdateAssetSkuReq) (err error) {
getOne, err := dao.AssetSku.GetOne(ctx, &dto.GetAssetSkuReq{Id: req.Id})
if err != nil {
return errors.New("SUK不存在")
}
@@ -78,7 +74,8 @@ func (s *assetSku) UpdateAssetSku(ctx context.Context, req *dto.UpdateAssetSkuRe
return err
}
// 更新数据库
return dao.AssetSku.Update(ctx, req)
_, err = dao.AssetSku.Update(ctx, req)
return
}
func (s *assetSku) parameterValidation(ctx context.Context, assetEntity *entity.Asset, list []entity.AssetSku, skuName string, specValues []map[string]interface{}) (err error) {
@@ -112,7 +109,8 @@ func (s *assetSku) parameterValidation(ctx context.Context, assetEntity *entity.
for key, subValue := range specValues {
// 1. 检查请求参数 map 中是否存在该键
for _, subValues := range list.SpecValues {
parentValue, ok := subValues[gconv.String(key)]
mapSubValues := gconv.Map(subValues)
parentValue, ok := mapSubValues[gconv.String(key)]
if ok {
// 2. 检查对应的值是否相等
if reflect.DeepEqual(parentValue, subValue) {
@@ -146,13 +144,14 @@ func (s *assetSku) parameterValidation(ctx context.Context, assetEntity *entity.
}
// DeleteAssetSku 删除SKU软删除
func (s *assetSku) DeleteAssetSku(ctx context.Context, req *dto.DeleteAssetSkuReq) error {
return dao.AssetSku.DeleteFake(ctx, req)
func (s *assetSku) DeleteAssetSku(ctx context.Context, req *dto.DeleteAssetSkuReq) (err error) {
_, err = dao.AssetSku.Delete(ctx, req)
return
}
// GetAssetSku 获取SKU详情
func (s *assetSku) GetAssetSku(ctx context.Context, req *dto.GetAssetSkuReq) (res *dto.GetAssetSkuRes, err error) {
one, err := dao.AssetSku.GetOne(ctx, req, false)
one, err := dao.AssetSku.GetOne(ctx, req)
if err != nil {
return
}
@@ -163,7 +162,7 @@ func (s *assetSku) GetAssetSku(ctx context.Context, req *dto.GetAssetSkuReq) (re
// ListAssetSkus 获取SKU列表
func (s *assetSku) ListAssetSkus(ctx context.Context, req *dto.ListAssetSkuReq) (res *dto.ListAssetSkuRes, err error) {
// 查询数据库
list, total, err := dao.AssetSku.List(ctx, req, false)
list, total, err := dao.AssetSku.List(ctx, req)
if err != nil {
return
}
@@ -176,14 +175,19 @@ func (s *assetSku) ListAssetSkus(ctx context.Context, req *dto.ListAssetSkuReq)
// GetAssetSkuModule 获取SKU详情
func (s *assetSku) GetAssetSkuModule(ctx context.Context, req *dto.GetAssetSkuModuleReq) (res *dto.GetAssetSkuModuleRes, err error) {
one, err := dao.AssetSku.GetOne(ctx, &dto.GetAssetSkuReq{Id: req.Id}, true)
one, err := dao.AssetSku.GetOne(ctx, &dto.GetAssetSkuReq{Id: req.Id})
if err != nil {
return
}
res = &dto.GetAssetSkuModuleRes{}
// 计算到期时间
if one.SpecsUnit != nil && one.SpecsCount > 0 {
durationType := public.DurationType(one.SpecsUnit.Key)
var specsUnit *entity.SpecsUnitKeyValue
err = gconv.Struct(one.SpecsUnit, &specsUnit)
if err != nil {
return
}
durationType := public.DurationType(specsUnit.Key)
res.ExpireAt = durationType.AddTime(one.SpecsCount)
res.AssetId = one.AssetId
}