275 lines
8.8 KiB
Go
275 lines
8.8 KiB
Go
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
|
|
}
|