作者 陈志颖

合并分支 'dev' 到 'test'

Dev



查看合并请求 !37
... ... @@ -336,6 +336,10 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
}
log.Logger.Info("查找合约数据", map[string]interface{}{
"cooperationContractsMap": cooperationContractsMap,
})
// 分红订单DAO初始化
var dividendsOrderDao *dao.DividendsOrderDao
if value, err := factory.CreateDividendsOrderDao(map[string]interface{}{
... ... @@ -370,12 +374,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 +425,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 +446,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
myRow = append(myRow, tmpRow)
continue
}
}
case 5: // 产品数量校验
... ... @@ -451,6 +459,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
myRow = append(myRow, tmpRow)
continue
}
// 正负判断
if orderGoodQuantity < 0 {
... ... @@ -459,6 +468,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
tmpRow.ImportDividendsOrderData = &dividendsOrder
myRow = append(myRow, tmpRow)
continue
}
}
case 6: // 产品价格校验
... ... @@ -471,16 +481,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 +501,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 +538,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 +559,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
}
... ... @@ -581,6 +613,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
DividendsOrderNumber: "",
CooperationContractNumber: dividendsOrder.CooperationContractNumber,
OrderGoodExpense: expense,
LineNumber: i,
},
},
CompanyId: importDividendsOrderCommand.CompanyId,
... ... @@ -593,16 +626,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
}
... ... @@ -617,6 +665,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
DividendsOrderNumber: "",
CooperationContractNumber: dividendsOrder.CooperationContractNumber,
OrderGoodExpense: expense,
LineNumber: i, // 记录订单产品行号
})
// 记录聚合行号
... ... @@ -624,6 +673,10 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
}
log.Logger.Info("聚合订单", map[string]interface{}{
"dividendsOrderCommands": dividendsOrderCommands,
})
// 聚合订单错误返回
if len(aggregateErrorList) > 0 {
return map[string]interface{}{
... ... @@ -634,22 +687,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 {
... ... @@ -657,13 +708,16 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
row := &domain.ImportInfo{
Error: application.ThrowError(application.BUSINESS_ERROR, err.Error()), // 错误信息
LineNumbers: cmd.LineNumbers, // 错误影响的行
GoodLine: map[int]interface{}{},
GoodLine: map[int]string{},
}
errorDataList = append(errorDataList, row)
continue
}
}
// 新增失败记录
failureDataList := make([]interface{}, 0)
// 循环校验命令失败返回
if len(errorDataList) > 0 {
successDataCount = 0
... ... @@ -695,8 +749,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
},
}, nil
}
/******************************************************************************************************************/
// 创建分红订单领域模型
/********************************************** 创建分红订单领域模型 *************************************************/
var creatDividendsOrder []*domain.DividendsOrder
// 统计当前分红订单数
... ... @@ -710,7 +765,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]string{},
}
errorDataList = append(errorDataList, row)
continue
}
// 订单时间转换
... ... @@ -719,18 +780,19 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
row := &domain.ImportInfo{
Error: application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("订单日期转换错误:%s", err)),
LineNumbers: dividendsOrder.LineNumbers, // 错误影响的行
GoodLine: map[int]interface{}{},
GoodLine: map[int]string{},
}
errorDataList = append(errorDataList, row)
continue
}
orderTime := utils.TransformTimestampToTime(orderTimeInt)
// 新增订单产品
var orderGoods []*domain.OrderGood
var dividendsOrderAmount float64
orderGoodErrMap := make(map[int]interface{}, 0)
for i, orderGood := range dividendsOrder.OrderGoods {
orderGoodErrMap := make(map[int]string, 0)
for j, orderGood := range dividendsOrder.OrderGoods {
// 校验共创合约是否合法
contractNumberExist := false
for _, cooperationContract := range cooperationContracts {
... ... @@ -740,18 +802,24 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
}
if !contractNumberExist {
orderGoodErrMap[dividendsOrder.OrderGoods[i].LineNumber] = application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("共创合约编号不存在:%s", err))
orderGoodErrMap[dividendsOrder.OrderGoods[j].LineNumber] = "订单产品关联的共创合约不存在"
continue
}
// 计算产品金额
orderGoodAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(orderGood.OrderGoodQuantity)).Sub(decimal.NewFromFloat(orderGood.OrderGoodExpense)).Float64()
// 校验共创合约激励类型是否正确
ruleMatchedFlag := false
if orderGood.CooperationContractNumber != "" {
// 校验共创合约激励类型是否正确
log.Logger.Info("订单产品匹配的合约", map[string]interface{}{
"contractMatched": cooperationContractsMap[orderGood.CooperationContractNumber],
})
if cooperationContractsMap[orderGood.CooperationContractNumber] != nil && cooperationContractsMap[orderGood.CooperationContractNumber].IncentivesType != 1 {
orderGoodErrMap[dividendsOrder.OrderGoods[i].LineNumber] = application.ThrowError(application.INTERNAL_SERVER_ERROR, "退货单产品不能关联金额激励规则")
orderGoodErrMap[dividendsOrder.OrderGoods[j].LineNumber] = "分红订单不能关联金额激励规则"
continue
}
// 校验产品关联合约的激励规则是否匹配订单时间
if cooperationContractsMap[orderGood.CooperationContractNumber] != nil {
for _, incentivesRule := range cooperationContractsMap[orderGood.CooperationContractNumber].DividendsIncentivesRules {
... ... @@ -763,7 +831,8 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}
}
if !ruleMatchedFlag {
orderGoodErrMap[dividendsOrder.OrderGoods[i].LineNumber] = application.ThrowError(application.INTERNAL_SERVER_ERROR, "订单时间无法匹配分红激励规则,请重新选择")
orderGoodErrMap[dividendsOrder.OrderGoods[j].LineNumber] = "订单时间无法匹配分红激励规则"
continue
}
}
... ... @@ -787,9 +856,13 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
// 计算分红订单金额
dividendsOrderAmount, _ = decimal.NewFromFloat(dividendsOrderAmount).Add(decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(orderGood.OrderGoodQuantity).Sub(decimal.NewFromFloat(orderGood.OrderGoodExpense)))).Float64()
}
if len(orderGoodErrMap) > 0 {
log.Logger.Info("校验订单产品", map[string]interface{}{
"orderGoodErrMap": orderGoodErrMap,
})
row := &domain.ImportInfo{
Error: application.ThrowError(application.BUSINESS_ERROR, "共创合约不存在"),
Error: nil,
LineNumbers: dividendsOrder.LineNumbers, // 错误影响的行
GoodLine: orderGoodErrMap, // 错误产品行号记录
}
... ... @@ -823,8 +896,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
creatDividendsOrder = append(creatDividendsOrder, newDividendsOrder)
}
/******************************************************************************************************************/
// 错误报告处理
/*********************************************** 错误报告处理 *******************************************************/
if len(errorDataList) <= 0 { // 成功返回
if _, err2 := dividendsOrderRepository.SaveMany(creatDividendsOrder); err2 != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err2.Error())
... ... @@ -836,7 +910,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
if successDataCount == int64(rowCnt) {
return map[string]interface{}{
"successCount": successDataCount,
"fail": map[string]interface{}{},
"fail": make(map[string]interface{}),
}, nil
}
} else { // 失败返回
... ... @@ -846,7 +920,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
if len(errorData.GoodLine) == 0 { // 订单错误
for _, line := range errorData.LineNumbers {
tmpRow := &domain.FailInfo{
FailReason: "订单数据新增错误:" + errorData.Error.Error(),
FailReason: errorData.Error.Error(),
}
tmpRow.ImportDividendsOrderData = &importDividendsOrderCommand.DividendsOrderData[line]
failureDataList = append(failureDataList, tmpRow)
... ... @@ -854,7 +928,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
} else if len(errorData.GoodLine) > 0 { // 订单产品错误
for line := range errorData.GoodLine {
tmpRow := &domain.FailInfo{
FailReason: "订单数据新增错误:" + errorData.Error.Error(),
FailReason: errorData.GoodLine[line],
}
tmpRow.ImportDividendsOrderData = &importDividendsOrderCommand.DividendsOrderData[line]
failureDataList = append(failureDataList, tmpRow)
... ... @@ -1048,6 +1122,8 @@ func (dividendsOrderService *DividendsOrderService) BatchRemoveDividendsOrder(ba
}
}
// TODO 校验分红订单是否有分红预算
dividendsOrdersRemoved, err := dividendsOrderRepository.BatchRemove(dividendsOrders)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
... ...
... ... @@ -707,7 +707,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
row := &domain.ImportInfo{
Error: application.ThrowError(application.BUSINESS_ERROR, err.Error()), // 错误信息
LineNumbers: cmd.LineNumbers, // 错误影响的行
GoodLine: map[int]interface{}{},
GoodLine: map[int]string{},
}
errorDataList = append(errorDataList, row)
continue
... ... @@ -862,7 +862,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
row := &domain.ImportInfo{
Error: application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存退货单数据失败:%s", err)),
LineNumbers: dividendsReturnedOrder.LineNumbers, // 错误影响的行
GoodLine: map[int]interface{}{},
GoodLine: map[int]string{},
}
errorDataList = append(errorDataList, row)
continue
... ...
... ... @@ -53,9 +53,9 @@ type DividendsOrder struct {
// ImportInfo 导入错误信息
type ImportInfo struct {
Error error `json:"error"`
LineNumbers []int `json:"lineNumbers"`
GoodLine map[int]interface{} `json:"goodLine"`
Error error `json:"error"`
LineNumbers []int `json:"lineNumbers"`
GoodLine map[int]string `json:"goodLine"`
}
// FailInfo 返回的失败信息
... ...
... ... @@ -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:"更新时间"`
}
... ...