瀏覽代碼

Merge branch 'master' of huangrui/CostAccount into dev

lijiaxi 4 年之前
父節點
當前提交
0ab719baf8

+ 49 - 44
src/main/java/com/imed/costaccount/model/HospProfitAndLoss.java

@@ -1,6 +1,7 @@
 package com.imed.costaccount.model;
 
 import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
 import com.baomidou.mybatisplus.annotation.TableName;
 
 import java.math.BigDecimal;
@@ -14,9 +15,9 @@ import lombok.experimental.Accessors;
 
 /**
  * 全院损益表
- * 
+ *
  * @author huangrui
- * @email 
+ * @email
  * @date 2021-08-27 14:20:53
  */
 @Data
@@ -25,49 +26,53 @@ import lombok.experimental.Accessors;
 @NoArgsConstructor
 @TableName("cost_hosp_profit_and_loss")
 public class HospProfitAndLoss implements Serializable {
-	private static final long serialVersionUID = 1L;
+    private static final long serialVersionUID = 1L;
 
-	/**
-	 * 主键
-	 */
-	@TableId
-	private Long id;
-	/**
-	 * 年
-	 */
-	private Integer dateYear;
-	/**
-	 * 月
-	 */
-	private Integer dateMonth;
-	/**
-	 * 报表项目id
+    /**
+     * 主键
+     */
+    @TableId
+    private Long id;
+    /**
+     * 年
+     */
+    private Integer dateYear;
+    /**
+     * 月
+     */
+    private Integer dateMonth;
+    /**
+     * 报表项目id
+     */
+    private Integer reportNum;
+    /**
+     * 报表项目名称
+     */
+    private String reportName;
+    /**
+     * 成本类型
+     */
+    private Integer costType;
+    /**
+     * 收入类型
+     */
+    private Integer incomeType;
 
-	 */
-	private Integer reportNum;
-	/**
-	 * 报表项目名称
-	 */
-	private String reportName;
-	/**
-	 * 成本类型
-	 */
-	private Integer costType;
-	/**
-	 * 收入类型
-	 */
-	private Integer incomeType;
-	/**
-	 * 金额
-	 */
-	private BigDecimal amount;
-	/**
-	 * 创建时间
-	 */
-	private Long createTime;
-	/**
-	 * 删除时间
-	 */
-	private Long deleteTime;
+    // 0. 报表项目 1. 全院其他收支
+    private Integer originType;
+    /**
+     * 金额
+     */
+    private BigDecimal amount;
+    private Long hospId;
+    /**
+     * 创建时间
+     */
+    private Long createTime;
+    /**
+     * 删除时间
+     */
+    @TableLogic(value = "0", delval = "UNIX_TIMESTAMP(NOW()) * 1000")
+    private Long deleteTime;
 
 }

+ 11 - 0
src/main/java/com/imed/costaccount/service/CostOtherPaymentsDataService.java

@@ -6,6 +6,8 @@ import com.imed.costaccount.model.CostOtherPaymentsData;
 import com.imed.costaccount.model.dto.CostOtherPaymentsDataEditDto;
 import com.imed.costaccount.model.dto.CostOtherPaymentsDataSaveDto;
 
+import java.util.List;
+
 /**
  * 全院其他收支数据
  *
@@ -37,5 +39,14 @@ public interface CostOtherPaymentsDataService extends IService<CostOtherPayments
      * @param hospId
      */
     void updateOtherPaymentData(CostOtherPaymentsDataEditDto costOtherPaymentsDataEditDto, Long hospId);
+
+    /**
+     * 某个月全月其他收支
+     * @param year 年
+     * @param month 月
+     * @param hospId 医院id
+     * @return
+     */
+    List<CostOtherPaymentsData> getByMonth(int year, int month, Long hospId);
 }
 

+ 32 - 12
src/main/java/com/imed/costaccount/service/impl/CostOtherPaymentsDataServiceImpl.java

@@ -2,6 +2,7 @@ package com.imed.costaccount.service.impl;
 
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -32,10 +33,10 @@ public class CostOtherPaymentsDataServiceImpl extends ServiceImpl<CostOtherPayme
     /**
      * 分页查询
      *
-     * @param current 第几页
+     * @param current  第几页
      * @param pageSize 每页大小
      * @param dateTime 时间
-     * @param hospId 医院Id
+     * @param hospId   医院Id
      * @return
      */
     @Override
@@ -51,8 +52,8 @@ public class CostOtherPaymentsDataServiceImpl extends ServiceImpl<CostOtherPayme
         Page<CostOtherPaymentsData> paymentsDataPage = new Page<>(current, pageSize);
         Page<CostOtherPaymentsData> pages = this.page(paymentsDataPage, new QueryWrapper<CostOtherPaymentsData>().lambda()
                 .eq(CostOtherPaymentsData::getHospId, hospId)
-                .eq(StrUtil.isNotBlank(dateTime),CostOtherPaymentsData::getDateYear,year)
-                .eq(StrUtil.isNotBlank(dateTime),CostOtherPaymentsData::getDateMonth,month)
+                .eq(StrUtil.isNotBlank(dateTime), CostOtherPaymentsData::getDateYear, year)
+                .eq(StrUtil.isNotBlank(dateTime), CostOtherPaymentsData::getDateMonth, month)
                 .orderByDesc(CostOtherPaymentsData::getCreateTime));
         List<CostOtherPaymentsData> records = pages.getRecords();
         List<CostOtherPaymentsDataVO> costOtherPaymentsDataVOList = BeanUtil.convertList(records, CostOtherPaymentsDataVO.class);
@@ -65,10 +66,10 @@ public class CostOtherPaymentsDataServiceImpl extends ServiceImpl<CostOtherPayme
      * 保存全院其他收支数据
      *
      * @param costOtherPaymentsDataSaveDto 全院其他收支
-     * @param hospId 医院的Id
+     * @param hospId                       医院的Id
      */
     @Override
-    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
+    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
     public void addOtherPaymentData(CostOtherPaymentsDataSaveDto costOtherPaymentsDataSaveDto, Long hospId) {
         String dateTime = costOtherPaymentsDataSaveDto.getDateTime();
         // 先检验当前年月是否存在数据
@@ -87,6 +88,7 @@ public class CostOtherPaymentsDataServiceImpl extends ServiceImpl<CostOtherPayme
         costOtherPaymentsData.setDateMonth(month);
         this.save(costOtherPaymentsData);
     }
+
     // 先检验当前年月是否存在数据
     private void checkOtherPaymentData(CostOtherPaymentsDataSaveDto costOtherPaymentsDataSaveDto, Long hospId, String dateTime) {
         int year = 0;
@@ -102,8 +104,8 @@ public class CostOtherPaymentsDataServiceImpl extends ServiceImpl<CostOtherPayme
                 .eq(CostOtherPaymentsData::getHospId, hospId)
                 .eq(CostOtherPaymentsData::getDateYear, year).eq(CostOtherPaymentsData::getDateMonth, month)
                 .eq(CostOtherPaymentsData::getPaymentsType, paymentsType).eq(CostOtherPaymentsData::getPaymentsName, paymentsName));
-        if (Objects.nonNull(paymentsData)){
-            throw new CostException(500,year+"年"+month+"月"+paymentsName+"已存在");
+        if (Objects.nonNull(paymentsData)) {
+            throw new CostException(500, year + "年" + month + "月" + paymentsName + "已存在");
         }
     }
 
@@ -114,7 +116,7 @@ public class CostOtherPaymentsDataServiceImpl extends ServiceImpl<CostOtherPayme
      * @param hospId
      */
     @Override
-    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
+    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
     public void updateOtherPaymentData(CostOtherPaymentsDataEditDto costOtherPaymentsDataEditDto, Long hospId) {
         String dateTime = costOtherPaymentsDataEditDto.getDateTime();
         int year = 0;
@@ -126,13 +128,13 @@ public class CostOtherPaymentsDataServiceImpl extends ServiceImpl<CostOtherPayme
         }
         Long id = costOtherPaymentsDataEditDto.getId();
         CostOtherPaymentsData otherPaymentsData = this.getById(id);
-        if (Objects.isNull(otherPaymentsData)){
-            throw new CostException(500,"全院其他收支数据不存在");
+        if (Objects.isNull(otherPaymentsData)) {
+            throw new CostException(500, "全院其他收支数据不存在");
         }
         this.removeById(id);
         CostOtherPaymentsDataSaveDto costOtherPaymentsDataSaveDto = BeanUtil.convertObj(costOtherPaymentsDataEditDto, CostOtherPaymentsDataSaveDto.class);
         // 检验数据
-        checkOtherPaymentData(costOtherPaymentsDataSaveDto,hospId,dateTime);
+        checkOtherPaymentData(costOtherPaymentsDataSaveDto, hospId, dateTime);
         // 实现数据添加
         CostOtherPaymentsData costOtherPaymentsData = BeanUtil.convertObj(costOtherPaymentsDataSaveDto, CostOtherPaymentsData.class);
         costOtherPaymentsData.setCreateTime(System.currentTimeMillis());
@@ -142,4 +144,22 @@ public class CostOtherPaymentsDataServiceImpl extends ServiceImpl<CostOtherPayme
         this.save(costOtherPaymentsData);
     }
 
+
+    /**
+     * 某个月全月其他收支
+     *
+     * @param year   年
+     * @param month  月
+     * @param hospId 医院id
+     * @return
+     */
+    @Override
+    public List<CostOtherPaymentsData> getByMonth(int year, int month, Long hospId) {
+        return this.list(
+                new LambdaQueryWrapper<CostOtherPaymentsData>()
+                        .eq(CostOtherPaymentsData::getDateYear, year)
+                        .eq(CostOtherPaymentsData::getDateMonth, month)
+                        .eq(CostOtherPaymentsData::getHospId, hospId)
+        );
+    }
 }

+ 142 - 93
src/main/java/com/imed/costaccount/service/impl/HospProfitAndLossServiceImpl.java

@@ -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);
 
     }
 }

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

@@ -6,11 +6,13 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 @Api(tags = "全院损益相关")
-@RestController("/costAccount/hospProfitAndLoss")
+@RestController
+@RequestMapping("/costAccount/hospProfitAndLoss")
 public class HospProfitAndLossController extends AbstractController{
 
     private final HospProfitAndLossService hospProfitAndLossService;

+ 2 - 0
src/main/resources/mapper/HospProfitAndLossMapper.xml

@@ -13,8 +13,10 @@
         <result property="costType" column="cost_type"/>
         <result property="incomeType" column="income_type"/>
         <result property="amount" column="amount"/>
+        <result property="originType" column="origin_type"/>
         <result property="createTime" column="create_time"/>
         <result property="deleteTime" column="delete_time"/>
+        <result property="hospId" column="hosp_id"/>
     </resultMap>