作者 陈志颖

refactor:编辑分红订单增加校验

@@ -26,7 +26,6 @@ type ApplyForCooperationCommand struct { @@ -26,7 +26,6 @@ type ApplyForCooperationCommand struct {
26 } 26 }
27 27
28 func (applyForCooperationCommand *ApplyForCooperationCommand) Valid(validation *validation.Validation) { 28 func (applyForCooperationCommand *ApplyForCooperationCommand) Valid(validation *validation.Validation) {
29 - //validation.SetError("CustomValid", "未实现的自定义认证")  
30 } 29 }
31 30
32 func (applyForCooperationCommand *ApplyForCooperationCommand) ValidateCommand() error { 31 func (applyForCooperationCommand *ApplyForCooperationCommand) ValidateCommand() error {
@@ -168,7 +168,7 @@ func (dividendsOrderService *DividendsOrderService) CreateDividendsOrder(createD @@ -168,7 +168,7 @@ func (dividendsOrderService *DividendsOrderService) CreateDividendsOrder(createD
168 // 校验产品关联合约的激励规则是否匹配订单时间 168 // 校验产品关联合约的激励规则是否匹配订单时间
169 if cooperationContractsMap[orderGood.CooperationContractNumber] != nil { 169 if cooperationContractsMap[orderGood.CooperationContractNumber] != nil {
170 for _, incentivesRule := range cooperationContractsMap[orderGood.CooperationContractNumber].DividendsIncentivesRules { 170 for _, incentivesRule := range cooperationContractsMap[orderGood.CooperationContractNumber].DividendsIncentivesRules {
171 - if orderTime.After(incentivesRule.DividendsIncentivesStageStart) && orderTime.Before(incentivesRule.DividendsIncentivesStageEnd) { 171 + if (orderTime.After(incentivesRule.DividendsIncentivesStageStart) || orderTime.Equal(incentivesRule.DividendsIncentivesStageStart)) && (orderTime.Before(incentivesRule.DividendsIncentivesStageEnd) || orderTime.Equal(incentivesRule.DividendsIncentivesStageEnd)) {
172 ruleMatchedFlag = true 172 ruleMatchedFlag = true
173 break 173 break
174 } 174 }
@@ -1156,20 +1156,68 @@ func (dividendsOrderService *DividendsOrderService) UpdateDividendsOrder(updateD @@ -1156,20 +1156,68 @@ func (dividendsOrderService *DividendsOrderService) UpdateDividendsOrder(updateD
1156 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateDividendsOrderCommand.DividendsOrderId))) 1156 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateDividendsOrderCommand.DividendsOrderId)))
1157 } 1157 }
1158 1158
  1159 + // 合约仓储初始化
  1160 + var cooperationContractRepository domain.CooperationContractRepository
  1161 + if value, err := factory.CreateCooperationContractRepository(map[string]interface{}{
  1162 + "transactionContext": transactionContext,
  1163 + }); err != nil {
  1164 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1165 + } else {
  1166 + cooperationContractRepository = value
  1167 + }
  1168 +
  1169 + // 查找合约
  1170 + cooperationContractsMap := make(map[string]*domain.CooperationContract)
  1171 + if count, cooperationContracts, err := cooperationContractRepository.Find(map[string]interface{}{
  1172 + "offsetLimit": false,
  1173 + "companyId": updateDividendsOrderCommand.CompanyId,
  1174 + "orgId": updateDividendsOrderCommand.OrgId,
  1175 + }); err != nil {
  1176 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1177 + } else {
  1178 + if count > 0 {
  1179 + for _, cooperationContract := range cooperationContracts {
  1180 + cooperationContractsMap[cooperationContract.CooperationContractNumber] = cooperationContract
  1181 + }
  1182 + }
  1183 + }
  1184 +
1159 // 获取订单产品 1185 // 获取订单产品
1160 var orderGoods []*domain.OrderGood 1186 var orderGoods []*domain.OrderGood
1161 var dividendsOrderAmount float64 1187 var dividendsOrderAmount float64
1162 for _, orderGood := range updateDividendsOrderCommand.OrderGoods { 1188 for _, orderGood := range updateDividendsOrderCommand.OrderGoods {
1163 - // TODO 合约合法性校验,订单时间匹配规则校验, 1189 + // 计算订单产品金额
  1190 + orderGoodAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(orderGood.OrderGoodQuantity)).Float64()
1164 1191
1165 // 产品ID类型转换 1192 // 产品ID类型转换
1166 orderGoodId, err3 := strconv.ParseInt(orderGood.OrderGoodId, 10, 64) 1193 orderGoodId, err3 := strconv.ParseInt(orderGood.OrderGoodId, 10, 64)
1167 if err3 != nil { 1194 if err3 != nil {
1168 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err3.Error()) 1195 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err3.Error())
1169 } 1196 }
  1197 +
  1198 + ruleMatchedFlag := false
  1199 + if orderGood.CooperationContractNumber != "" {
  1200 + // 校验共创合约激励类型是否正确
  1201 + if cooperationContractsMap[orderGood.CooperationContractNumber] != nil && cooperationContractsMap[orderGood.CooperationContractNumber].IncentivesType != 1 {
  1202 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "分红订单产品不能关联金额激励规则")
  1203 + }
  1204 + // 校验产品关联合约的激励规则是否匹配订单时间
  1205 + if cooperationContractsMap[orderGood.CooperationContractNumber] != nil {
  1206 + for _, incentivesRule := range cooperationContractsMap[orderGood.CooperationContractNumber].DividendsIncentivesRules {
  1207 + if (orderTime.After(incentivesRule.DividendsIncentivesStageStart) || orderTime.Equal(incentivesRule.DividendsIncentivesStageStart)) && (orderTime.Before(incentivesRule.DividendsIncentivesStageEnd) || orderTime.Equal(incentivesRule.DividendsIncentivesStageEnd)) {
  1208 + ruleMatchedFlag = true
  1209 + break
  1210 + }
  1211 + }
  1212 + }
  1213 + if !ruleMatchedFlag {
  1214 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "订单时间无法匹配分红激励规则,请重新选择")
  1215 + }
  1216 + }
  1217 +
1170 orderGoods = append(orderGoods, &domain.OrderGood{ 1218 orderGoods = append(orderGoods, &domain.OrderGood{
1171 OrderGoodId: orderGoodId, 1219 OrderGoodId: orderGoodId,
1172 - OrderGoodAmount: orderGood.OrderGoodAmount, 1220 + OrderGoodAmount: orderGoodAmount,
1173 OrderGoodName: orderGood.OrderGoodName, 1221 OrderGoodName: orderGood.OrderGoodName,
1174 OrderGoodPrice: orderGood.OrderGoodPrice, 1222 OrderGoodPrice: orderGood.OrderGoodPrice,
1175 OrderGoodQuantity: orderGood.OrderGoodQuantity, 1223 OrderGoodQuantity: orderGood.OrderGoodQuantity,
@@ -1184,7 +1232,7 @@ func (dividendsOrderService *DividendsOrderService) UpdateDividendsOrder(updateD @@ -1184,7 +1232,7 @@ func (dividendsOrderService *DividendsOrderService) UpdateDividendsOrder(updateD
1184 UpdatedAt: time.Now(), 1232 UpdatedAt: time.Now(),
1185 }) 1233 })
1186 // 计算分红订单金额 1234 // 计算分红订单金额
1187 - dividendsOrderAmount = dividendsOrderAmount + orderGood.OrderGoodAmount 1235 + dividendsOrderAmount, _ = decimal.NewFromFloat(dividendsOrderAmount).Add(decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(orderGood.OrderGoodQuantity)).Sub(decimal.NewFromFloat(orderGood.OrderGoodExpense))).Float64()
1188 } 1236 }
1189 1237
1190 if err := dividendsOrder.Update(tool_funs.SimpleStructToMap(updateDividendsOrderCommand)); err != nil { 1238 if err := dividendsOrder.Update(tool_funs.SimpleStructToMap(updateDividendsOrderCommand)); err != nil {