192 lines
5.8 KiB
Go
192 lines
5.8 KiB
Go
|
|
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"
|
|||
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
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: req.AssetId}, false)
|
|||
|
|
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
|
|||
|
|
// TODO: 类型不匹配 uint64 vs *bson.ObjectID
|
|||
|
|
// req.CategoryId = assetEntity.CategoryId
|
|||
|
|
req.CategoryPath = assetEntity.CategoryPath
|
|||
|
|
// TODO: 类型不匹配 string vs beans.TenantModuleType
|
|||
|
|
// req.TenantModuleType = assetEntity.TenantModuleType
|
|||
|
|
// 插入数据库
|
|||
|
|
ids, err := dao.AssetSku.Insert(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
id := ids[0].(bson.ObjectID)
|
|||
|
|
res = &dto.CreateAssetSkuRes{
|
|||
|
|
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)
|
|||
|
|
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
|
|||
|
|
}
|
|||
|
|
// 更新数据库
|
|||
|
|
return dao.AssetSku.Update(ctx, req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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 {
|
|||
|
|
parentValue, ok := subValues[gconv.String(key)]
|
|||
|
|
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) error {
|
|||
|
|
return dao.AssetSku.DeleteFake(ctx, req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 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)
|
|||
|
|
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, false)
|
|||
|
|
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}, true)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
res = &dto.GetAssetSkuModuleRes{}
|
|||
|
|
// 计算到期时间
|
|||
|
|
if one.SpecsUnit != nil && one.SpecsCount > 0 {
|
|||
|
|
durationType := public.DurationType(one.SpecsUnit.Key)
|
|||
|
|
res.ExpireAt = durationType.AddTime(one.SpecsCount)
|
|||
|
|
res.AssetId = one.AssetId
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|