Prechádzať zdrojové kódy

Merge branch 'master' of huangrui/CostAccount into dev

lijiaxi 4 rokov pred
rodič
commit
dcce2e6a4c

+ 6 - 0
src/main/java/com/imed/costaccount/model/AllocationQuery.java

@@ -81,6 +81,12 @@ public class AllocationQuery implements Serializable {
 	 * 会计科目名称
 	 */
 	private String accountingName;
+
+	private String targetResponsibilityCode;
+
+	private String targetResponsibilityName;
+
+	private Long shareLevelId;
 	/**
 	 * 创建时间
 	 */

+ 4 - 0
src/main/java/com/imed/costaccount/model/HospProfitAndLoss.java

@@ -65,6 +65,10 @@ public class HospProfitAndLoss implements Serializable {
      */
     private BigDecimal amount;
     private Long hospId;
+
+    private String responsibilityCode;
+
+    private String responsibilityName;
     /**
      * 创建时间
      */

+ 8 - 1
src/main/java/com/imed/costaccount/service/AllocationQueryService.java

@@ -45,6 +45,13 @@ public interface AllocationQueryService extends IService<AllocationQuery> {
      */
     List<AllocationQuery> getAllByDate(Long hospId, int year, int month);
 
-    List<AllocationQuery> getByDate(int year, int month, Long hospId, List<Integer> levelSorts);
+    List<AllocationQuery> getByDate(int year, int month, Long hospId, List<Long> levelSorts);
+
+    List<AllocationQuery> getByDateAndResp(int year, int month, Long hospId, String responsibilityCode);
+
+    List<AllocationQuery> getByDateRespn(int year, int month, Long hospId, List<Long> shareLevelId, String responsibilityCode);
+
+
+
 }
 

+ 1 - 2
src/main/java/com/imed/costaccount/service/AllocationService.java

@@ -75,9 +75,8 @@ public interface AllocationService extends IService<Allocation> {
      * @param year 年月
      * @param month 月
      * @param hospId
-     * @param levelSorts
      * @return
      */
-    List<Allocation> getByDate(int year, int month, Long hospId, List<Integer> levelSorts);
+    List<Allocation> getByDate(int year, int month, Long hospId);
 }
 

+ 2 - 0
src/main/java/com/imed/costaccount/service/CostAccountShareService.java

@@ -87,5 +87,7 @@ public interface CostAccountShareService extends IService<CostAccountShare> {
      * @return 成本分摊参数对应列表
      */
     List<CostAccountShare> getByResponsibility(String responsibilityCode, Long hospId);
+
+    List<CostAccountShare> getByShareLevelSort(List<Integer> levelSorts, Long hospId);
 }
 

+ 6 - 0
src/main/java/com/imed/costaccount/service/HospProfitAndLossService.java

@@ -42,5 +42,11 @@ public interface HospProfitAndLossService extends IService<HospProfitAndLoss> {
      */
     void hospProfitReport(String date, Long hospId, HttpServletResponse response);
 
+    /**
+     *
+     * @param date
+     * @param hospId
+     */
+    void calcByResponsibility(String date, Long hospId);
 }
 

+ 2 - 0
src/main/java/com/imed/costaccount/service/IncomeCollectionService.java

@@ -69,5 +69,7 @@ public interface IncomeCollectionService extends IService<IncomeCollection> {
      * @return 收入数据
      */
     List<IncomeCollection> getCollectionsByDate(int year, int month, Long hospId);
+
+    List<IncomeCollection> getCollectionsByDateAndResp(int year, int month, Long hospId, String responsibilityCode);
 }
 

+ 25 - 2
src/main/java/com/imed/costaccount/service/impl/AllocationQueryServiceImpl.java

@@ -80,13 +80,36 @@ public class AllocationQueryServiceImpl extends ServiceImpl<AllocationQueryMappe
     }
 
     @Override
-    public List<AllocationQuery> getByDate(int year, int month, Long hospId, List<Integer> levelSorts) {
+    public List<AllocationQuery> getByDate(int year, int month, Long hospId, List<Long> levelSorts) {
         return this.list(
                 new LambdaQueryWrapper<AllocationQuery>()
                         .eq(AllocationQuery::getDateYear, year)
                         .eq(AllocationQuery::getDateMonth, month)
                         .eq(AllocationQuery::getHospId, hospId)
-                        .in(AllocationQuery::getLevelSort, levelSorts)
+                        .in(AllocationQuery::getShareLevelId, levelSorts)
+        );
+    }
+
+    @Override
+    public List<AllocationQuery> getByDateAndResp(int year, int month, Long hospId, String responsibilityCode) {
+        return this.list(
+                new LambdaQueryWrapper<AllocationQuery>()
+                        .eq(AllocationQuery::getDateYear, year)
+                        .eq(AllocationQuery::getDateMonth, month)
+                        .eq(AllocationQuery::getHospId, hospId)
+                        .eq(AllocationQuery::getTargetResponsibilityCode, responsibilityCode)
+        );
+    }
+
+    @Override
+    public List<AllocationQuery> getByDateRespn(int year, int month, Long hospId, List<Long> shareLevelId, String responsibilityCode) {
+        return this.list(
+                new LambdaQueryWrapper<AllocationQuery>()
+                        .eq(AllocationQuery::getDateYear, year)
+                        .eq(AllocationQuery::getDateMonth, month)
+                        .eq(AllocationQuery::getHospId, hospId)
+                        .eq(AllocationQuery::getTargetResponsibilityCode, responsibilityCode)
+                        .in(AllocationQuery::getShareLevelId, shareLevelId)
         );
     }
 }

+ 16 - 12
src/main/java/com/imed/costaccount/service/impl/AllocationServiceImpl.java

@@ -250,6 +250,9 @@ public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocat
                     .setAccountingCode(accountingCodes).setAccountingName(accountingNames)
                     .setCreateTime(System.currentTimeMillis())
                     .setLevelSort(i.getLevelSort()).setLevelName(i.getLevelName())
+                    .setTargetResponsibilityCode(i.getTargetResponsibilityCode())
+                    .setTargetResponsibilityName(i.getTargetResponsibilityName())
+                    .setShareLevelId(i.getShareLevelId())
             ;
             saveList.add(allocationQuery);
         });
@@ -272,6 +275,9 @@ public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocat
                     .setAccountingCode(i.getAccountCode()).setAccountingName(i.getAccountName())
                     .setCreateTime(System.currentTimeMillis())
                     .setLevelSort(byId.getLeverSort()).setLevelName(byId.getShareName())
+                    .setTargetResponsibilityCode(responsibilityCode)
+                    .setTargetResponsibilityName(i.getResponsibilityName())
+                    .setShareLevelId(0L)
             ;
             saveList.add(allocationQuery);
         });
@@ -580,14 +586,14 @@ public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocat
             String cellValue1 = sheet.getRow(0).getCell(m).getStringCellValue();
             String cellValue2 = sheet.getRow(0).getCell(m + 1).getStringCellValue();
             if (!cellValue1.equals(cellValue2)) {
-                if (cc!=m){
+                if (cc != m) {
                     writer.merge(0, 0, cc, m, cellValue1, false);
-                }else {
-                    writer.writeCellValue(cc,0,cellValue1);
+                } else {
+                    writer.writeCellValue(cc, 0, cellValue1);
                 }
 
                 cc = m + 1;
-            }else if (m == column - 2) {
+            } else if (m == column - 2) {
                 writer.merge(0, 0, cc, m + 1, sheet.getRow(0).getCell(m + 1).getStringCellValue(), false);
             }
         }
@@ -598,10 +604,10 @@ public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocat
             String cellValue1 = sheet.getRow(i).getCell(0).getStringCellValue();
             String cellValue2 = sheet.getRow(i + 1).getCell(0).getStringCellValue();
             if (!cellValue1.equals(cellValue2)) {
-                if (jj != i){
+                if (jj != i) {
                     writer.merge(jj, i, 0, 0, cellValue1, false);
-                }else {
-                    writer.writeCellValue(0,jj,cellValue1);
+                } else {
+                    writer.writeCellValue(0, jj, cellValue1);
                 }
                 jj = i + 1;
             } else if (i == num - 2) {
@@ -786,20 +792,18 @@ public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocat
     /**
      * 按时间计算分摊数据
      *
-     * @param year       年月
-     * @param month      
+     * @param year   年月
+     * @param month  月
      * @param hospId
-     * @param levelSorts
      * @return
      */
     @Override
-    public List<Allocation> getByDate(int year, int month, Long hospId, List<Integer> levelSorts) {
+    public List<Allocation> getByDate(int year, int month, Long hospId) {
         return this.list(
                 new LambdaQueryWrapper<Allocation>()
                         .eq(Allocation::getDateYear, year)
                         .eq(Allocation::getDateMonth, month)
                         .eq(Allocation::getHospId, hospId)
-                        .in(Allocation::getLevelSort, levelSorts)
         );
     }
 }

+ 10 - 0
src/main/java/com/imed/costaccount/service/impl/CostAccountShareServiceImpl.java

@@ -434,4 +434,14 @@ public class CostAccountShareServiceImpl extends ServiceImpl<CostAccountShareMap
         );
         return list;
     }
+
+    @Override
+    public List<CostAccountShare> getByShareLevelSort(List<Integer> levelSorts, Long hospId) {
+        return this.list(
+                new LambdaQueryWrapper<CostAccountShare>()
+                        .eq(CostAccountShare::getHospId, hospId)
+                        .in(CostAccountShare::getShareLevel, levelSorts)
+        );
+    }
+
 }

+ 5 - 3
src/main/java/com/imed/costaccount/service/impl/CostDepartmentProfitServiceImpl.java

@@ -429,9 +429,11 @@ public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentP
                 // 查询报表里面是当前分摊层级的数据
                 List<Allocation> allocations = allocationList.stream().filter(m -> levelShortList.contains(m.getLevelSort()) && m.getTargetResponsibilityCode().equals(i.getResponsibilityCode())).collect(Collectors.toList());
                 if (CollUtil.isNotEmpty(allocations)) {
-                    allocations.forEach(m -> {
-                        sum.updateAndGet(v -> v.add(m.getAmount()));
-                    });
+//                    allocations.forEach(m -> {
+//                        sum.updateAndGet(v -> v.add(m.getAmount()));
+//                    });
+                    BigDecimal reduce = allocations.stream().map(Allocation::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
+                    sum.set(reduce);
                 }
             }
         }

+ 367 - 123
src/main/java/com/imed/costaccount/service/impl/HospProfitAndLossServiceImpl.java

@@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.IoUtil;
-import cn.hutool.core.util.RandomUtil;
 import cn.hutool.core.util.ReUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.poi.excel.ExcelUtil;
@@ -21,6 +20,7 @@ import com.imed.costaccount.model.vo.HospProfitVO;
 import com.imed.costaccount.model.vo.RelationVO;
 import com.imed.costaccount.model.vo.ReportFormVO;
 import com.imed.costaccount.service.*;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
@@ -37,7 +37,7 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
 
-
+@Slf4j
 @Service("hospProfitAndLossService")
 public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossMapper, HospProfitAndLoss> implements HospProfitAndLossService {
 
@@ -50,6 +50,7 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
     private final CostShareLevelService shareLevelService;
     private final CostOtherPaymentsDataService otherPaymentsDataService;
     private final ResponsibilityService responsibilityService;
+    private final CostAccountShareService accountShareService;
 
     public HospProfitAndLossServiceImpl(ReportFormService reportFormService,
                                         IncomeCollectionService collectionService,
@@ -57,7 +58,9 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
                                         AllocationService allocationService,
                                         ReportRelationService reportRelationService,
                                         CostShareLevelService shareLevelService,
-                                        CostOtherPaymentsDataService otherPaymentsDataService, ResponsibilityService responsibilityService) {
+                                        CostOtherPaymentsDataService otherPaymentsDataService,
+                                        ResponsibilityService responsibilityService,
+                                        CostAccountShareService accountShareService) {
         this.reportFormService = reportFormService;
         this.collectionService = collectionService;
         this.allocationQueryService = allocationQueryService;
@@ -66,6 +69,7 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
         this.shareLevelService = shareLevelService;
         this.otherPaymentsDataService = otherPaymentsDataService;
         this.responsibilityService = responsibilityService;
+        this.accountShareService = accountShareService;
     }
 
 
@@ -100,52 +104,35 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
             throw new CostException("医院未分摊本月数据");
         }
         List<HospProfitAndLoss> list = new ArrayList<>();
-        reportForms.forEach(i -> {
-            Integer calcType = i.getCalcType();
-            if (calcType == CalcTypeEnum.BY_ACCOUNT.getType()) {
-                // 按会计科目计算
-                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 = 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()) {
-                return;
-            } else if (calcType == CalcTypeEnum.CALC_FORMULA.getType()) {
-                // todo 留在最后加减
-                return;
-            } else if (calcType == CalcTypeEnum.BY_RESPONSIBILITY.getType()) {
-                // 责任中心
-                HospProfitAndLoss loss = calcByResponsibility(hospId, i, incomes, allocationQueries);
-                if (loss == null) {
-                    return;
+        List<ReportForm> parentForms = reportForms.stream().filter(i -> i.getParentId().equals(0L)).collect(Collectors.toList());
+        for (ReportForm parentForm : parentForms) {
+            Long parentId = parentForm.getId();
+            List<ReportForm> children = reportForms.stream().filter(i -> i.getParentId().equals(parentId)).collect(Collectors.toList());
+            for (ReportForm child : children) {
+                Integer calcType = child.getCalcType();
+                if (calcType == CalcTypeEnum.BY_ACCOUNT.getType()) {
+                    // 按会计科目计算单的话
+                    calcByAccount(hospId, child, incomes, list, allocationQueries, month, year);
+                } else if (calcType == CalcTypeEnum.BY_SHARE_LEVEL.getType()) {
+                    // 分摊层级计算
+                    calcByShareLevel(hospId, child, list, year, month);
+                } else if (calcType == CalcTypeEnum.LITTER_COUNT.getType()) {
+                    // 处理小计 todo  默认认为 小计都是在同一个下面最后一个
+                    calcByLitterCount(year, month, child, children, list, hospId);
+                } else if (calcType == CalcTypeEnum.CALC_FORMULA.getType()) {
+                    // 按公式 (要保证总合计放到最后呀)
+                    calcByFormula(year, month, child, children, list);
+                } else if (calcType == CalcTypeEnum.BY_RESPONSIBILITY.getType()) {
+                    // 责任中心
+                    calcByResponsibility(hospId, child, allocationQueries, list, year, month);
+                } else if (calcType == CalcTypeEnum.NO_CONFIG.getType()) {
+                    // 不设置不计算
+                } else {
+
                 }
-                loss.setDateMonth(month);
-                loss.setDateYear(year);
-                list.add(loss);
-            } else {
-                // 不设置不计算
-                return;
             }
-        });
+        }
 
-        // 先处理按公式
-        calcByFormula(year, month, reportForms, list);
-        // 处理小计
-        calcByLitterCount(year, month, reportForms, list, hospId);
 
         // 处理医院其他收支
         List<CostOtherPaymentsData> otherPaymentsDatas = otherPaymentsDataService.getByMonth(year, month, hospId);
@@ -164,47 +151,48 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
     }
 
     // 计算公式中钱
-    private BigDecimal calcAmount(List<HospProfitAndLoss> list, String calcFormula) {
+    private BigDecimal calcAmount(List<HospProfitAndLoss> list, String calcFormula, ReportForm reportForm) {
+        // 得到所有的编号
         String replace = calcFormula.replace("[", "").replace("]", "").replace("-", ",").replace("+", ",");
         ArrayList<String> numList = CollUtil.newArrayList(replace.split(","));
-        // 得到数字
-        Map<Integer, Object> numMap = new ConcurrentHashMap<>();
+        // 得到编号的map
+        Map<Integer, Integer> numMap = new ConcurrentHashMap<>();
         for (int j = 0; j < numList.size(); j++) {
-            numMap.put(j, numList.get(j));
+            numMap.put(j, Integer.parseInt(numList.get(j)));
         }
         List<String> expressions = ReUtil.findAll("[^0-9]", "+" + calcFormula.replace("[", "").replace("]", "").trim(), 0)
                 .stream().filter(StrUtil::isNotBlank).collect(Collectors.toList());
-        // 得到预算表达式
+        // 得到预算表达式 得到所有表达式的Map  +  -  相关的
         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 (Objects.isNull(bigDecimal)) {
-                bigDecimal = BigDecimal.ZERO;
+        AtomicReference<BigDecimal> totalAmount = new AtomicReference<>();
+        totalAmount.set(BigDecimal.ZERO);
+        for (int i = 0; i < nums.size(); i++) {
+            // 编号
+            Integer num = numMap.get(i);
+            List<HospProfitAndLoss> losses = list.stream().filter(item -> item.getReportNum().equals(num)).collect(Collectors.toList());
+            if (CollUtil.isEmpty(losses)) {
+                continue;
             }
-            if ("+".equals(expressionMap.get(x))) {
-                total.set(total.get().add(bigDecimal));
+//            log.info("data={}", reportForm);
+//            if (losses.size() != 0 && losses.size() != 1) {
+//                throw new CostException("数据异常");
+//            }
+            HospProfitAndLoss loss = losses.get(0);
+            BigDecimal amount = loss.getAmount();
+            String str = expressionMap.get(i);
+            if (str.equals("+")) {
+                totalAmount.set(totalAmount.get().add(amount));
             } else {
-                total.set(total.get().subtract(bigDecimal));
+                totalAmount.set(totalAmount.get().subtract(amount));
             }
         }
-        return total.get();
+        return totalAmount.get();
     }
 
     /**
@@ -212,13 +200,15 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
      *
      * @param hospId            医院id
      * @param reportForm        报表
-     * @param incomes           归集收入数据
      * @param allocationQueries 分摊成本数据
+     * @param list
+     * @param year
+     * @param month
      */
-    private HospProfitAndLoss calcByResponsibility(Long hospId, ReportForm reportForm, List<IncomeCollection> incomes, List<AllocationQuery> allocationQueries) {
-        List<RelationVO> responsibilities = reportRelationService.getAccountRelation(reportForm.getId(), hospId);
+    private void calcByResponsibility(Long hospId, ReportForm reportForm, List<AllocationQuery> allocationQueries, List<HospProfitAndLoss> list, int year, int month) {
+        List<RelationVO> responsibilities = reportRelationService.getResponsibilities(reportForm.getId(), hospId);
         if (responsibilities.isEmpty()) {
-            return null;
+            return;
         }
         List<String> accountCodes = responsibilities.stream().map(RelationVO::getCode).collect(Collectors.toList());
         AtomicReference<BigDecimal> calcTotal = new AtomicReference<>();
@@ -229,64 +219,57 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
             calcTotal.set(calcTotal.get().add(costAmount));
         });
         HospProfitAndLoss loss = new HospProfitAndLoss();
-        return loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
-                .setAmount(calcTotal.get()).setCreateTime(System.currentTimeMillis()).setHospId(hospId);
+        loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
+                .setAmount(calcTotal.get()).setCreateTime(System.currentTimeMillis()).setHospId(hospId).setDateYear(year).setDateMonth(month);
+        list.add(loss);
     }
 
 
     /**
      * 按计算公式计算
      */
-    private void calcByFormula(Integer year, Integer month, List<ReportForm> reportForms, List<HospProfitAndLoss> list) {
+    private void calcByFormula(Integer year, Integer month, ReportForm child, 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 -> {
+        calcFormulas = calcFormulas.stream().filter(i -> i.getNum().equals(child.getNum())).collect(Collectors.toList());
+        for (ReportForm i : calcFormulas) {
             String calcFormula = i.getCalcFormula();
             // TODO: 2021/8/27 校验公式合法性
             if (StrUtil.isBlank(calcFormula)) {
                 throw new CostException("reportForm名称为" + i.getReportName() + "计算公式不正确");
             }
-            BigDecimal bigDecimal = calcAmount(list, calcFormula);
+            BigDecimal bigDecimal = calcAmount(list, calcFormula, i);
             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);
-
-        });
-
+        }
 
     }
 
     /**
      * 按小计计算
      */
-    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;
+    private void calcByLitterCount(Integer year, Integer month, ReportForm child, List<ReportForm> reportForms, List<HospProfitAndLoss> list, Long hospId) {
+        // 其他的
+        List<ReportForm> calcList = reportForms.stream().filter(i -> !i.getCalcType().equals(CalcTypeEnum.LITTER_COUNT.getType())).collect(Collectors.toList());
+        List<HospProfitAndLoss> thisList = new ArrayList<>();
+        list.forEach(i -> {
+            calcList.forEach(j -> {
+                if (i.getReportNum().equals(j.getNum())) {
+                    thisList.add(i);
                 }
-                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);
         });
+        if (thisList.isEmpty()) {
+            return;
+        }
+        BigDecimal reduce = thisList.stream().map(HospProfitAndLoss::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
+        HospProfitAndLoss loss = new HospProfitAndLoss();
+        loss.setDateYear(year).setDateMonth(month).setAmount(reduce).setCreateTime(System.currentTimeMillis()).setHospId(hospId);
+        // 小计只能有一个
+        loss.setReportNum(child.getNum()).setReportName(child.getReportName());
+        list.add(loss);
     }
 
     /**
@@ -294,31 +277,37 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
      *
      * @param hospId     医院id
      * @param reportForm 报表
+     * @param list
      * @param year
      * @param 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());
+    private void calcByShareLevel(Long hospId, ReportForm reportForm, List<HospProfitAndLoss> list, int year, int month) {
+        List<RelationVO> shareLevels = reportRelationService.getShareLevel(reportForm.getId(), hospId);
+        List<Long> shareLevelId = shareLevels.stream().map(RelationVO::getCode).map(Long::valueOf).sorted(Long::compareTo).collect(Collectors.toList());
         if (CollUtil.isEmpty(shareLevelId)) {
-            return null;
+            return;
         }
-        // 分摊层级计算金额
-        // TODO: 2021/8/31  
-        
         List<CostShareLevel> costShareLevels = shareLevelService.listByIds(shareLevelId);
         if (costShareLevels.isEmpty()) {
             throw new CostException("医院分摊层级设置错误," + reportForm.getReportName());
         }
         List<Integer> levelSorts = costShareLevels.stream().map(CostShareLevel::getLeverSort).collect(Collectors.toList());
-        List<AllocationQuery> allocations = allocationQueryService.getByDate(year, month, hospId, levelSorts);
+        List<AllocationQuery> allocations = allocationQueryService.getByDate(year, month, hospId, shareLevelId);
+
         if (allocations.isEmpty()) {
             throw new CostException("医院未分摊本月数据");
         }
+        List<CostAccountShare> accountShares = accountShareService.getByShareLevelSort(levelSorts, hospId);
+        if (accountShares.isEmpty()) {
+            return;
+        }
+
+//        allocations = allocations.stream().filter(i -> levelSorts.contains(i.getLevelSort())).collect(Collectors.toList());
         BigDecimal reduce = allocations.stream().map(AllocationQuery::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
         HospProfitAndLoss loss = new HospProfitAndLoss();
-        return loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
-                .setAmount(reduce).setCreateTime(System.currentTimeMillis()).setHospId(hospId);
+        loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
+                .setAmount(reduce).setCreateTime(System.currentTimeMillis()).setHospId(hospId).setDateYear(year).setDateMonth(month);
+        list.add(loss);
     }
 
     /**
@@ -327,12 +316,16 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
      * @param hospId            医院id
      * @param reportForm        报表
      * @param incomes           归集收入数据
+     * @param list
      * @param allocationQueries 分摊成本数据
+     * @param month
+     * @param year
      */
-    private HospProfitAndLoss calcByAccount(Long hospId, ReportForm reportForm, List<IncomeCollection> incomes, List<AllocationQuery> allocationQueries) {
+    private void calcByAccount(Long hospId, ReportForm reportForm, List<IncomeCollection> incomes, List<HospProfitAndLoss> list, List<AllocationQuery> allocationQueries, int month, int year) {
+        // 报表项目关联的会计科目对象
         List<RelationVO> accountRelations = reportRelationService.getAccountRelation(reportForm.getId(), hospId);
         if (accountRelations.isEmpty()) {
-            return null;
+            return;
         }
         List<String> accountCodes = accountRelations.stream().map(RelationVO::getCode).collect(Collectors.toList());
         AtomicReference<BigDecimal> calcTotal = new AtomicReference<>();
@@ -346,9 +339,9 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
             calcTotal.set(calcTotal.get().add(total));
         });
         HospProfitAndLoss loss = new HospProfitAndLoss();
-        return loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
-                .setAmount(calcTotal.get()).setCreateTime(System.currentTimeMillis()).setHospId(hospId);
-
+        loss.setReportName(reportForm.getReportName()).setReportNum(reportForm.getNum())
+                .setAmount(calcTotal.get()).setCreateTime(System.currentTimeMillis()).setHospId(hospId).setDateYear(year).setDateMonth(month);
+        list.add(loss);
     }
 
 
@@ -412,6 +405,8 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
         if (allHospList.isEmpty()) {
             return;
         }
+        // 查询所有的全院损益数据  内存溢出问题
+        List<HospProfitAndLoss> list = getAllDataByDate(year, month, hospId);
         int lastRow = 3;
         for (int i = 0; i < allHospList.size(); i++) {
             ReportFormVO parentFormVO = allHospList.get(i);
@@ -447,4 +442,253 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
 
 
     }
+
+    private List<HospProfitAndLoss> getAllDataByDate(int year, int month, Long hospId) {
+        return this.list(
+                new LambdaQueryWrapper<HospProfitAndLoss>()
+                        .eq(HospProfitAndLoss::getHospId, hospId)
+                        .eq(HospProfitAndLoss::getDateMonth, month)
+                        .eq(HospProfitAndLoss::getDateYear, year)
+        );
+    }
+
+
+    /**
+     * @param date
+     * @param hospId
+     */
+    @Override
+    @Transactional
+    public void calcByResponsibility(String date, Long hospId) {
+        List<Responsibility> leafResp = responsibilityService.getLeafResp(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("医院未设置全院损益计算报表");
+        }
+
+        // 得到这个月所有收入数据
+        List<IncomeCollection> incomes = collectionService.getCollectionsByDate(year, month, hospId);
+        if (incomes.isEmpty()) {
+            throw new CostException("医院未归集本月收入数据");
+        }
+
+        // 得到这个月的所有成本数据
+        List<AllocationQuery> allocationQueries = allocationQueryService.getAllByDate(hospId, year, month);
+        if (allocationQueries.isEmpty()) {
+            throw new CostException("医院未分摊本月数据");
+        }
+        List<HospProfitAndLoss> list = new ArrayList<>();
+        List<ReportForm> parentForms = reportForms.stream().filter(i -> i.getParentId().equals(0L)).collect(Collectors.toList());
+        for (Responsibility responsibility : leafResp) {
+            for (ReportForm parentForm : parentForms) {
+                Long parentId = parentForm.getId();
+                List<ReportForm> children = reportForms.stream().filter(i -> i.getParentId().equals(parentId)).collect(Collectors.toList());
+                for (ReportForm child : children) {
+                    Integer calcType = child.getCalcType();
+                    if (calcType == CalcTypeEnum.BY_ACCOUNT.getType()) {
+                        // 按会计科目计算单的话
+                        calcByAccountByResp(hospId, child, list, month, year, responsibility);
+                    } else if (calcType == CalcTypeEnum.BY_SHARE_LEVEL.getType()) {
+                        // 分摊层级计算
+                        calcByShareLevelResp(hospId, child, list, year, month, responsibility);
+                    } else if (calcType == CalcTypeEnum.LITTER_COUNT.getType()) {
+                        // 处理小计 todo  默认认为 小计都是在同一个下面最后一个
+                        calcByLitterCountResp(year, month, child, children, list, hospId, responsibility);
+                    } else if (calcType == CalcTypeEnum.CALC_FORMULA.getType()) {
+                        // 按公式 (要保证总合计放到最后呀)
+                        calcByFormulaResp(year, month, child, list, responsibility);
+                    } else if (calcType == CalcTypeEnum.BY_RESPONSIBILITY.getType()) {
+                        // 责任中心
+                        calcByResponsibilityResp(hospId, child, list, year, month, responsibility);
+                    } else if (calcType == CalcTypeEnum.NO_CONFIG.getType()) {
+                        // 不设置不计算
+                    } else {
+
+                    }
+                }
+            }
+        }
+
+
+        // 处理医院其他收支
+        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 void calcByResponsibilityResp(Long hospId, ReportForm child, List<HospProfitAndLoss> list, int year, int month, Responsibility responsibility) {
+
+        List<RelationVO> responsibilities = reportRelationService.getResponsibilities(child.getId(), hospId);
+        if (responsibilities.isEmpty()) {
+            return;
+        }
+        List<String> responsibilityCodes = responsibilities.stream().map(RelationVO::getCode).collect(Collectors.toList());
+        AtomicReference<BigDecimal> calcTotal = new AtomicReference<>();
+        calcTotal.set(BigDecimal.ZERO);
+        List<AllocationQuery> allocationQueries = allocationQueryService.getByDateAndResp(year, month, hospId, responsibility.getResponsibilityCode());
+        responsibilityCodes.forEach(i -> {
+            BigDecimal costAmount = allocationQueries.stream().filter(j -> i.equals(j.getResponsibilityCode())).map(AllocationQuery::getAmount)
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
+            calcTotal.set(calcTotal.get().add(costAmount));
+        });
+        HospProfitAndLoss loss = new HospProfitAndLoss();
+        loss.setReportName(child.getReportName()).setReportNum(child.getNum()).setResponsibilityCode(responsibility.getResponsibilityCode()).setResponsibilityName(responsibility.getResponsibilityName())
+                .setAmount(calcTotal.get()).setCreateTime(System.currentTimeMillis()).setHospId(hospId).setDateYear(year).setDateMonth(month);
+        list.add(loss);
+
+    }
+
+    private void calcByFormulaResp(int year, int month, ReportForm child, List<HospProfitAndLoss> list, Responsibility responsibility) {
+        // 得到这个责任中心下所有的已经收集的数据
+        List<HospProfitAndLoss> hadCalcLosses = list.stream().filter(i -> i.getResponsibilityCode().equals(responsibility.getResponsibilityCode())).collect(Collectors.toList());
+        String calcFormula = child.getCalcFormula();
+        // TODO: 2021/8/27 校验公式合法性
+        if (StrUtil.isBlank(calcFormula)) {
+            throw new CostException("reportForm名称为" + child.getReportName() + "计算公式不正确");
+        }
+        BigDecimal bigDecimal = calcAmountResp(hadCalcLosses, calcFormula);
+
+        HospProfitAndLoss loss = new HospProfitAndLoss();
+        loss.setDateYear(year).setDateMonth(month).setReportNum(child.getNum()).setReportName(child.getReportName())
+                .setAmount(bigDecimal).setCreateTime(System.currentTimeMillis()).setHospId(child.getHospId()).setResponsibilityCode(responsibility.getResponsibilityCode()).setResponsibilityName(responsibility.getResponsibilityName());
+        list.add(loss);
+
+    }
+
+    private BigDecimal calcAmountResp(List<HospProfitAndLoss> hadCalcLosses, String calcFormula) {
+        // 得到所有的编号
+        String replace = calcFormula.replace("[", "").replace("]", "").replace("-", ",").replace("+", ",");
+        ArrayList<String> numList = CollUtil.newArrayList(replace.split(","));
+        // 得到编号的map
+        Map<Integer, Integer> numMap = new ConcurrentHashMap<>();
+        for (int j = 0; j < numList.size(); j++) {
+            numMap.put(j, Integer.parseInt(numList.get(j)));
+        }
+        List<String> expressions = ReUtil.findAll("[^0-9]", "+" + calcFormula.replace("[", "").replace("]", "").trim(), 0)
+                .stream().filter(StrUtil::isNotBlank).collect(Collectors.toList());
+        // 得到预算表达式 得到所有表达式的Map  +  -  相关的
+        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);
+        AtomicReference<BigDecimal> totalAmount = new AtomicReference<>();
+        totalAmount.set(BigDecimal.ZERO);
+        for (int i = 0; i < nums.size(); i++) {
+            // 编号
+            Integer num = numMap.get(i);
+            List<HospProfitAndLoss> losses = hadCalcLosses.stream().filter(item -> item.getReportNum().equals(num)).collect(Collectors.toList());
+            if (CollUtil.isEmpty(losses)) {
+                continue;
+            }
+            HospProfitAndLoss loss = losses.get(0);
+            BigDecimal amount = loss.getAmount();
+            String str = expressionMap.get(i);
+            if (str.equals("+")) {
+                totalAmount.set(totalAmount.get().add(amount));
+            } else {
+                totalAmount.set(totalAmount.get().subtract(amount));
+            }
+        }
+        return totalAmount.get();
+
+    }
+
+    private void calcByLitterCountResp(int year, int month, ReportForm child, List<ReportForm> children,
+                                       List<HospProfitAndLoss> list, Long hospId, Responsibility responsibility) {
+        List<HospProfitAndLoss> thisRespCodeList = list.stream().filter(i -> i.getResponsibilityCode().equals(responsibility.getResponsibilityCode())).collect(Collectors.toList());
+        List<ReportForm> thisForEachList = children.stream().filter(i -> !i.getCalcType().equals(CalcTypeEnum.LITTER_COUNT.getType())).collect(Collectors.toList());
+        List<HospProfitAndLoss> collect = thisRespCodeList.stream().filter(i -> thisForEachList.stream().map(ReportForm::getNum).collect(Collectors.toList()).contains(i.getReportNum())).collect(Collectors.toList());
+        if (collect.isEmpty()) {
+            return;
+        }
+        BigDecimal reduce = collect.stream().map(HospProfitAndLoss::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
+        HospProfitAndLoss loss = new HospProfitAndLoss();
+        loss.setDateYear(year).setDateMonth(month)
+                .setAmount(reduce).setCreateTime(System.currentTimeMillis()).setHospId(hospId);
+        // 小计只能有一个
+        loss.setReportNum(child.getNum()).setReportName(child.getReportName())
+                .setResponsibilityCode(responsibility.getResponsibilityCode())
+                .setResponsibilityName(responsibility.getResponsibilityName());
+        list.add(loss);
+    }
+
+    private void calcByShareLevelResp(Long hospId, ReportForm child, List<HospProfitAndLoss> list, int year, int month, Responsibility responsibility) {
+        List<RelationVO> shareLevels = reportRelationService.getShareLevel(child.getId(), hospId);
+        List<Long> shareLevelId = shareLevels.stream().map(RelationVO::getCode).map(Long::valueOf).sorted(Long::compareTo).collect(Collectors.toList());
+        if (CollUtil.isEmpty(shareLevelId)) {
+            return;
+        }
+        List<CostShareLevel> costShareLevels = shareLevelService.listByIds(shareLevelId);
+        if (costShareLevels.isEmpty()) {
+            throw new CostException("医院分摊层级设置错误," + child.getReportName());
+        }
+        List<Integer> levelSorts = costShareLevels.stream().map(CostShareLevel::getLeverSort).collect(Collectors.toList());
+        List<AllocationQuery> allocations = allocationQueryService.getByDateRespn(year, month, hospId, shareLevelId, responsibility.getResponsibilityCode());
+
+        if (allocations.isEmpty()) {
+            throw new CostException("医院未分摊本月数据");
+        }
+        List<CostAccountShare> accountShares = accountShareService.getByShareLevelSort(levelSorts, hospId);
+        if (accountShares.isEmpty()) {
+            return;
+        }
+
+        BigDecimal reduce = allocations.stream().map(AllocationQuery::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
+        HospProfitAndLoss loss = new HospProfitAndLoss();
+        loss.setReportName(child.getReportName()).setReportNum(child.getNum())
+                .setAmount(reduce).setCreateTime(System.currentTimeMillis()).setHospId(hospId).setDateYear(year).setDateMonth(month)
+                .setResponsibilityCode(responsibility.getResponsibilityCode()).setResponsibilityName(responsibility.getResponsibilityName())
+        ;
+        list.add(loss);
+
+
+    }
+
+    private void calcByAccountByResp(Long hospId, ReportForm child, List<HospProfitAndLoss> list, int month, int year, Responsibility responsibility) {
+
+        // 报表项目关联的会计科目对象
+        List<RelationVO> accountRelations = reportRelationService.getAccountRelation(child.getId(), hospId);
+        if (accountRelations.isEmpty()) {
+            return;
+        }
+        List<String> accountCodes = accountRelations.stream().map(RelationVO::getCode).collect(Collectors.toList());
+        AtomicReference<BigDecimal> calcTotal = new AtomicReference<>();
+        calcTotal.set(BigDecimal.ZERO);
+        List<IncomeCollection> incomes = collectionService.getCollectionsByDateAndResp(year, month, hospId, responsibility.getResponsibilityCode());
+        List<AllocationQuery> allocationQueries = allocationQueryService.getByDateAndResp(year, month, hospId, responsibility.getResponsibilityCode());
+        accountCodes.forEach(i -> {
+            BigDecimal incomeAmount = incomes.stream().filter(j -> i.equals(j.getAccountingCode())).map(IncomeCollection::getAmount)
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
+            BigDecimal costAmount = allocationQueries.stream().filter(j -> i.equals(j.getAccountingCode())).map(AllocationQuery::getAmount)
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
+            BigDecimal total = incomeAmount.add(costAmount);
+            calcTotal.set(calcTotal.get().add(total));
+        });
+        HospProfitAndLoss loss = new HospProfitAndLoss();
+        loss.setReportName(child.getReportName()).setReportNum(child.getNum())
+                .setAmount(calcTotal.get()).setCreateTime(System.currentTimeMillis()).setHospId(hospId).setDateYear(year).setDateMonth(month)
+                .setResponsibilityCode(responsibility.getResponsibilityCode()).setResponsibilityName(responsibility.getResponsibilityName())
+        ;
+        list.add(loss);
+
+    }
 }

+ 12 - 0
src/main/java/com/imed/costaccount/service/impl/IncomeCollectionServiceImpl.java

@@ -420,4 +420,16 @@ public class IncomeCollectionServiceImpl
         );
         return list;
     }
+
+    @Override
+    public List<IncomeCollection> getCollectionsByDateAndResp(int year, int month, Long hospId, String responsibilityCode) {
+        List<IncomeCollection> list = this.list(
+                new LambdaQueryWrapper<IncomeCollection>()
+                        .eq(IncomeCollection::getYear, year)
+                        .eq(IncomeCollection::getMonth, month)
+                        .eq(IncomeCollection::getHospId, hospId)
+                        .eq(IncomeCollection::getResponsibilityCode, responsibilityCode)
+        );
+        return list;
+    }
 }

+ 1 - 1
src/main/java/com/imed/costaccount/service/impl/ReportFormServiceImpl.java

@@ -377,7 +377,7 @@ public class ReportFormServiceImpl extends ServiceImpl<ReportFormMapper, ReportF
                 new LambdaQueryWrapper<ReportForm>()
                         .eq(ReportForm::getReportType, type)
                         .eq(ReportForm::getHospId, hospId)
-                        .orderByAsc(ReportForm::getSort)
+//                        .orderByAsc(ReportForm::getSort)
         );
         return list;
     }

+ 2 - 1
src/main/java/com/imed/costaccount/web/HospProfitAndLossController.java

@@ -25,7 +25,8 @@ public class HospProfitAndLossController extends AbstractController {
     @ApiOperation("计算全院损益")
     @PostMapping("/calc")
     public Result calc(@RequestParam @ApiParam(name = "date", value = "yyyy-MM-dd") String date) {
-        hospProfitAndLossService.calc(date, getHospId());
+//        hospProfitAndLossService.calc(date, getHospId());
+        hospProfitAndLossService.calcByResponsibility(date, getHospId());
         return Result.ok();
     }
 

+ 7 - 3
src/main/resources/mapper/AllocationMapper.xml

@@ -31,14 +31,18 @@
         <result property="deleteTime" column="delete_time"/>
     </resultMap>
     <select id="queryAfterAllocationList" resultType="com.imed.costaccount.model.vo.AfterAllocationVO">
-        select *,sum(amount) as amount from cost_allocation_query where delete_time = 0 and hosp_id = #{hospId}
+        select id as id , date_year as dateYear,date_month as dateMonth, level_sort as levelSort, level_name as
+        levelName,
+        target_responsibility_code as responsibilityCode, target_responsibility_name as responsibilityName,
+        accounting_code as accountingCode, accounting_name as accountingName,
+        sum(amount) as amount from cost_allocation_query where delete_time = 0 and hosp_id = #{hospId}
         <if test="dateYear != null">
             and date_year = #{dateYear} and date_month = #{dateMonth}
         </if>
         <if test="responsibilityCode != null and responsibilityCode != ''">
             and responsibility_code = #{responsibilityCode}
         </if>
-        group by accounting_code
+        group by accounting_code , target_responsibility_code ,share_level_id
         limit #{startIndex},#{pageSize}
     </select>
     <select id="queryAfterAllocationListCount" resultType="java.lang.Integer">
@@ -50,7 +54,7 @@
         <if test="responsibilityCode != null and responsibilityCode != ''">
             and responsibility_code = #{responsibilityCode}
         </if>
-        group by accounting_code)t
+        group by accounting_code, target_responsibility_code,share_level_id)t
     </select>
     <select id="queryAfterAllocationListSum" resultType="java.math.BigDecimal">
         select sum(amount) from cost_allocation_query where delete_time = 0 and hosp_id = #{hospId}

+ 3 - 0
src/main/resources/mapper/AllocationQueryMapper.xml

@@ -16,6 +16,9 @@
         <result property="amount" column="amount"/>
         <result property="accountingCode" column="accounting_code"/>
         <result property="accountingName" column="accounting_name"/>
+        <result property="targetResponsibilityCode" column="target_responsibility_code"/>
+        <result property="targetResponsibilityName" column="target_responsibility_code"/>
+        <result property="shareLevelId" column="share_level_id"/>
         <result property="createTime" column="create_time"/>
         <result property="deleteTime" column="delete_time"/>
     </resultMap>