作者 陈志颖

refactor:优化分红退货单新增、编辑校验

@@ -168,11 +168,25 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) CreateDivide @@ -168,11 +168,25 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) CreateDivide
168 orderGoodAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(orderGood.OrderGoodQuantity)).Float64() 168 orderGoodAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(orderGood.OrderGoodQuantity)).Float64()
169 169
170 // 校验合约激励类型是否正确 170 // 校验合约激励类型是否正确
  171 + ruleMatchedFlag := false
171 if orderGood.CooperationContractNumber != "" { 172 if orderGood.CooperationContractNumber != "" {
172 if cooperationContractsMap[orderGood.CooperationContractNumber] != nil && cooperationContractsMap[orderGood.CooperationContractNumber].IncentivesType != 1 { 173 if cooperationContractsMap[orderGood.CooperationContractNumber] != nil && cooperationContractsMap[orderGood.CooperationContractNumber].IncentivesType != 1 {
173 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "退货单产品不能关联金额激励规则") 174 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "退货单产品不能关联金额激励规则")
174 } 175 }
  176 + // 校验产品关联合约的激励规则是否匹配订单时间
  177 + if cooperationContractsMap[orderGood.CooperationContractNumber] != nil {
  178 + for _, incentivesRule := range cooperationContractsMap[orderGood.CooperationContractNumber].DividendsIncentivesRules {
  179 + if (orderTime.After(incentivesRule.DividendsIncentivesStageStart) || orderTime.Equal(incentivesRule.DividendsIncentivesStageStart)) && (orderTime.Before(incentivesRule.DividendsIncentivesStageEnd) || orderTime.Equal(incentivesRule.DividendsIncentivesStageEnd)) {
  180 + ruleMatchedFlag = true
  181 + break
  182 + }
  183 + }
175 } 184 }
  185 + if !ruleMatchedFlag {
  186 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "订单时间无法匹配分红激励规则,请重新选择")
  187 + }
  188 + }
  189 +
176 orderGoods = append(orderGoods, &domain.OrderGood{ 190 orderGoods = append(orderGoods, &domain.OrderGood{
177 OrderGoodId: 0, 191 OrderGoodId: 0,
178 OrderGoodAmount: orderGoodAmount, 192 OrderGoodAmount: orderGoodAmount,
@@ -190,7 +204,8 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) CreateDivide @@ -190,7 +204,8 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) CreateDivide
190 DeletedAt: time.Time{}, 204 DeletedAt: time.Time{},
191 UpdatedAt: time.Time{}, 205 UpdatedAt: time.Time{},
192 }) 206 })
193 - dividendsReturnedOrderRefund, _ = decimal.NewFromFloat(dividendsReturnedOrderRefund).Add(decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(float64(orderGood.OrderGoodQuantity)))).Float64() 207 + // 计算退货订单金额
  208 + dividendsReturnedOrderRefund, _ = decimal.NewFromFloat(dividendsReturnedOrderRefund).Add(decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(orderGood.OrderGoodQuantity))).Float64()
194 } 209 }
195 210
196 newDividendsReturnedOrder := &domain.DividendsReturnedOrder{ 211 newDividendsReturnedOrder := &domain.DividendsReturnedOrder{
@@ -1105,6 +1120,32 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) UpdateDivide @@ -1105,6 +1120,32 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) UpdateDivide
1105 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateDividendsReturnedOrderCommand.DividendsReturnedOrderId))) 1120 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateDividendsReturnedOrderCommand.DividendsReturnedOrderId)))
1106 } 1121 }
1107 1122
  1123 + // 合约仓储初始化
  1124 + var cooperationContractRepository domain.CooperationContractRepository
  1125 + if value, err := factory.CreateCooperationContractRepository(map[string]interface{}{
  1126 + "transactionContext": transactionContext,
  1127 + }); err != nil {
  1128 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1129 + } else {
  1130 + cooperationContractRepository = value
  1131 + }
  1132 +
  1133 + // 查找合约
  1134 + cooperationContractsMap := make(map[string]*domain.CooperationContract)
  1135 + if count, cooperationContracts, err := cooperationContractRepository.Find(map[string]interface{}{
  1136 + "offsetLimit": false,
  1137 + "companyId": updateDividendsReturnedOrderCommand.CompanyId,
  1138 + "orgId": updateDividendsReturnedOrderCommand.OrgId,
  1139 + }); err != nil {
  1140 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1141 + } else {
  1142 + if count > 0 {
  1143 + for _, cooperationContract := range cooperationContracts {
  1144 + cooperationContractsMap[cooperationContract.CooperationContractNumber] = cooperationContract
  1145 + }
  1146 + }
  1147 + }
  1148 +
1108 // 获取订单产品 1149 // 获取订单产品
1109 var orderGoods []*domain.OrderGood 1150 var orderGoods []*domain.OrderGood
1110 var dividendsReturnedOrderAmount float64 1151 var dividendsReturnedOrderAmount float64
@@ -1114,9 +1155,33 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) UpdateDivide @@ -1114,9 +1155,33 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) UpdateDivide
1114 if err3 != nil { 1155 if err3 != nil {
1115 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err3.Error()) 1156 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err3.Error())
1116 } 1157 }
  1158 +
  1159 + // 退货产品金额计算
  1160 + orderGoodAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(orderGood.OrderGoodQuantity)).Float64()
  1161 +
  1162 + // 校验合约激励类型是否正确
  1163 + ruleMatchedFlag := false
  1164 + if orderGood.CooperationContractNumber != "" {
  1165 + if cooperationContractsMap[orderGood.CooperationContractNumber] != nil && cooperationContractsMap[orderGood.CooperationContractNumber].IncentivesType != 1 {
  1166 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "退货单产品不能关联金额激励规则")
  1167 + }
  1168 + // 校验产品关联合约的激励规则是否匹配订单时间
  1169 + if cooperationContractsMap[orderGood.CooperationContractNumber] != nil {
  1170 + for _, incentivesRule := range cooperationContractsMap[orderGood.CooperationContractNumber].DividendsIncentivesRules {
  1171 + if (orderTime.After(incentivesRule.DividendsIncentivesStageStart) || orderTime.Equal(incentivesRule.DividendsIncentivesStageStart)) && (orderTime.Before(incentivesRule.DividendsIncentivesStageEnd) || orderTime.Equal(incentivesRule.DividendsIncentivesStageEnd)) {
  1172 + ruleMatchedFlag = true
  1173 + break
  1174 + }
  1175 + }
  1176 + }
  1177 + if !ruleMatchedFlag {
  1178 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "订单时间无法匹配分红激励规则,请重新选择")
  1179 + }
  1180 + }
  1181 +
1117 orderGoods = append(orderGoods, &domain.OrderGood{ 1182 orderGoods = append(orderGoods, &domain.OrderGood{
1118 OrderGoodId: orderGoodId, 1183 OrderGoodId: orderGoodId,
1119 - OrderGoodAmount: orderGood.OrderGoodAmount, 1184 + OrderGoodAmount: orderGoodAmount,
1120 OrderGoodName: orderGood.OrderGoodName, 1185 OrderGoodName: orderGood.OrderGoodName,
1121 OrderGoodPrice: orderGood.OrderGoodPrice, 1186 OrderGoodPrice: orderGood.OrderGoodPrice,
1122 OrderGoodQuantity: orderGood.OrderGoodQuantity, 1187 OrderGoodQuantity: orderGood.OrderGoodQuantity,
@@ -1130,7 +1195,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) UpdateDivide @@ -1130,7 +1195,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) UpdateDivide
1130 UpdatedAt: time.Now(), 1195 UpdatedAt: time.Now(),
1131 }) 1196 })
1132 // 计算分红退货单金额 1197 // 计算分红退货单金额
1133 - dividendsReturnedOrderAmount = dividendsReturnedOrderAmount + orderGood.OrderGoodAmount 1198 + dividendsReturnedOrderAmount, _ = decimal.NewFromFloat(dividendsReturnedOrderAmount).Add(decimal.NewFromFloat(orderGood.OrderGoodAmount)).Float64()
1134 } 1199 }
1135 1200
1136 if err := dividendsReturnedOrder.Update(tool_funs.SimpleStructToMap(updateDividendsReturnedOrderCommand)); err != nil { 1201 if err := dividendsReturnedOrder.Update(tool_funs.SimpleStructToMap(updateDividendsReturnedOrderCommand)); err != nil {