作者 陈志颖

fix:修复导出素币清单问题

... ... @@ -6,7 +6,7 @@ import (
)
type ExportExchangeCashListCommand struct {
IDs []int `json:"ids"`
Ids []int `json:"ids"`
Where map[string]interface{} `json:"where"`
}
... ...
... ... @@ -723,16 +723,6 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
cashPoolRepository = value
}
// 现金池DAO初始化
//var cashPoolDao *dao.CashPoolDao
//if value, err := factory.CreateCashPoolDao(map[string]interface{}{
// "transactionContext": transactionContext,
//}); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
//} else {
// cashPoolDao = value
//}
// 员工DAO初始化
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
... ... @@ -764,16 +754,6 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))
}
// 获取活动素币统计
//activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityFound.ActivityId)
//if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//}
//if activitySuMoneyStatistics == nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业")
//}
//activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
// 判断兑换活动清单中现金总金额是否超过平台未兑换现金值
if activityFound.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
... ... @@ -792,7 +772,6 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
personFoundExchangedSuMoney := personFound.ExchangedSuMoney
// 更新兑换清单命令
//updateExchangeCashActivityExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", updateExchangeCashActivityCommand.ExchangeRate * personFoundExchangedSuMoney), 64)
updateExchangeCashPerson := &command.UpdateExchangeCashPersonCommand {
ExchangedCash: updateExchangeCashActivityCommand.ExchangeRate * personFoundExchangedSuMoney,
ExchangedSuMoney: personFoundExchangedSuMoney,
... ... @@ -820,17 +799,6 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 获取平台现金状况
//systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
//if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//}
//if systemCashStatistics == nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
//}
//systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
//systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
// 更新兑换活动命令已兑换现金
updateExchangeCashActivityCommand.ExchangedCash = updateExchangeCashActivityCommand.ExchangeRate * activityFound.ExchangedSuMoney
... ... @@ -899,8 +867,8 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
}
}
// 新增兑换素币清单
func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeCashPersonCommand *command.CreateExchangeCashPersonCommand) (interface{}, error) {
// 新增导入兑换素币清单
func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExchangeCashPersonCommand *command.CreateExchangeCashPersonCommand) (interface{}, error) {
if err := createExchangeCashPersonCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
... ... @@ -955,16 +923,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
employeeRepository = value
}
//// 现金池DAO初始化
//var cashPoolDao *dao.CashPoolDao
//if value, err := factory.CreateCashPoolDao(map[string]interface{}{
// "transactionContext": transactionContext,
//}); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
//} else {
// cashPoolDao = value
//}
// 员工DAO初始化
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
... ... @@ -991,18 +949,10 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 判断导入方式,页面/Excel
getEmployee := map[string]interface{}{}
if createExchangeCashPersonCommand.PersonAccount == "" {
getEmployee = map[string]interface{}{
"uid": createExchangeCashPersonCommand.Uid,
"companyId": activityFound.CompanyId,
}
} else {
getEmployee = map[string]interface{}{
"account": createExchangeCashPersonCommand.PersonAccount,
"companyId": activityFound.CompanyId,
}
// 获取员工查询条件
getEmployee := map[string]interface{}{
"account": createExchangeCashPersonCommand.PersonAccount,
"companyId": activityFound.CompanyId,
}
// 判断当前员工是否有效
... ... @@ -1020,19 +970,436 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
}
// 判断当前员工是否已经在素币兑换清单中
count, _, err := exchangeCashPersonListRepository.Find(map[string]interface{}{
"uid": employeeFound.EmployeeInfo.Uid,
count, peopleFound, err := exchangeCashPersonListRepository.Find(map[string]interface{}{
"employeeAccount": employeeFound.EmployeeInfo.EmployeeAccount,
"exchangeCashActivityId": activityFound.ActivityId,
"offset": 0,
"limit": 1,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if count > 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "当前员工已经在素币兑换清单中")
// 更新兑换素币清单命令
updateExchangeCashPersonCommand := command.UpdateExchangeCashPersonCommand{
ListId: peopleFound[0].ListId,
ExchangedSuMoney: peopleFound[0].ExchangedSuMoney,
ExchangedCash: peopleFound[0].ExchangedCash,
}
if count > 0 { // 追加素币兑换或撤回素币兑换
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
OperationType: 4,
OperationDescription: "素币兑换现金",
}
if createExchangeCashPersonCommand.ExchangedSuMoney < peopleFound[0].ExchangedSuMoney { // 当前兑换的素币小于已兑换素币,撤回兑换素币
//suMoneyDecrement := peopleFound[0].ExchangedSuMoney - createExchangeCashPersonCommand.ExchangedSuMoney // 减量
// 更新兑换素币清单命令
// 操作素币命令
operationSuMoneyCommand.OperationType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH_RESTORE
operationSuMoneyCommand.OperationDescription = ""
} else if createExchangeCashPersonCommand.ExchangedSuMoney > peopleFound[0].ExchangedSuMoney { // 当前兑换素币大于已兑换素币,追加兑换素币
//suMoneyIncrement := createExchangeCashPersonCommand.ExchangedSuMoney - peopleFound[0].ExchangedSuMoney // 增量
// 更新兑换素币清单命令
// 操作素币命令
operationSuMoneyCommand.OperationType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH
operationSuMoneyCommand.OperationDescription = ""
}
// 更新兑换素币清单
if err := peopleFound[0].Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存兑换素币清单更新
personUpdated, err := exchangeCashPersonListRepository.Save(peopleFound[0])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 操作素币,生成素币流水
task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
// 更新兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: personUpdated.ExchangeCashActivityId,
ExchangeActivityName: activityFound.ExchangeActivityName,
ExchangedSuMoney: activityFound.ExchangedSuMoney + createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: activityFound.ExchangedCash + createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
}
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存兑换现金活动更新
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if activityUpdated == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
}
// 获取当前现金池
_, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(cashPoolsFound) == 0 {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, "当前现金池为空,请先投入现金")
}
// 判断兑换活动的现金是否超过现金池未兑换现金
if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
}
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate = (cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash) / systemExchangedSuMoney
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - personUpdated.ExchangedCash,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
LastRate: cashPoolsFound[0].LastRate,
}
// 更新现金池
if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存现金池更新
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if cashPoolUpdated == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
} else if count == 0 { // 新增
// 新增兑换清单命令
newPersonExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
// 新增兑换素币清单命令
newPerson := &domain.ExchangeCashPersonList{
EmployeeInfo: &domain.EmployeeInfo{
Uid: employeeFound.EmployeeInfo.Uid,
EmployeeName: employeeFound.EmployeeInfo.EmployeeName,
EmployeeAccount: employeeFound.EmployeeInfo.EmployeeAccount,
},
ExchangeCashActivityId: createExchangeCashPersonCommand.ExchangeCashActivityId,
ExchangedSuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: newPersonExchangedCash,
}
// 保存兑换素币清单
personSaved, err := exchangeCashPersonListRepository.Save(newPerson)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 操作素币命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
OperationType: 4,
OperationDescription: "素币兑换现金",
}
// 操作素币,生成素币流水
task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
// 更新兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: personSaved.ExchangeCashActivityId,
ExchangeActivityName: activityFound.ExchangeActivityName,
ExchangedSuMoney: activityFound.ExchangedSuMoney + createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: activityFound.ExchangedCash + createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
}
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存兑换现金活动更新
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if activityUpdated == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
}
// 获取当前现金池
_, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(cashPoolsFound) == 0 {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, "当前现金池为空,请先投入现金")
}
// 判断兑换活动的现金是否超过现金池未兑换现金
if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
}
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate = (cashPoolsFound[0].ExchangedCash + personSaved.ExchangedCash) / systemExchangedSuMoney
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: cashPoolsFound[0].ExchangedCash + personSaved.ExchangedCash,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - personSaved.ExchangedCash,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
LastRate: cashPoolsFound[0].LastRate,
}
// 更新现金池
if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存现金池更新
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if cashPoolUpdated == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return personSaved, nil
}
return nil, nil
}
// 新增兑换素币清单
func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeCashPersonCommand *command.CreateExchangeCashPersonCommand) (interface{}, error) {
if err := createExchangeCashPersonCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
// 兑换活动仓储初始化
var exchangeCashActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
exchangeCashActivityRepository = value
}
// 兑换素币清单仓储初始化
var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
exchangeCashPersonListRepository = value
}
// 现金池仓储初始化
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cashPoolRepository = value
}
// 员工仓储初始化
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
employeeRepository = value
}
// 员工DAO初始化
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
// 操作素币服务初始化
var operationSuMoneyService service.OperationSuMoneyService
if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
operationSuMoneyService = value
}
// 获取当前兑换清单兑换活动
activityFound, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": createExchangeCashPersonCommand.ExchangeCashActivityId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
getEmployee := map[string]interface{}{
"uid": createExchangeCashPersonCommand.Uid,
"companyId": activityFound.CompanyId,
}
// 判断当前员工是否有效
employeeFound, err := employeeRepository.FindOne(getEmployee)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if employeeFound == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的员工")
}
// 判断该员工兑换的素币是否超过本人持有的素币
if employeeFound.SuMoney < createExchangeCashPersonCommand.ExchangedSuMoney {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "当前兑换素币超过本人持有的素币值")
}
//// 判断当前员工是否已经在素币兑换清单中,区分excel导入新增和页面添加新增
//if !createFlag { // 页面新增
// count, _, err := exchangeCashPersonListRepository.Find(map[string]interface{}{
// "uid": employeeFound.EmployeeInfo.Uid,
// "exchangeCashActivityId": activityFound.ActivityId,
// })
// if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
// if count > 0 { // 已存在兑换素币清单
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "当前员工已经在素币兑换清单中")
// }
// if count == 0 { // 未存在,新增兑换素币清单
//
// }
//} else { // 导入新增
// count, peopleFound, err := exchangeCashPersonListRepository.Find(map[string]interface{}{
// "employeeAccount": employeeFound.EmployeeInfo.EmployeeAccount,
// "exchangeCashActivityId": activityFound.ActivityId,
// "offset": 0,
// "limit": 1,
// })
// if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
// if count > 0 { // 追加素币兑换或撤回素币兑换
// if createExchangeCashPersonCommand.ExchangedSuMoney < peopleFound[0].ExchangedSuMoney { // 当前兑换的素币小于已兑换素币,撤回兑换素币
// suMoneyDecrement := peopleFound[0].ExchangedSuMoney - createExchangeCashPersonCommand.ExchangedSuMoney // 减量
//
// } else if createExchangeCashPersonCommand.ExchangedSuMoney > peopleFound[0].ExchangedSuMoney { // 当前兑换素币大于已兑换素币,追加兑换素币
// suMoneyIncrement := createExchangeCashPersonCommand.ExchangedSuMoney - peopleFound[0].ExchangedSuMoney // 增量
//
// }
// }
// if count == 0 { // 新增
//
// }
//}
// 新增兑换清单命令
newPersonExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
newPerson := &domain.ExchangeCashPersonList{
EmployeeInfo: &domain.EmployeeInfo{
Uid: employeeFound.EmployeeInfo.Uid,
... ... @@ -1093,16 +1460,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
}
// 获取兑换活动素币兑换情况
//activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityFound.ActivityId)
//if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//}
//if activitySuMoneyStatistics == nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业")
//}
//activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
// 获取当前现金池
_, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
... ... @@ -1119,17 +1476,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
}
// 获取平台现金兑换情况
//systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
//if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//}
//if systemCashStatistics == nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
//}
//systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
//systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
if err != nil {
... ... @@ -1360,16 +1706,6 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
operationSuMoneyService = value
}
//// 现金池DAO初始化
//var cashPoolDao *dao.CashPoolDao
//if value, err := factory.CreateCashPoolDao(map[string]interface{}{
// "transactionContext": transactionContext,
//}); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
//} else {
// cashPoolDao = value
//}
// 用户DAO初始化
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
... ...
... ... @@ -68,10 +68,13 @@ func (repository *ExchangeCashPersonListRepository) FindById(queryOptions map[st
if ids, ok := queryOptions["ids"]; ok && len(ids.([]int)) != 0 {
query = query.Where("exchange_cash_person_list.id IN (?)", pg.In(ids.([]int)) )
}
// TODO 获取where条件
if where, ok := queryOptions["where"]; ok && where != nil {
query = query.Where(`exchange_cash_person_list.employee_name LIKE ?`, where.(map[string]interface{})["personNameMatch"])
query = query.Where("exchange_cash_person_list.activity_id = ?", where.(map[string]interface{})["activityId"])
if where, ok := queryOptions["where"]; ok && where.(map[string]interface{}) != nil {
if personNameMatch, ok := where.(map[string]interface{})["personNameMatch"]; ok && (personNameMatch != "") {
query = query.Where(`exchange_cash_person_list.employee_name LIKE ?`, fmt.Sprintf("%%%s%%", personNameMatch.(string)))
}
if activityId, ok := where.(map[string]interface{})["activityId"]; ok && activityId.(float64) != 0 {
query = query.Where("exchange_cash_person_list.activity_id = ?", activityId)
}
}
if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
return 0, exchangeCashPeople, err
... ... @@ -137,6 +140,9 @@ func (repository *ExchangeCashPersonListRepository) Find(queryOptions map[string
if personNameMatch, ok := queryOptions["exchangeCashPersonNameMatch"]; ok && (personNameMatch != ""){
query = query.Where("exchange_cash_person_list.employee_name LIKE ?", fmt.Sprintf("%%%s%%", personNameMatch.(string)))
}
if employeeAccount, ok := queryOptions["employeeAccount"]; ok && (employeeAccount != "") {
query = query.Where("exchange_cash_person_list.employee_account =?", employeeAccount)
}
if uid, ok := queryOptions["uid"]; ok {
query = query.Where("exchange_cash_person_list.uid = ?", uid)
}
... ...
... ... @@ -397,55 +397,107 @@ func (controller *SuMoneyController) ImportExchangeList () {
createExchangeCashPersonCommand := &command.CreateExchangeCashPersonCommand{}
activityId, _ := controller.GetInt64("activityId")
operator, _ := controller.GetInt64("operator")
file, h, _ := controller.GetFile("file") //获取上传的文件
ext := path.Ext(h.Filename)
file, h, _ := controller.GetFile("file")
// 错误信息返回
var response utils.JsonResponse
// 返回字段定义
ret := map[string]interface{}{}
// 返回信息表头定义
var tableHeader = [...]string{"错误详情", "员工姓名", "手机号", "已兑换素币"}
// 文件后缀名校验
ext := path.Ext(h.Filename)
AllowExtMap := map[string]bool{
".xlsx":true,
}
if _,ok:=AllowExtMap[ext];!ok{
controller.Ctx.WriteString( "后缀名不符合上传要求" )
return
err := fmt.Errorf("%s", "后缀名不符合上传要求")
response = utils.ResponseError(controller.Ctx, err)
controller.Data["json"] = response
controller.ServeJSON()
}
xlsx, err := excelize.OpenReader(file)
if err != nil {
fmt.Println(err)
return
}
// 记录非空校验
var response utils.JsonResponse
dataList := make([]interface{}, 0)
// 文件行数校验
rows, _ := xlsx.GetRows("Sheet1")
if len(rows) > 1000 {
controller.Ctx.WriteString( "导入行数超过1000行" )
return
if len(rows) > 302 {
err := fmt.Errorf("%s", "导入行数超过300行")
response = utils.ResponseError(controller.Ctx, err)
controller.Data["json"] = response
controller.ServeJSON()
}
// 空行记录
nullLine := make([]interface{}, 0)
// 单元格非空校验
for i, row := range rows {
if i > 2 {
for _, _ = range row {
createExchangeCashPersonCommand.ExchangeCashActivityId = activityId
r2, _ := strconv.ParseFloat(row[2], 64)
if row[0] == "" || row[1] == "" || r2 == 0 {
controller.Ctx.WriteString("单元格包含空字段")
return
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()
}
// 新增失败记录
failureDataList := make([]interface{}, 0)
// 新增成功计数
var successDataCount int64
for i, row := range rows {
if i > 2 {
for _, _ = range row {
r2, _ := strconv.ParseFloat(row[2], 64)
createExchangeCashPersonCommand.ExchangeCashActivityId = activityId
createExchangeCashPersonCommand.Operator = operator
createExchangeCashPersonCommand.PersonName = row[0]
createExchangeCashPersonCommand.PersonAccount = row[1]
createExchangeCashPersonCommand.ExchangedSuMoney, _ = strconv.ParseFloat(row[2], 64)
createExchangeCashPersonCommand.ExchangedSuMoney = r2
}
data, err := cashPoolService.CreateExchangeCashPerson(createExchangeCashPersonCommand)
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
dataList = append(dataList, data)
// 创建兑换素币清单
_, err := cashPoolService.ImportCreateExchangeCashPerson(createExchangeCashPersonCommand)
if err != nil { // 导入失败处理
rows[i] = append(rows[i], err.Error())
failureDataList = append(failureDataList, rows[i]) // 导入失败的记录
} else { // 导入成功处理
successDataCount += 1
}
}
}
ret := map[string]interface{}{
"count": len(dataList),
"people": dataList,
ret = map[string]interface{}{
"successCount": successDataCount,
"fail": map[string]interface{}{
"tableHeader": tableHeader,
"tableData": failureDataList,
},
}
response = utils.ResponseData(controller.Ctx, ret)
controller.Data["json"] = response
controller.ServeJSON()
... ... @@ -514,18 +566,23 @@ func (controller *SuMoneyController) ExportExchangeList() {
}
}
}
f.SetActiveSheet(index)
// 创建下载文件夹
mkErr :=os.Mkdir("download", os.ModePerm)
if err!=nil{
fmt.Println(mkErr)
}
//保存为文件
f.Path="download/素币兑换清单.xlsx"
if err := f.Save(); err != nil {
fmt.Println(err)
}
controller.Ctx.Output.Download(f.Path,"素币兑换清单.xlsx")
// 下载完成删除文件
removeErr := os.Remove(f.Path)
if err != nil {
... ... @@ -565,12 +622,15 @@ func (controller *SuMoneyController) ExportSuMoney() {
} else {
// 新建文件
f := excelize.NewFile()
// 新建工作簿
index := f.NewSheet("Sheet1")
//列标题赋值
for column, v := range titles {
f.SetCellValue("Sheet1", cells[column]+"1", v)
}
for lineNum, v := range data { //行数 lineNum 行内容 v
for columnNum := 0; columnNum < len(v); columnNum ++ {
cell := cells[columnNum]
... ... @@ -591,18 +651,23 @@ func (controller *SuMoneyController) ExportSuMoney() {
}
}
}
f.SetActiveSheet(index)
// 创建下载文件夹
mkErr :=os.Mkdir("download", os.ModePerm)
if err!=nil{
fmt.Println(mkErr)
}
//保存为文件
f.Path="download/素币流水.xlsx"
if err := f.Save(); err != nil {
fmt.Println(err)
}
controller.Ctx.Output.Download(f.Path,"素币流水.xlsx")
// 下载完成删除文件
removeErr := os.Remove(f.Path)
if err != nil {
... ...