package dict import ( "context" consts "dataengine/consts/public" dto "dataengine/model/dto/dict" entity "dataengine/model/entity/dict" "gitea.redpowerfuture.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/os/gtime" "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, creator string, tenantId uint64) (id int64, err error) { var res *entity.ApiInterface if err = gconv.Struct(req, &res); err != nil { return } // 设置审计字段(服务端生成,不依赖前端入参) now := gtime.Now() res.TenantId = tenantId res.Creator = creator res.CreatedAt = now res.Updater = creator res.UpdatedAt = now 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, updater string, tenantId uint64) (rows int64, err error) { // 设置更新人和更新时间(服务端生成,不依赖前端入参) data := gconv.Map(req) data[entity.ApiInterfaceCols.Updater] = updater data[entity.ApiInterfaceCols.UpdatedAt] = gtime.Now() r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable). Data(data). OmitEmpty(). Where(entity.ApiInterfaceCols.Id, req.Id). Where(entity.ApiInterfaceCols.TenantId, tenantId). Update() if err != nil { return } return r.RowsAffected() } // Delete 删除接口 func (d *apiInterfaceDao) Delete(ctx context.Context, req *dto.DeleteApiInterfaceReq, tenantId uint64) (rows int64, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable). Where(entity.ApiInterfaceCols.Id, req.Id). Where(entity.ApiInterfaceCols.TenantId, tenantId). Delete() if err != nil { return } return r.RowsAffected() } // GetOne 获取单个接口 func (d *apiInterfaceDao) GetOne(ctx context.Context, req *dto.GetApiInterfaceReq, tenantId uint64) (res *entity.ApiInterface, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable). Where(entity.ApiInterfaceCols.Id, req.Id). Where(entity.ApiInterfaceCols.TenantId, tenantId). One() if err != nil { return } err = r.Struct(&res) return } // Count 获取接口数量 func (d *apiInterfaceDao) Count(ctx context.Context, req *dto.ListApiInterfaceReq, tenantId uint64) (count int, err error) { return d.buildListFilter(ctx, req, tenantId).Count() } // List 获取接口列表 func (d *apiInterfaceDao) List(ctx context.Context, req *dto.ListApiInterfaceReq, tenantId uint64) (res []entity.ApiInterface, total int, err error) { model := d.buildListFilter(ctx, req, tenantId) 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, tenantId uint64) *gdb.Model { model := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable).Model // 租户隔离 model.Where(entity.ApiInterfaceCols.TenantId, tenantId) if !g.IsEmpty(req.Keyword) { model.WhereLike(entity.ApiInterfaceCols.Name, "%"+req.Keyword+"%") model.WhereOrLike(entity.ApiInterfaceCols.Code, "%"+req.Keyword+"%") } if req.PlatformId > 0 { model.Where(entity.ApiInterfaceCols.PlatformId, req.PlatformId) } if !g.IsEmpty(req.Name) { model.Where(entity.ApiInterfaceCols.Name, req.Name) } if !g.IsEmpty(req.Code) { model.Where(entity.ApiInterfaceCols.Code, req.Code) } if !g.IsEmpty(req.Method) { model.Where(entity.ApiInterfaceCols.Method, req.Method) } if !g.IsEmpty(req.Status) { model.Where(entity.ApiInterfaceCols.Status, req.Status) } model.OmitEmptyWhere() return model } // UpdateStatus 更新接口状态 func (d *apiInterfaceDao) UpdateStatus(ctx context.Context, id int64, status string, updater string, tenantId uint64) (rows int64, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable). Data(map[string]interface{}{ entity.ApiInterfaceCols.Status: status, entity.ApiInterfaceCols.Updater: updater, entity.ApiInterfaceCols.UpdatedAt: gtime.Now(), }). Where(entity.ApiInterfaceCols.Id, id). Where(entity.ApiInterfaceCols.TenantId, tenantId). Update() if err != nil { return } return r.RowsAffected() } // GetByIds 根据ID列表获取接口列表 func (d *apiInterfaceDao) GetByIds(ctx context.Context, ids []int64, tenantId uint64) (res []entity.ApiInterface, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.ApiInterfaceTable). WhereIn(entity.ApiInterfaceCols.Id, ids). Where(entity.ApiInterfaceCols.TenantId, tenantId). All() if err != nil { return } err = r.Structs(&res) return }