|
@@ -5,6 +5,7 @@ import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.ReUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.imed.costaccount.common.enums.CalcTypeEnum;
|
|
|
import com.imed.costaccount.common.enums.ReportTypeEnum;
|
|
|
import com.imed.costaccount.common.exception.CostException;
|
|
@@ -19,9 +20,7 @@ import org.springframework.transaction.annotation.Propagation;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -37,17 +36,22 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
|
|
|
|
|
|
private final ReportRelationService reportRelationService;
|
|
|
private final CostShareLevelService shareLevelService;
|
|
|
+ private final CostOtherPaymentsDataService otherPaymentsDataService;
|
|
|
|
|
|
public HospProfitAndLossServiceImpl(ReportFormService reportFormService,
|
|
|
IncomeCollectionService collectionService,
|
|
|
AllocationQueryService allocationQueryService,
|
|
|
- AllocationService allocationService, ReportRelationService reportRelationService, CostShareLevelService shareLevelService) {
|
|
|
+ AllocationService allocationService,
|
|
|
+ ReportRelationService reportRelationService,
|
|
|
+ CostShareLevelService shareLevelService,
|
|
|
+ CostOtherPaymentsDataService otherPaymentsDataService) {
|
|
|
this.reportFormService = reportFormService;
|
|
|
this.collectionService = collectionService;
|
|
|
this.allocationQueryService = allocationQueryService;
|
|
|
this.allocationService = allocationService;
|
|
|
this.reportRelationService = reportRelationService;
|
|
|
this.shareLevelService = shareLevelService;
|
|
|
+ this.otherPaymentsDataService = otherPaymentsDataService;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -60,14 +64,16 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
|
|
|
@Override
|
|
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
|
|
|
public void calc(String date, Long hospId) {
|
|
|
+ DateTime parse = DateUtil.parse(date);
|
|
|
+ int year = DateUtil.year(parse);
|
|
|
+ int month = DateUtil.month(parse) + 1;
|
|
|
+ this.remove(new LambdaQueryWrapper<HospProfitAndLoss>().eq(HospProfitAndLoss::getDateYear, year).eq(HospProfitAndLoss::getDateMonth, month).eq(HospProfitAndLoss::getHospId, hospId));
|
|
|
// 得到全院损益计算报表
|
|
|
List<ReportForm> reportForms = reportFormService.getListByReportType(hospId, ReportTypeEnum.HOSP_PROFIT_LOSS.getType());
|
|
|
if (CollUtil.isEmpty(reportForms)) {
|
|
|
throw new CostException("医院未设置全院损益计算报表");
|
|
|
}
|
|
|
- DateTime parse = DateUtil.parse(date);
|
|
|
- int year = DateUtil.year(parse);
|
|
|
- int month = DateUtil.month(parse) + 1;
|
|
|
+
|
|
|
// 得到这个月所有收入数据
|
|
|
List<IncomeCollection> incomes = collectionService.getCollectionsByDate(year, month, hospId);
|
|
|
if (incomes.isEmpty()) {
|
|
@@ -84,40 +90,36 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
|
|
|
Integer calcType = i.getCalcType();
|
|
|
if (calcType == CalcTypeEnum.BY_ACCOUNT.getType()) {
|
|
|
// 按会计科目计算
|
|
|
- HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
- calcByAccount(hospId, i, loss, incomes, allocationQueries);
|
|
|
+
|
|
|
+ HospProfitAndLoss loss = calcByAccount(hospId, i, incomes, allocationQueries);
|
|
|
+ if (Objects.isNull(loss)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
loss.setDateMonth(month);
|
|
|
loss.setDateYear(year);
|
|
|
list.add(loss);
|
|
|
return;
|
|
|
} else if (calcType == CalcTypeEnum.BY_SHARE_LEVEL.getType()) {
|
|
|
// 分摊层级计算
|
|
|
- HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
- calcByShareLevel(hospId, i, loss, year, month);
|
|
|
+ HospProfitAndLoss loss = calcByShareLevel(hospId, i, year, month);
|
|
|
+ if (Objects.isNull(loss)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
loss.setDateMonth(month);
|
|
|
loss.setDateYear(year);
|
|
|
list.add(loss);
|
|
|
return;
|
|
|
} else if (calcType == CalcTypeEnum.LITTER_COUNT.getType()) {
|
|
|
- // todo 留在最后直接加 减
|
|
|
-// HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
-// loss.setDateYear(year).setDateMonth(month).setAmount(BigDecimal.ZERO)
|
|
|
-// .setReportName(i.getReportName()).setReportNum(i.getNum())
|
|
|
-// .setCreateTime(System.currentTimeMillis());
|
|
|
-// list.add(loss);
|
|
|
return;
|
|
|
} else if (calcType == CalcTypeEnum.CALC_FORMULA.getType()) {
|
|
|
// todo 留在最后加减
|
|
|
-// HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
-// loss.setDateYear(year).setDateMonth(month).setAmount(BigDecimal.ZERO)
|
|
|
-// .setReportName(i.getReportName()).setReportNum(i.getNum())
|
|
|
-// .setCreateTime(System.currentTimeMillis());
|
|
|
-// list.add(loss);
|
|
|
return;
|
|
|
} else if (calcType == CalcTypeEnum.BY_RESPONSIBILITY.getType()) {
|
|
|
// 责任中心
|
|
|
- HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
- calcByResponsibility(hospId, i, loss, incomes, allocationQueries);
|
|
|
+ HospProfitAndLoss loss = calcByResponsibility(hospId, i, incomes, allocationQueries);
|
|
|
+ if (loss == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
loss.setDateMonth(month);
|
|
|
loss.setDateYear(year);
|
|
|
list.add(loss);
|
|
@@ -128,101 +130,146 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
|
|
|
});
|
|
|
|
|
|
// 先处理按公式
|
|
|
- List<ReportForm> calcFormulas = reportForms.stream().filter(i -> i.getCalcType() == CalcTypeEnum.CALC_FORMULA.getType()).collect(Collectors.toList());
|
|
|
- calcFormulas.forEach(i -> {
|
|
|
- String calcFormula = i.getCalcFormula();
|
|
|
- // TODO: 2021/8/27 校验公式合法性
|
|
|
- if (StrUtil.isBlank(calcFormula)) {
|
|
|
- throw new CostException("reportForm名称为" + i.getReportName() + "计算公式不正确");
|
|
|
- }
|
|
|
- String replace = calcFormula.replace("[", "").replace("]", "").replace("-", ",").replace("+", ",");
|
|
|
- ArrayList<String> strings = CollUtil.newArrayList(replace.split(","));
|
|
|
- Map<Integer, Object> map = new ConcurrentHashMap<>();
|
|
|
- for (int j = 0; j < strings.size(); j++) {
|
|
|
- map.put(j, strings.get(j));
|
|
|
- }
|
|
|
- String s = "+" + calcFormula.replace("[", "").replace("]", "").replace("\\d+", ",");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- });
|
|
|
+ calcByFormula(year, month, reportForms, list);
|
|
|
// 处理小计
|
|
|
+ calcByLitterCount(year, month, reportForms, list, hospId);
|
|
|
+
|
|
|
+ // 处理医院其他收支
|
|
|
+ List<CostOtherPaymentsData> otherPaymentsDatas = otherPaymentsDataService.getByMonth(year, month, hospId);
|
|
|
+ if (!otherPaymentsDatas.isEmpty()) {
|
|
|
+ otherPaymentsDatas.forEach(ele -> {
|
|
|
+ HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
+ loss.setDateYear(year).setDateMonth(month).setReportName(ele.getPaymentsName()).setReportNum(ele.getId().intValue())
|
|
|
+ .setCreateTime(System.currentTimeMillis()).setAmount(ele.getTotalAmount()).setHospId(hospId);
|
|
|
+ if (ele.getPaymentsType() == 2) {
|
|
|
+ loss.setAmount(BigDecimal.ZERO.subtract(ele.getTotalAmount()));
|
|
|
+ }
|
|
|
+ list.add(loss);
|
|
|
+ });
|
|
|
+ }
|
|
|
this.saveBatch(list);
|
|
|
}
|
|
|
|
|
|
+ // 计算公式中钱
|
|
|
+ private BigDecimal calcAmount(List<HospProfitAndLoss> list, String calcFormula) {
|
|
|
+ String replace = calcFormula.replace("[", "").replace("]", "").replace("-", ",").replace("+", ",");
|
|
|
+ ArrayList<String> numList = CollUtil.newArrayList(replace.split(","));
|
|
|
+ // 得到数字
|
|
|
+ Map<Integer, Object> numMap = new ConcurrentHashMap<>();
|
|
|
+ for (int j = 0; j < numList.size(); j++) {
|
|
|
+ numMap.put(j, numList.get(j));
|
|
|
+ }
|
|
|
+ List<String> expressions = ReUtil.findAll("[^0-9]", "+" + calcFormula.replace("[", "").replace("]", "").trim(), 0)
|
|
|
+ .stream().filter(StrUtil::isNotBlank).collect(Collectors.toList());
|
|
|
+ // 得到预算表达式
|
|
|
+ Map<Integer, String> expressionMap = new ConcurrentHashMap<>();
|
|
|
+ for (int k = 0; k < expressions.size(); k++) {
|
|
|
+ expressionMap.put(k, expressions.get(k));
|
|
|
+ }
|
|
|
+ Set<Integer> numSet = numMap.keySet();
|
|
|
+ List<Integer> nums = new ArrayList<>(numSet);
|
|
|
+ Map<Integer, BigDecimal> map = new HashMap<>();
|
|
|
+ for (int l = 0; l < nums.size(); l++) {
|
|
|
+ for (HospProfitAndLoss z : list) {
|
|
|
+ if (z.getReportNum().equals(nums.get(l))) {
|
|
|
+ map.put(l, z.getAmount());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Set<Integer> integers = map.keySet();
|
|
|
+ List<Integer> mapList = new ArrayList<>(integers);
|
|
|
+ AtomicReference<BigDecimal> total = new AtomicReference<>();
|
|
|
+ total.set(BigDecimal.ZERO);
|
|
|
+ for (int x = 0; x < mapList.size(); x++) {
|
|
|
+ BigDecimal bigDecimal = map.get(x);
|
|
|
+ if (expressionMap.get(x).equals("+")) {
|
|
|
+ total.set(total.get().add(bigDecimal));
|
|
|
+ } else {
|
|
|
+ total.set(total.get().subtract(bigDecimal));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return total.get();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 按责任中心计算
|
|
|
*
|
|
|
* @param hospId 医院id
|
|
|
* @param reportForm 报表
|
|
|
- * @param loss 创建的对象
|
|
|
* @param incomes 归集收入数据
|
|
|
* @param allocationQueries 分摊成本数据
|
|
|
*/
|
|
|
- private void calcByResponsibility(Long hospId, ReportForm reportForm, HospProfitAndLoss loss, List<IncomeCollection> incomes, List<AllocationQuery> allocationQueries) {
|
|
|
+ private HospProfitAndLoss calcByResponsibility(Long hospId, ReportForm reportForm, List<IncomeCollection> incomes, List<AllocationQuery> allocationQueries) {
|
|
|
List<RelationVO> responsibilities = reportRelationService.getAccountRelation(reportForm.getId(), hospId);
|
|
|
if (responsibilities.isEmpty()) {
|
|
|
- return;
|
|
|
+ return null;
|
|
|
}
|
|
|
List<String> accountCodes = responsibilities.stream().map(RelationVO::getCode).collect(Collectors.toList());
|
|
|
AtomicReference<BigDecimal> calcTotal = new AtomicReference<>();
|
|
|
calcTotal.set(BigDecimal.ZERO);
|
|
|
accountCodes.forEach(i -> {
|
|
|
-// BigDecimal incomeAmount = incomes.stream().filter(j -> i.equals(j.getResponsibilityCode())).map(IncomeCollection::getAmount)
|
|
|
-// .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
BigDecimal costAmount = allocationQueries.stream().filter(j -> i.equals(j.getResponsibilityCode())).map(AllocationQuery::getAmount)
|
|
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
-// BigDecimal total = incomeAmount.subtract(costAmount);
|
|
|
calcTotal.set(calcTotal.get().add(costAmount));
|
|
|
});
|
|
|
- loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
|
|
|
- .setAmount(calcTotal.get()).setCreateTime(System.currentTimeMillis());
|
|
|
-
|
|
|
-
|
|
|
+ HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
+ return loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
|
|
|
+ .setAmount(calcTotal.get()).setCreateTime(System.currentTimeMillis()).setHospId(hospId);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 按计算公式计算
|
|
|
- *
|
|
|
- * @param hospId 医院id
|
|
|
- * @param reportForm 报表
|
|
|
- * @param loss 创建的对象
|
|
|
- * @param incomes 归集收入数据
|
|
|
- * @param allocationQueries 分摊成本数据
|
|
|
*/
|
|
|
- private void calcByFormula(Long hospId, ReportForm reportForm, HospProfitAndLoss loss, List<IncomeCollection> incomes, List<AllocationQuery> allocationQueries) {
|
|
|
- String calcFormula = reportForm.getCalcFormula();
|
|
|
- // TODO: 2021/8/27 校验公式合法性
|
|
|
- if (StrUtil.isBlank(calcFormula)) {
|
|
|
- throw new CostException("reportForm名称为" + reportForm.getReportName() + "计算公式不正确");
|
|
|
- }
|
|
|
- // 得到11 + 12 - 1
|
|
|
- String sub = StrUtil.removeAny(calcFormula, "[", "]");
|
|
|
- // TODO: 2021/8/27
|
|
|
+ private void calcByFormula(Integer year, Integer month, List<ReportForm> reportForms, List<HospProfitAndLoss> list) {
|
|
|
+ List<ReportForm> calcFormulas = reportForms.stream().filter(i -> i.getCalcType() == CalcTypeEnum.CALC_FORMULA.getType()).collect(Collectors.toList());
|
|
|
+ calcFormulas.forEach(i -> {
|
|
|
+ String calcFormula = i.getCalcFormula();
|
|
|
+ // TODO: 2021/8/27 校验公式合法性
|
|
|
+ if (StrUtil.isBlank(calcFormula)) {
|
|
|
+ throw new CostException("reportForm名称为" + i.getReportName() + "计算公式不正确");
|
|
|
+ }
|
|
|
+ BigDecimal bigDecimal = calcAmount(list, calcFormula);
|
|
|
+ HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
+ loss.setDateYear(year).setDateMonth(month).setReportNum(i.getNum()).setReportName(i.getReportName())
|
|
|
+ .setAmount(bigDecimal).setCreateTime(System.currentTimeMillis()).setHospId(i.getHospId());
|
|
|
+ list.add(loss);
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 按小计计算
|
|
|
- *
|
|
|
- * @param hospId 医院id
|
|
|
- * @param reportForm 报表
|
|
|
- * @param loss 创建的对象
|
|
|
- * @param incomes 归集收入数据
|
|
|
- * @param allocationQueries 分摊成本数据
|
|
|
*/
|
|
|
- private void calcByLitterCount(Long hospId, ReportForm reportForm, HospProfitAndLoss loss, List<IncomeCollection> incomes, List<AllocationQuery> allocationQueries) {
|
|
|
- Long parentId = reportForm.getParentId();
|
|
|
- List<ReportForm> forms = reportFormService.getByParentId(hospId, parentId);
|
|
|
- forms.forEach(i -> {
|
|
|
- // 排除自己
|
|
|
- if (i.getId().equals(reportForm.getId())) {
|
|
|
- return;
|
|
|
- }
|
|
|
- // 计算其他几项之和
|
|
|
- // TODO: 2021/8/27
|
|
|
+ private void calcByLitterCount(Integer year, Integer month, List<ReportForm> reportForms, List<HospProfitAndLoss> list, Long hospId) {
|
|
|
+ List<ReportForm> litterCounts = reportForms.stream().filter(i -> i.getCalcType() == CalcTypeEnum.LITTER_COUNT.getType()).collect(Collectors.toList());
|
|
|
+ litterCounts.forEach(reportForm -> {
|
|
|
+ Long parentId = reportForm.getParentId();
|
|
|
+ List<ReportForm> reportFormByParents = reportFormService.getByParentId(hospId, parentId);
|
|
|
+ List<Integer> reportNums = new ArrayList<>();
|
|
|
+ reportFormByParents.forEach(form -> {
|
|
|
+ // 去掉自己
|
|
|
+ if (form.getId().equals(reportForm.getId())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ reportNums.add(form.getNum());
|
|
|
+ });
|
|
|
|
|
|
+ AtomicReference<BigDecimal> total = new AtomicReference<>();
|
|
|
+ total.set(BigDecimal.ZERO);
|
|
|
+ reportNums.forEach(num -> {
|
|
|
+ list.forEach(item -> {
|
|
|
+ if (num.equals(item.getReportNum())) {
|
|
|
+ total.set(total.get().add(item.getAmount()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
+ loss.setDateYear(year).setDateMonth(month).setReportNum(reportForm.getNum())
|
|
|
+ .setReportName(reportForm.getReportName()).setAmount(total.get()).setCreateTime(System.currentTimeMillis()).setHospId(hospId);
|
|
|
+ list.add(loss);
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -231,13 +278,15 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
|
|
|
*
|
|
|
* @param hospId 医院id
|
|
|
* @param reportForm 报表
|
|
|
- * @param loss 创建的对象
|
|
|
* @param year
|
|
|
* @param month
|
|
|
*/
|
|
|
- private void calcByShareLevel(Long hospId, ReportForm reportForm, HospProfitAndLoss loss, int year, int month) {
|
|
|
+ private HospProfitAndLoss calcByShareLevel(Long hospId, ReportForm reportForm, int year, int month) {
|
|
|
List<RelationVO> shareLevels = reportRelationService.getAccountRelation(reportForm.getId(), hospId);
|
|
|
List<Long> shareLevelId = shareLevels.stream().map(RelationVO::getCode).map(Long::valueOf).collect(Collectors.toList());
|
|
|
+ if (CollUtil.isEmpty(shareLevelId)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
List<CostShareLevel> costShareLevels = shareLevelService.listByIds(shareLevelId);
|
|
|
if (costShareLevels.isEmpty()) {
|
|
|
throw new CostException("医院分摊层级设置错误," + reportForm.getReportName());
|
|
@@ -248,9 +297,9 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
|
|
|
throw new CostException("医院未分摊本月数据");
|
|
|
}
|
|
|
BigDecimal reduce = allocations.stream().map(Allocation::getAmount).reduce(BigDecimal.ZERO, BigDecimal::subtract);
|
|
|
-
|
|
|
- loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
|
|
|
- .setAmount(reduce).setCreateTime(System.currentTimeMillis()).setDateYear(year).setDateMonth(month);
|
|
|
+ HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
+ return loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
|
|
|
+ .setAmount(reduce).setCreateTime(System.currentTimeMillis()).setHospId(hospId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -258,14 +307,13 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
|
|
|
*
|
|
|
* @param hospId 医院id
|
|
|
* @param reportForm 报表
|
|
|
- * @param loss 创建的对象
|
|
|
* @param incomes 归集收入数据
|
|
|
* @param allocationQueries 分摊成本数据
|
|
|
*/
|
|
|
- private void calcByAccount(Long hospId, ReportForm reportForm, HospProfitAndLoss loss, List<IncomeCollection> incomes, List<AllocationQuery> allocationQueries) {
|
|
|
+ private HospProfitAndLoss calcByAccount(Long hospId, ReportForm reportForm, List<IncomeCollection> incomes, List<AllocationQuery> allocationQueries) {
|
|
|
List<RelationVO> accountRelations = reportRelationService.getAccountRelation(reportForm.getId(), hospId);
|
|
|
if (accountRelations.isEmpty()) {
|
|
|
- return;
|
|
|
+ return null;
|
|
|
}
|
|
|
List<String> accountCodes = accountRelations.stream().map(RelationVO::getCode).collect(Collectors.toList());
|
|
|
AtomicReference<BigDecimal> calcTotal = new AtomicReference<>();
|
|
@@ -278,8 +326,9 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
|
|
|
BigDecimal total = incomeAmount.subtract(costAmount);
|
|
|
calcTotal.set(calcTotal.get().add(total));
|
|
|
});
|
|
|
- loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
|
|
|
- .setAmount(calcTotal.get()).setCreateTime(System.currentTimeMillis());
|
|
|
+ HospProfitAndLoss loss = new HospProfitAndLoss();
|
|
|
+ return loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
|
|
|
+ .setAmount(calcTotal.get()).setCreateTime(System.currentTimeMillis()).setHospId(hospId);
|
|
|
|
|
|
}
|
|
|
}
|