作者 yangfu

refactor: code optimize

package domain
type ByteLibService interface {
// LoadDataTable 加载表数据
LoadDataTable(param ReqLoadDataTable) (*DataLoadDataTable, error)
// EditTable 编辑表结构(列重名,类型修改,列拆分 ...)
EditTable(param ReqEditDataTable) (*DataEditDataTable, error)
// SaveTable 编辑后进行表结构保存(底层生成一份新的excel文件)
SaveTable(param ReqSaveTable) (*DataSaveTable, error)
// GenerateTable 生成表 (主表)
GenerateTable(ctx *Context, param ReqGenerateTable) (*DataGenerateTable, error)
// CopyTable 复制表
CopyTable(param ReqCopyTable) (*DataCopyTable, error)
// AppendData 追加数据到指定表
AppendData(param ReqAppendData) (*DataAppendData, error)
// SplitTable 拆分表 (分表)
SplitTable(param ReqSplitTable) (*DataSplitTable, error)
// DeleteTable 删除表
DeleteTable(param ReqDeleteTable) (*DataDeleteTable, error)
// CancelFile 取消对表的操作
CancelFile(param ReqCancelFile) (*DataCancelFile, error)
// EditTableData 编辑表数据
EditTableData(param ReqEditTableData) (*DataEditTableData, error)
// FieldOptionalValues 字段可选值
FieldOptionalValues(param ReqFieldOptionalValues) (*DataFieldOptionalValues, error)
// FormulasGenerate 表达式生成 (底层生成视图)
FormulasGenerate(param ReqFormulasGenerate) (*DataFormulasGenerate, error)
// FormulasClear 表达式清除
FormulasClear(param ReqFormulasClear) (*DataFormulasClear, error)
}
... ...
package domain
// FileTableService 文件表服务(导入模块)
type FileTableService interface {
// Preview 预览(Excel文件)
Preview(ctx *Context, fileId int, fields []*Field, where Where) (interface{}, error)
// Edit 编辑表
Edit()
// Flush 保存编辑状态的表元数据
Flush(ctx *Context, fileId int, table *Table) (interface{}, error)
// DeleteFiles 批量删除文件
DeleteFiles(ctx *Context, files ...*File) error
// GenerateTable 生成主表
GenerateTable(ctx *Context, fileId int, tableName string) (interface{}, error)
}
// TableService 表服务
type TableService interface {
// Preview 预览
Preview(ctx *Context, tableId int, where Where) (interface{}, error)
// CopyTable 拷贝表
CopyTable(ctx *Context, tableId int, tableName string) (interface{}, error)
// DeleteTables 删除表
DeleteTables(ctx *Context, tables ...*Table) error
// AppendData 追加表数据
AppendData(ctx *Context, fileId int, tableId int, mappingFields []*MappingField) (interface{}, error)
// UpdateTableStruct 更新表结构(分表)
UpdateTableStruct(ctx *Context, tableId int, fields []*Field, name string) (interface{}, error)
// AddTableStruct 添加表结构(分表)
AddTableStruct(ctx *Context, parentTableId int, fields []*Field, name string) (interface{}, error)
}
// PreviewDataTableService 具体的表预览服务
type PreviewDataTableService interface {
// Preview 预览
Preview(ctx *Context, fileId int, fields []*Field, where Where) (interface{}, error)
// RePreview 重新预览
RePreview(ctx *Context, fileId int, fields []*Field, where Where) (interface{}, error)
// CreateTemporaryFile 创建临时文件(计算集修改的时候,先创建临时文件,保存成功后覆盖原文件)
CreateTemporaryFile(ctx *Context, fileId int) (*File, error)
GetFileId() int
}
// EditDataTableService 编辑表数据(副表)
type EditDataTableService interface {
Edit(ctx *Context, param EditTableRequest) (*DataEditDataTable, error)
}
// FlushDataTableService 保存编辑状态的表
type FlushDataTableService interface {
Flush(ctx *Context, fileId int, table *Table) (interface{}, error)
}
// DeleteFileService 删除文件服务
type DeleteFileService interface {
DeleteFiles(ctx *Context, files ...*File) error
}
// GenerateMainTableService 生成主表服务
type GenerateMainTableService interface {
GenerateTable(ctx *Context, fileId int, tableName string) (interface{}, error)
}
// CopyDataTableService 拷贝表服务
type CopyDataTableService interface {
CopyTable(ctx *Context, tableId int, tableName string) (interface{}, error)
}
// DeleteDataTableService 删除表服务
type DeleteDataTableService interface {
DeleteTable(ctx *Context, tableId int) (interface{}, error)
DeleteTables(ctx *Context, tables ...*Table) error
}
// AppendDataToTableService 追加数据服务
type AppendDataToTableService interface {
AppendData(ctx *Context, fileId int, tableId int, mappingFields []*MappingField) (interface{}, error)
PreflightCheck(ctx *Context, fileId int, tableId int, mappingFields []*MappingField) (interface{}, error)
... ... @@ -56,10 +80,12 @@ type AppendDataToTableService interface {
/************************************/
// UpdateTableStructService 更新表结构服务(分表)
type UpdateTableStructService interface {
UpdateTableStruct(ctx *Context, tableId int, fields []*Field, name string) (interface{}, error)
}
// AddTableStructService 添加表结构服务(分表)
type AddTableStructService interface {
AddTableStruct(ctx *Context, parentTableId int, fields []*Field, name string) (*Table, error)
}
... ...
... ... @@ -20,6 +20,7 @@ const (
QuerySetDeleteEvent EventType = "table.query.set.delete" // 删除
)
// EventTable 表事件
type EventTable struct {
Context *Context
Type EventType
... ... @@ -39,6 +40,7 @@ func (et *EventTable) WithContext(t *Context) *EventTable {
et.Context = t
return et
}
func (et *EventTable) WithType(t EventType) *EventTable {
et.Type = t
return et
... ...
... ... @@ -14,6 +14,8 @@ type ByteCoreService struct {
TempDataTable map[int]*domain.DataLoadDataTable
}
// ByteCore 字库底层核心
// 主表、拆分、数据读取等功能
var ByteCore domain.ByteLibService = &WrapByteCoreService{} //&ByteCoreService{}
var _ domain.ByteLibService = (*ByteCoreService)(nil)
... ...
... ... @@ -7,6 +7,7 @@ import (
"time"
)
// AsyncEvent 同步事件
func AsyncEvent(e *domain.EventTable) {
go func() {
defer func() {
... ...
... ... @@ -14,7 +14,7 @@ type PreviewDataTableService struct {
transactionContext *pgTransaction.TransactionContext
}
// RePreview 重新预览
// RePreview 重新预览,表头重置时,前端调用重新预览接口,重置缓存的表头行号
func (ptr *PreviewDataTableService) RePreview(ctx *domain.Context, fileId int, fields []*domain.Field, where domain.Where) (interface{}, error) {
fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId})
... ...
... ... @@ -14,6 +14,7 @@ type PGLogService struct {
transactionContext *pgTransaction.TransactionContext
}
// NewPGLogService 服务操作日志记录
func NewPGLogService(transactionContext *pgTransaction.TransactionContext) (*PGLogService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ... @@ -24,6 +25,7 @@ func NewPGLogService(transactionContext *pgTransaction.TransactionContext) (*PGL
}
}
// FastLog 快速记录日志
func FastLog(transactionContext *pgTransaction.TransactionContext, logType domain.LogType, sourceId int, logEntry Log) error {
logService, _ := NewPGLogService(transactionContext)
return logService.Log(logType, sourceId, logEntry)
... ...
... ... @@ -21,6 +21,7 @@ var (
ErrFieldsNotMatch = func(table string) error { return fmt.Errorf("[%v]字段数量不一致或类型不匹配", table) }
)
// QuerySetService 查询集服务,对拆分、计算模块数据集合的处理
type QuerySetService struct {
transactionContext *pgTransaction.TransactionContext
}
... ...
... ... @@ -5,6 +5,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain/astexpr"
)
// LoadCalculateItemData 加载计算项数据,实时计算某个列的值
func (ptr *QuerySetService) LoadCalculateItemData(ctx *domain.Context, t *domain.Table, formula *domain.FieldFormulaExpr) (*domain.DataTable, error) {
var (
res = &domain.DataTable{}
... ...
... ... @@ -15,6 +15,7 @@ import (
const DefaultExpandNum = 1000
const MaxExpandNum = 5000
// LoadCalculateSetData 加载计算集数据,根据排列规则,动态进行集合数据排列,返回一个二维表
func (ptr *QuerySetService) LoadCalculateSetData(ctx *domain.Context, qs *domain.QuerySet, queryComponents []*domain.QueryComponent) (*domain.DataTable, error) {
var (
res = &domain.DataTable{}
... ...
... ... @@ -21,6 +21,7 @@ func NewAddTableStructService(transactionContext *pgTransaction.TransactionConte
}
}
// AddTableStruct 添加表结构(新建分表)
func (ptr *AddTableStructService) AddTableStruct(ctx *domain.Context, parentTableId int, fields []*domain.Field, name string) (*domain.Table, error) {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
mainTable, err := tableRepository.FindOne(map[string]interface{}{"tableId": parentTableId})
... ...
... ... @@ -43,6 +43,9 @@ func NewCircleDependError(t *domain.Table, qs *domain.QuerySet) CircleDependErro
}
}
// TableDependencyService 表依赖服务
// 查询表所有的依赖关系
// 判断是否循环依赖
type TableDependencyService struct {
transactionContext *pgTransaction.TransactionContext
DetectedCycleCallBack func()
... ...
... ... @@ -23,6 +23,7 @@ func NewTableEditDataService(transactionContext *pgTransaction.TransactionContex
}
}
// RowEdit 行数据编辑
func (ptr *TableEditDataService) RowEdit(ctx *domain.Context, request domain.EditDataRequest) (interface{}, error) {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
var table *domain.Table = request.Table
... ...
... ... @@ -24,6 +24,7 @@ func NewUpdateTableStructService(transactionContext *pgTransaction.TransactionCo
}
}
// UpdateTableStruct 更新表结构
func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tableId int, fields []*domain.Field, name string) (interface{}, error) {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
... ... @@ -83,6 +84,7 @@ func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tabl
return struct{}{}, nil
}
// DependencyTables 依赖的表列表
func DependencyTables(ptr *pgTransaction.TransactionContext, context *domain.Context, tableId int) ([]TableNode, error) {
ret := make([]TableNode, 0)
// tableId 相关联的
... ...
... ... @@ -14,9 +14,6 @@ func Insert(db *gorm.DB, tableName string, fields []*domain.FieldValue) error {
value := map[string]interface{}{}
for _, f := range fields {
if f.Field.Flag == domain.PKField && f.Value == "" {
//continue
//id, _ := uuid.NewUUID()
// idString := id.String()
// 需要调用分布式id生成
id, _ := utils.NewSnowflakeId()
idString := fmt.Sprintf("%d", id)
... ... @@ -24,8 +21,6 @@ func Insert(db *gorm.DB, tableName string, fields []*domain.FieldValue) error {
}
value[f.Field.SQLName] = f.TypeValue()
}
//tx := db.Table(tableName).Create(value)
//return tx.Error
sql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Table(tableName).Create(value)
})
... ... @@ -45,8 +40,6 @@ func Update(db *gorm.DB, tableName string, fields []*domain.FieldValue) error {
if pk == nil && pk.Value != "" && pk.TypeValue() != nil {
return fmt.Errorf("主键不能为空")
}
//tx := db.Table(tableName).Where("? = ?", pk.Field.SQLName, pk.Value).Updates(value)
//return tx.Error
sql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Table(tableName).Create(value)
})
... ... @@ -64,8 +57,6 @@ func Delete(db *gorm.DB, tableName string, fields []*domain.FieldValue) error {
if pk == nil {
return fmt.Errorf("主键不能为空")
}
//tx := db.Table(tableName).Delete("? = ?", pk.Field.SQLName, pk.Value)
//return tx.Error
sql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Table(tableName).Delete("? = ?", pk.Field.SQLName, pk.Value)
})
... ...
... ... @@ -222,9 +222,6 @@ func (c Condition) CastTypeByField(f *domain.Field, t string) string {
}
func formatFiled(f *domain.Field) string {
//if f.SQLType == domain.Float.ToString() || f.SQLType == domain.DECIMAL279.ToString() {
// return castTypeAlias(f.SQLName, domain.DECIMALV2.ToString())
//}
return f.SQLName
}
... ... @@ -346,15 +343,6 @@ func WrapDeleteFuncWithDB(db *gorm.DB) func(QueryOptions) (int64, error) {
return func(params QueryOptions) (int64, error) {
query := db.Table(params.TableName)
queryWithoutLimitOffset(query, params)
//if params.Offset > 0 {
// query.Offset(params.Offset)
//}
//if params.Limit > 0 {
// query.Limit(params.Limit)
//}
//if params.Context != nil {
// query.Where(fmt.Sprintf("context->>'companyId'='%v'", params.Context.CompanyId))
//}
rows, err := query.Rows()
defer rows.Close()
if err != nil {
... ...
... ... @@ -34,17 +34,6 @@ func Init() error {
}
func Test() {
//var result [][]interface{}
//query := DB.Raw("select * from sales_performance_area_belongs").Select(&result)
//if query.Error != nil {
// log.Logger.Error(query.Error.Error())
//}
//if len(result) > 0 {
//
//}
//rows, err := DB.Raw("select * from sales_performance_area_belongs").Rows()
rows, err := DB.Table("sales_performance_area_categorys").Rows()
if err != nil {
log.Logger.Error(err.Error())
... ...