111 lines
3.3 KiB
Go
111 lines
3.3 KiB
Go
package dao
|
||
|
||
import (
|
||
"assets/consts/public"
|
||
dto "assets/model/dto/asset"
|
||
entity "assets/model/entity/asset"
|
||
"context"
|
||
|
||
"gitea.com/red-future/common/db/gfdb"
|
||
"github.com/gogf/gf/v2/database/gdb"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/gogf/gf/v2/util/gconv"
|
||
)
|
||
|
||
var Asset = new(assetDao)
|
||
|
||
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 {
|
||
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
|
||
}
|
||
|
||
// 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,
|
||
}
|
||
_, err = gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where("id", req.Id).Update(data)
|
||
return
|
||
}
|
||
|
||
// 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
|
||
}
|
||
|
||
// 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)
|
||
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
|
||
}
|
||
|
||
// List 获取资产列表
|
||
func (d *assetDao) List(ctx context.Context, req *dto.ListAssetReq) (res []entity.Asset, total int, err error) {
|
||
m := d.buildListFilter(ctx, req)
|
||
if req.Page != nil {
|
||
m = m.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||
}
|
||
err = m.ScanAndCount(&res, &total, false)
|
||
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)
|
||
}
|
||
if !g.IsEmpty(req.CategoryPath) {
|
||
m = m.WhereLike("category_path", req.CategoryPath+"%")
|
||
}
|
||
if !g.IsEmpty(req.Keyword) {
|
||
m = m.WhereLike("name", "%"+req.Keyword+"%")
|
||
}
|
||
return m
|
||
}
|