119 lines
3.6 KiB
Go
119 lines
3.6 KiB
Go
package dict
|
|
|
|
import (
|
|
consts "cid/consts/public"
|
|
dto "cid/model/dto/dict"
|
|
entity "cid/model/entity/dict"
|
|
"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 ApiInterface = new(apiInterfaceDao)
|
|
|
|
type apiInterfaceDao struct{}
|
|
|
|
// Insert 插入接口
|
|
func (d *apiInterfaceDao) Insert(ctx context.Context, req *dto.CreateApiInterfaceReq) (id int64, err error) {
|
|
var res *entity.ApiInterface
|
|
if err = gconv.Struct(req, &res); err != nil {
|
|
return
|
|
}
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Data(&res).Insert()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
// Update 更新接口
|
|
func (d *apiInterfaceDao) Update(ctx context.Context, req *dto.UpdateApiInterfaceReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Data(&req).OmitEmpty().Where(entity.ApiInterfaceCols.Id, req.Id).Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// Delete 删除接口
|
|
func (d *apiInterfaceDao) Delete(ctx context.Context, req *dto.DeleteApiInterfaceReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Where(entity.ApiInterfaceCols.Id, req.Id).Delete()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// GetOne 获取单个接口
|
|
func (d *apiInterfaceDao) GetOne(ctx context.Context, req *dto.GetApiInterfaceReq) (res *entity.ApiInterface, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Where(entity.ApiInterfaceCols.Id, req.Id).One()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Struct(&res)
|
|
return
|
|
}
|
|
|
|
// Count 获取接口数量
|
|
func (d *apiInterfaceDao) Count(ctx context.Context, req *dto.ListApiInterfaceReq) (count int, err error) {
|
|
return d.buildListFilter(ctx, req).Count()
|
|
}
|
|
|
|
// List 获取接口列表
|
|
func (d *apiInterfaceDao) List(ctx context.Context, req *dto.ListApiInterfaceReq) (res []entity.ApiInterface, total int, err error) {
|
|
model := d.buildListFilter(ctx, req)
|
|
model.OrderDesc(entity.ApiInterfaceCols.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 *apiInterfaceDao) buildListFilter(ctx context.Context, req *dto.ListApiInterfaceReq) *gdb.Model {
|
|
model := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Model
|
|
if !g.IsEmpty(req.Keyword) {
|
|
model.WhereLike(entity.ApiInterfaceCols.Name, "%"+req.Keyword+"%")
|
|
model.WhereOrLike(entity.ApiInterfaceCols.Code, "%"+req.Keyword+"%")
|
|
}
|
|
model.Where(entity.ApiInterfaceCols.PlatformId, req.PlatformId)
|
|
model.Where(entity.ApiInterfaceCols.Name, req.Name)
|
|
model.Where(entity.ApiInterfaceCols.Code, req.Code)
|
|
model.Where(entity.ApiInterfaceCols.Method, req.Method)
|
|
model.Where(entity.ApiInterfaceCols.Status, req.Status)
|
|
model.OmitEmptyWhere()
|
|
return model
|
|
}
|
|
|
|
// UpdateStatus 更新接口状态
|
|
func (d *apiInterfaceDao) UpdateStatus(ctx context.Context, id int64, status string) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).
|
|
Data(map[string]interface{}{"status": status}).
|
|
Where(entity.ApiInterfaceCols.Id, id).
|
|
Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// GetByIds 根据ID列表获取接口列表
|
|
func (d *apiInterfaceDao) GetByIds(ctx context.Context, ids []int64) (res []entity.ApiInterface, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).
|
|
WhereIn(entity.ApiInterfaceCols.Id, ids).
|
|
All()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Structs(&res)
|
|
return
|
|
}
|