139 lines
4.4 KiB
Go
139 lines
4.4 KiB
Go
package mapping
|
||
|
||
import (
|
||
consts "cid/consts/public"
|
||
dto "cid/model/dto/mapping"
|
||
entity "cid/model/entity/mapping"
|
||
"context"
|
||
|
||
"gitea.com/red-future/common/db/gfdb"
|
||
"github.com/gogf/gf/v2/database/gdb"
|
||
"github.com/gogf/gf/v2/util/gconv"
|
||
)
|
||
|
||
var DataMapping = new(dataMappingDao)
|
||
|
||
type dataMappingDao struct{}
|
||
|
||
// Insert 插入数据映射
|
||
func (d *dataMappingDao) Insert(ctx context.Context, req *dto.CreateDataMappingReq) (id int64, err error) {
|
||
var res *entity.DataMapping
|
||
if err = gconv.Struct(req, &res); err != nil {
|
||
return
|
||
}
|
||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Data(&res).Insert()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.LastInsertId()
|
||
}
|
||
|
||
// BatchInsert 批量插入数据映射
|
||
func (d *dataMappingDao) BatchInsert(ctx context.Context, mappings []entity.DataMapping) (rows int64, err error) {
|
||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Data(&mappings).Insert()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.RowsAffected()
|
||
}
|
||
|
||
// Update 更新数据映射
|
||
func (d *dataMappingDao) Update(ctx context.Context, req *dto.UpdateDataMappingReq) (rows int64, err error) {
|
||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Data(&req).OmitEmpty().Where(entity.DataMappingCols.Id, req.Id).Update()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.RowsAffected()
|
||
}
|
||
|
||
// Delete 删除数据映射
|
||
func (d *dataMappingDao) Delete(ctx context.Context, req *dto.DeleteDataMappingReq) (rows int64, err error) {
|
||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Where(entity.DataMappingCols.Id, req.Id).Delete()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.RowsAffected()
|
||
}
|
||
|
||
// GetOne 获取单个数据映射
|
||
func (d *dataMappingDao) GetOne(ctx context.Context, req *dto.GetDataMappingReq) (res *entity.DataMapping, err error) {
|
||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Where(entity.DataMappingCols.Id, req.Id).One()
|
||
if err != nil {
|
||
return
|
||
}
|
||
err = r.Struct(&res)
|
||
return
|
||
}
|
||
|
||
// Count 获取数据映射数量
|
||
func (d *dataMappingDao) Count(ctx context.Context, req *dto.ListDataMappingReq) (count int, err error) {
|
||
return d.buildListFilter(ctx, req).Count()
|
||
}
|
||
|
||
// List 获取数据映射列表
|
||
func (d *dataMappingDao) List(ctx context.Context, req *dto.ListDataMappingReq) (res []entity.DataMapping, total int, err error) {
|
||
model := d.buildListFilter(ctx, req)
|
||
model.OrderAsc(entity.DataMappingCols.Priority)
|
||
model.OrderDesc(entity.DataMappingCols.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 *dataMappingDao) buildListFilter(ctx context.Context, req *dto.ListDataMappingReq) *gdb.Model {
|
||
model := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).Model
|
||
model.Where(entity.DataMappingCols.PlatformId, req.PlatformId)
|
||
model.Where(entity.DataMappingCols.InterfaceId, req.InterfaceId)
|
||
model.Where(entity.DataMappingCols.SourceField, req.SourceField)
|
||
model.Where(entity.DataMappingCols.TargetField, req.TargetField)
|
||
model.Where(entity.DataMappingCols.Status, req.Status)
|
||
model.OmitEmptyWhere()
|
||
return model
|
||
}
|
||
|
||
// GetByInterfaceId 根据接口ID获取映射规则列表(按优先级排序)
|
||
func (d *dataMappingDao) GetByInterfaceId(ctx context.Context, interfaceId int64) (res []entity.DataMapping, err error) {
|
||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).
|
||
Where(entity.DataMappingCols.InterfaceId, interfaceId).
|
||
Where("status", "active").
|
||
OrderAsc(entity.DataMappingCols.Priority).
|
||
All()
|
||
if err != nil {
|
||
return
|
||
}
|
||
err = r.Structs(&res)
|
||
return
|
||
}
|
||
|
||
// GetByInterfaceIdAndTargetField 根据接口ID和目标字段获取映射规则
|
||
func (d *dataMappingDao) GetByInterfaceIdAndTargetField(ctx context.Context, interfaceId int64, targetField string) (res *entity.DataMapping, err error) {
|
||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).
|
||
Where(entity.DataMappingCols.InterfaceId, interfaceId).
|
||
Where(entity.DataMappingCols.TargetField, targetField).
|
||
Where("status", "active").
|
||
One()
|
||
if err != nil {
|
||
return
|
||
}
|
||
err = r.Struct(&res)
|
||
return
|
||
}
|
||
|
||
// DeleteByInterfaceId 根据接口ID删除所有映射规则
|
||
func (d *dataMappingDao) DeleteByInterfaceId(ctx context.Context, interfaceId int64) (rows int64, err error) {
|
||
r, err := gfdb.DB(ctx).Model(ctx, consts.DataMappingTable).
|
||
Where(entity.DataMappingCols.InterfaceId, interfaceId).
|
||
Delete()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.RowsAffected()
|
||
}
|