代码初始化
This commit is contained in:
239
dao/dict/api_datasource_platform_dao.go
Normal file
239
dao/dict/api_datasource_platform_dao.go
Normal file
@@ -0,0 +1,239 @@
|
||||
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()
|
||||
}
|
||||
274
dao/dict/api_field_mapping_config_dao.go
Normal file
274
dao/dict/api_field_mapping_config_dao.go
Normal file
@@ -0,0 +1,274 @@
|
||||
package dict
|
||||
|
||||
import (
|
||||
consts "cid/consts/public"
|
||||
dto "cid/model/dto/dict"
|
||||
entity "cid/model/entity/dict"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"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 FieldMappingConfig = new(fieldMappingConfigDao)
|
||||
|
||||
type fieldMappingConfigDao struct{}
|
||||
|
||||
// Insert 插入字段映射配置
|
||||
func (d *fieldMappingConfigDao) Insert(ctx context.Context, req *dto.CreateFieldMappingConfigReq) (id int64, err error) {
|
||||
var config entity.FieldMappingConfig
|
||||
if err = gconv.Struct(req, &config); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
config.CreatedTime = now
|
||||
config.UpdatedTime = now
|
||||
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).Data(&config).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// Update 更新字段映射配置
|
||||
func (d *fieldMappingConfigDao) Update(ctx context.Context, req *dto.UpdateFieldMappingConfigReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).Data(&req).OmitEmpty().Where(entity.FieldMappingConfigCol.Id, req.Id).Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// Delete 删除字段映射配置
|
||||
func (d *fieldMappingConfigDao) Delete(ctx context.Context, req *dto.DeleteFieldMappingConfigReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).
|
||||
Where(entity.FieldMappingConfigCol.Id, req.Id).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetOne 获取单个字段映射配置
|
||||
func (d *fieldMappingConfigDao) GetOne(ctx context.Context, req *dto.GetFieldMappingConfigReq) (res *entity.FieldMappingConfig, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).
|
||||
Where(entity.FieldMappingConfigCol.Id, req.Id).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 获取字段映射配置数量
|
||||
func (d *fieldMappingConfigDao) Count(ctx context.Context, req *dto.ListFieldMappingConfigReq) (count int, err error) {
|
||||
return d.buildListFilter(ctx, req).Count()
|
||||
}
|
||||
|
||||
// List 获取字段映射配置列表
|
||||
func (d *fieldMappingConfigDao) List(ctx context.Context, req *dto.ListFieldMappingConfigReq) (res []entity.FieldMappingConfig, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req)
|
||||
model.OrderDesc(entity.FieldMappingConfigCol.CreatedTime)
|
||||
|
||||
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 *fieldMappingConfigDao) buildListFilter(ctx context.Context, req *dto.ListFieldMappingConfigReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).Model
|
||||
|
||||
if !g.IsEmpty(req.Keyword) {
|
||||
model.WhereLike(entity.FieldMappingConfigCol.ConfigName, "%"+req.Keyword+"%").
|
||||
WhereOrLike(entity.FieldMappingConfigCol.SourceFieldDesc, "%"+req.Keyword+"%").
|
||||
WhereOrLike(entity.FieldMappingConfigCol.TargetFieldDesc, "%"+req.Keyword+"%")
|
||||
}
|
||||
|
||||
if !g.IsEmpty(req.ConfigName) {
|
||||
model.Where(entity.FieldMappingConfigCol.ConfigName, req.ConfigName)
|
||||
}
|
||||
if !g.IsEmpty(req.VendorName) {
|
||||
model.Where(entity.FieldMappingConfigCol.VendorName, req.VendorName)
|
||||
}
|
||||
if !g.IsEmpty(req.ApiName) {
|
||||
model.Where(entity.FieldMappingConfigCol.ApiName, req.ApiName)
|
||||
}
|
||||
if !g.IsEmpty(req.ApiVersion) {
|
||||
model.Where(entity.FieldMappingConfigCol.ApiVersion, req.ApiVersion)
|
||||
}
|
||||
if !g.IsEmpty(req.SourceField) {
|
||||
model.Where(entity.FieldMappingConfigCol.SourceField, req.SourceField)
|
||||
}
|
||||
if !g.IsEmpty(req.TargetField) {
|
||||
model.Where(entity.FieldMappingConfigCol.TargetField, req.TargetField)
|
||||
}
|
||||
if !g.IsEmpty(req.TransformType) {
|
||||
model.Where(entity.FieldMappingConfigCol.TransformType, req.TransformType)
|
||||
}
|
||||
if !g.IsEmpty(req.BusinessDomain) {
|
||||
model.Where(entity.FieldMappingConfigCol.BusinessDomain, req.BusinessDomain)
|
||||
}
|
||||
if !g.IsEmpty(req.FieldGroup) {
|
||||
model.Where(entity.FieldMappingConfigCol.FieldGroup, req.FieldGroup)
|
||||
}
|
||||
if req.IsActive != nil {
|
||||
model.Where(entity.FieldMappingConfigCol.IsActive, *req.IsActive)
|
||||
}
|
||||
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
|
||||
// UpdateStatus 更新配置状态
|
||||
func (d *fieldMappingConfigDao) UpdateStatus(ctx context.Context, id int64, isActive bool) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).
|
||||
Data(g.Map{
|
||||
"is_active": isActive,
|
||||
"updated_time": time.Now(),
|
||||
}).
|
||||
Where(entity.FieldMappingConfigCol.Id, id).
|
||||
Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetByIds 根据ID列表获取配置列表
|
||||
func (d *fieldMappingConfigDao) GetByIds(ctx context.Context, ids []int64) (res []entity.FieldMappingConfig, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).
|
||||
WhereIn(entity.FieldMappingConfigCol.Id, ids).
|
||||
All()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByVendorAndApi 根据厂商和接口获取字段映射配置
|
||||
func (d *fieldMappingConfigDao) GetByVendorAndApi(ctx context.Context, vendorName, apiName, apiVersion string, isActive *bool) (res []*entity.FieldMappingConfig, err error) {
|
||||
model := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).
|
||||
Where(entity.FieldMappingConfigCol.VendorName, vendorName).
|
||||
Where(entity.FieldMappingConfigCol.ApiName, apiName)
|
||||
|
||||
if !g.IsEmpty(apiVersion) {
|
||||
model.Where(entity.FieldMappingConfigCol.ApiVersion, apiVersion)
|
||||
}
|
||||
|
||||
if isActive != nil {
|
||||
model.Where(entity.FieldMappingConfigCol.IsActive, *isActive)
|
||||
}
|
||||
|
||||
model.OrderDesc(entity.FieldMappingConfigCol.Priority).
|
||||
OrderAsc(entity.FieldMappingConfigCol.Id)
|
||||
|
||||
r, err := model.All()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// CheckDuplicate 检查重复配置
|
||||
func (d *fieldMappingConfigDao) CheckDuplicate(ctx context.Context, vendorName, apiName, sourceField, targetField string, excludeId int64) (exists bool, err error) {
|
||||
ctx = context.WithValue(ctx, "user", &beans.User{UserName: "admin"})
|
||||
model := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).
|
||||
Where(entity.FieldMappingConfigCol.VendorName, vendorName).
|
||||
Where(entity.FieldMappingConfigCol.ApiName, apiName).
|
||||
Where(entity.FieldMappingConfigCol.SourceField, sourceField).
|
||||
Where(entity.FieldMappingConfigCol.TargetField, targetField)
|
||||
|
||||
if excludeId > 0 {
|
||||
model.WhereNot(entity.FieldMappingConfigCol.Id, excludeId)
|
||||
}
|
||||
|
||||
count, err := model.Count()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
exists = count > 0
|
||||
return
|
||||
}
|
||||
|
||||
// GetActiveConfigsByBusinessDomain 根据业务域获取启用的配置
|
||||
func (d *fieldMappingConfigDao) GetActiveConfigsByBusinessDomain(ctx context.Context, businessDomain string) (res []entity.FieldMappingConfig, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).
|
||||
Where(entity.FieldMappingConfigCol.BusinessDomain, businessDomain).
|
||||
Where(entity.FieldMappingConfigCol.IsActive, true).
|
||||
OrderDesc(entity.FieldMappingConfigCol.Priority).
|
||||
OrderAsc(entity.FieldMappingConfigCol.Id).
|
||||
All()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// GetFieldGroupsByVendorApi 获取指定厂商接口的字段分组
|
||||
func (d *fieldMappingConfigDao) GetFieldGroupsByVendorApi(ctx context.Context, vendorName, apiName string) (groups []string, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).
|
||||
Fields(entity.FieldMappingConfigCol.FieldGroup).
|
||||
Where(entity.FieldMappingConfigCol.VendorName, vendorName).
|
||||
Where(entity.FieldMappingConfigCol.ApiName, apiName).
|
||||
Where(entity.FieldMappingConfigCol.IsActive, true).
|
||||
Group(entity.FieldMappingConfigCol.FieldGroup).
|
||||
All()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, record := range r {
|
||||
group := record.Map()[entity.FieldMappingConfigCol.FieldGroup]
|
||||
if groupStr, ok := group.(string); ok && groupStr != "" {
|
||||
groups = append(groups, groupStr)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteExpiredConfigs 删除已过期的配置
|
||||
func (d *fieldMappingConfigDao) DeleteExpiredConfigs(ctx context.Context) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).
|
||||
Where("expiry_date IS NOT NULL AND expiry_date < ?", time.Now()).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetConfigsEffectiveNow 获取当前生效的配置
|
||||
func (d *fieldMappingConfigDao) GetConfigsEffectiveNow(ctx context.Context) (res []entity.FieldMappingConfig, err error) {
|
||||
now := time.Now()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.FieldMappingConfigTable).
|
||||
Where(entity.FieldMappingConfigCol.IsActive, true).
|
||||
Where("(effective_date IS NULL OR effective_date <= ?)", now).
|
||||
Where("(expiry_date IS NULL OR expiry_date > ?)", now).
|
||||
OrderDesc(entity.FieldMappingConfigCol.Priority).
|
||||
OrderAsc(entity.FieldMappingConfigCol.Id).
|
||||
All()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
118
dao/dict/api_interface_dao.go
Normal file
118
dao/dict/api_interface_dao.go
Normal file
@@ -0,0 +1,118 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user