240 lines
7.8 KiB
Go
240 lines
7.8 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/contexts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/exportcenter"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/handler"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/hook"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/convert"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
type sAgentTApiResource struct{}
|
|
|
|
// NewAgentTApiResource 创建开放接口资源后台服务实例。
|
|
func NewAgentTApiResource() *sAgentTApiResource {
|
|
return &sAgentTApiResource{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterAgentTApiResource(NewAgentTApiResource())
|
|
}
|
|
|
|
// Model 返回开放接口资源 ORM 模型,默认启用租户和数据权限过滤。
|
|
func (s *sAgentTApiResource) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
|
|
if len(option) == 0 {
|
|
option = append(option, &handler.Option{
|
|
FilterTenant: true,
|
|
FilterAuth: true,
|
|
})
|
|
}
|
|
return handler.Model(dao.TApiResource.Ctx(ctx), option...)
|
|
}
|
|
|
|
// List 分页查询开放接口资源列表。
|
|
func (s *sAgentTApiResource) List(ctx context.Context, in *model.TApiResourceListParam) (list []*model.TApiResourceListModel, totalCount int, err error) {
|
|
deptId := contexts.GetDeptId(ctx)
|
|
if deptId <= 0 {
|
|
return []*model.TApiResourceListModel{}, 0, nil
|
|
}
|
|
|
|
mod := s.Model(ctx, &handler.Option{FilterTenant: true, FilterAuth: false}).
|
|
Fields(model.TApiResourceListModel{})
|
|
|
|
isTop, dept, err := service.AgentDept().IsTopDept(ctx)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if dept != nil && (dept.DeptId == 100 || dept.ParentId == 100) {
|
|
isTop = true
|
|
}
|
|
|
|
if !isTop {
|
|
var apiInfo struct {
|
|
GroupId int64 `orm:"group_id"`
|
|
}
|
|
if err = dao.TApi.Ctx(ctx).
|
|
Fields(dao.TApi.Columns().GroupId).
|
|
Where(dao.TApi.Columns().DeptId, deptId).
|
|
Where(dao.TApi.Columns().TenantId, contexts.GetTenantId(ctx)).
|
|
OrderDesc(dao.TApi.Columns().Id).
|
|
Scan(&apiInfo); err != nil {
|
|
return nil, 0, gerror.Wrap(err, "get current dept open api group failed")
|
|
}
|
|
if apiInfo.GroupId <= 0 {
|
|
return []*model.TApiResourceListModel{}, 0, nil
|
|
}
|
|
|
|
apiIds, err := dao.TApiGroupResource.Ctx(ctx).
|
|
Fields(dao.TApiGroupResource.Columns().ApiId).
|
|
Where(dao.TApiGroupResource.Columns().GroupId, apiInfo.GroupId).
|
|
Array()
|
|
if err != nil {
|
|
return nil, 0, gerror.Wrap(err, "get open api group resources failed")
|
|
}
|
|
if len(apiIds) == 0 {
|
|
return []*model.TApiResourceListModel{}, 0, nil
|
|
}
|
|
mod = mod.WhereIn(dao.TApiResource.Columns().Id, gconv.Int64s(apiIds))
|
|
}
|
|
|
|
if in.Id > 0 {
|
|
mod = mod.Where(dao.TApiResource.Columns().Id, in.Id)
|
|
}
|
|
if in.Status > 0 {
|
|
mod = mod.Where(dao.TApiResource.Columns().Status, in.Status)
|
|
}
|
|
if in.ApiName != "" {
|
|
mod = mod.Where(dao.TApiResource.Columns().ApiName+" LIKE ?", "%"+in.ApiName+"%")
|
|
}
|
|
if in.Version != "" {
|
|
mod = mod.Where(dao.TApiResource.Columns().Version+" LIKE ?", "%"+in.Version+"%")
|
|
}
|
|
|
|
if len(in.CreatedAt) == 2 {
|
|
in.CreatedAt[1] = in.CreatedAt[1].EndOfDay()
|
|
mod = mod.WhereBetween(dao.TApiResource.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
|
|
}
|
|
|
|
mod = mod.Page(in.Page, in.PageSize).
|
|
OrderDesc(dao.TApiResource.Columns().Id).
|
|
Hook(hook.MemberSummary)
|
|
|
|
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
|
return nil, 0, gerror.Wrap(err, "获取开放接口资源列表失败,请稍后重试!")
|
|
}
|
|
return
|
|
}
|
|
|
|
// Export 导出开放接口资源(异步:提交任务后到下载中心取文件)
|
|
func (s *sAgentTApiResource) Export(ctx context.Context, in *model.TApiResourceListParam) (err error) {
|
|
_, err = exportcenter.Submit(ctx, &exportcenter.Request{
|
|
BizKey: "tApiResource",
|
|
BizName: "开放接口资源",
|
|
PermCode: "cpm:agent:tApiResource:export",
|
|
QueryParam: in,
|
|
Tags: func() ([]string, error) {
|
|
return convert.GetEntityDescTags(model.TApiResourceListModel{})
|
|
},
|
|
Fetch: func(ctx context.Context, page, pageSize int) (rows []interface{}, total int, err error) {
|
|
query := *in
|
|
query.Page = page
|
|
query.PageSize = pageSize
|
|
list, count, err := s.List(ctx, &query)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
rows = make([]interface{}, 0, len(list))
|
|
for _, item := range list {
|
|
rows = append(rows, item)
|
|
}
|
|
return rows, count, nil
|
|
},
|
|
})
|
|
return err
|
|
}
|
|
|
|
// Edit 新增或修改开放接口资源。
|
|
func (s *sAgentTApiResource) Edit(ctx context.Context, in *model.TApiResourceEditParam) (err error) {
|
|
if err = in.Filter(ctx); err != nil {
|
|
return
|
|
}
|
|
if in.Status == nil {
|
|
status := 0
|
|
in.Status = &status
|
|
}
|
|
if in.Method != nil {
|
|
method := *in.Method
|
|
in.Method = &method
|
|
}
|
|
|
|
if in.ApiCode != nil && gstr.Trim(*in.ApiCode) != "" {
|
|
if err = xgorm.IsUnique(ctx, &dao.TApiResource, g.Map{dao.TApiResource.Columns().ApiCode: gstr.Trim(*in.ApiCode)}, "接口权限标识已存在", in.Id); err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
|
if in.Id > 0 {
|
|
updatedBy := contexts.GetUserId(ctx)
|
|
in.UpdatedBy = &updatedBy
|
|
in.UpdatedAt = gtime.Now()
|
|
tenantId := contexts.GetTenantId(ctx)
|
|
in.TenantId = &tenantId
|
|
if _, err = s.Model(ctx).
|
|
Fields(model.TApiResourceUpdateFields{}).
|
|
WherePri(in.Id).
|
|
Data(in).
|
|
Update(); err != nil {
|
|
return gerror.Wrap(err, "修改开放接口资源失败,请稍后重试!")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
tenantId := contexts.GetTenantId(ctx)
|
|
in.TenantId = &tenantId
|
|
createdDept := contexts.GetDeptId(ctx)
|
|
in.CreatedDept = &createdDept
|
|
createdBy := contexts.GetUserId(ctx)
|
|
in.CreatedBy = &createdBy
|
|
in.CreatedAt = gtime.Now()
|
|
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
|
|
Fields(model.TApiResourceInsertFields{}).
|
|
Data(in).
|
|
Insert(); err != nil {
|
|
return gerror.Wrap(err, "新增开放接口资源失败,请稍后重试!")
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Delete 删除开放接口资源,并清理权限组资源关联。
|
|
func (s *sAgentTApiResource) Delete(ctx context.Context, in *model.TApiResourceDeleteParam) (err error) {
|
|
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
|
apiIds := gconv.Int64s(in.Id)
|
|
if len(apiIds) > 0 {
|
|
if _, err = tx.Ctx(ctx).Model(dao.TApiGroupResource.Table()).
|
|
WhereIn(dao.TApiGroupResource.Columns().ApiId, apiIds).
|
|
Delete(); err != nil {
|
|
return gerror.Wrap(err, "删除开放接口权限组资源关联失败,请稍后重试!")
|
|
}
|
|
}
|
|
if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
|
|
return gerror.Wrap(err, "删除开放接口资源失败,请稍后重试!")
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// View 查询开放接口资源详情。
|
|
func (s *sAgentTApiResource) View(ctx context.Context, in *model.TApiResourceViewParam) (res *model.TApiResourceViewModel, err error) {
|
|
if err = s.Model(ctx).WherePri(in.Id).Hook(hook.MemberSummary).Scan(&res); err != nil {
|
|
return nil, gerror.Wrap(err, "获取开放接口资源信息失败,请稍后重试!")
|
|
}
|
|
return
|
|
}
|
|
|
|
// Status 更新开放接口资源启停状态。
|
|
func (s *sAgentTApiResource) Status(ctx context.Context, in *model.TApiResourceStatusParam) (err error) {
|
|
if _, err = s.Model(ctx).WherePri(in.Id).Data(g.Map{
|
|
dao.TApiResource.Columns().Status: in.Status,
|
|
dao.TApiResource.Columns().UpdatedBy: contexts.GetUserId(ctx),
|
|
dao.TApiResource.Columns().UpdatedAt: gtime.Now(),
|
|
}).Update(); err != nil {
|
|
return gerror.Wrap(err, "更新开放接口资源状态失败,请稍后重试!")
|
|
}
|
|
return
|
|
}
|