95 lines
2.7 KiB
Go
95 lines
2.7 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 res *entity.Asset
|
|
if err = gconv.Struct(req, &res); err != nil {
|
|
return
|
|
}
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameAsset).Data(&res).Insert()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
// Update 更新资产
|
|
func (d *assetDao) Update(ctx context.Context, req *dto.UpdateAssetReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameAsset).Data(&req).Where(entity.AssetCol.Id, req.Id).Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// Delete 删除资产-根据id进行假删
|
|
func (d *assetDao) Delete(ctx context.Context, req *dto.DeleteAssetReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameAsset).Where(entity.AssetCol.Id, req.Id).Delete()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// GetOne 获取单个资产
|
|
func (d *assetDao) GetOne(ctx context.Context, req *dto.GetAssetReq, fields ...string) (res *entity.Asset, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameAsset).Where(entity.AssetCol.Id, req.Id).Fields(fields).One()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Struct(&res)
|
|
return
|
|
}
|
|
|
|
// Count 获取资产数量
|
|
func (d *assetDao) Count(ctx context.Context, req *dto.ListAssetReq) (count int, err error) {
|
|
return d.buildListFilter(ctx, req).Count()
|
|
}
|
|
|
|
// List 获取资产列表
|
|
func (d *assetDao) List(ctx context.Context, req *dto.ListAssetReq, fields ...string) (res []entity.Asset, total int, err error) {
|
|
model := d.buildListFilter(ctx, req).Fields(fields).OrderDesc(entity.AssetCol.CreatedAt)
|
|
if req.Page != nil {
|
|
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
|
}
|
|
r, total, err := model.AllAndCount(false)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Structs(&res)
|
|
return
|
|
}
|
|
|
|
// buildListFilter 构建列表查询的过滤条件
|
|
func (d *assetDao) buildListFilter(ctx context.Context, req *dto.ListAssetReq) *gdb.Model {
|
|
model := gfdb.DB(ctx).Model(ctx, public.TableNameAsset).Cache(ctx)
|
|
if !g.IsEmpty(req.Keyword) {
|
|
model.WhereLike(entity.AssetCol.Name, "%"+req.Keyword+"%")
|
|
}
|
|
if !g.IsEmpty(req.CategoryPath) {
|
|
model.WhereLike(entity.AssetCol.CategoryPath, req.CategoryPath+"%")
|
|
}
|
|
model.Where(entity.AssetCol.Name, req.Name)
|
|
model.Where(entity.AssetCol.Type, req.Type)
|
|
model.Where(entity.AssetCol.CategoryId, req.CategoryId)
|
|
model.Where(entity.AssetCol.Status, req.Status)
|
|
return model
|
|
}
|