168 lines
4.9 KiB
Go
168 lines
4.9 KiB
Go
// Package iot
|
|
// @Link https://github.com/xiujiecn/xiu-admin
|
|
// @Copyright Copyright (c) 2026 LiXiujie
|
|
// @Author Lxj <xxx@example.com>
|
|
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
|
// @AutoGenerate Version
|
|
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/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"
|
|
)
|
|
|
|
type sAgentTApi struct{}
|
|
|
|
func NewAgentTApi() *sAgentTApi {
|
|
return &sAgentTApi{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterAgentTApi(NewAgentTApi())
|
|
}
|
|
|
|
// Model API集成ORM模型
|
|
func (s *sAgentTApi) 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.TApi.Ctx(ctx), option...)
|
|
}
|
|
|
|
// List 获取API集成列表
|
|
func (s *sAgentTApi) List(ctx context.Context, in *model.TApiListParam) (list []*model.TApiListModel, totalCount int, err error) {
|
|
mod := s.Model(ctx)
|
|
tApiTable := dao.TApi.Table()
|
|
sysDeptTable := dao.SysDept.Table()
|
|
|
|
// 字段过滤
|
|
mod = mod.FieldsPrefix(tApiTable, model.TApiListModel{})
|
|
mod = mod.Fields(
|
|
sysDeptTable+"."+dao.SysDept.Columns().DeptKey+" AS deptKey",
|
|
sysDeptTable+"."+dao.SysDept.Columns().DeptName+" AS deptName",
|
|
)
|
|
mod = mod.LeftJoin(
|
|
sysDeptTable,
|
|
sysDeptTable+"."+dao.SysDept.Columns().DeptId+"="+tApiTable+"."+dao.TApi.Columns().DeptId,
|
|
)
|
|
|
|
// 查询APPKey
|
|
if in.AppKey != "" {
|
|
mod = mod.Where(dao.TApi.Columns().AppKey+" like ?", "%"+in.AppKey+"%")
|
|
}
|
|
|
|
// 分页
|
|
mod = mod.Page(in.Page, in.PageSize)
|
|
|
|
// 排序
|
|
mod = mod.OrderDesc(dao.TApi.Columns().Id)
|
|
|
|
// 操作人摘要信息
|
|
mod = mod.Hook(hook.MemberSummary)
|
|
|
|
// 查询数据
|
|
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
|
err = gerror.Wrap(err, "获取API集成列表失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// Export 导出API集成(异步:提交任务后到下载中心取文件)
|
|
func (s *sAgentTApi) Export(ctx context.Context, in *model.TApiListParam) (err error) {
|
|
_, err = exportcenter.Submit(ctx, &exportcenter.Request{
|
|
BizKey: "tApi",
|
|
BizName: "API集成",
|
|
PermCode: "cpm:agent:tApi:export",
|
|
QueryParam: in,
|
|
Tags: func() ([]string, error) {
|
|
return convert.GetEntityDescTags(model.TApiListModel{})
|
|
},
|
|
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 修改/新增API集成
|
|
func (s *sAgentTApi) Edit(ctx context.Context, in *model.TApiEditParam) (err error) {
|
|
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
|
|
|
// 修改
|
|
if in.Id > 0 {
|
|
userId := contexts.GetUserId(ctx)
|
|
tenantId := contexts.GetTenantId(ctx)
|
|
in.TenantId = &tenantId
|
|
in.UpdatedBy = &userId
|
|
in.UpdatedAt = gtime.Now()
|
|
if _, err = s.Model(ctx).
|
|
Fields(model.TApiUpdateFields{}).
|
|
WherePri(in.Id).Data(in).Update(); err != nil {
|
|
err = gerror.Wrap(err, "修改API集成失败,请稍后重试!")
|
|
}
|
|
return
|
|
}
|
|
|
|
// 新增
|
|
tenantId := contexts.GetTenantId(ctx)
|
|
in.TenantId = &tenantId
|
|
deptId := contexts.GetDeptId(ctx)
|
|
in.CreatedDept = &deptId
|
|
userId := contexts.GetUserId(ctx)
|
|
in.CreatedBy = &userId
|
|
in.CreatedAt = gtime.Now()
|
|
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
|
|
Fields(model.TApiInsertFields{}).
|
|
Data(in).Insert(); err != nil {
|
|
err = gerror.Wrap(err, "新增API集成失败,请稍后重试!")
|
|
}
|
|
return
|
|
})
|
|
}
|
|
|
|
// Delete 删除API集成
|
|
func (s *sAgentTApi) Delete(ctx context.Context, in *model.TApiDeleteParam) (err error) {
|
|
|
|
if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
|
|
err = gerror.Wrap(err, "删除API集成失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// View 获取API集成指定信息
|
|
func (s *sAgentTApi) View(ctx context.Context, in *model.TApiViewParam) (res *model.TApiViewModel, err error) {
|
|
if err = s.Model(ctx).Where(dao.TApi.Columns().AppKey, in.AppKey).Hook(hook.MemberSummary).Scan(&res); err != nil {
|
|
err = gerror.Wrap(err, "获取API集成信息,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|