作者 陈志颖

refactor:优化订单导入

... ... @@ -370,12 +370,14 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
"cooperationContractNumber": "项目合约编号",
}
// 空文件校验
/************************************************ 1.空文件校验 ******************************************************/
if len(importDividendsOrderCommand.DividendsOrderData) == 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "导入的Excel文件为空文件,请上传正确的文件")
}
/******************************************************************************************************************/
// 必填项校验
/************************************************ 2.必填项校验 ******************************************************/
// 空单元格错误
nullCellError := make([]*domain.FailInfo, 0)
// 数据行计数
... ... @@ -419,15 +421,16 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
},
}, nil
}
/******************************************************************************************************************/
// 单元格类型校验
typeError := make([]interface{}, 0)
for i, dividendsOrder := range importDividendsOrderCommand.DividendsOrderData { // 行
/***************************************************** 3.单元格类型校验 *********************************************/
typeError := make([]*domain.FailInfo, 0)
for _, dividendsOrder := range importDividendsOrderCommand.DividendsOrderData { // 行
var myRow []*domain.FailInfo
t := reflect.TypeOf(dividendsOrder)
v := reflect.ValueOf(dividendsOrder)
for k := 0; k < t.NumField(); k++ { // 列
r := strconv.Itoa(i + 1)
//r := strconv.Itoa(i + 1)
switch k {
case 3: // 订单日期校验
{
... ... @@ -439,6 +442,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
myRow = append(myRow, tmpRow)
continue
}
}
case 5: // 产品数量校验
... ... @@ -451,6 +455,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
myRow = append(myRow, tmpRow)
continue
}
// 正负判断
if orderGoodQuantity < 0 {
... ... @@ -459,6 +464,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
myRow = append(myRow, tmpRow)
continue
}
}
case 6: // 产品价格校验
... ... @@ -471,16 +477,17 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
myRow = append(myRow, tmpRow)
continue
}
// 长度校验
if univalent >= 1e16 {
tmpRow := &domain.FailInfo{
FailReason: "第" + r + "行产品价格超过最大限制,产品价格小数点前面不能超过十六位数字,并保留两位小数",
FailReason: "产品价格超过最大限制,产品价格小数点前面不能超过十六位数字,并保留两位小数",
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
// 错误行数据
myRow = append(myRow, tmpRow)
continue
}
}
case 7: // 费用校验
... ... @@ -490,28 +497,30 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
univalent, typeErr := strconv.ParseFloat(fmt.Sprintf("%v", v.Field(k).Interface()), 64)
if typeErr != nil {
tmpRow := &domain.FailInfo{
FailReason: "第" + r + "行费用格式错误,费用必须为数字类型",
FailReason: "费用格式错误,费用必须为数字类型",
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
myRow = append(myRow, tmpRow)
log.Logger.Info("费用类型错误", map[string]interface{}{
"tmpRow": tmpRow,
})
continue
}
// 长度校验
if univalent >= 1e16 {
tmpRow := &domain.FailInfo{
FailReason: "第" + r + "行费用超过最大限制,费用小数点前面不能超过十六位数字,并保留两位小数",
FailReason: "费用超过最大限制,费用小数点前面不能超过十六位数字,并保留两位小数",
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
myRow = append(myRow, tmpRow)
continue
}
}
}
}
}
if myRow != nil {
typeError = append(typeError, myRow)
if len(myRow) > 0 {
typeError = append(typeError, myRow...)
}
}
... ... @@ -525,11 +534,15 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
},
}, nil
}
/******************************************************************************************************************/
// 聚合订单并进行类型校验
/******************************************** 4.聚合订单并进行类型校验 ************************************************/
// 订单聚合错误
aggregateErrorList := make([]interface{}, 0)
// 新增订单命令集
var dividendsOrderCommands = make(map[string]*command.CreateDividendsOrderCommand)
for i, dividendsOrder := range importDividendsOrderCommand.DividendsOrderData {
hashValue := md5.Sum([]byte(dividendsOrder.OriginalOrderNum))
hashString := hex.EncodeToString(hashValue[:])
... ... @@ -542,22 +555,37 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
aggregateErrorList = append(aggregateErrorList, tmpRow)
break
continue
}
// 产品相关:产品名称,产品数量、产品价格、费用
quantity, err := strconv.ParseFloat(dividendsOrder.OrderGoodQuantity, 64)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
tmpRow := &domain.FailInfo{
FailReason: "产品数量类型错误",
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
aggregateErrorList = append(aggregateErrorList, tmpRow)
continue
}
price, err := strconv.ParseFloat(dividendsOrder.OrderGoodPrice, 64)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
tmpRow := &domain.FailInfo{
FailReason: "产品价格类型错误",
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
aggregateErrorList = append(aggregateErrorList, tmpRow)
continue
}
var expense float64
if dividendsOrder.Expense != "" {
if expenseParse, err := strconv.ParseFloat(dividendsOrder.Expense, 64); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
tmpRow := &domain.FailInfo{
FailReason: "费用类型错误",
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
aggregateErrorList = append(aggregateErrorList, tmpRow)
continue
} else {
expense = expenseParse
}
... ... @@ -593,16 +621,31 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
// 产品相关:产品名称,产品数量、产品价格、费用
quantity, err := strconv.ParseFloat(dividendsOrder.OrderGoodQuantity, 64)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
tmpRow := &domain.FailInfo{
FailReason: "产品数量类型错误",
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
aggregateErrorList = append(aggregateErrorList, tmpRow)
continue
}
price, err := strconv.ParseFloat(dividendsOrder.OrderGoodPrice, 64)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
tmpRow := &domain.FailInfo{
FailReason: "产品价格类型错误",
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
aggregateErrorList = append(aggregateErrorList, tmpRow)
continue
}
var expense float64
if dividendsOrder.Expense != "" {
if expenseParse, err := strconv.ParseFloat(dividendsOrder.Expense, 64); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
tmpRow := &domain.FailInfo{
FailReason: "费用类型错误",
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
aggregateErrorList = append(aggregateErrorList, tmpRow)
continue
} else {
expense = expenseParse
}
... ... @@ -634,22 +677,20 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
},
}, nil
}
/******************************************************************************************************************/
// 错误数据返回
errorDataList := make([]*domain.ImportInfo, 0)
// 批量创建分红订单命令集
/******************************************** 批量创建分红订单命令集 **************************************************/
var createDividendsOrderCommands []*command.CreateDividendsOrderCommand
for _, dividendsOrderCommand := range dividendsOrderCommands {
createDividendsOrderCommands = append(createDividendsOrderCommands, dividendsOrderCommand)
}
// 新增失败记录
failureDataList := make([]interface{}, 0)
// 新增成功记录计数
var successDataCount int64
// 错误数据返回
var errorDataList []*domain.ImportInfo
// 循环校验命令
for _, cmd := range createDividendsOrderCommands {
if err = cmd.ValidateCommand(); err != nil {
... ... @@ -664,6 +705,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
}
// 新增失败记录
failureDataList := make([]interface{}, 0)
// 循环校验命令失败返回
if len(errorDataList) > 0 {
successDataCount = 0
... ... @@ -695,8 +739,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
},
}, nil
}
/******************************************************************************************************************/
// 创建分红订单领域模型
/********************************************** 创建分红订单领域模型 *************************************************/
var creatDividendsOrder []*domain.DividendsOrder
// 统计当前分红订单数
... ... @@ -710,7 +755,13 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
// 生成分红订单号
dividendsOrderNumber, err := dividendsOrder.GenerateSpecificDividendsOrderNumber(int64(count), time.Time{}, int64(i))
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
row := &domain.ImportInfo{
Error: application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("分红订单号生成错误:%s", err)),
LineNumbers: dividendsOrder.LineNumbers, // 错误影响的行
GoodLine: map[int]interface{}{},
}
errorDataList = append(errorDataList, row)
continue
}
// 订单时间转换
... ... @@ -1048,6 +1099,8 @@ func (dividendsOrderService *DividendsOrderService) BatchRemoveDividendsOrder(ba
}
}
// TODO 校验分红订单是否有分红预算
dividendsOrdersRemoved, err := dividendsOrderRepository.BatchRemove(dividendsOrders)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
... ...
... ... @@ -31,7 +31,7 @@ type OrderGood struct {
// 创建时间
CreatedAt time.Time `comment:"创建时间"`
// 删除时间
DeletedAt time.Time `comment:"删除时间"`
DeletedAt time.Time `comment:"删除时间" pg:",soft_delete"`
// 更新时间
UpdatedAt time.Time `comment:"更新时间"`
}
... ...