作者 陈志颖

fix:空现金池更新错误

... ... @@ -102,42 +102,22 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 不存在现金池
if count == 0 { // 新增现金池
newCashPool := &domain.CashPool{
CompanyId: createCashPoolCommand.CompanyId,
Cash: createCashPoolCommand.Cash,
ExchangedCash: 0,
UnExchangeCash: createCashPoolCommand.Cash,
ExchangedSuMoney: 0,
UnExchangeSuMoney: systemUnExchangeSuMoney,
LastRate: 0,
Rate: 0,
CreateTime: time.Now().Local(),
}
if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
cashPool.Rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.Rate), 64)
cashPool.UnExchangeCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeCash), 64)
cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
return cashPool, nil
}
} else { // 更新现金池
if len(cashPools) > 0 { // 更新现金池
cashPoolExchangeCash := cashPools[0].ExchangedCash
cashPoolUnExchangeCash := cashPools[0].UnExchangeCash
cashPoolCash := cashPools[0].Cash
if createCashPoolCommand.Cash < cashPoolExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "投入的现金值必须大于当前已兑换现金值")
}
// 重新计算平均兑换汇率
newRate := cashPoolExchangeCash / systemExchangedSuMoney
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate = cashPoolExchangeCash / systemExchangedSuMoney
}
// 获取上次兑换活动兑换汇率查询
var lastActivityRate float64
... ... @@ -147,10 +127,10 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
"nearest": true,
"limit": 1,
}
if count, activities, err := exchangeActivityRepository.Find(listExchangeCashActivityQuery); err != nil {
if _, activities, err := exchangeActivityRepository.Find(listExchangeCashActivityQuery); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if count > 1 {
if len(activities) > 0 {
lastActivityRate = activities[0].Rate
} else { // 未查询到相关兑换活动
lastActivityRate = 0
... ... @@ -162,7 +142,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
CashPoolId: cashPools[0].CashPoolId,
Cash: createCashPoolCommand.Cash,
ExchangedCash: cashPoolExchangeCash,
UnExchangeCash: cashPoolUnExchangeCash + (createCashPoolCommand.Cash - cashPools[0].Cash),
UnExchangeCash: cashPoolUnExchangeCash + (createCashPoolCommand.Cash - cashPoolCash),
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
... ... @@ -186,6 +166,32 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
cashPool.LastRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.LastRate), 64)
return cashPool, nil
}
} else { // 新增现金池
newCashPool := &domain.CashPool{
CompanyId: createCashPoolCommand.CompanyId,
Cash: createCashPoolCommand.Cash,
ExchangedCash: 0,
UnExchangeCash: createCashPoolCommand.Cash,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
LastRate: 0,
Rate: 0,
CreateTime: time.Now().Local(),
}
if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
cashPool.Rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.Rate), 64)
cashPool.UnExchangeCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeCash), 64)
cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
return cashPool, nil
}
}
... ...
... ... @@ -5,8 +5,8 @@ import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/astaxie/beego"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/web/beego/utils"
utils_tool "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/command"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/query"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/service"
... ... @@ -399,7 +399,6 @@ func (controller *SuMoneyController) ImportExchangeList () {
cashPoolService := service.NewCashPoolService(nil)
where := controller.GetString("where")
file, h, _ := controller.GetFile("file")
jsonMap := make(map[string]interface{})
_ = json.Unmarshal([]byte(where), &jsonMap)
... ... @@ -418,7 +417,10 @@ func (controller *SuMoneyController) ImportExchangeList () {
".xlsx":true,
}
if _,ok:=AllowExtMap[ext];!ok{
err := fmt.Errorf("%s", "后缀名不符合上传要求")
err := &application.ServiceError{
Code: 500,
Message: "后缀名不符合上传要求",
}
response = utils.ResponseError(controller.Ctx, err)
controller.Data["json"] = response
controller.ServeJSON()
... ... @@ -433,10 +435,14 @@ func (controller *SuMoneyController) ImportExchangeList () {
// 文件行数校验
rows, _ := xlsx.GetRows("Sheet1")
fmt.Print(len(rows), "\n")
// 表格超行判断
if len(rows) > 302 {
err := fmt.Errorf("%s", "导入行数超过300行")
err := &application.ServiceError{
Code: 500,
Message: "导入的文件超过300行",
}
response = utils.ResponseError(controller.Ctx, err)
controller.Data["json"] = response
controller.ServeJSON()
... ... @@ -444,45 +450,59 @@ func (controller *SuMoneyController) ImportExchangeList () {
}
// 空表格判断
if len(rows) < 3 {
err := fmt.Errorf("%s", "当前导入的为空表格")
if len(rows) < 4 {
err := &application.ServiceError{
Code: 500,
Message: "当前导入的为空表格",
}
response = utils.ResponseError(controller.Ctx, err)
controller.Data["json"] = response
controller.ServeJSON()
return
}
newstr := []string{"null"}
// 空字段判断
nullLine := make([]interface{}, 0)
for i, row := range rows {
if i > 2 {
if len(row) != 3 {
for j, cell := range row {
if cell == " " {
utils_tool.InsertSlice(j, row, newstr)
}
}
row = append(row, "单元格包含空字符")
nullLine = append(nullLine, row)
}
}
}
if len(nullLine) > 0 {
ret = map[string]interface{}{
"successCount": 0,
"fail": map[string]interface{}{
"tableHeader": tableHeader,
"tableData": nullLine,
},
}
response = utils.ResponseData(controller.Ctx, ret)
controller.Data["json"] = response
controller.ServeJSON()
return
}
//newstr := []string{"null"}
//nullChar := []string{"单元格包含空字符"}
//// 空字段判断
//nullLine := make([]interface{}, 0)
//for i, row := range rows {
// if i > 2 {
// if len(row) < 3 {
// r2, _ := strconv.ParseFloat(row[2], 64)
// if row[0] == "" {
// utils_tool.InsertSlice(0, row, newstr)
// } else if row[1] == "" {
// utils_tool.InsertSlice(1, row, newstr)
// } else if r2 == 0 {
// utils_tool.InsertSlice(2, row, newstr)
// }
//
// //for j, cell := range row {
// // if cell == "" {
// // utils_tool.InsertSlice(j, row, newstr)
// // }
// //}
// //utils_tool.InsertSlice(1, row, nullChar)
// row = append(row, "单元格包含空字符")
// nullLine = append(nullLine, row)
// }
// }
//}
//
//if len(nullLine) > 0 {
// ret = map[string]interface{}{
// "successCount": 0,
// "fail": map[string]interface{}{
// "tableHeader": tableHeader,
// "tableData": nullLine,
// },
// }
// response = utils.ResponseData(controller.Ctx, ret)
// controller.Data["json"] = response
// controller.ServeJSON()
// return
//}
// 新增失败记录
failureDataList := make([]interface{}, 0)
... ...