177 lines
4.8 KiB
Go
177 lines
4.8 KiB
Go
|
|
package service
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"assets/consts/stock"
|
|||
|
|
dao "assets/dao/asset"
|
|||
|
|
"assets/dao/base"
|
|||
|
|
dto "assets/model/dto/asset"
|
|||
|
|
"context"
|
|||
|
|
"errors"
|
|||
|
|
"fmt"
|
|||
|
|
"gitea.com/red-future/common/http"
|
|||
|
|
"github.com/gogf/gf/v2/frame/g"
|
|||
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|||
|
|
|
|||
|
|
"gitea.com/red-future/common/beans"
|
|||
|
|
"gitea.com/red-future/common/minio"
|
|||
|
|
"gitea.com/red-future/common/utils"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
type asset struct{}
|
|||
|
|
|
|||
|
|
// Asset 资产服务
|
|||
|
|
var Asset = new(asset)
|
|||
|
|
|
|||
|
|
// Create 创建资产
|
|||
|
|
func (s *asset) Create(ctx context.Context, req *dto.CreateAssetReq) (res *dto.CreateAssetRes, err error) {
|
|||
|
|
count, err := dao.Asset.Count(ctx, &dto.ListAssetReq{Name: req.Name})
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if count > 0 {
|
|||
|
|
return nil, errors.New("资产名称已存在")
|
|||
|
|
}
|
|||
|
|
// 检查是否是超级管理员
|
|||
|
|
isSuperAdmin := false
|
|||
|
|
// 获取当前请求的 headers 并传递到下游
|
|||
|
|
headers := make(map[string]string)
|
|||
|
|
if r := g.RequestFromCtx(ctx); r != nil {
|
|||
|
|
for k, v := range r.Request.Header {
|
|||
|
|
if len(v) > 0 {
|
|||
|
|
headers[k] = v[0]
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if err = http.Get(ctx, "admin-go/api/v1/system/user/checkIsSuperAdmin", headers, &isSuperAdmin); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if !isSuperAdmin {
|
|||
|
|
req.StockMode = stock.StockModeDetail
|
|||
|
|
|
|||
|
|
//var getUserInfo beans.User
|
|||
|
|
//getUserInfo, err = utils.GetUserInfo(ctx)
|
|||
|
|
//if err != nil {
|
|||
|
|
// return
|
|||
|
|
//}
|
|||
|
|
//var get *gvar.Var
|
|||
|
|
//get, err = message.GetRedisClientTest("test").Get(ctx, fmt.Sprintf("module_tenant:tenantId-%v", getUserInfo.TenantId))
|
|||
|
|
//if err != nil {
|
|||
|
|
// return
|
|||
|
|
//}
|
|||
|
|
//if !g.IsEmpty(get.String()) {
|
|||
|
|
// list := new(beans.ModuleTenant)
|
|||
|
|
// if err = json.Unmarshal(get.Bytes(), &list); err != nil {
|
|||
|
|
// return
|
|||
|
|
// }
|
|||
|
|
// req.TenantModuleType = list.TenantModuleType
|
|||
|
|
//} else {
|
|||
|
|
// moduleTenantRes := new(beans.ModuleTenant)
|
|||
|
|
// err = message.CallRPC(ctx, "moduleService.AddRedisByTenantId", map[string]interface{}{"tenantId": getUserInfo.TenantId}, moduleTenantRes)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return
|
|||
|
|
// }
|
|||
|
|
// if !g.IsEmpty(moduleTenantRes.TenantModuleType) {
|
|||
|
|
// req.TenantModuleType = moduleTenantRes.TenantModuleType
|
|||
|
|
// } else {
|
|||
|
|
// return nil, errors.New("您未开通此模块,请开通后再使用")
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
} else {
|
|||
|
|
req.TenantModuleType = beans.TenantModuleTypePlatform
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 插入数据库
|
|||
|
|
id, err := dao.Asset.Insert(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
res = &dto.CreateAssetRes{
|
|||
|
|
Id: gconv.Uint64(id),
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// List 获取资产列表
|
|||
|
|
func (s *asset) List(ctx context.Context, req *dto.ListAssetReq) (res *dto.ListAssetRes, err error) {
|
|||
|
|
assetList, total, err := dao.Asset.List(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
user, err := utils.GetUserInfo(ctx)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
fmt.Println(user)
|
|||
|
|
res = &dto.ListAssetRes{
|
|||
|
|
Total: total,
|
|||
|
|
}
|
|||
|
|
err = utils.Struct(assetList, &res.List)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetOne 获取单个资产
|
|||
|
|
func (s *asset) GetOne(ctx context.Context, req *dto.GetAssetReq) (res *dto.GetAssetRes, err error) {
|
|||
|
|
assetOne, err := dao.Asset.GetOne(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
// TODO: CategoryId类型不匹配,需要同步修改category为uint64
|
|||
|
|
// getCategoryRes, err := dao.Category.GetOne(ctx, &dto.GetCategoryReq{
|
|||
|
|
// Id: assetOne.CategoryId,
|
|||
|
|
// })
|
|||
|
|
// if err != nil {
|
|||
|
|
// return
|
|||
|
|
// }
|
|||
|
|
return &dto.GetAssetRes{
|
|||
|
|
Asset: assetOne,
|
|||
|
|
CategoryName: "", // getCategoryRes.Name,
|
|||
|
|
ImgAddressPrefix: minio.GetFileAddressPrefix(ctx),
|
|||
|
|
}, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetAssetAndSku 获取资产和Sku详情
|
|||
|
|
func (s *asset) GetAssetAndSku(ctx context.Context, req *dto.GetAssetAndSkuReq) (res *dto.GetAssetAndSkuRes, err error) {
|
|||
|
|
// 跳过租户ID过滤获取资产
|
|||
|
|
// TODO: AssetId 类型不匹配,bson.ObjectID 需要转换为 uint64
|
|||
|
|
// 使用 SkipTenantId 跳过租户ID过滤
|
|||
|
|
assetOne, err := dao.Asset.GetOneById(base.SkipTenantId(ctx), 0)
|
|||
|
|
_ = req.AssetId
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
// TODO: AssetId类型不匹配,需要适配
|
|||
|
|
// moduleType, err := service.Enum.GetTenantModuleType(ctx, &enumDto.GetTenantModuleTypeReq{AssetId: req.AssetId.Hex()})
|
|||
|
|
// if err != nil {
|
|||
|
|
// return
|
|||
|
|
// }
|
|||
|
|
// TODO: AssetId类型不匹配,需要同步修改AssetSku为uint64
|
|||
|
|
// skus, _, err := dao.AssetSku.List(ctx, &dto.ListAssetSkuReq{AssetId: req.AssetId}, true)
|
|||
|
|
// if err != nil {
|
|||
|
|
// return
|
|||
|
|
// }
|
|||
|
|
return &dto.GetAssetAndSkuRes{
|
|||
|
|
Asset: assetOne,
|
|||
|
|
Skus: nil, // skus,
|
|||
|
|
TenantModuleType: nil, // moduleType.Options,
|
|||
|
|
ImgAddressPrefix: minio.GetFileAddressPrefix(ctx),
|
|||
|
|
}, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Update 更新资产
|
|||
|
|
func (s *asset) Update(ctx context.Context, req *dto.UpdateAssetReq) error {
|
|||
|
|
return dao.Asset.Update(ctx, req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdateStatus 更新资产状态
|
|||
|
|
func (s *asset) UpdateStatus(ctx context.Context, req *dto.UpdateAssetStatusReq) (err error) {
|
|||
|
|
var updateReq *dto.UpdateAssetReq
|
|||
|
|
err = utils.Struct(req, &updateReq)
|
|||
|
|
return dao.Asset.Update(ctx, updateReq)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Delete 删除资产
|
|||
|
|
func (s *asset) Delete(ctx context.Context, req *dto.DeleteAssetReq) error {
|
|||
|
|
return dao.Asset.DeleteFake(ctx, req)
|
|||
|
|
}
|