作者 陈志颖

fix:空现金池更新错误

@@ -102,42 +102,22 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co @@ -102,42 +102,22 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
102 if err != nil { 102 if err != nil {
103 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 103 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
104 } 104 }
105 -  
106 - // 不存在现金池  
107 - if count == 0 { // 新增现金池  
108 - newCashPool := &domain.CashPool{  
109 - CompanyId: createCashPoolCommand.CompanyId,  
110 - Cash: createCashPoolCommand.Cash,  
111 - ExchangedCash: 0,  
112 - UnExchangeCash: createCashPoolCommand.Cash,  
113 - ExchangedSuMoney: 0,  
114 - UnExchangeSuMoney: systemUnExchangeSuMoney,  
115 - LastRate: 0,  
116 - Rate: 0,  
117 - CreateTime: time.Now().Local(),  
118 - }  
119 - if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {  
120 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
121 - } else {  
122 - if err := transactionContext.CommitTransaction(); err != nil {  
123 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
124 - }  
125 - cashPool.Rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.Rate), 64)  
126 - cashPool.UnExchangeCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeCash), 64)  
127 - cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)  
128 - cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)  
129 - cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)  
130 - return cashPool, nil  
131 - }  
132 - } else { // 更新现金池 105 + if len(cashPools) > 0 { // 更新现金池
133 cashPoolExchangeCash := cashPools[0].ExchangedCash 106 cashPoolExchangeCash := cashPools[0].ExchangedCash
134 cashPoolUnExchangeCash := cashPools[0].UnExchangeCash 107 cashPoolUnExchangeCash := cashPools[0].UnExchangeCash
  108 + cashPoolCash := cashPools[0].Cash
135 109
136 if createCashPoolCommand.Cash < cashPoolExchangeCash { 110 if createCashPoolCommand.Cash < cashPoolExchangeCash {
137 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "投入的现金值必须大于当前已兑换现金值") 111 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "投入的现金值必须大于当前已兑换现金值")
138 } 112 }
139 - // 重新计算平均兑换汇率  
140 - newRate := cashPoolExchangeCash / systemExchangedSuMoney 113 +
  114 + // 计算平均兑换汇率
  115 + var newRate float64
  116 + if systemExchangedSuMoney == 0 {
  117 + newRate = 0
  118 + } else {
  119 + newRate = cashPoolExchangeCash / systemExchangedSuMoney
  120 + }
141 121
142 // 获取上次兑换活动兑换汇率查询 122 // 获取上次兑换活动兑换汇率查询
143 var lastActivityRate float64 123 var lastActivityRate float64
@@ -147,10 +127,10 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co @@ -147,10 +127,10 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
147 "nearest": true, 127 "nearest": true,
148 "limit": 1, 128 "limit": 1,
149 } 129 }
150 - if count, activities, err := exchangeActivityRepository.Find(listExchangeCashActivityQuery); err != nil { 130 + if _, activities, err := exchangeActivityRepository.Find(listExchangeCashActivityQuery); err != nil {
151 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 131 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
152 } else { 132 } else {
153 - if count > 1 { 133 + if len(activities) > 0 {
154 lastActivityRate = activities[0].Rate 134 lastActivityRate = activities[0].Rate
155 } else { // 未查询到相关兑换活动 135 } else { // 未查询到相关兑换活动
156 lastActivityRate = 0 136 lastActivityRate = 0
@@ -162,7 +142,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co @@ -162,7 +142,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
162 CashPoolId: cashPools[0].CashPoolId, 142 CashPoolId: cashPools[0].CashPoolId,
163 Cash: createCashPoolCommand.Cash, 143 Cash: createCashPoolCommand.Cash,
164 ExchangedCash: cashPoolExchangeCash, 144 ExchangedCash: cashPoolExchangeCash,
165 - UnExchangeCash: cashPoolUnExchangeCash + (createCashPoolCommand.Cash - cashPools[0].Cash), 145 + UnExchangeCash: cashPoolUnExchangeCash + (createCashPoolCommand.Cash - cashPoolCash),
166 ExchangedSuMoney: systemExchangedSuMoney, 146 ExchangedSuMoney: systemExchangedSuMoney,
167 UnExchangeSuMoney: systemUnExchangeSuMoney, 147 UnExchangeSuMoney: systemUnExchangeSuMoney,
168 Rate: newRate, 148 Rate: newRate,
@@ -186,6 +166,32 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co @@ -186,6 +166,32 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
186 cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64) 166 cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
187 cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64) 167 cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
188 cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64) 168 cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
  169 + cashPool.LastRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.LastRate), 64)
  170 + return cashPool, nil
  171 + }
  172 + } else { // 新增现金池
  173 + newCashPool := &domain.CashPool{
  174 + CompanyId: createCashPoolCommand.CompanyId,
  175 + Cash: createCashPoolCommand.Cash,
  176 + ExchangedCash: 0,
  177 + UnExchangeCash: createCashPoolCommand.Cash,
  178 + ExchangedSuMoney: systemExchangedSuMoney,
  179 + UnExchangeSuMoney: systemUnExchangeSuMoney,
  180 + LastRate: 0,
  181 + Rate: 0,
  182 + CreateTime: time.Now().Local(),
  183 + }
  184 + if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
  185 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  186 + } else {
  187 + if err := transactionContext.CommitTransaction(); err != nil {
  188 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  189 + }
  190 + cashPool.Rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.Rate), 64)
  191 + cashPool.UnExchangeCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeCash), 64)
  192 + cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
  193 + cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
  194 + cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
189 return cashPool, nil 195 return cashPool, nil
190 } 196 }
191 } 197 }
@@ -5,8 +5,8 @@ import ( @@ -5,8 +5,8 @@ import (
5 "fmt" 5 "fmt"
6 "github.com/360EntSecGroup-Skylar/excelize/v2" 6 "github.com/360EntSecGroup-Skylar/excelize/v2"
7 "github.com/astaxie/beego" 7 "github.com/astaxie/beego"
  8 + "github.com/linmadan/egglib-go/core/application"
8 "github.com/linmadan/egglib-go/web/beego/utils" 9 "github.com/linmadan/egglib-go/web/beego/utils"
9 - utils_tool "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/utils"  
10 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/command" 10 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/command"
11 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/query" 11 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/query"
12 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/service" 12 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/service"
@@ -399,7 +399,6 @@ func (controller *SuMoneyController) ImportExchangeList () { @@ -399,7 +399,6 @@ func (controller *SuMoneyController) ImportExchangeList () {
399 cashPoolService := service.NewCashPoolService(nil) 399 cashPoolService := service.NewCashPoolService(nil)
400 where := controller.GetString("where") 400 where := controller.GetString("where")
401 file, h, _ := controller.GetFile("file") 401 file, h, _ := controller.GetFile("file")
402 -  
403 jsonMap := make(map[string]interface{}) 402 jsonMap := make(map[string]interface{})
404 _ = json.Unmarshal([]byte(where), &jsonMap) 403 _ = json.Unmarshal([]byte(where), &jsonMap)
405 404
@@ -418,7 +417,10 @@ func (controller *SuMoneyController) ImportExchangeList () { @@ -418,7 +417,10 @@ func (controller *SuMoneyController) ImportExchangeList () {
418 ".xlsx":true, 417 ".xlsx":true,
419 } 418 }
420 if _,ok:=AllowExtMap[ext];!ok{ 419 if _,ok:=AllowExtMap[ext];!ok{
421 - err := fmt.Errorf("%s", "后缀名不符合上传要求") 420 + err := &application.ServiceError{
  421 + Code: 500,
  422 + Message: "后缀名不符合上传要求",
  423 + }
422 response = utils.ResponseError(controller.Ctx, err) 424 response = utils.ResponseError(controller.Ctx, err)
423 controller.Data["json"] = response 425 controller.Data["json"] = response
424 controller.ServeJSON() 426 controller.ServeJSON()
@@ -433,10 +435,14 @@ func (controller *SuMoneyController) ImportExchangeList () { @@ -433,10 +435,14 @@ func (controller *SuMoneyController) ImportExchangeList () {
433 435
434 // 文件行数校验 436 // 文件行数校验
435 rows, _ := xlsx.GetRows("Sheet1") 437 rows, _ := xlsx.GetRows("Sheet1")
  438 + fmt.Print(len(rows), "\n")
436 439
437 // 表格超行判断 440 // 表格超行判断
438 if len(rows) > 302 { 441 if len(rows) > 302 {
439 - err := fmt.Errorf("%s", "导入行数超过300行") 442 + err := &application.ServiceError{
  443 + Code: 500,
  444 + Message: "导入的文件超过300行",
  445 + }
440 response = utils.ResponseError(controller.Ctx, err) 446 response = utils.ResponseError(controller.Ctx, err)
441 controller.Data["json"] = response 447 controller.Data["json"] = response
442 controller.ServeJSON() 448 controller.ServeJSON()
@@ -444,45 +450,59 @@ func (controller *SuMoneyController) ImportExchangeList () { @@ -444,45 +450,59 @@ func (controller *SuMoneyController) ImportExchangeList () {
444 } 450 }
445 451
446 // 空表格判断 452 // 空表格判断
447 - if len(rows) < 3 {  
448 - err := fmt.Errorf("%s", "当前导入的为空表格") 453 + if len(rows) < 4 {
  454 + err := &application.ServiceError{
  455 + Code: 500,
  456 + Message: "当前导入的为空表格",
  457 + }
449 response = utils.ResponseError(controller.Ctx, err) 458 response = utils.ResponseError(controller.Ctx, err)
450 controller.Data["json"] = response 459 controller.Data["json"] = response
451 controller.ServeJSON() 460 controller.ServeJSON()
452 return 461 return
453 } 462 }
454 463
455 - newstr := []string{"null"}  
456 -  
457 - // 空字段判断  
458 - nullLine := make([]interface{}, 0)  
459 - for i, row := range rows {  
460 - if i > 2 {  
461 - if len(row) != 3 {  
462 - for j, cell := range row {  
463 - if cell == " " {  
464 - utils_tool.InsertSlice(j, row, newstr)  
465 - }  
466 - }  
467 - row = append(row, "单元格包含空字符")  
468 - nullLine = append(nullLine, row)  
469 - }  
470 - }  
471 - }  
472 -  
473 - if len(nullLine) > 0 {  
474 - ret = map[string]interface{}{  
475 - "successCount": 0,  
476 - "fail": map[string]interface{}{  
477 - "tableHeader": tableHeader,  
478 - "tableData": nullLine,  
479 - },  
480 - }  
481 - response = utils.ResponseData(controller.Ctx, ret)  
482 - controller.Data["json"] = response  
483 - controller.ServeJSON()  
484 - return  
485 - } 464 + //newstr := []string{"null"}
  465 + //nullChar := []string{"单元格包含空字符"}
  466 +
  467 + //// 空字段判断
  468 + //nullLine := make([]interface{}, 0)
  469 + //for i, row := range rows {
  470 + // if i > 2 {
  471 + // if len(row) < 3 {
  472 + // r2, _ := strconv.ParseFloat(row[2], 64)
  473 + // if row[0] == "" {
  474 + // utils_tool.InsertSlice(0, row, newstr)
  475 + // } else if row[1] == "" {
  476 + // utils_tool.InsertSlice(1, row, newstr)
  477 + // } else if r2 == 0 {
  478 + // utils_tool.InsertSlice(2, row, newstr)
  479 + // }
  480 + //
  481 + // //for j, cell := range row {
  482 + // // if cell == "" {
  483 + // // utils_tool.InsertSlice(j, row, newstr)
  484 + // // }
  485 + // //}
  486 + // //utils_tool.InsertSlice(1, row, nullChar)
  487 + // row = append(row, "单元格包含空字符")
  488 + // nullLine = append(nullLine, row)
  489 + // }
  490 + // }
  491 + //}
  492 + //
  493 + //if len(nullLine) > 0 {
  494 + // ret = map[string]interface{}{
  495 + // "successCount": 0,
  496 + // "fail": map[string]interface{}{
  497 + // "tableHeader": tableHeader,
  498 + // "tableData": nullLine,
  499 + // },
  500 + // }
  501 + // response = utils.ResponseData(controller.Ctx, ret)
  502 + // controller.Data["json"] = response
  503 + // controller.ServeJSON()
  504 + // return
  505 + //}
486 506
487 // 新增失败记录 507 // 新增失败记录
488 failureDataList := make([]interface{}, 0) 508 failureDataList := make([]interface{}, 0)