240 lines
7.3 KiB
Go
240 lines
7.3 KiB
Go
package dict
|
|
|
|
import (
|
|
consts1 "cid/consts/api-feature"
|
|
consts "cid/consts/public"
|
|
dto "cid/model/dto/dict"
|
|
entity "cid/model/entity/dict"
|
|
"context"
|
|
"strconv"
|
|
"time"
|
|
|
|
"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 DatasourcePlatform = new(datasourcePlatformDao)
|
|
|
|
type datasourcePlatformDao struct{}
|
|
|
|
// Insert 插入数据源平台
|
|
func (d *datasourcePlatformDao) Insert(ctx context.Context, req *dto.CreateDatasourcePlatformReq) (ID int64, err error) {
|
|
var res *entity.DatasourcePlatform
|
|
if err = gconv.Struct(req, &res); err != nil {
|
|
return
|
|
}
|
|
// 设置创建时间
|
|
// 设置创建时间和更新时间
|
|
now := time.Now()
|
|
res.CreatedAt = &now
|
|
res.UpdatedAt = &now
|
|
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).Data(&res).Insert()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
// Update 更新数据源平台
|
|
func (d *datasourcePlatformDao) Update(ctx context.Context, req *dto.UpdateDatasourcePlatformReq) (rows int64, err error) {
|
|
// 设置更新时间
|
|
data := gconv.Map(req)
|
|
data[entity.DatasourcePlatformCols.UpdatedAt] = time.Now()
|
|
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).
|
|
Data(data).
|
|
OmitEmpty().
|
|
Where(entity.DatasourcePlatformCols.ID, req.Id).
|
|
Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// Delete 删除数据源平台
|
|
func (d *datasourcePlatformDao) Delete(ctx context.Context, req *dto.DeleteDatasourcePlatformReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).
|
|
Where(entity.DatasourcePlatformCols.ID, req.Id).
|
|
Delete()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// GetOne 获取单个数据源平台
|
|
func (d *datasourcePlatformDao) GetOne(ctx context.Context, req *dto.GetDatasourcePlatformReq) (res *entity.DatasourcePlatform, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).
|
|
Where(entity.DatasourcePlatformCols.ID, req.Id).
|
|
One()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Struct(&res)
|
|
return
|
|
}
|
|
|
|
// GetByPlatformCode 根据平台编码获取数据源平台
|
|
func (d *datasourcePlatformDao) GetByPlatformCode(ctx context.Context, platformCode string) (res *entity.DatasourcePlatform, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).
|
|
Where(entity.DatasourcePlatformCols.PlatformCode, platformCode).
|
|
One()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Struct(&res)
|
|
return
|
|
}
|
|
|
|
// Count 获取数据源平台数量
|
|
func (d *datasourcePlatformDao) Count(ctx context.Context, req *dto.ListDatasourcePlatformReq) (count int, err error) {
|
|
return d.buildListFilter(ctx, req).Count()
|
|
}
|
|
|
|
// List 获取数据源平台列表
|
|
func (d *datasourcePlatformDao) List(ctx context.Context, req *dto.ListDatasourcePlatformReq) (res []entity.DatasourcePlatform, total int, err error) {
|
|
model := d.buildListFilter(ctx, req)
|
|
model.OrderDesc(entity.DatasourcePlatformCols.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 *datasourcePlatformDao) buildListFilter(ctx context.Context, req *dto.ListDatasourcePlatformReq) *gdb.Model {
|
|
model := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).Model
|
|
|
|
// 关键字搜索(平台名称或编码)
|
|
if !g.IsEmpty(req.Keyword) {
|
|
model.WhereLike(entity.DatasourcePlatformCols.PlatformName, "%"+req.Keyword+"%")
|
|
model.WhereOrLike(entity.DatasourcePlatformCols.PlatformCode, "%"+req.Keyword+"%")
|
|
model.WhereOrLike(entity.DatasourcePlatformCols.Description, "%"+req.Keyword+"%")
|
|
}
|
|
|
|
// 精确匹配条件
|
|
if !g.IsEmpty(req.PlatformCode) {
|
|
model.Where(entity.DatasourcePlatformCols.PlatformCode, req.PlatformCode)
|
|
}
|
|
if !g.IsEmpty(req.PlatformName) {
|
|
model.Where(entity.DatasourcePlatformCols.PlatformName, req.PlatformName)
|
|
}
|
|
if !g.IsEmpty(req.Status) {
|
|
model.Where(entity.DatasourcePlatformCols.Status, req.Status)
|
|
}
|
|
if !g.IsEmpty(req.AuthType) {
|
|
model.Where(entity.DatasourcePlatformCols.AuthType, req.AuthType)
|
|
}
|
|
|
|
model.OmitEmptyWhere()
|
|
return model
|
|
}
|
|
|
|
// UpdateStatus 更新数据源平台状态
|
|
func (d *datasourcePlatformDao) UpdateStatus(ctx context.Context, ID int64, status string, updatedBy string) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).
|
|
Data(map[string]interface{}{
|
|
entity.DatasourcePlatformCols.Status: status,
|
|
entity.DatasourcePlatformCols.UpdatedBy: updatedBy,
|
|
entity.DatasourcePlatformCols.UpdatedAt: time.Now(),
|
|
}).
|
|
Where(entity.DatasourcePlatformCols.ID, ID).
|
|
Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// ExistsByPlatformCode 检查平台编码是否存在
|
|
func (d *datasourcePlatformDao) ExistsByPlatformCode(ctx context.Context, platformCode string, excludeId ...int64) (exists bool, err error) {
|
|
model := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).
|
|
Where(entity.DatasourcePlatformCols.PlatformCode, platformCode)
|
|
|
|
if len(excludeId) > 0 && excludeId[0] > 0 {
|
|
model.WhereNot(entity.DatasourcePlatformCols.ID, excludeId[0])
|
|
}
|
|
|
|
count, err := model.Count()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
// ListActivePlatforms 获取所有启用的平台
|
|
func (d *datasourcePlatformDao) ListActivePlatforms(ctx context.Context) (res []entity.DatasourcePlatform, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).
|
|
Where(entity.DatasourcePlatformCols.Status, consts1.PlatformStatusActive).
|
|
OrderAsc(entity.DatasourcePlatformCols.PlatformName).
|
|
All()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Structs(&res)
|
|
return
|
|
}
|
|
|
|
// GetPlatformStatistics 获取平台统计信息
|
|
func (d *datasourcePlatformDao) GetPlatformStatistics(ctx context.Context) (stats map[string]int64, err error) {
|
|
stats = make(map[string]int64)
|
|
|
|
// 总平台数
|
|
total, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).Count()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stats["totalPlatforms"] = int64(total)
|
|
|
|
// 启用平台数
|
|
active, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).
|
|
Where(entity.DatasourcePlatformCols.Status, consts1.MappingStatusActive).
|
|
Count()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stats["activePlatforms"] = int64(active)
|
|
|
|
// 停用平台数
|
|
stats["inactivePlatforms"] = int64(total - active)
|
|
|
|
// 按认证类型统计
|
|
authTypes := []string{"TOKEN", "API_KEY", "OAUTH2", "BASIC"}
|
|
for _, authType := range authTypes {
|
|
count, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).
|
|
Where(entity.DatasourcePlatformCols.AuthType, authType).
|
|
Count()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stats[authType+"AuthPlatforms"] = int64(count)
|
|
}
|
|
|
|
return stats, nil
|
|
}
|
|
|
|
// BatchUpdateStatus 批量更新平台状态
|
|
func (d *datasourcePlatformDao) BatchUpdateStatus(ctx context.Context, ids []int64, status string, updatedBy string) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DatasourcePlatformTable).
|
|
Data(map[string]interface{}{
|
|
entity.DatasourcePlatformCols.Status: status,
|
|
entity.DatasourcePlatformCols.UpdatedBy: updatedBy,
|
|
entity.DatasourcePlatformCols.UpdatedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
|
}).
|
|
WhereIn(entity.DatasourcePlatformCols.ID, ids).
|
|
Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|