refactor: 优化数据库查询构建链式调用
This commit is contained in:
@@ -32,7 +32,7 @@ func (d *assetDao) Insert(ctx context.Context, req *dto.CreateAssetReq) (id int6
|
||||
|
||||
// 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).OmitEmpty().Where(entity.AssetCol.Id, req.Id).Update()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameAsset).Data(&req).Where(entity.AssetCol.Id, req.Id).Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -65,9 +65,7 @@ func (d *assetDao) Count(ctx context.Context, req *dto.ListAssetReq) (count int,
|
||||
|
||||
// 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)
|
||||
model.Fields(fields)
|
||||
model.OrderDesc(entity.AssetCol.CreatedAt)
|
||||
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))
|
||||
}
|
||||
@@ -92,6 +90,5 @@ func (d *assetDao) buildListFilter(ctx context.Context, req *dto.ListAssetReq) *
|
||||
model.Where(entity.AssetCol.Type, req.Type)
|
||||
model.Where(entity.AssetCol.CategoryId, req.CategoryId)
|
||||
model.Where(entity.AssetCol.Status, req.Status)
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user