9 changed files with 473 additions and 7 deletions
@ -0,0 +1,401 @@ |
|||||
|
package com.qs.serve.modules.tbs.service; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
|
import com.qs.serve.common.util.CollectionUtil; |
||||
|
import com.qs.serve.common.util.CopierUtil; |
||||
|
import com.qs.serve.modules.tbs.entity.*; |
||||
|
import com.qs.serve.modules.tbs.mapper.*; |
||||
|
import com.qs.serve.modules.vtb.entity.VtbVerification; |
||||
|
import com.qs.serve.modules.vtb.entity.VtbVerificationSubject; |
||||
|
import com.qs.serve.modules.vtb.mapper.VtbVerificationMapper; |
||||
|
import com.qs.serve.modules.vtb.mapper.VtbVerificationSubjectMapper; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.jetbrains.annotations.NotNull; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.math.BigDecimal; |
||||
|
import java.math.RoundingMode; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2024/5/25 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Service |
||||
|
@AllArgsConstructor |
||||
|
public class TbsActivityDebugApplicationService { |
||||
|
|
||||
|
private TbsActivityMapper activityMapper; |
||||
|
private TbsActivitySubjectService activitySubjectService; |
||||
|
private TbsActivitySubjectMapper activitySubjectMapper; |
||||
|
private TbsActivityCenterService activityCenterService; |
||||
|
private TbsActivityCenterGoodsService activityCenterGoodsService; |
||||
|
private TbsBudgetLogService budgetLogService; |
||||
|
private VtbVerificationMapper verificationMapper; |
||||
|
//private VtbVerificationSubjectMapper verificationSubjectMapper;
|
||||
|
|
||||
|
public String reset(Long activityId){ |
||||
|
|
||||
|
BigDecimal n100 = new BigDecimal("100"); |
||||
|
|
||||
|
QueryWrapper wrapper = new QueryWrapper(); |
||||
|
wrapper.eq("activity_id",activityId); |
||||
|
|
||||
|
TbsActivity activity = activityMapper.selectById(activityId); |
||||
|
|
||||
|
BigDecimal checkeAmt = activity.getUsedAmount(); |
||||
|
if(activity.getReleaseFlag().equals(1)){ |
||||
|
checkeAmt = checkeAmt.add(activity.getReleaseAmount()); |
||||
|
} |
||||
|
if(checkeAmt.compareTo(activity.getTotalAmount())>0){ |
||||
|
return "活动自身金额异常"; |
||||
|
} |
||||
|
|
||||
|
Long unMatchNum = activitySubjectMapper.checkActAndSubjectAmt(activityId); |
||||
|
if(unMatchNum!=null&&unMatchNum>0){ |
||||
|
return "活动科目金额异常"; |
||||
|
} |
||||
|
|
||||
|
//校验核销金额,错误则不执行
|
||||
|
BigDecimal totalVerificationAmt = verificationMapper.getActivityVerificationAmt(activityId); |
||||
|
if(activity.getUsedAmount().compareTo(BigDecimal.ZERO)!=0 |
||||
|
&&activity.getUsedAmount().compareTo(totalVerificationAmt)!=0){ |
||||
|
return "核销金额异常"; |
||||
|
} |
||||
|
|
||||
|
// subject是和activity的 申请金额和核销金额一致,不进行更新
|
||||
|
List<TbsActivitySubject> activitySubjectList = activitySubjectService.list(wrapper); |
||||
|
List<TbsActivityCenter> activityCenterList = activityCenterService.list(wrapper); |
||||
|
List<TbsActivityCenterGoods> activityCenterGoodsList = activityCenterGoodsService.list(wrapper); |
||||
|
|
||||
|
Map<Long,List<TbsActivityCenterGoods>> activityCenterGoodsListMap = activityCenterGoodsList.stream() |
||||
|
.collect(Collectors.groupingBy(TbsActivityCenterGoods::getSubjectId)); |
||||
|
|
||||
|
Map<Long,List<TbsActivityCenter>> activityCenterListMap = activityCenterList.stream() |
||||
|
.collect(Collectors.groupingBy(TbsActivityCenter::getSubjectId)); |
||||
|
|
||||
|
boolean updateSubject = false; |
||||
|
for (TbsActivitySubject activitySubject : activitySubjectList) { |
||||
|
Long subjectId = activitySubject.getSubjectId(); |
||||
|
List<TbsActivityCenter> subjectCenterList = activityCenterListMap.get(subjectId); |
||||
|
BigDecimal totalCenterRate = BigDecimal.ZERO; |
||||
|
BigDecimal totalCenterAmt = BigDecimal.ZERO; |
||||
|
|
||||
|
BigDecimal subjectUsedAmt = activitySubject.getUsedAmount(); |
||||
|
BigDecimal subjectAmt = activitySubject.getAmount(); |
||||
|
for (TbsActivityCenter activityCenter : subjectCenterList) { |
||||
|
if(activityCenter.getCenterRate().compareTo(BigDecimal.ZERO)>=0){ |
||||
|
totalCenterRate = totalCenterRate.add(activityCenter.getCenterRate()); |
||||
|
} |
||||
|
if(activityCenter.getCenterAmount().compareTo(BigDecimal.ZERO)>=0){ |
||||
|
totalCenterAmt = totalCenterAmt.add(activityCenter.getCenterAmount()); |
||||
|
} |
||||
|
} |
||||
|
boolean isUpdateCenter = false; |
||||
|
if(n100.compareTo(totalCenterRate)!=0){ |
||||
|
log.warn("centerRate不为100:{}",activityId); |
||||
|
isUpdateCenter = true; |
||||
|
BigDecimal centerRate = n100; |
||||
|
for (int i = 0; i < subjectCenterList.size(); i++) { |
||||
|
TbsActivityCenter activityCenter = subjectCenterList.get(i); |
||||
|
if(i+1 == subjectCenterList.size()){ |
||||
|
activityCenter.setCenterRate(centerRate); |
||||
|
}else { |
||||
|
if(activityCenter.getCenterRate().compareTo(BigDecimal.ZERO)<0){ |
||||
|
activityCenter.setCenterRate(BigDecimal.ZERO); |
||||
|
} |
||||
|
centerRate = centerRate.subtract(activityCenter.getCenterRate()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
// 分配占用金额
|
||||
|
if(totalCenterAmt.compareTo(activitySubject.getAmount())!=0){ |
||||
|
log.warn("合计金额异常,将重新分配活动的成本中心金额"); |
||||
|
isUpdateCenter = true; |
||||
|
BigDecimal centerAmt = subjectAmt; |
||||
|
BigDecimal centerUsedAmt = subjectUsedAmt; |
||||
|
for (int i = 0; i < subjectCenterList.size(); i++) { |
||||
|
TbsActivityCenter activityCenter = subjectCenterList.get(i); |
||||
|
if(i+1 == subjectCenterList.size()){ |
||||
|
activityCenter.setCenterAmount(centerAmt); |
||||
|
}else { |
||||
|
BigDecimal currAmt = subjectAmt |
||||
|
.multiply(activityCenter.getCenterRate()) |
||||
|
.divide(n100, 2,RoundingMode.DOWN); |
||||
|
centerAmt = centerAmt.subtract(currAmt); |
||||
|
activityCenter.setCenterAmount(currAmt); |
||||
|
} |
||||
|
//设置已用金额
|
||||
|
if(subjectAmt.compareTo(subjectUsedAmt)==0){ |
||||
|
activityCenter.setUsedAmount(activityCenter.getCenterAmount()); |
||||
|
}else { |
||||
|
if(i+1 == subjectCenterList.size()){ |
||||
|
activityCenter.setUsedAmount(centerUsedAmt); |
||||
|
}else { |
||||
|
BigDecimal currAmt = subjectUsedAmt |
||||
|
.multiply(activityCenter.getCenterRate()) |
||||
|
.divide(n100, 2,RoundingMode.DOWN); |
||||
|
centerUsedAmt = centerUsedAmt.subtract(currAmt); |
||||
|
activityCenter.setUsedAmount(centerAmt); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if(subjectUsedAmt==null){ |
||||
|
subjectUsedAmt = BigDecimal.ZERO; |
||||
|
for (TbsActivityCenter center : subjectCenterList) { |
||||
|
subjectUsedAmt = subjectUsedAmt.add(center.getUsedAmount()); |
||||
|
} |
||||
|
activitySubject.setUsedAmount(subjectUsedAmt); |
||||
|
updateSubject = true; |
||||
|
} |
||||
|
|
||||
|
List<TbsActivityCenterGoods> activityCenterGoodsListOfSubject = activityCenterGoodsListMap.get(subjectId); |
||||
|
|
||||
|
for (TbsActivityCenter center : subjectCenterList) { |
||||
|
List<TbsActivityCenterGoods> goodsList = activityCenterGoodsListOfSubject.stream() |
||||
|
.filter(a->a.getCenterId().equals(center.getCenterId())&&a.getCenterType().equals(center.getCenterType())) |
||||
|
.collect(Collectors.toList()); |
||||
|
final BigDecimal centerUsed = center.getUsedAmount(); |
||||
|
final BigDecimal centerAmt = center.getCenterAmount(); |
||||
|
|
||||
|
BigDecimal totalGoodRate = BigDecimal.ZERO; |
||||
|
for (TbsActivityCenterGoods goods : goodsList) { |
||||
|
BigDecimal rate = goods.getCenterGoodsRate(); |
||||
|
if(rate.compareTo(BigDecimal.ZERO)>0){ |
||||
|
totalGoodRate = totalGoodRate.add(rate); |
||||
|
} |
||||
|
} |
||||
|
//重新分配比率
|
||||
|
if(totalGoodRate.compareTo(n100)!=0){ |
||||
|
log.warn("totalGoodRate不为100,重新分配"); |
||||
|
BigDecimal avgRate = n100.divide(new BigDecimal(goodsList.size()),2,RoundingMode.DOWN); |
||||
|
BigDecimal addRate = BigDecimal.ZERO; |
||||
|
for (int i = 0; i < goodsList.size(); i++) { |
||||
|
TbsActivityCenterGoods goods = goodsList.get(i); |
||||
|
if(i+1 == goodsList.size()){ |
||||
|
goods.setCenterGoodsRate(n100.subtract(addRate)); |
||||
|
}else { |
||||
|
goods.setCenterGoodsRate(avgRate); |
||||
|
addRate = addRate.add(avgRate); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//分配申请金额
|
||||
|
BigDecimal centerAmt3 = centerAmt; |
||||
|
for (int i = 0; i < goodsList.size(); i++) { |
||||
|
TbsActivityCenterGoods goods = goodsList.get(i); |
||||
|
if(i+1 == goodsList.size()){ |
||||
|
goods.setCenterGoodsAmount(centerAmt3); |
||||
|
}else { |
||||
|
BigDecimal currentAmt = centerAmt |
||||
|
.multiply(goods.getCenterGoodsRate()) |
||||
|
.divide(n100,2,RoundingMode.DOWN); |
||||
|
goods.setCenterGoodsAmount(currentAmt); |
||||
|
centerAmt3 = centerAmt3.subtract(currentAmt); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//分配已用金额
|
||||
|
BigDecimal centerUsed2 = centerUsed; |
||||
|
for (int i = 0; i < goodsList.size(); i++) { |
||||
|
TbsActivityCenterGoods goods = goodsList.get(i); |
||||
|
if(i+1 == goodsList.size()){ |
||||
|
goods.setUsedAmount(centerUsed2); |
||||
|
}else { |
||||
|
BigDecimal currentAmt = centerUsed |
||||
|
.multiply(goods.getCenterGoodsRate()) |
||||
|
.divide(n100,2,RoundingMode.DOWN); |
||||
|
goods.setUsedAmount(currentAmt); |
||||
|
centerUsed2 = centerUsed2.subtract(currentAmt); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
//二次校验
|
||||
|
BigDecimal totalSubjectAmt = BigDecimal.ZERO; |
||||
|
for (TbsActivitySubject activitySubject : activitySubjectList) { |
||||
|
Long subjectId = activitySubject.getSubjectId(); |
||||
|
List<TbsActivityCenter> subjectCenterList = activityCenterListMap.get(subjectId); |
||||
|
BigDecimal totalCenterRate = BigDecimal.ZERO; |
||||
|
BigDecimal totalCenterAmt = BigDecimal.ZERO; |
||||
|
BigDecimal totalCenterUsedAmt = BigDecimal.ZERO; |
||||
|
|
||||
|
BigDecimal subjectUsedAmt = activitySubject.getUsedAmount(); |
||||
|
BigDecimal subjectAmt = activitySubject.getAmount(); |
||||
|
totalSubjectAmt = totalSubjectAmt.add(subjectAmt); |
||||
|
|
||||
|
for (TbsActivityCenter activityCenter : subjectCenterList) { |
||||
|
totalCenterRate = totalCenterRate.add(activityCenter.getCenterRate()); |
||||
|
totalCenterAmt = totalCenterAmt.add(activityCenter.getCenterAmount()); |
||||
|
totalCenterUsedAmt = totalCenterUsedAmt.add(activityCenter.getUsedAmount()); |
||||
|
} |
||||
|
if(totalCenterAmt.compareTo(subjectAmt)!=0){ |
||||
|
log.error("合计totalCenterUsedAmt金额异常:{}",activityId); |
||||
|
return "合计totalCenterUsedAmt金额异常"; |
||||
|
} |
||||
|
if(totalCenterUsedAmt.compareTo(subjectUsedAmt)!=0){ |
||||
|
log.error("合计subjectUsedAmt金额异常,将重新分配活动的成本中心金额:{}",activityId); |
||||
|
return "合计subjectUsedAmt金额异常"; |
||||
|
} |
||||
|
if(n100.compareTo(totalCenterRate)!=0){ |
||||
|
log.error("centerRate不为100:{}",activityId); |
||||
|
return "centerRate不为100"; |
||||
|
} |
||||
|
List<TbsActivityCenterGoods> activityCenterGoodsListOfSubject = activityCenterGoodsListMap.get(subjectId); |
||||
|
|
||||
|
for (TbsActivityCenter center : subjectCenterList) { |
||||
|
List<TbsActivityCenterGoods> goodsList = activityCenterGoodsListOfSubject.stream() |
||||
|
.filter(a->a.getCenterId().equals(center.getCenterId())&&a.getCenterType().equals(center.getCenterType())) |
||||
|
.collect(Collectors.toList()); |
||||
|
final BigDecimal centerUsed = center.getUsedAmount(); |
||||
|
final BigDecimal centerAmt = center.getCenterAmount(); |
||||
|
|
||||
|
BigDecimal totalGoodRate = BigDecimal.ZERO; |
||||
|
BigDecimal totalGoodUsed = BigDecimal.ZERO; |
||||
|
BigDecimal totalGoodAmt = BigDecimal.ZERO; |
||||
|
for (TbsActivityCenterGoods goods : goodsList) { |
||||
|
BigDecimal rate = goods.getCenterGoodsRate(); |
||||
|
totalGoodRate = totalGoodRate.add(rate); |
||||
|
totalGoodAmt = totalGoodAmt.add(goods.getCenterGoodsAmount()); |
||||
|
totalGoodUsed = totalGoodUsed.add(goods.getUsedAmount()); |
||||
|
} |
||||
|
//重新分配比率
|
||||
|
if(totalGoodRate.compareTo(n100)!=0){ |
||||
|
log.error("totalGoodRate不为100:{}",activityId); |
||||
|
return "totalGoodRate不为100"; |
||||
|
} |
||||
|
|
||||
|
if(totalGoodAmt.compareTo(centerAmt)!=0){ |
||||
|
log.error("totalGoodAmt:{}",activityId); |
||||
|
return "totalGoodAmt错误"; |
||||
|
} |
||||
|
if(totalGoodUsed.compareTo(centerUsed)!=0){ |
||||
|
log.error("totalGoodUsed:{}",activityId); |
||||
|
return "totalGoodUsed错误"; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
if(activity.getTotalAmount().compareTo(totalSubjectAmt)!=0){ |
||||
|
log.error("科目和活动的申请金额不匹配"); |
||||
|
return "科目和活动的申请金额不匹配"; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// 处理预算占用
|
||||
|
List<TbsBudgetLog> actBudgetLogs = budgetLogService.list(wrapper); |
||||
|
|
||||
|
List<TbsBudgetLog> applyLogs = actBudgetLogs.stream().filter(a-> a.getOptType()!=4).collect(Collectors.toList()); |
||||
|
|
||||
|
List<TbsBudgetLog> releaseLogs = actBudgetLogs.stream().filter(a-> a.getOptType()==4).collect(Collectors.toList()); |
||||
|
|
||||
|
BigDecimal applyAmt = totalBudgetLogAmount(applyLogs); |
||||
|
|
||||
|
BigDecimal releaseAmt = totalBudgetLogAmount(releaseLogs); |
||||
|
|
||||
|
//检查下占用预算和释放
|
||||
|
|
||||
|
if(applyAmt.compareTo(activity.getTotalAmount())!=0){ |
||||
|
log.warn("历史预算占用金额异常"); |
||||
|
} |
||||
|
|
||||
|
if(activity.getReleaseAmount()!=null && releaseAmt.compareTo(activity.getReleaseAmount())!=0){ |
||||
|
log.warn("历史预算释放金额异常"); |
||||
|
} |
||||
|
|
||||
|
List<TbsBudgetLog> newApplyLogList = new ArrayList<>(); |
||||
|
List<TbsBudgetLog> newReleaseLogList = new ArrayList<>(); |
||||
|
|
||||
|
for (TbsActivityCenterGoods goods : activityCenterGoodsList) { |
||||
|
|
||||
|
List<TbsBudgetLog> goodApplyLogs = actBudgetLogs.stream().filter(a-> |
||||
|
a.getSubjectId().equals(goods.getSubjectId()) |
||||
|
&&a.getCenterType().equals(goods.getCenterType()) |
||||
|
&&a.getCenterId().equals(goods.getCenterId()) |
||||
|
&&a.getOptType()!=4 |
||||
|
).collect(Collectors.toList()); |
||||
|
|
||||
|
List<TbsBudgetLog> goodReleaseLogs = actBudgetLogs.stream().filter(a-> |
||||
|
a.getSubjectId().equals(goods.getSubjectId()) |
||||
|
&&a.getCenterType().equals(goods.getCenterType()) |
||||
|
&&a.getCenterId().equals(goods.getCenterId()) |
||||
|
&&a.getOptType()==4 |
||||
|
).collect(Collectors.toList()); |
||||
|
|
||||
|
if(goodApplyLogs.size()==0 && goodReleaseLogs.size() ==0 |
||||
|
&&goods.getCenterGoodsAmount().equals(goods.getUsedAmount())){ |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
// 新log
|
||||
|
TbsBudgetLog newApplyLog = CopierUtil.copy(goodApplyLogs.get(0),new TbsBudgetLog()); |
||||
|
newApplyLog.setId(null); |
||||
|
newApplyLog.setAmount(goods.getCenterGoodsAmount().negate()); |
||||
|
newApplyLog.setOptType(1); |
||||
|
newApplyLogList.add(newApplyLog); |
||||
|
|
||||
|
if(goodReleaseLogs.size()>0){ |
||||
|
TbsBudgetLog newReleaseLog = CopierUtil.copy(goodReleaseLogs.get(0),new TbsBudgetLog()); |
||||
|
newReleaseLog.setId(null); |
||||
|
newReleaseLog.setAmount(goods.getCenterGoodsAmount().subtract(goods.getUsedAmount())); |
||||
|
newReleaseLog.setOptType(4); |
||||
|
newReleaseLogList.add(newReleaseLog); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
BigDecimal newApplyAmt = totalBudgetLogAmount(newApplyLogList); |
||||
|
BigDecimal newReleaseAmt = totalBudgetLogAmount(newReleaseLogList); |
||||
|
|
||||
|
if(newApplyAmt.negate().compareTo(activity.getTotalAmount())!=0){ |
||||
|
log.error("预算占用金额异常"); |
||||
|
return "预算占用金额异常"; |
||||
|
} |
||||
|
|
||||
|
if(activity.getReleaseAmount()!=null && newReleaseAmt.compareTo(activity.getReleaseAmount())!=0){ |
||||
|
log.error("预算释放金额异常 activity{},.getReleaseAmount():{} newReleaseAmt:{}",activityId,activity.getReleaseAmount(),newReleaseAmt); |
||||
|
return "预算释放金额异常"; |
||||
|
} |
||||
|
|
||||
|
if(updateSubject){ |
||||
|
activitySubjectService.updateBatchById(activitySubjectList); |
||||
|
} |
||||
|
activityCenterService.updateBatchById(activityCenterList); |
||||
|
activityCenterGoodsService.updateBatchById(activityCenterGoodsList); |
||||
|
List<Long> logIds = actBudgetLogs.stream().map(a->a.getId()).collect(Collectors.toList()); |
||||
|
if(!newApplyLogList.isEmpty()){ |
||||
|
budgetLogService.saveBatch(newApplyLogList); |
||||
|
} |
||||
|
if(CollectionUtil.isNotEmpty(newReleaseLogList)){ |
||||
|
budgetLogService.saveBatch(newReleaseLogList); |
||||
|
} |
||||
|
if(CollectionUtil.isNotEmpty(logIds)){ |
||||
|
budgetLogService.removeBatchByIds(logIds); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
private BigDecimal totalBudgetLogAmount(List<TbsBudgetLog> releaseLogs) { |
||||
|
BigDecimal amt = BigDecimal.ZERO; |
||||
|
if(releaseLogs!=null){ |
||||
|
for (TbsBudgetLog reLog : releaseLogs) { |
||||
|
amt = amt.add(reLog.getAmount()); |
||||
|
} |
||||
|
} |
||||
|
return amt; |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue