作者 陈志颖

refactor:整理导入素币兑换清单流程

... ... @@ -1184,11 +1184,12 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
return personUpdated, nil
}
// 新增导入兑换素币清单
// 导入新增兑换素币清单
func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(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())
... ... @@ -1265,6 +1266,20 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if activityFound == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR,"当前未创建任务兑换活动")
}
// 获取当前公司现金池
_, 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, "当前现金池为空,请先投入现金")
}
// 获取员工查询条件
getEmployee := map[string]interface{}{
... ... @@ -1281,11 +1296,6 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的员工")
}
// 判断该员工兑换的素币是否超过本人持有的素币
if employeeFound.SuMoney < createExchangeCashPersonCommand.ExchangedSuMoney {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "当前兑换素币超过本人持有的素币值")
}
// 判断当前员工是否已经在素币兑换清单中
_, peopleFound, err := exchangeCashPersonListRepository.Find(map[string]interface{}{
"employeeAccount": employeeFound.EmployeeInfo.EmployeeAccount,
... ... @@ -1297,17 +1307,25 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(peopleFound) > 0 { // 追加素币兑换或撤回素币兑换
// 获取兑换清单员工已兑换素币
// 判断该员工兑换的素币是否超过本人持有的素币
if employeeFound.SuMoney < createExchangeCashPersonCommand.ExchangedSuMoney {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "当前兑换素币超过本人持有的素币值")
}
if len(peopleFound) > 0 { // 当前导入员工在素币兑换清单中,判断追加素币兑换或撤回素币兑换
// 获取当前员工已兑换素币
personFoundExchangedSuMoney := peopleFound[0].ExchangedSuMoney
fmt.Print(createExchangeCashPersonCommand.ExchangedSuMoney, "\n")
fmt.Print(personFoundExchangedSuMoney, "\n")
// 获取当前兑换员工已兑换现金
//personFoundExchangedCash := peopleFound[0].ExchangedCash
if createExchangeCashPersonCommand.ExchangedSuMoney < personFoundExchangedSuMoney { // 当前兑换的素币小于已兑换素币,撤回兑换素币
// 减量
if createExchangeCashPersonCommand.ExchangedSuMoney < personFoundExchangedSuMoney { // 当前兑换的素币小于本人已兑换素币,撤回兑换素币
// 素币减量
suMoneyDecrement := peopleFound[0].ExchangedSuMoney - createExchangeCashPersonCommand.ExchangedSuMoney
// 现金减量
cashDecrement := suMoneyDecrement * activityFound.Rate
// 更新兑换素币清单命令
updateExchangeCashPersonCommand := command.UpdateExchangeCashPersonCommand{
ListId: peopleFound[0].ListId,
... ... @@ -1329,8 +1347,8 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
// 更新素币兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: peopleFound[0].ExchangeCashActivityId,
ExchangedSuMoney: activityFound.ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney),
ExchangedCash: activityFound.ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityFound.Rate,
ExchangedSuMoney: activityFound.ExchangedSuMoney - suMoneyDecrement,
ExchangedCash: activityFound.ExchangedCash - cashDecrement,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
... ... @@ -1350,16 +1368,18 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
}
// 操作素币命令
// 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
// 个人素币操作命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
OperationType: domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH_RESTORE,
SuMoney: suMoneyDecrement,
OperationType: 41,
OperationDescription: activityFound.ExchangeActivityName + "调整",
}
// 操作素币,生成素币流水
// 操作个人素币,生成素币流水记录
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())
... ... @@ -1368,17 +1388,6 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
// 获取当前现金池
_, 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, "已超过现金池未兑换现金")
... ... @@ -1407,8 +1416,8 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - personUpdated.ExchangedCash,
ExchangedCash: cashPoolsFound[0].ExchangedCash - cashDecrement,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash + cashDecrement,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
... ... @@ -1432,11 +1441,13 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return personUpdated, nil
} else if createExchangeCashPersonCommand.ExchangedSuMoney > personFoundExchangedSuMoney { // 当前兑换素币大于已兑换素币,追加兑换素币
// 增量
suMoneyIncrement := createExchangeCashPersonCommand.ExchangedSuMoney - peopleFound[0].ExchangedSuMoney
// 素币增量
suMoneyIncrement := createExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney
// 现金增量
cashIncrement := suMoneyIncrement * activityFound.Rate
// 更新兑换素币清单命令
updateExchangeCashPersonCommand := command.UpdateExchangeCashPersonCommand{
... ... @@ -1459,8 +1470,8 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
// 更新素币兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: peopleFound[0].ExchangeCashActivityId,
ExchangedSuMoney: activityFound.ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney),
ExchangedCash: activityFound.ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityFound.Rate,
ExchangedSuMoney: activityFound.ExchangedSuMoney + suMoneyIncrement,
ExchangedCash: activityFound.ExchangedCash + cashIncrement,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
... ... @@ -1480,12 +1491,14 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
}
// 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
// 操作素币命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
OperationType: domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH_RESTORE,
OperationType: 41,
OperationDescription: activityFound.ExchangeActivityName + "调整",
}
... ... @@ -1498,17 +1511,6 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
// 获取当前现金池
_, 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, "已超过现金池未兑换现金")
... ... @@ -1537,8 +1539,8 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - personUpdated.ExchangedCash,
ExchangedCash: cashPoolsFound[0].ExchangedCash + cashIncrement,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - cashIncrement,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
... ... @@ -1562,10 +1564,14 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return personUpdated, nil
} else { // 当前导入兑换素币清单素币等于本人已兑换素币
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
} else if len(peopleFound) == 0 { // 新增
} else if len(peopleFound) == 0 { // 导入兑换素币清单员工不存在兑换素币清单中,新增兑换素币清单
// 新增兑换素币清单命令
newPersonExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
newPerson := &domain.ExchangeCashPersonList{
... ... @@ -1585,6 +1591,8 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
// 操作素币命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
... ... @@ -1628,17 +1636,6 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
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, "已超过现金池未兑换现金")
... ... @@ -1655,7 +1652,7 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 计算平均兑换汇率
// 重新计算现金池平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
... ... @@ -1693,15 +1690,20 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return personSaved, nil
} else { // 未知情况
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
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())
... ... @@ -1812,7 +1814,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
// 新增兑换清单命令
newPersonExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
newPerson := &domain.ExchangeCashPersonList{
EmployeeInfo: &domain.EmployeeInfo{
Uid: employeeFound.EmployeeInfo.Uid,
... ... @@ -1830,6 +1831,8 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
// 操作素币命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
... ... @@ -1945,6 +1948,7 @@ func (cashPoolService *CashPoolService) GetExchangeCashPerson(getExchangeCashPer
if err := getExchangeCashPersonQuery.ValidateQuery(); 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())
... ... @@ -1981,11 +1985,12 @@ func (cashPoolService *CashPoolService) GetExchangeCashPerson(getExchangeCashPer
}
}
// 返回兑换素币清单列表
// 返回兑换素币清单列表(全量导出该公司兑换素币清单)
func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashPersonQuery *query.ListExchangeCashPersonQuery) (interface{}, error) {
if err := listExchangeCashPersonQuery.ValidateQuery(); 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())
... ... @@ -2031,6 +2036,7 @@ func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashP
activityIds = append(activityIds, activity.ActivityId)
}
// 获取该公司活动下所有兑换员工
if count, people, err := exchangeCashPersonListRepository.FindAll(map[string]interface{}{
"exchangeCashActivityIds": activityIds,
"offset": listExchangeCashPersonQuery.Offset,
... ... @@ -2068,6 +2074,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if err := removeExchangeCashPersonCommand.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())
... ... @@ -2261,11 +2268,12 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
}
}
// 根据员工ids获取兑换清单
// 导出员工兑换素币清单
func (cashPoolService *CashPoolService) ListExchangeCashPersonById(exportExchangeCashListCommand *command.ExportExchangeCashListCommand) ([]*domain.ExchangeCashPersonList, error) {
if err := exportExchangeCashListCommand.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())
... ... @@ -2287,11 +2295,12 @@ func (cashPoolService *CashPoolService) ListExchangeCashPersonById(exportExchang
exchangeCashPersonListRepository = value
}
// 判断勾选、筛选条件
if len(exportExchangeCashListCommand.Ids) == 0 && exportExchangeCashListCommand.Where == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "未勾选需要导出的数据")
}
// 获取兑换清单
// 获取兑换素币清单
if _, people, err := exchangeCashPersonListRepository.FindById(tool_funs.SimpleStructToMap(exportExchangeCashListCommand)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ...
... ... @@ -69,7 +69,7 @@ func (repository *EmployeeRepository) FindOne(queryOptions map[string]interface{
if companyId, ok := queryOptions["companyId"]; ok {
query = query.Where("employee.company_id = ?", companyId)
}
query = query.Where(`employee.status = ?`, 1)
query = query.Where(`employee.status = ?`, 1) // 筛除已离职员工
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此员工")
... ...