Files
assets/service/asset/asset_sku_service.go

196 lines
5.8 KiB
Go
Raw Permalink Normal View History

2026-03-18 10:18:03 +08:00
package service
import (
"assets/consts/public"
dao "assets/dao/asset"
dto "assets/model/dto/asset"
entity "assets/model/entity/asset"
"context"
"errors"
"reflect"
"gitea.com/red-future/common/beans"
"gitea.com/red-future/common/utils"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
type assetSku struct{}
var AssetSku = new(assetSku)
// CreateAssetSku 创建SKU
func (s *assetSku) CreateAssetSku(ctx context.Context, req *dto.CreateAssetSkuReq) (res *dto.CreateAssetSkuRes, err error) {
// 验证资产是否存在, 如果存在,则获取资产信息(用于下面验证,自定义属性传过来的全不全)
assetEntity, err := dao.Asset.GetOne(ctx, &dto.GetAssetReq{Id: req.AssetId})
if err != nil {
return nil, errors.New("关联资产不存在")
}
// 根据资产ID查询SKU列表用于下面验证自定义属性和sku名称重不重复
skusList, _, err := dao.AssetSku.List(ctx, &dto.ListAssetSkuReq{AssetId: assetEntity.Id})
2026-03-18 10:18:03 +08:00
if err != nil {
return
}
// 验证参数
err = s.parameterValidation(ctx, assetEntity, skusList, req.SkuName, req.SpecValues)
if err != nil {
return
}
req.UnlimitedStock = assetEntity.UnlimitedStock
req.StockMode = assetEntity.StockMode
req.CategoryId = assetEntity.CategoryId
2026-03-18 10:18:03 +08:00
req.CategoryPath = assetEntity.CategoryPath
req.TenantModuleType = assetEntity.TenantModuleType
2026-03-18 10:18:03 +08:00
// 插入数据库
id, err := dao.AssetSku.Insert(ctx, req)
2026-03-18 10:18:03 +08:00
if err != nil {
return
}
res = &dto.CreateAssetSkuRes{
Id: id,
2026-03-18 10:18:03 +08:00
}
return
}
// UpdateAssetSku 更新SKU
func (s *assetSku) UpdateAssetSku(ctx context.Context, req *dto.UpdateAssetSkuReq) (err error) {
getOne, err := dao.AssetSku.GetOne(ctx, &dto.GetAssetSkuReq{Id: req.Id})
2026-03-18 10:18:03 +08:00
if err != nil {
return errors.New("SUK不存在")
}
// 根据资产ID查询SKU列表用于下面验证自定义属性和sku名称重不重复
skusList, _, err := dao.AssetSku.GetListByAssetIdExcludeCurrentSku(ctx, getOne.AssetId, &dto.ListAssetSkuReq{Id: req.Id, Page: &beans.Page{PageSize: -1}})
if err != nil {
return err
}
// 验证资产是否存在, 如果存在,则获取资产信息(用于下面验证,自定义属性传过来的全不全)
assetEntity, err := dao.Asset.GetOne(ctx, &dto.GetAssetReq{Id: getOne.AssetId})
if err != nil {
return errors.New("关联资产不存在")
}
// 验证参数
err = s.parameterValidation(ctx, assetEntity, skusList, req.SkuName, req.SpecValues)
if err != nil {
return err
}
// 更新数据库
_, err = dao.AssetSku.Update(ctx, req)
return
2026-03-18 10:18:03 +08:00
}
func (s *assetSku) parameterValidation(ctx context.Context, assetEntity *entity.Asset, list []entity.AssetSku, skuName string, specValues []map[string]interface{}) (err error) {
_ = ctx
specNoExist := true
metadataList := make([]string, 0)
// 验证,自定义属性传过来的全不全
// TODO: Metadata类型从[]map[string]interface{}变为*gjson.Json需要适配
// if assetEntity.Metadata != nil {
// for _, metadata := range assetEntity.Metadata {
// attributeType := gconv.String(gconv.Map(metadata)["type"])
// if attributeType == string(consts.AttributeTypeMultiSelect) {
// metadataList = append(metadataList, attributeType)
// }
// }
// if len(metadataList) != len(specValues) {
// // 如果请求参数中不存在该键,则跳过
// return errors.New("规格参数填写不完整")
// }
// }
// 验证自定义属性和sku名称重不重复
for _, list := range list {
if list.SkuName == skuName {
return errors.New("SKU名称已存在")
}
if !g.IsEmpty(metadataList) && len(metadataList) > 0 {
if !g.IsEmpty(specValues) {
exist := true
// 遍历请求参数 map
for key, subValue := range specValues {
// 1. 检查请求参数 map 中是否存在该键
for _, subValues := range list.SpecValues {
mapSubValues := gconv.Map(subValues)
parentValue, ok := mapSubValues[gconv.String(key)]
2026-03-18 10:18:03 +08:00
if ok {
// 2. 检查对应的值是否相等
if reflect.DeepEqual(parentValue, subValue) {
exist = false
} else {
exist = true
break
}
} else {
exist = true
break
}
}
}
if exist {
specNoExist = true
} else {
specNoExist = false
break
}
} else {
return errors.New("规格参数不能为空")
}
}
}
if !specNoExist {
return errors.New("规格参数已存在")
}
return nil
}
// DeleteAssetSku 删除SKU软删除
func (s *assetSku) DeleteAssetSku(ctx context.Context, req *dto.DeleteAssetSkuReq) (err error) {
_, err = dao.AssetSku.Delete(ctx, req)
return
2026-03-18 10:18:03 +08:00
}
// GetAssetSku 获取SKU详情
func (s *assetSku) GetAssetSku(ctx context.Context, req *dto.GetAssetSkuReq) (res *dto.GetAssetSkuRes, err error) {
one, err := dao.AssetSku.GetOne(ctx, req)
2026-03-18 10:18:03 +08:00
if err != nil {
return
}
err = utils.Struct(one, &res)
return
}
// 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)
2026-03-18 10:18:03 +08:00
if err != nil {
return
}
res = &dto.ListAssetSkuRes{
Total: total,
}
err = utils.Struct(list, &res.List)
return
}
// 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})
2026-03-18 10:18:03 +08:00
if err != nil {
return
}
res = &dto.GetAssetSkuModuleRes{}
// 计算到期时间
if one.SpecsUnit != nil && one.SpecsCount > 0 {
var specsUnit *entity.SpecsUnitKeyValue
err = gconv.Struct(one.SpecsUnit, &specsUnit)
if err != nil {
return
}
durationType := public.DurationType(specsUnit.Key)
2026-03-18 10:18:03 +08:00
res.ExpireAt = durationType.AddTime(one.SpecsCount)
res.AssetId = one.AssetId
}
return
}