Explorar o código

Merge branch 'master' of huangrui/CostAccount into dev

lijiaxi %!s(int64=4) %!d(string=hai) anos
pai
achega
d51339caa7
Modificáronse 33 ficheiros con 2203 adicións e 26 borrados
  1. 1 0
      .gitignore
  2. 1 0
      src/main/java/com/imed/costaccount/common/shiro/ShiroConfig.java
  3. 3 1
      src/main/java/com/imed/costaccount/mapper/AllocationMapper.java
  4. 28 0
      src/main/java/com/imed/costaccount/mapper/AllocationMapper.xml
  5. 34 0
      src/main/java/com/imed/costaccount/mapper/AllocationQueryMapper.java
  6. 11 2
      src/main/java/com/imed/costaccount/mapper/CostCostingCollectionMapper.java
  7. 16 0
      src/main/java/com/imed/costaccount/mapper/CostDepartmentProfitMapper.java
  8. 94 0
      src/main/java/com/imed/costaccount/model/AllocationQuery.java
  9. 82 0
      src/main/java/com/imed/costaccount/model/CostDepartmentProfit.java
  10. 94 0
      src/main/java/com/imed/costaccount/model/vo/AllocationReportVO.java
  11. 2 1
      src/main/java/com/imed/costaccount/model/vo/CostCostingVO.java
  12. 56 0
      src/main/java/com/imed/costaccount/model/vo/CostDepartmentProfitVO.java
  13. 34 0
      src/main/java/com/imed/costaccount/model/vo/ResponsibilityAccountVO.java
  14. 28 0
      src/main/java/com/imed/costaccount/model/vo/SplitLevelVO.java
  15. 30 0
      src/main/java/com/imed/costaccount/model/vo/TargetShareParamValue.java
  16. 44 0
      src/main/java/com/imed/costaccount/model/vo/TargetSplitLevelVO.java
  17. 39 0
      src/main/java/com/imed/costaccount/service/AllocationQueryService.java
  18. 26 0
      src/main/java/com/imed/costaccount/service/AllocationService.java
  19. 10 1
      src/main/java/com/imed/costaccount/service/CostCostingCollectionService.java
  20. 33 0
      src/main/java/com/imed/costaccount/service/CostDepartmentProfitService.java
  21. 62 0
      src/main/java/com/imed/costaccount/service/impl/AllocationQueryServiceImpl.java
  22. 390 11
      src/main/java/com/imed/costaccount/service/impl/AllocationServiceImpl.java
  23. 526 0
      src/main/java/com/imed/costaccount/service/impl/CostCostingCollectionServiceImpl.java
  24. 225 0
      src/main/java/com/imed/costaccount/service/impl/CostDepartmentProfitServiceImpl.java
  25. 1 1
      src/main/java/com/imed/costaccount/service/impl/ReportRelationServiceImpl.java
  26. 3 0
      src/main/java/com/imed/costaccount/web/CostCostingCollectionController.java
  27. 18 6
      src/main/java/com/imed/costaccount/web/CostCostingGroupController.java
  28. 98 0
      src/main/java/com/imed/costaccount/web/CostDepartmentProfitController.java
  29. 72 2
      src/main/java/com/imed/costaccount/web/ExcelController.java
  30. 81 0
      src/main/resources/mapper/AllocationQueryMapper.xml
  31. 5 0
      src/main/resources/mapper/CostCostingCollectionMapper.xml
  32. 24 0
      src/main/resources/mapper/CostDepartmentProfitMapper.xml
  33. 32 1
      src/test/java/com/imed/costaccount/service/impl/AllocationServiceImplTest.java

+ 1 - 0
.gitignore

@@ -19,6 +19,7 @@ target/
 *.iml
 *.ipr
 logs/*
+Demo*.java
 
 ### NetBeans ###
 /nbproject/private/

+ 1 - 0
src/main/java/com/imed/costaccount/common/shiro/ShiroConfig.java

@@ -42,6 +42,7 @@ public class ShiroConfig {
         filterMap.put("/v2/api-docs", "anon");
         filterMap.put("/swagger-ui.html", "anon");
         filterMap.put("/static/js/**", "anon");
+        filterMap.put("/demo", "anon");
         filterMap.put("/doc.html", "anon");
         filterMap.put("/swagger-resources/**", "anon");
         filterMap.put("/captcha.jpg", "anon");

+ 3 - 1
src/main/java/com/imed/costaccount/mapper/AllocationMapper.java

@@ -1,6 +1,8 @@
 package com.imed.costaccount.mapper;
 
+
 import com.imed.costaccount.model.Allocation;
+
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.imed.costaccount.model.vo.AfterAllocationVO;
 import org.apache.ibatis.annotations.Mapper;
@@ -13,7 +15,7 @@ import java.util.List;
  * 成本分摊后表
  * 
  * @author huangrui
- * @date 2021-08-24 16:05:16
+ * @date 2021-08-25 08:45:06
  */
 @Mapper
 public interface AllocationMapper extends BaseMapper<Allocation> {

+ 28 - 0
src/main/java/com/imed/costaccount/mapper/AllocationMapper.xml

@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="com.imed.costaccount.mapper.AllocationMapper">
+
+	<!-- 可根据自己的需求,是否要使用 -->
+    <resultMap type="com.imed.costaccount.model.Allocation" id="allocationMap">
+        <result property="id" column="id"/>
+        <result property="dateYear" column="date_year"/>
+        <result property="dateMonth" column="date_month"/>
+        <result property="levelSort" column="level_sort"/>
+        <result property="levelName" column="level_name"/>
+        <result property="hospId" column="hosp_id"/>
+        <result property="responsibilityCode" column="responsibility_code"/>
+        <result property="responsibilityName" column="responsibility_name"/>
+        <result property="accountShareId" column="account_share_id"/>
+        <result property="isBaseCost" column="is_base_cost"/>
+        <result property="amount" column="amount"/>
+        <result property="targetResponsibilityCode" column="target_responsibility_code"/>
+        <result property="targetResponsibilityName" column="target_responsibility_name"/>
+        <result property="shareParamCode" column="share_param_code"/>
+        <result property="shareParamName" column="share_param_name"/>
+        <result property="createTime" column="create_time"/>
+        <result property="deleteTime" column="delete_time"/>
+    </resultMap>
+
+
+</mapper>

+ 34 - 0
src/main/java/com/imed/costaccount/mapper/AllocationQueryMapper.java

@@ -0,0 +1,34 @@
+package com.imed.costaccount.mapper;
+
+import com.imed.costaccount.model.AllocationQuery;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.imed.costaccount.model.vo.CodeAndNameVO;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.math.BigDecimal;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * 
+ * 
+ * @author huangrui
+ * @date 2021-08-26 08:51:45
+ */
+@Mapper
+public interface AllocationQueryMapper extends BaseMapper<AllocationQuery> {
+
+    BigDecimal getTotalMoney(@Param("dateYear") int dateYear, @Param("month") int month, @Param("hospId") Long hospId);
+
+    List<CodeAndNameVO> getRespCodeAndName(@Param("hospId") Long hospId, @Param("dateYear") int dateYear, @Param("month") int month);
+
+    List<CodeAndNameVO> getAccountCodeAndName(@Param("hospId") Long hospId, @Param("dateYear") int dateYear, @Param("month") int month);
+
+    BigDecimal getCountByRespAndAccounts(@Param("hospId") Long hospId, @Param("dateYear") int dateYear, @Param("month") int month, @Param("code") String code, @Param("accountCodes") List<String> accountCodes);
+
+    BigDecimal getTotalByAccountAndResps(@Param("hospId") Long hospId, @Param("dateYear") int dateYear, @Param("month") int month, @Param("code") String code, @Param("respCodes") List<String> respCodes);
+
+    BigDecimal getTotalByAccountAndRespCode(@Param("hospId") Long hospId, @Param("dateYear") int dateYear, @Param("month") int month, @Param("accountCode") String accountCode, @Param("responsibilityCode") String responsibilityCode);
+
+}

+ 11 - 2
src/main/java/com/imed/costaccount/mapper/CostCostingCollectionMapper.java

@@ -1,8 +1,12 @@
 package com.imed.costaccount.mapper;
 
-import com.imed.costaccount.model.CostCostingCollection;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.imed.costaccount.model.CostCostingCollection;
+import com.imed.costaccount.model.vo.CostDepartmentProfitVO;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 /**
  * 成本数据归集后列表
@@ -12,5 +16,10 @@ import org.apache.ibatis.annotations.Mapper;
  */
 @Mapper
 public interface CostCostingCollectionMapper extends BaseMapper<CostCostingCollection> {
-	
+    /**
+     * 科室损益计算  按照会计科目计算方式  查询责任中心下的所有值
+     * @param hospId
+     * @return
+     */
+    List<CostDepartmentProfitVO> selectGroupBy(@Param("hospId") Long hospId);
 }

+ 16 - 0
src/main/java/com/imed/costaccount/mapper/CostDepartmentProfitMapper.java

@@ -0,0 +1,16 @@
+package com.imed.costaccount.mapper;
+
+import com.imed.costaccount.model.CostDepartmentProfit;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * 科室损益计算
+ * 
+ * @author KCYG
+ * @date 2021-08-24 16:24:08
+ */
+@Mapper
+public interface CostDepartmentProfitMapper extends BaseMapper<CostDepartmentProfit> {
+	
+}

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

@@ -0,0 +1,94 @@
+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;
+import java.io.Serializable;
+import java.util.Date;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.experimental.Accessors;
+
+/**
+ * 
+ * 
+ * @author huangrui
+ * @email 
+ * @date 2021-08-26 08:51:45
+ */
+@Data
+@Accessors(chain = true)
+@AllArgsConstructor
+@NoArgsConstructor
+@TableName("cost_allocation_query")
+public class AllocationQuery implements Serializable {
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * 主键
+	 */
+	@TableId
+	private Long id;
+	/**
+	 * 年
+	 */
+	private Integer dateYear;
+	/**
+	 * 月
+	 */
+	private Integer dateMonth;
+//	/**
+//	 * 分摊层级序号
+//	 */
+//	private Integer levelSort;
+//	/**
+//	 * 分摊层级数名称
+//	 */
+//	private String levelName;
+	/**
+	 * 医院id
+	 */
+	private Long hospId;
+	/**
+	 * 责任中心代码
+	 */
+	private String responsibilityCode;
+	/**
+	 * 责任中心名称
+	 */
+	private String responsibilityName;
+	/**
+	 * 来源id
+	 */
+	private Long originId;
+	/**
+	 * 来源类型
+	 */
+	private Long originType;
+	/**
+	 * 金额
+	 */
+	private BigDecimal amount;
+	/**
+	 * 会计科目
+	 */
+	private String accountingCode;
+	/**
+	 * 会计科目名称
+	 */
+	private String accountingName;
+	/**
+	 * 创建时间
+	 */
+	private Long createTime;
+	/**
+	 * 删除时间
+	 */
+	@TableLogic(value = "0",delval = "TIME_STAMP(NOW()) * 1000")
+	private Long deleteTime;
+
+}

+ 82 - 0
src/main/java/com/imed/costaccount/model/CostDepartmentProfit.java

@@ -0,0 +1,82 @@
+package com.imed.costaccount.model;
+
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+
+/**
+ * 科室损益计算
+ * 
+ * @author KCYG
+ * @email KCYG@xinxicom
+ * @date 2021-08-24 16:24:08
+ */
+@Data
+@Accessors(chain = true)
+@AllArgsConstructor
+@NoArgsConstructor
+@TableName("cost_department_profit")
+public class CostDepartmentProfit implements Serializable {
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * 主键
+	 */
+	@TableId
+	private Long id;
+	/**
+	 * 年份
+	 */
+	private Integer year;
+	/**
+	 * 月份
+	 */
+	private Integer month;
+	/**
+	 * 报表项目编号
+	 */
+	private String reportNum;
+	/**
+	 * 报表项目
+	 */
+	private String reportName;
+	/**
+	 * 责任中心代码
+	 */
+	private String responsibilityCode;
+	/**
+	 * 责任中心名称
+	 */
+	private String responsibilityName;
+	/**
+	 * 成本类型
+	 */
+	private Integer costType;
+	/**
+	 * 收入类型
+	 */
+	private Integer incomeType;
+	/**
+	 * 金额
+	 */
+	private BigDecimal amount;
+	/**
+	 * 医院id
+	 */
+	private Long hospId;
+	/**
+	 * 创建时间
+	 */
+	private Long createTime;
+	/**
+	 * 删除时间
+	 */
+	private Long deleteTime;
+
+}

+ 94 - 0
src/main/java/com/imed/costaccount/model/vo/AllocationReportVO.java

@@ -0,0 +1,94 @@
+package com.imed.costaccount.model.vo;
+
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+/**
+ * @author 李加喜
+ * @Package com.imed.costaccount.model
+ * @date 2021-08-25 9:56
+ */
+@Data
+@ApiModel("成本分摊报表输出")
+public class AllocationReportVO {
+    /**
+     * 责任中心代码
+     */
+    private String responsibilityCode;
+    /**
+     * 责任中心名称
+     */
+    private String responsibilityName;
+    /**
+     * 分摊层级序号
+     */
+    private Integer levelSort;
+    /**
+     * 分摊设置对应的Id
+     */
+    private Long accountShareId;
+    // TODO 自己获取
+    /**
+     * 会计科目代码
+     */
+    private String accountCode;
+    /**
+     *  会计科目名称
+     */
+    private String accountName;
+    /**
+     * 别名
+     */
+    private String alias;
+    /**
+     * 成本金额
+     */
+    private BigDecimal totalAmount;
+    /**
+     * 成本金额字符串类型  Excel显示
+     */
+    private String totalAmounts;
+    /**
+     * 目标层级责任中心代码
+     */
+    private String targetResponsibilityCode;
+    /**
+     * 目标层级责任中心名称
+     */
+    private String targetResponsibilityName;
+    /**
+     * 目标层级责任中心第n次分摊金额
+     */
+    private List<String> targetShareMoneys;
+    /**
+     * 分摊参数
+     */
+    private String shareParamName;
+    /**
+     * 分摊数值
+     */
+    private BigDecimal shareParamValueNum;
+    /**
+     * 分摊数值字符串类型  Excel显示
+     */
+    private String shareParamValueNums;
+    /**
+     *  分摊比例
+     */
+    private BigDecimal shareParamRate;
+    /**
+     * 分摊比例字符串类型   Excel显示
+     */
+    private String shareParamRates;
+    /**
+     * 各参数分摊到的成本金额
+     */
+    private BigDecimal amount;
+    /**
+     * 各参数分摊到的成本金额字符串类型  amounts
+     */
+    private String amounts;
+}

+ 2 - 1
src/main/java/com/imed/costaccount/model/vo/CostCostingVO.java

@@ -21,6 +21,7 @@ public class CostCostingVO {
      * 责任中心名称
      */
     private String responsibilityName;
+    // TODO 自己获取
     /**
      * 会计科目代码
      */
@@ -36,7 +37,7 @@ public class CostCostingVO {
     /**
      * 成本金额
      */
-    private String accountMoney;
+    private String amount;
     /**
      * 目标层级责任中心代码
      */

+ 56 - 0
src/main/java/com/imed/costaccount/model/vo/CostDepartmentProfitVO.java

@@ -0,0 +1,56 @@
+package com.imed.costaccount.model.vo;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+/**
+ * @author 李加喜
+ * @Package com.imed.costaccount.model.vo
+ * @date 2021-08-24 17:22
+ */
+@Data
+@ApiModel("科室损益计算返回类型")
+public class CostDepartmentProfitVO {
+
+    @ApiModelProperty(name = "id",value = "主键")
+    private Long id;
+
+    @ApiModelProperty(name = "year",value = "年")
+    private Integer year;
+
+    @ApiModelProperty(name = "month",value = "月")
+    private Integer month;
+
+    @ApiModelProperty(name = "reportParentId",value = "报表父级Id")
+    private Long reportParentId;
+
+    @ApiModelProperty(name = "levelSort",value = "分摊层级编号")
+    private Integer levelSort;
+
+    @ApiModelProperty(name = "reportNum",value = "报表项目编号")
+    private Integer reportNum;
+
+    @ApiModelProperty(name = "reportName",value = "报表项目名称")
+    private String reportName;
+
+    @ApiModelProperty(name = "responsibilityCode",value = "责任中心的代码")
+    private String responsibilityCode;
+
+    @ApiModelProperty(name = "responsibilityName",value = "责任中心名称")
+    private String responsibilityName;
+
+    @ApiModelProperty(name = "costType",value = "成本类型")
+    private Integer costType;
+
+    @ApiModelProperty(name = "incomeType",value = "收入类型")
+    private Integer incomeType;
+
+    @ApiModelProperty(name = "hospId",value = "医院类型")
+    private Long hospId;
+
+    @ApiModelProperty(name = "amount",value = "金额")
+    private BigDecimal amount;
+}

+ 34 - 0
src/main/java/com/imed/costaccount/model/vo/ResponsibilityAccountVO.java

@@ -0,0 +1,34 @@
+package com.imed.costaccount.model.vo;
+
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+
+/**
+ * @author 李加喜
+ * @Package com.imed.costaccount.model.vo
+ * @date 2021-08-23 11:24
+ */
+@Data
+@ApiModel("责任中心对应会计科目列表")
+public class ResponsibilityAccountVO {
+    /**
+     * 会计科目代码
+     */
+    private String accountingIds;
+    /**
+     * 会计科目的名称
+     */
+    private String accountingNames;
+    /**
+     * 会计科目的代码
+     */
+    private String accountingCodes;
+    /**
+     * 别名
+     */
+    private String alias;
+    /**
+     * 金额
+     */
+    private String money;
+}

+ 28 - 0
src/main/java/com/imed/costaccount/model/vo/SplitLevelVO.java

@@ -0,0 +1,28 @@
+package com.imed.costaccount.model.vo;
+
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @author 李加喜
+ * @Package com.imed.costaccount.model.vo
+ * @date 2021-08-23 11:20
+ */
+@Data
+@ApiModel("分摊层级责任中心相关数据")
+public class SplitLevelVO {
+    /**
+     * 责任中心代码
+     */
+    private String responsibilityCode;
+    /**
+     * 责任中心名称
+     */
+    private String responsibilityName;
+    /**
+     * 当前责任中心下面所有的会计科目和别名信息
+     */
+    private List<ResponsibilityAccountVO> responsibilityAccountVOList;
+}

+ 30 - 0
src/main/java/com/imed/costaccount/model/vo/TargetShareParamValue.java

@@ -0,0 +1,30 @@
+package com.imed.costaccount.model.vo;
+
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+
+/**
+ * @author 李加喜
+ * @Package com.imed.costaccount.model.vo
+ * @date 2021-08-23 11:39
+ */
+@Data
+@ApiModel("报表目标责任中心数据的分摊参数数据")
+public class TargetShareParamValue {
+    /**
+     *  分摊参数代码
+     */
+    private String shareParamCode;
+    /**
+     *  分摊参数名称
+     */
+    private String shareParamName;
+    /**
+     *  分摊参数值
+     */
+    private String shareParamValue;
+    /**
+     * 参数比例
+     */
+    private String shareParamProportion;
+}

+ 44 - 0
src/main/java/com/imed/costaccount/model/vo/TargetSplitLevelVO.java

@@ -0,0 +1,44 @@
+package com.imed.costaccount.model.vo;
+
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @author 李加喜
+ * @Package com.imed.costaccount.model.vo
+ * @date 2021-08-23 11:33
+ */
+@Data
+@ApiModel("目标责任中心对应的")
+public class TargetSplitLevelVO {
+    /**
+     * 当前责任中心的id
+     */
+    private Long responsibilityId;
+    /**
+     * 当前责任中心的代码
+     */
+    private String responsibilityCode;
+    /**
+     * 当前责任中心的名称
+     */
+    private String responsibilityName;
+    /**
+     * 第一次分摊的金额
+     */
+    private String oneShareMoney;
+    /**
+     * 第二次分摊金额
+     */
+    private String twoShareMoney;
+    /**
+     * 第三次分摊金额
+     */
+    private String threeShareMoney;
+    /**
+     * 分摊参数设置数据
+     */
+    private List<TargetShareParamValue> targetShareParamValueList;
+}

+ 39 - 0
src/main/java/com/imed/costaccount/service/AllocationQueryService.java

@@ -0,0 +1,39 @@
+package com.imed.costaccount.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.imed.costaccount.model.AllocationQuery;
+import com.imed.costaccount.model.vo.CodeAndNameVO;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+
+/**
+ * 
+ *
+ * @author huangrui
+ * @email 
+ * @date 2021-08-26 08:51:45
+ */
+public interface AllocationQueryService extends IService<AllocationQuery> {
+
+    /**
+     * 这个月总金额
+     * @param dateYear
+     * @param month
+     * @param hospId
+     * @return
+     */
+    BigDecimal getTotalMoney(int dateYear, int month, Long hospId);
+
+    List<CodeAndNameVO> getRespCodeAndName(Long hospId, int dateYear, int month);
+
+    List<CodeAndNameVO> getAccountCodeAndName(Long hospId, int dateYear, int month);
+
+    BigDecimal getCountByRespAndAccounts(Long hospId, int dateYear, int month, String code, List<String> accountCodes);
+
+    BigDecimal getTotalByAccountAndResps(Long hospId, int dateYear, int month, String code, List<String> respCodes);
+
+    BigDecimal getTotalByAccountAndRespCode(Long hospId, int dateYear, int month, String accountCode, String responsibilityCode);
+}
+

+ 26 - 0
src/main/java/com/imed/costaccount/service/AllocationService.java

@@ -1,9 +1,14 @@
 package com.imed.costaccount.service;
 
+import cn.hutool.poi.excel.ExcelWriter;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.imed.costaccount.common.util.PageUtils;
 import com.imed.costaccount.model.dto.StartDTO;
 import com.imed.costaccount.model.Allocation;
+import com.imed.costaccount.model.vo.CollectDataFormVO;
+import org.apache.poi.ss.usermodel.Sheet;
+
+import java.util.List;
 
 /**
  * 成本分摊后表
@@ -33,5 +38,26 @@ public interface AllocationService extends IService<Allocation> {
      */
     PageUtils queryAfterAllocation(String year, String responsibilityCode, Integer current, Integer pageSize, Long hospId);
 
+    /**
+     * 分摊后报表
+     * @param year 年月(yyyy-MM-dd)
+     * @param responsibilityCode 责任中心代码
+     * @param hospId
+     * @return List
+     */
+    CollectDataFormVO queryAfterAllocationForm(String year, String responsibilityCode, Long hospId);
+
+
+
+    /**
+     *  分摊报表导出
+     * @param writer {@link ExcelWriter}
+     * @param levelSort 分摊层级  就是第几次分摊
+     * @param sheet 报表
+     * @param year 年
+     * @param month 月
+     * @return
+     */
+    ExcelWriter getShareReportTemplate(ExcelWriter writer, Integer levelSort, Sheet sheet, Integer year, Integer month);
 }
 

+ 10 - 1
src/main/java/com/imed/costaccount/service/CostCostingCollectionService.java

@@ -1,7 +1,9 @@
 package com.imed.costaccount.service;
 
+import cn.hutool.poi.excel.ExcelWriter;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.imed.costaccount.model.CostCostingCollection;
+import org.apache.poi.ss.usermodel.Sheet;
 
 /**
  * 成本数据归集后列表
@@ -11,6 +13,13 @@ import com.imed.costaccount.model.CostCostingCollection;
  * @date 2021-08-18 15:27:02
  */
 public interface CostCostingCollectionService extends IService<CostCostingCollection> {
-
+    /**
+     * 分摊后报表输出
+     * @param writer
+     * @param shareNumber
+     * @param sheet
+     * @return
+     */
+    ExcelWriter getShareReportTemplate(ExcelWriter writer, Integer shareNumber, Sheet sheet);
 }
 

+ 33 - 0
src/main/java/com/imed/costaccount/service/CostDepartmentProfitService.java

@@ -0,0 +1,33 @@
+package com.imed.costaccount.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.imed.costaccount.common.util.PageUtils;
+import com.imed.costaccount.model.CostDepartmentProfit;
+
+/**
+ * 科室损益计算
+ *
+ * @author KCYG
+ * @email KCYG@xinxicom
+ * @date 2021-08-24 16:24:08
+ */
+public interface CostDepartmentProfitService extends IService<CostDepartmentProfit> {
+    /**
+     * 查询科室损益数据
+     * @param current
+     * @param pageSize
+     * @param responsibilityCode
+     * @param date
+     * @param hospId
+     * @return
+     */
+    PageUtils queryList(Integer current, Integer pageSize, String responsibilityCode, String date, Long hospId);
+
+    /**
+     * 科室损益计算
+     * @param date
+     * @param hospId
+     */
+    void setDepartmentProfit(String date, Long hospId);
+}
+

+ 62 - 0
src/main/java/com/imed/costaccount/service/impl/AllocationQueryServiceImpl.java

@@ -0,0 +1,62 @@
+package com.imed.costaccount.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.imed.costaccount.model.vo.CodeAndNameVO;
+import org.springframework.stereotype.Service;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+
+import com.imed.costaccount.mapper.AllocationQueryMapper;
+import com.imed.costaccount.model.AllocationQuery;
+import com.imed.costaccount.service.AllocationQueryService;
+
+import java.math.BigDecimal;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+
+@Service("allocationQueryService")
+public class AllocationQueryServiceImpl extends ServiceImpl<AllocationQueryMapper, AllocationQuery> implements AllocationQueryService {
+
+
+    /**
+     * 这个月总金额
+     *
+     * @param dateYear
+     * @param month
+     * @param hospId
+     * @return
+     */
+    @Override
+    public BigDecimal getTotalMoney(int dateYear, int month, Long hospId) {
+        return baseMapper.getTotalMoney(dateYear, month, hospId);
+    }
+
+    @Override
+    public List<CodeAndNameVO> getRespCodeAndName(Long hospId, int dateYear, int month) {
+        List<CodeAndNameVO> respCodeAndName = baseMapper.getRespCodeAndName(hospId, dateYear, month);
+        return respCodeAndName.stream().distinct().collect(Collectors.toList());
+    }
+
+    @Override
+    public List<CodeAndNameVO> getAccountCodeAndName(Long hospId, int dateYear, int month) {
+        List<CodeAndNameVO> respCodeAndName = baseMapper.getAccountCodeAndName(hospId, dateYear, month);
+        return respCodeAndName.stream().distinct().collect(Collectors.toList());
+    }
+
+    @Override
+    public BigDecimal getCountByRespAndAccounts(Long hospId, int dateYear, int month, String code, List<String> accountCodes) {
+        return baseMapper.getCountByRespAndAccounts(hospId, dateYear, month, code, accountCodes);
+    }
+
+    @Override
+    public BigDecimal getTotalByAccountAndResps(Long hospId, int dateYear, int month, String code, List<String> respCodes) {
+        return baseMapper.getTotalByAccountAndResps(hospId, dateYear, month, code, respCodes);
+    }
+
+    @Override
+    public BigDecimal getTotalByAccountAndRespCode(Long hospId, int dateYear, int month, String accountCode, String responsibilityCode) {
+        return baseMapper.getTotalByAccountAndRespCode(hospId, dateYear, month, accountCode, responsibilityCode);
+    }
+}

+ 390 - 11
src/main/java/com/imed/costaccount/service/impl/AllocationServiceImpl.java

@@ -4,27 +4,33 @@ import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.poi.excel.ExcelWriter;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.imed.costaccount.common.constants.NumberConstant;
 import com.imed.costaccount.common.exception.CostException;
+import com.imed.costaccount.common.util.BeanUtil;
 import com.imed.costaccount.common.util.JacksonUtil;
 import com.imed.costaccount.common.util.PageUtils;
+import com.imed.costaccount.common.util.UserContext;
 import com.imed.costaccount.mapper.AllocationMapper;
 import com.imed.costaccount.model.*;
 import com.imed.costaccount.model.dto.StartDTO;
-import com.imed.costaccount.model.vo.AccountShareVO;
-import com.imed.costaccount.model.vo.AfterAllocationVO;
-import com.imed.costaccount.model.vo.CostShareLevelVO;
+import com.imed.costaccount.model.vo.*;
 import com.imed.costaccount.service.*;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.usermodel.Sheet;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
 
-
+@Slf4j
 @Service("allocationService")
 public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocation> implements AllocationService {
 
@@ -34,15 +40,20 @@ public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocat
     private final CostAccountShareService accountShareService;
     private final ShareParamValueService shareParamValueService;
     private final CostShareParamService shareParamService;
+    private final AllocationQueryService allocationQueryService;
 
     public AllocationServiceImpl(CostCostingGroupService costCostingGroupService,
-                                 CostShareLevelService shareLevelService, ResponsibilityService responsibilityService, CostAccountShareService accountShareService, ShareParamValueService shareParamValueService, CostShareParamService shareParamService) {
+                                 CostShareLevelService shareLevelService,
+                                 ResponsibilityService responsibilityService,
+                                 CostAccountShareService accountShareService,
+                                 ShareParamValueService shareParamValueService, CostShareParamService shareParamService, AllocationQueryService allocationQueryService, AllocationQueryService allocationQueryService1) {
         this.costCostingGroupService = costCostingGroupService;
         this.shareLevelService = shareLevelService;
         this.responsibilityService = responsibilityService;
         this.accountShareService = accountShareService;
         this.shareParamValueService = shareParamValueService;
         this.shareParamService = shareParamService;
+        this.allocationQueryService = allocationQueryService1;
     }
 
     /**
@@ -68,12 +79,7 @@ public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocat
         }
         Map<String, List<ShareParamValue>> paramValueRespCodeMap = shareParamValues.stream().collect(Collectors.groupingBy(ShareParamValue::getResponsibilityCode));
         // 删除该年月已经分摊过的数据
-        this.remove(
-                new LambdaQueryWrapper<Allocation>()
-                        .eq(Allocation::getDateYear, startDTO.getYear())
-                        .eq(Allocation::getDateMonth, startDTO.getMonth())
-                        .eq(Allocation::getHospId, hospId)
-        );
+        removeData(startDTO, hospId);
         // 得到这个医院所有的分摊层级列表排序
         List<CostShareLevelVO> shareLevelVOs = shareLevelService.getAll(hospId);
         if (CollUtil.isEmpty(shareLevelVOs)) {
@@ -169,6 +175,7 @@ public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocat
                                     .setShareParamCode(paramValue.getShareParamCode()).setShareParamName(shareParamName).setTotalAmount(totalAmount).setShareParamValueNum(paramValue.getValueNum())
                                     .setShareParamRate(numerator.divide(reduce, 4))
                             ;
+
                             // todo 目标分摊层级责任中心 就是当前列个表中的责任中心
                             allocations.add(targetAllocation);
                             costList.add(targetAllocation);
@@ -180,6 +187,71 @@ public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocat
             }
         }
         this.saveBatch(allocations);
+        List<Allocation> list = this.list(
+                new LambdaQueryWrapper<Allocation>()
+                        .eq(Allocation::getHospId, hospId).eq(Allocation::getDateYear, startDTO.getYear()).eq(Allocation::getDateMonth, startDTO.getMonth())
+        );
+        if (list.isEmpty()) {
+            log.error("未分摊到数据......");
+            return;
+        }
+
+        // 入cost_allocation_query 表 便于后续操作
+        this.saveAllocationQuery(list, costingGroups, hospId);
+    }
+
+    private void removeData(StartDTO startDTO, Long hospId) {
+        this.remove(
+                new LambdaQueryWrapper<Allocation>()
+                        .eq(Allocation::getDateYear, startDTO.getYear())
+                        .eq(Allocation::getDateMonth, startDTO.getMonth())
+                        .eq(Allocation::getHospId, hospId)
+        );
+        allocationQueryService.remove(
+                new LambdaQueryWrapper<AllocationQuery>()
+                        .eq(AllocationQuery::getDateYear, startDTO.getYear())
+                        .eq(AllocationQuery::getDateMonth, startDTO.getMonth())
+                        .eq(AllocationQuery::getHospId, hospId)
+        );
+    }
+
+    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
+    public void saveAllocationQuery(List<Allocation> list, List<CostCostingGroup> costingGroups, Long hospId) {
+        List<AllocationQuery> saveList = new ArrayList<>();
+        list.forEach(i -> {
+            Long accountShareId = i.getAccountShareId();
+            CostAccountShare byId = accountShareService.getById(accountShareId);
+            if (Objects.isNull(byId)) {
+                return;
+            }
+            String accountingCodes = byId.getAccountingCodes();
+            if (StrUtil.isBlank(accountingCodes)) {
+                return;
+            }
+            String accountingNames = byId.getAccountingNames();
+            String alias = byId.getAlias();
+            if (StrUtil.isNotBlank(alias)) {
+                accountingNames = alias;
+            }
+            AllocationQuery allocationQuery = new AllocationQuery();
+            allocationQuery.setDateYear(i.getDateYear()).setDateMonth(i.getDateMonth())
+                    .setHospId(hospId).setResponsibilityCode(i.getResponsibilityCode()).setResponsibilityName(i.getResponsibilityName())
+                    .setOriginId(i.getId()).setOriginType(2L).setAmount(i.getAmount()).setAccountingCode(accountingCodes).setAccountingName(accountingNames)
+                    .setCreateTime(System.currentTimeMillis())
+            ;
+            saveList.add(allocationQuery);
+        });
+
+        costingGroups.forEach(i -> {
+            AllocationQuery allocationQuery = new AllocationQuery();
+            allocationQuery.setDateYear(i.getDateYear()).setDateMonth(i.getDateMonth())
+                    .setHospId(hospId).setResponsibilityCode(i.getResponsibilityCode()).setResponsibilityName(i.getResponsibilityName())
+                    .setOriginId(i.getId()).setOriginType(2L).setAmount(i.getAmount()).setAccountingCode(i.getAccountCode()).setAccountingName(i.getAccountName())
+                    .setCreateTime(System.currentTimeMillis())
+            ;
+            saveList.add(allocationQuery);
+        });
+        allocationQueryService.saveBatch(saveList);
     }
 
     /**
@@ -305,4 +377,311 @@ public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocat
         BigDecimal sum = baseMapper.queryAfterAllocationListSum(dateYear, dateMonth, responsibilityCode, hospId);
         return new PageUtils(list, totalCount, pageSize, current, sum);
     }
+
+
+    /**
+     * 分摊报表导出
+     *
+     * @param writer    {@link ExcelWriter}
+     * @param levelSort 分摊层级  就是第几次分摊
+     * @param sheet     报表
+     * @param year      年
+     * @param month     月
+     * @return
+     */
+    @Override
+    public ExcelWriter getShareReportTemplate(ExcelWriter writer, Integer levelSort, Sheet sheet, Integer year, Integer month) {
+        // 获取数据
+        List<AllocationReportVO> allocationReportVOList = getAllocationReportVOS(levelSort, year, month);
+        // 设置导出
+        Map<String, List<AllocationReportVO>> responsibilityMap = allocationReportVOList.stream().collect(Collectors.groupingBy(AllocationReportVO::getResponsibilityCode));
+        Map<String, List<AllocationReportVO>> targetResponsibilityMap = allocationReportVOList.stream().collect(Collectors.groupingBy(AllocationReportVO::getTargetResponsibilityCode));
+        Map<String, AllocationReportVO> allAccMap = allocationReportVOList.stream().collect(Collectors.toMap(k -> k.getResponsibilityName() + k.getAccountName() + k.getTargetResponsibilityName() + k.getShareParamName(), synOne -> synOne));
+        Map<String, AllocationReportVO> allAliMap = allocationReportVOList.stream().collect(Collectors.toMap(k -> k.getResponsibilityName() + k.getAlias() + k.getTargetResponsibilityName() + k.getShareParamName(), synOne -> synOne));
+        // 当前责任中心下面有几个会计科目  后面进行合并使用
+        int numResponsibility;
+//        // 从第几列开始编写数据
+        int column = levelSort + 3;
+        Set<String> keySet = responsibilityMap.keySet();
+        for (String key : keySet) {
+            List<AllocationReportVO> allocationReportVOS = responsibilityMap.get(key);
+            Map<String, AllocationReportVO> linkedHashMap = new LinkedHashMap<>();
+            allocationReportVOS.forEach(i -> {
+                String s = i.getResponsibilityCode() + i.getAccountCode();
+                if (!linkedHashMap.containsKey(s)) {
+                    linkedHashMap.put(s, i);
+                }
+            });
+            numResponsibility = linkedHashMap.size();
+            if (numResponsibility >= NumberConstant.TWO) {
+                Set<String> strings = linkedHashMap.keySet();
+                for (String s : strings) {
+                    AllocationReportVO allocationReportVO = linkedHashMap.get(s);
+                    if (StrUtil.isBlank(allocationReportVO.getAlias())) {
+                        writer.writeCellValue(column, 0, allocationReportVO.getResponsibilityName());
+                        // 别名不存在
+                        writer.writeCellValue(column, 1, allocationReportVO.getAccountName());
+                    } else {
+                        // 不为空 设置别名
+                        writer.writeCellValue(column, 0, allocationReportVO.getResponsibilityName());
+                        writer.writeCellValue(column, 1, allocationReportVO.getAlias());
+                    }
+                    writer.writeCellValue(column, 2, allocationReportVO.getTotalAmounts());
+                    column++;
+                }
+            } else {
+                // 不需要合并单元格
+                writer.writeCellValue(column, 0, allocationReportVOS.get(0).getResponsibilityName());
+                if (StrUtil.isNotBlank(allocationReportVOS.get(0).getAlias())) {
+                    writer.writeCellValue(column, 1, allocationReportVOS.get(0).getAlias());
+                } else {
+                    writer.writeCellValue(column, 1, allocationReportVOS.get(0).getAccountName());
+                }
+                writer.writeCellValue(column, 2, allocationReportVOS.get(0).getTotalAmounts());
+                column++;
+            }
+        }
+        // 设置单元格合并
+        for (int j = 1; j < levelSort; j++) {
+            writer.merge(0, 1, j, j, "第" + j + "次分摊", false);
+        }
+        // 目标责任集合
+        writer.passCurrentRow();
+        // 从第三行开始
+        int num = 3;
+        Set<String> targetSet = targetResponsibilityMap.keySet();
+        for (String target : targetSet) {
+            List<AllocationReportVO> allocationReportVOS = targetResponsibilityMap.get(target);
+            Map<String, AllocationReportVO> linkedHashMap = new LinkedHashMap<>();
+            allocationReportVOS.forEach(i -> {
+                String s = i.getTargetResponsibilityCode() + i.getShareParamName();
+                if (!linkedHashMap.containsKey(s)) {
+                    linkedHashMap.put(s, i);
+                }
+            });
+            int shareParamSize = linkedHashMap.size();
+            if (shareParamSize >= NumberConstant.TWO) {
+                // 责任中心
+                AllocationReportVO costCostingVO = allocationReportVOS.get(0);
+                // 设置第几次分摊的值
+                for (int k = 0; k < levelSort - 1; k++) {
+                    writer.merge(num, num + shareParamSize - 1, k + 1, k + 1, costCostingVO.getTargetShareMoneys().get(k), false);
+                }
+                // 设置对应的分摊参数值
+                Set<String> strings = linkedHashMap.keySet();
+                for (String s : strings) {
+                    AllocationReportVO allocationReportVO = linkedHashMap.get(s);
+                    writer.writeCellValue(0, num, allocationReportVO.getTargetResponsibilityName());
+                    writer.writeCellValue(levelSort, num, allocationReportVO.getShareParamName());
+                    writer.writeCellValue(levelSort + 1, num, allocationReportVO.getShareParamValueNums());
+                    writer.writeCellValue(levelSort + 2, num, allocationReportVO.getShareParamRates());
+                    for (int m = levelSort + 3; m < column; m++) {
+                        // x是m y是num
+                        // 获取当前这一列对应的责任中心 以及会计科目
+                        // 第一行责任中心
+                        String responsibilityName = sheet.getRow(0).getCell(m).getStringCellValue();
+                        // 第二行的会计科目或者
+                        String accountNameOrAlias = sheet.getRow(1).getCell(m).getStringCellValue();
+                        // 这一行的目标责任中心
+                        String otherResponsibilityName = sheet.getRow(num).getCell(0).getStringCellValue();
+                        // 分摊参数
+                        String shareName = sheet.getRow(num).getCell(levelSort).getStringCellValue();
+                        String sss = responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName;
+                        AllocationReportVO vo = allAliMap.get(sss);
+                        AllocationReportVO allocationReportVO2 = allAccMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
+                        AllocationReportVO allocationReportVO3 = allAliMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
+
+                        if (Objects.nonNull(allocationReportVO2)) {
+                            writer.writeCellValue(m, num, allocationReportVO2.getAmounts());
+                        } else if (Objects.nonNull(allocationReportVO3)) {
+                            writer.writeCellValue(m, num, allocationReportVO3.getAmounts());
+                        } else {
+                            writer.writeCellValue(m, num, 0);
+                        }
+                    }
+                    num++;
+                }
+            }
+            if (shareParamSize < NumberConstant.TWO) {
+                writer.writeCellValue(0, num, allocationReportVOS.get(0).getTargetResponsibilityName());
+                for (int k = 0; k < levelSort - 1; k++) {
+                    writer.writeCellValue(k + 1, num, allocationReportVOS.get(0).getTargetShareMoneys().get(k));
+                }
+                writer.writeCellValue(levelSort, num, allocationReportVOS.get(0).getShareParamName());
+                writer.writeCellValue(levelSort + 1, num, allocationReportVOS.get(0).getShareParamValueNums());
+                writer.writeCellValue(levelSort + 2, num, allocationReportVOS.get(0).getShareParamRates());
+                for (int m = levelSort + 4; m < column; m++) {
+                    // x是m y是num
+                    // 获取当前这一列对应的责任中心 以及会计科目
+                    // 第一行责任中心
+                    String responsibilityName = sheet.getRow(0).getCell(m).getStringCellValue();
+                    // 第二行的会计科目或者
+                    String accountNameOrAlias = sheet.getRow(1).getCell(m).getStringCellValue();
+                    // 这一行的目标责任中心
+                    String otherResponsibilityName = sheet.getRow(num).getCell(0).getStringCellValue();
+                    // 分摊参数
+                    String shareName = sheet.getRow(num).getCell(levelSort).getStringCellValue();
+                    AllocationReportVO allocationReportVO2 = allAccMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
+                    AllocationReportVO allocationReportVO3 = allAliMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
+                    if (Objects.nonNull(allocationReportVO2)) {
+                        writer.writeCellValue(m, num, allocationReportVO2.getAmounts());
+                    } else if (Objects.nonNull(allocationReportVO3)) {
+                        writer.writeCellValue(m, num, allocationReportVO3.getAmounts());
+                    } else {
+                        writer.writeCellValue(m, num, 0);
+                    }
+                }
+                num++;
+            }
+        }
+        for (int i = 0; i < 30; i++) {
+            // 调整每一列宽度
+            sheet.autoSizeColumn((short) i);
+            // 解决自动设置列宽中文失效的问题
+            sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 14 / 10);
+        }
+        return writer;
+    }
+
+    /**
+     * 查询数据
+     *
+     * @param levelSort
+     * @param year
+     * @param month
+     * @return
+     */
+    private List<AllocationReportVO> getAllocationReportVOS(Integer levelSort, Integer year, Integer month) {
+        Long hospId = UserContext.getHospId();
+        if (Objects.isNull(levelSort) || Objects.isNull(year) || Objects.isNull(month)) {
+            throw new CostException(500, "参数异常");
+        }
+        List<Allocation> allocationList = baseMapper.selectList(new QueryWrapper<Allocation>().lambda()
+                .eq(Allocation::getHospId, hospId).eq(Allocation::getLevelSort, levelSort).eq(Allocation::getDateYear, year)
+                .eq(Allocation::getDateMonth, month));
+        // 处理第几次分摊计算值
+        List<Allocation> allocations = baseMapper.selectList(new QueryWrapper<Allocation>().lambda()
+                .eq(Allocation::getHospId, hospId).eq(Allocation::getDateYear, year)
+                .eq(Allocation::getDateMonth, month));
+        Map<String, List<Allocation>> accrepMap = allocations.stream().collect(Collectors.groupingBy(k -> k.getLevelSort() + "cost" + k.getResponsibilityCode()));
+        LinkedList<String> shareMoney = new LinkedList<>();
+
+        List<CostAccountShare> costAccountShareList = accountShareService.list(new QueryWrapper<CostAccountShare>().lambda()
+                .eq(CostAccountShare::getHospId, hospId));
+        Map<Long, CostAccountShare> accountShareMap = costAccountShareList.stream().collect(Collectors.toMap(CostAccountShare::getId, synOne -> synOne));
+        List<AllocationReportVO> allocationReportVOList = BeanUtil.convertList(allocationList, AllocationReportVO.class);
+        allocationReportVOList.forEach(i -> {
+            Long accountShareId = i.getAccountShareId();
+            CostAccountShare costAccountShare = accountShareMap.get(accountShareId);
+            if (Objects.isNull(costAccountShare)) {
+                throw new CostException(500, "成本参数参数设置对应不存在");
+            }
+            i.setAccountCode(costAccountShare.getAccountingCodes());
+            i.setAccountName(costAccountShare.getAccountingNames());
+            i.setAlias(costAccountShare.getAlias());
+            if (levelSort > 1) {
+                for (int j = 1; j < levelSort; j++) {
+                    AtomicReference<BigDecimal> money = new AtomicReference<>(new BigDecimal("0.0000"));
+                    List<Allocation> allocations1 = accrepMap.get(j + "cost" + i.getResponsibilityCode());
+                    if (CollUtil.isNotEmpty(allocations1)) {
+                        allocations1.forEach(m -> {
+                            money.updateAndGet(v -> v.add(m.getAmount()));
+                            shareMoney.add(month.toString());
+                        });
+                    } else {
+                        // TODO 封装测试数据
+                        shareMoney.add("1000");
+                    }
+                }
+            }
+            i.setTargetShareMoneys(shareMoney);
+            // 设置字符串类型数据
+            i.setTotalAmounts(i.getTotalAmount().toString());
+            i.setShareParamValueNums(i.getShareParamValueNum().toString());
+            i.setShareParamRates(i.getShareParamRate().toString());
+            i.setAmounts(i.getAmount().toString());
+        });
+
+        return allocationReportVOList;
+    }
+
+    /**
+     * 分摊后报表
+     *
+     * @param year               年月(yyyy-MM-dd)
+     * @param responsibilityCode 责任中心代码
+     * @param hospId             医院id
+     * @return List
+     */
+    @Override
+    public CollectDataFormVO queryAfterAllocationForm(String year, String responsibilityCode, Long hospId) {
+        DateTime dateTime = DateUtil.parseDate(year);
+        int dateYear = DateUtil.year(dateTime);
+        int month = DateUtil.month(dateTime) + 1;
+        List<CodeAndNameVO> responsibilityCodeAndNames = allocationQueryService.getRespCodeAndName(hospId, dateYear, month);
+        List<CodeAndNameVO> accountCodeAndNames = allocationQueryService.getAccountCodeAndName(hospId, dateYear, month);
+        // todo 校验两个List是否为空
+        // 填充
+        responsibilityCodeAndNames.add(0, new CodeAndNameVO("#", "#"));
+        responsibilityCodeAndNames.add(responsibilityCodeAndNames.size(), new CodeAndNameVO("合计", "合计"));
+
+        List<String> titleData = responsibilityCodeAndNames.stream().map(CodeAndNameVO::getName).collect(Collectors.toList());
+        List<String> respCodes = responsibilityCodeAndNames.stream().map(CodeAndNameVO::getCode).collect(Collectors.toList());
+        Map<Integer, String> titleMap = new HashMap<>();
+        for (int i = 0; i < titleData.size(); i++) {
+            titleMap.put(i + 1, titleData.get(i));
+        }
+
+        List<Map<Integer, Object>> realDatas = new ArrayList<>();
+        List<String> accountCodes = accountCodeAndNames.stream().map(CodeAndNameVO::getCode).collect(Collectors.toList());
+        for (CodeAndNameVO account : accountCodeAndNames) {
+            Map<Integer, Object> map = new HashMap<>();
+            for (int i = 0; i < responsibilityCodeAndNames.size(); i++) {
+                if (i == 0) {
+                    map.put(i + 1, account.getName());
+                    continue;
+                } else if (i == responsibilityCodeAndNames.size() - 1) {
+                    // todo 计算最右侧合计的钱
+                    BigDecimal amount = allocationQueryService.getTotalByAccountAndResps(hospId, dateYear, month, account.getCode(), respCodes);
+                    map.put(i + 1, amount);
+                    continue;
+                }
+                // TODO: 2021/8/26 计算 中间的钱
+                BigDecimal amount = allocationQueryService.getTotalByAccountAndRespCode(hospId, dateYear, month,  account.getCode(), respCodes.get(i));
+                map.put(i + 1, amount);
+
+            }
+            realDatas.add(map);
+        }
+
+        // 尾栏计算
+        Map<Integer, Object> map = new HashMap<>();
+        for (int i = 0; i < titleData.size(); i++) {
+            if (i == 0) {
+                map.put(i + 1, "合计");
+                continue;
+            } else if (i == titleData.size() - 1) {
+                // TODO: 2021/8/26 计算
+                BigDecimal bigDecimal = allocationQueryService.getTotalMoney(dateYear, month, hospId);
+                map.put(i + 1, bigDecimal);
+                continue;
+            }
+            BigDecimal bigDecimal = allocationQueryService.getCountByRespAndAccounts(hospId, dateYear, month, responsibilityCodeAndNames.get(i).getCode(), accountCodes);
+            map.put(i + 1, bigDecimal);
+        }
+        return new CollectDataFormVO(titleMap, realDatas, map);
+    }
+
+    /**
+     * 这年月的数据
+     */
+    private List<AllocationQuery> getAllocationQueriesByYear(Long hospId, int dateYear, int month) {
+        List<AllocationQuery> list = allocationQueryService.list(
+                new LambdaQueryWrapper<AllocationQuery>()
+                        .eq(AllocationQuery::getDateYear, dateYear)
+                        .eq(AllocationQuery::getDateMonth, month)
+                        .eq(AllocationQuery::getHospId, hospId)
+        );
+        return list;
+    }
 }

+ 526 - 0
src/main/java/com/imed/costaccount/service/impl/CostCostingCollectionServiceImpl.java

@@ -1,14 +1,540 @@
 package com.imed.costaccount.service.impl;
 
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.poi.excel.ExcelWriter;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.imed.costaccount.common.exception.CostException;
+import com.imed.costaccount.constants.NumberConstant;
 import com.imed.costaccount.mapper.CostCostingCollectionMapper;
 import com.imed.costaccount.model.CostCostingCollection;
+import com.imed.costaccount.model.vo.*;
 import com.imed.costaccount.service.CostCostingCollectionService;
+import com.imed.costaccount.service.CostShareLevelService;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.usermodel.Sheet;
 import org.springframework.stereotype.Service;
 
+import java.util.*;
+import java.util.stream.Collectors;
 
+@Slf4j
 @Service("costCostingCollectionService")
 public class CostCostingCollectionServiceImpl extends ServiceImpl<CostCostingCollectionMapper, CostCostingCollection> implements CostCostingCollectionService {
 
+    private final CostShareLevelService costShareLevelService;
 
+    public CostCostingCollectionServiceImpl(CostShareLevelService costShareLevelService) {
+        this.costShareLevelService = costShareLevelService;
+    }
+
+    /**
+     * 分摊后报表输出
+     *
+     * @param writer
+     * @param shareNumber
+     * @param sheet
+     * @return
+     */
+    @Override
+    public ExcelWriter getShareReportTemplate(ExcelWriter writer, Integer shareNumber, Sheet sheet) {
+        // 需要封装两个  分摊层级责任中心集合  目标层级责任中心
+        // 分摊层级责任中心集合
+//        LinkedList<SplitLevelVO> splitLevelVOLinkedList = new LinkedList<>();
+//        // 目标层级责任中心集合
+//        LinkedList<TargetSplitLevelVO> targetSplitLevelVOLinkedList = new LinkedList<>();
+//        setMockData(splitLevelVOLinkedList, targetSplitLevelVOLinkedList);
+        List<CostCostingVO> costCostingVOS = setMockDa();
+        Map<String, List<CostCostingVO>> responsibilityMap = costCostingVOS.stream().collect(Collectors.groupingBy(CostCostingVO::getResponsibilityCode));
+//        Map<String, String> collect = costCostingVOS.stream().collect(Collectors.toMap(CostCostingVO::getResponsibilityCode, CostCostingVO::getAccountCode));
+        Map<String, List<CostCostingVO>> targetResponsibilityMap = costCostingVOS.stream().collect(Collectors.groupingBy(CostCostingVO::getTargetResponsibilityCode));
+        Map<String, CostCostingVO> allAccMap = costCostingVOS.stream().collect(Collectors.toMap(k -> k.getResponsibilityName()+k.getAccountName()+k.getTargetResponsibilityName()+k.getShareParamName(), synOne -> synOne));
+        Map<String, CostCostingVO> allAliMap = costCostingVOS.stream().collect(Collectors.toMap(k -> k.getResponsibilityName()+k.getAlias()+k.getTargetResponsibilityName()+k.getShareParamName(), synOne -> synOne));
+        // 当前责任中心下面有几个会计科目  后面进行合并使用
+        int numResponsibility;
+        // 从第几列开始编写数据
+        int column = shareNumber + 4;
+        // 总共有多少个单元格
+//        Integer shareLevelGrid = 0;
+        Set<String> keySet = responsibilityMap.keySet();
+        for (String key : keySet) {
+            List<CostCostingVO> costCostingVOS1 = responsibilityMap.get(key);
+            Map<String, CostCostingVO> linkedHashMap = new LinkedHashMap<>();
+            costCostingVOS1.forEach(i -> {
+                String s = i.getResponsibilityCode() + i.getAccountCode();
+                if (!linkedHashMap.containsKey(s)) {
+                    linkedHashMap.put(s, i);
+                }
+            });
+            numResponsibility = linkedHashMap.size();
+            if (numResponsibility >= NumberConstant.TWO) {
+                // 需要合并单元格
+//                writer.merge(0, 0, column, column + numResponsibility - 1, costCostingVOS1.get(0).getResponsibilityName(), false);
+                // 设置对应的会计科目或者别名
+                Set<String> strings = linkedHashMap.keySet();
+                for (String s : strings) {
+                    CostCostingVO costCostingVO = linkedHashMap.get(s);
+                    if (StrUtil.isBlank(costCostingVO.getAlias())) {
+                        writer.writeCellValue(column,0,costCostingVO.getResponsibilityName());
+                        // 别名不存在
+                        writer.writeCellValue(column, 1, costCostingVO.getAccountName());
+                    } else {
+                        // 不为空 设置别名
+                        writer.writeCellValue(column,0,costCostingVO.getResponsibilityName());
+                        writer.writeCellValue(column, 1, costCostingVO.getAlias());
+                    }
+                    writer.writeCellValue(column, 2, costCostingVO.getAmount());
+                    column++;
+                }
+//                for (int j=0;j<numResponsibility;j++){
+//                    CostCostingVO costCostingVO = costCostingVOS1.get(j);
+//                    if (StrUtil.isBlank(costCostingVO.getAlias())){
+//                        // 别名不存在
+//                        writer.writeCellValue(j+column,1,costCostingVO.getAccountName());
+//                    }else {
+//                        // 不为空 设置别名
+//                        writer.writeCellValue(j+column,1,costCostingVO.getAlias());
+//                    }
+//                    writer.writeCellValue(j+column,2,costCostingVO.getAmount());
+//                }
+//                column+=numResponsibility;
+            } else {
+                // 不需要合并单元格
+                writer.writeCellValue(column, 0, costCostingVOS1.get(0).getResponsibilityName());
+                writer.writeCellValue(column, 1, costCostingVOS1.get(0).getAccountName());
+                writer.writeCellValue(column, 2, costCostingVOS1.get(0).getAmount());
+                column++;
+            }
+        }
+//        for (int i=0; i< responsibilityMap.size();i++){
+        // 判断当前当成的责任中心对应的分摊参数的数量
+//            List<CostCostingVO> costCostingVOS1 = responsibilityMap.get(responsibilityCode);
+//            numResponsibility=costCostingVOS1.size();
+//            shareLevelGrid+=numResponsibility;
+//            if (numResponsibility>= NumberConstant.TWO){
+//                writer.merge(0,0,column,column+numResponsibility-1,costCostingVOS1.get(0).getResponsibilityName(),false);
+//                // 设置对应的会计科目或者金额
+//                for (int j=0;j<numResponsibility;j++){
+//                    CostCostingVO costCostingVO = costCostingVOS1.get(j);
+//                    if (StrUtil.isBlank(costCostingVO.getAlias())){
+//                        // 别名不存在的时候
+//                        writer.writeCellValue(j+column,1,costCostingVO.getAccountName());
+//                    }else {
+//                        // 为空的话设置别名
+//                        writer.writeCellValue(j+column,1,costCostingVO.getAlias());
+//                    }
+//                    writer.writeCellValue(j+column,2,costCostingVO.getAccountName());
+//                }
+//                column+=numResponsibility;
+//            }else {
+//                writer.writeCellValue(column,0,costCostingVOS1.get(0).getResponsibilityName());
+//                writer.writeCellValue(column,1,costCostingVOS1.get(0).getAccountName());
+//                writer.writeCellValue(column,2,costCostingVOS1.get(0).getMoney());
+//                column++;
+//            }
+//        }
+        // 设置单元格合并
+        for (int j = 1; j <= shareNumber; j++) {
+            writer.merge(0, 1, j, j, "第" + j + "次分摊", false);
+        }
+        // 目标责任集合
+        writer.passCurrentRow();
+        // 从第三行开始
+        int num = 3;
+        Set<String> targetSet = targetResponsibilityMap.keySet();
+        for (String target : targetSet) {
+            List<CostCostingVO> costCostingVOS1 = targetResponsibilityMap.get(target);
+            Map<String, CostCostingVO> linkedHashMap = new LinkedHashMap<>();
+            costCostingVOS1.forEach(i -> {
+                String s = i.getTargetResponsibilityCode() + i.getShareParamName();
+                if (!linkedHashMap.containsKey(s)) {
+                    linkedHashMap.put(s, i);
+                }
+            });
+            int shareParamSize = linkedHashMap.size();
+            if (shareParamSize >= NumberConstant.TWO) {
+                // 责任中心
+                CostCostingVO costCostingVO = costCostingVOS1.get(0);
+//                writer.merge(num, num + shareParamSize - 1, 0, 0, costCostingVO.getTargetResponsibilityName(), false);
+                // 设置第几次分摊的值
+                for (int k = 0; k < shareNumber; k++) {
+                    writer.merge(num, num + shareParamSize - 1, k + 1, k + 1, costCostingVO.getTargetShareMoneys().get(k), false);
+                }
+                // 设置对应的分摊参数值
+                Set<String> strings = linkedHashMap.keySet();
+                for (String s : strings) {
+                    CostCostingVO costCostingVO1 = linkedHashMap.get(s);
+                    writer.writeCellValue(0,num,costCostingVO1.getTargetResponsibilityName());
+                    writer.writeCellValue(shareNumber + 1, num, costCostingVO1.getShareParamName());
+                    writer.writeCellValue(shareNumber + 2, num, costCostingVO1.getShareValue());
+                    writer.writeCellValue(shareNumber + 3, num, costCostingVO1.getShareParamProportion());
+                    for (int m=shareNumber+4;m<column;m++){
+                        // x是m y是num
+                        // 获取当前这一列对应的责任中心 以及会计科目
+                        // 第一行责任中心
+                        String responsibilityName = sheet.getRow(0).getCell(m).getStringCellValue();
+                        // 第二行的会计科目或者
+                        String accountNameOrAlias = sheet.getRow(1).getCell(m).getStringCellValue();
+                        // 这一行的目标责任中心
+                        String otherResponsibilityName = sheet.getRow(num).getCell(0).getStringCellValue();
+                        // 分摊参数
+                        String shareName = sheet.getRow(num).getCell(shareNumber + 1).getStringCellValue();
+                        CostCostingVO costCostingVO2 = allAccMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
+                        CostCostingVO costCostingVO3 = allAliMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
+                        if (Objects.nonNull(costCostingVO2)){
+                            writer.writeCellValue(m,num,costCostingVO2.getMoney());
+                        }else if (Objects.nonNull(costCostingVO3)){
+                            writer.writeCellValue(m,num,costCostingVO3.getMoney());
+                        }else {
+                            throw new CostException(500,"数据异常");
+                        }
+                        // 第二行的会计科目/别名
+//                        System.out.println(responsibilityName+"--"+accountNameOrAlias+"--"+otherResponsibilityName+"--"+shareName);
+                    }
+                    num++;
+                }
+            }
+            if (shareParamSize < NumberConstant.TWO) {
+                writer.writeCellValue(0, num, costCostingVOS1.get(0).getTargetResponsibilityName());
+                for (int k = 0; k < shareNumber; k++) {
+                    writer.writeCellValue(k + 1, num, costCostingVOS1.get(0).getTargetShareMoneys().get(k));
+                }
+                writer.writeCellValue(shareNumber + 1, num, costCostingVOS1.get(0).getShareParamName());
+                writer.writeCellValue(shareNumber + 2, num, costCostingVOS1.get(0).getShareValue());
+                writer.writeCellValue(shareNumber + 3, num, costCostingVOS1.get(0).getShareParamProportion());
+                //TODO 金额没有写
+                for (int m=shareNumber+4;m<column;m++){
+                    // x是m y是num
+                    // 获取当前这一列对应的责任中心 以及会计科目
+                    // 第一行责任中心
+                    String responsibilityName = sheet.getRow(0).getCell(m).getStringCellValue();
+                    // 第二行的会计科目或者
+                    String accountNameOrAlias = sheet.getRow(1).getCell(m).getStringCellValue();
+                    // 这一行的目标责任中心
+                    String otherResponsibilityName = sheet.getRow(num).getCell(0).getStringCellValue();
+                    // 分摊参数
+                    String shareName = sheet.getRow(num).getCell(shareNumber + 1).getStringCellValue();
+                    CostCostingVO costCostingVO2 = allAccMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
+                    CostCostingVO costCostingVO3 = allAliMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
+                    if (Objects.nonNull(costCostingVO2)){
+                        writer.writeCellValue(m,num,costCostingVO2.getMoney());
+                    }else if (Objects.nonNull(costCostingVO3)){
+                        writer.writeCellValue(m,num,costCostingVO3.getMoney());
+                    }else {
+                        throw new CostException(500,"数据异常");
+                    }
+                    // 第二行的会计科目/别名
+                    System.out.println(responsibilityName+"--"+accountNameOrAlias+"--"+otherResponsibilityName+"--"+shareName);
+                }
+                num++;
+            }
+        }
+//        for (int j=0;j<targetResponsibilityList.size();j++){
+//            String targetResponsibilityCode = targetResponsibilityList.get(j);
+//            // 需要判断是否需要合并单元格
+//            List<CostCostingVO> costingVOList = targetResponsibilityMap.get(targetResponsibilityCode);
+//            // 当前责任中心下面有多少个分摊参数
+//            int shareParamSize=costingVOList.size();
+//            if (shareParamSize>=NumberConstant.TWO){
+//                //设置责任中心
+//                CostCostingVO costCostingVO = costingVOList.get(0);
+//                writer.merge(j+num,j+num+shareParamSize-1,0,0,costCostingVO.getResponsibilityName(),false);
+//                // 设置第一次分摊参数值
+//                for (int k=0;k<shareNumber;k++){
+//                    writer.merge(j+num,j+num+shareParamSize-1,k+1,k+1,costCostingVO.getTargetShareMoneys().get(k),false);
+//
+//                }
+//                // 设置对应的分摊参数值
+//                for (int i=0;i<shareParamSize;i++){
+//                    // 设置分摊参数
+//                    CostCostingVO costCostingVO1 = costingVOList.get(i);
+//                    int row=j+num+i;
+//                    writer.writeCellValue(shareNumber+1,row,costCostingVO1.getShareParamName());
+//                    writer.writeCellValue(shareNumber+2,row,costCostingVO1.getShareValue());
+//                    writer.writeCellValue(shareNumber+3,row,costCostingVO1.getShareParamProportion());
+//                    //TODO 金额没有写
+////                    for (int k=shareNumber+4;k<shareLevelGrid+6;k++){
+////                        writer.writeCellValue(k,row,"0.1111");
+////                    }
+//                }
+//
+//            }
+//            num+=shareParamSize-1;
+//            if (shareParamSize<NumberConstant.TWO){
+//                writer.writeCellValue(0, j + num, costingVOList.get(0).getResponsibilityName());
+//                for (int k=0;k<shareNumber;k++){
+//                    writer.merge(j+num,j+num+shareParamSize-1,k+1,k+1,costingVOList.get(0).getTargetShareMoneys().get(k),false);
+//                }
+//                writer.writeCellValue(shareNumber+1,j+num,costingVOList.get(0).getShareParamName());
+//                writer.writeCellValue(shareNumber+2,j+num,costingVOList.get(0).getShareValue());
+//                writer.writeCellValue(shareNumber+3,j+num,costingVOList.get(0).getShareParamProportion());
+//                // TODO 金额暂时不设置
+////                for (int k=shareNumber+4;k<shareLevelGrid+6;k++){
+////                    writer.writeCellValue(k,j+num,"0.3456");
+////                }
+//            }
+//
+//        }
+        // TODO 统一设置列宽  处理中文问题
+        for (int i = 0; i < 30; i++) {
+            // 调整每一列宽度
+            sheet.autoSizeColumn((short) i);
+            // 解决自动设置列宽中文失效的问题
+            sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 13 / 10);
+        }
+        // todo 合并单元格
+        return writer;
+    }
+
+//    private void setMockData(LinkedList<SplitLevelVO> splitLevelVOLinkedList, LinkedList<TargetSplitLevelVO> targetSplitLevelVOLinkedList) {
+//        // 设置分摊层级责任中心数据
+//        SplitLevelVO splitLevelVO = new SplitLevelVO();
+//        splitLevelVO.setResponsibilityName("责任中心1");
+//        splitLevelVO.setResponsibilityCode("zr1");
+//        LinkedList<ResponsibilityAccountVO> responsibilityAccountVOS = new LinkedList<>();
+//
+//        ResponsibilityAccountVO responsibilityAccountVO = new ResponsibilityAccountVO();
+//        responsibilityAccountVO.setAccountingNames("其他费用,资产改良及摊销");
+//        responsibilityAccountVO.setAccountingCodes("1825,1824");
+//        responsibilityAccountVO.setAccountingIds("57,56");
+//        responsibilityAccountVO.setMoney("12.6666");
+//        responsibilityAccountVOS.add(responsibilityAccountVO);
+//
+//        ResponsibilityAccountVO responsibilityAccountVORequest = new ResponsibilityAccountVO();
+//        responsibilityAccountVORequest.setAccountingNames("低值易耗品");
+//        responsibilityAccountVORequest.setAccountingCodes("1822");
+//        responsibilityAccountVORequest.setAccountingIds("54");
+//        responsibilityAccountVORequest.setAlias("设置了别名");
+//        responsibilityAccountVORequest.setMoney("12.1111");
+//        responsibilityAccountVOS.add(responsibilityAccountVORequest);
+//        splitLevelVO.setResponsibilityAccountVOList(responsibilityAccountVOS);
+//        splitLevelVOLinkedList.add(splitLevelVO);
+//
+//        SplitLevelVO splitLevelVO1 = new SplitLevelVO();
+//        splitLevelVO1.setResponsibilityName("责任中心2");
+//        splitLevelVO1.setResponsibilityCode("zr2");
+//        LinkedList<ResponsibilityAccountVO> responsibilityAccountVOS1 = new LinkedList<>();
+//        ResponsibilityAccountVO responsibilityAccountVORequest1 = new ResponsibilityAccountVO();
+//        responsibilityAccountVORequest1.setAccountingNames("低值易耗品");
+//        responsibilityAccountVORequest1.setAccountingCodes("1822");
+//        responsibilityAccountVORequest1.setAccountingIds("54");
+//        responsibilityAccountVORequest1.setAlias("设置的别名");
+//        responsibilityAccountVORequest1.setMoney("12.1111");
+//        responsibilityAccountVOS1.add(responsibilityAccountVORequest1);
+//        splitLevelVO1.setResponsibilityAccountVOList(responsibilityAccountVOS1);
+//        splitLevelVOLinkedList.add(splitLevelVO1);
+//
+//        TargetSplitLevelVO targetSplitLevelVO = new TargetSplitLevelVO();
+//        targetSplitLevelVO.setResponsibilityCode("zr1");
+//        targetSplitLevelVO.setResponsibilityName("责任中心1");
+//        targetSplitLevelVO.setOneShareMoney("100.1111");
+//        targetSplitLevelVO.setTwoShareMoney("200.1111");
+//        targetSplitLevelVO.setThreeShareMoney("300.1111");
+//        LinkedList<TargetShareParamValue> targetShareParamValueLinkedList = new LinkedList<>();
+//        TargetShareParamValue targetShareParamValue = new TargetShareParamValue();
+//        targetShareParamValue.setShareParamName("员工费");
+//        targetShareParamValue.setShareParamCode("A11");
+//        targetShareParamValue.setShareParamValue("66");
+//        targetShareParamValue.setShareParamProportion("0.1111");
+//        targetShareParamValueLinkedList.add(targetShareParamValue);
+//        TargetShareParamValue targetShareParamValueRequest = new TargetShareParamValue();
+//        targetShareParamValueRequest.setShareParamName("医疗收入");
+//        targetShareParamValueRequest.setShareParamCode("A22");
+//        targetShareParamValueRequest.setShareParamValue("77");
+//        targetShareParamValueRequest.setShareParamProportion("0.4444");
+//        targetShareParamValueLinkedList.add(targetShareParamValueRequest);
+//        targetSplitLevelVO.setTargetShareParamValueList(targetShareParamValueLinkedList);
+//        targetSplitLevelVOLinkedList.add(targetSplitLevelVO);
+//
+//        TargetSplitLevelVO targetSplitLevelVORequest = new TargetSplitLevelVO();
+//        targetSplitLevelVORequest.setResponsibilityCode("zr2");
+//        targetSplitLevelVORequest.setResponsibilityName("责任中心2");
+//        targetSplitLevelVORequest.setOneShareMoney("102.1111");
+//        targetSplitLevelVORequest.setTwoShareMoney("202.1111");
+//        targetSplitLevelVORequest.setThreeShareMoney("300.1111");
+//        LinkedList<TargetShareParamValue> targetShareParamValueLinkedList1 = new LinkedList<>();
+//        TargetShareParamValue targetShareParamValue1 = new TargetShareParamValue();
+//        targetShareParamValue1.setShareParamName("水费");
+//        targetShareParamValue1.setShareParamCode("A66");
+//        targetShareParamValue1.setShareParamValue("777");
+//        targetShareParamValue1.setShareParamProportion("0.777");
+//        targetShareParamValueLinkedList1.add(targetShareParamValue1);
+//        targetSplitLevelVORequest.setTargetShareParamValueList(targetShareParamValueLinkedList1);
+//        targetSplitLevelVOLinkedList.add(targetSplitLevelVORequest);
+//
+//    }
+
+    private List<CostCostingVO> setMockDa() {
+        List<CostCostingVO> costCostingVOS = new LinkedList<>();
+        CostCostingVO costCostingVO = new CostCostingVO();
+        costCostingVO.setResponsibilityCode("zr1");
+        costCostingVO.setResponsibilityName("责任中心1");
+        costCostingVO.setAccountCode("A11");
+        costCostingVO.setAccountName("人事费用");
+        costCostingVO.setAmount("800");
+        costCostingVO.setTargetResponsibilityCode("tzr1");
+        costCostingVO.setTargetResponsibilityName("目标责任中心1");
+        List<String> strings = new ArrayList<>();
+        strings.add("1000");
+        strings.add("2000");
+        costCostingVO.setTargetShareMoneys(strings);
+        costCostingVO.setShareParamName("员工数");
+        costCostingVO.setShareValue("3");
+        costCostingVO.setShareParamProportion("0.3344");
+        costCostingVO.setMoney("5.28");
+        costCostingVOS.add(costCostingVO);
+
+        CostCostingVO costCostingVO1 = new CostCostingVO();
+        costCostingVO1.setResponsibilityCode("zr1");
+        costCostingVO1.setResponsibilityName("责任中心1");
+        // 会计科目可能为空
+        costCostingVO1.setAccountCode("A12");
+        costCostingVO1.setAccountName("其他费用");
+        costCostingVO1.setAmount("600");
+        costCostingVO1.setAlias("设置的别名");
+        costCostingVO1.setTargetResponsibilityCode("tzr1");
+        costCostingVO1.setTargetResponsibilityName("目标责任中心1");
+        List<String> strings1 = new ArrayList<>();
+        strings1.add("1000");
+        strings1.add("2000");
+        costCostingVO1.setTargetShareMoneys(strings);
+        costCostingVO1.setShareParamName("员工数");
+        costCostingVO1.setShareValue("3");
+        costCostingVO1.setShareParamProportion("0.3344");
+        costCostingVO1.setMoney("0.88");
+        costCostingVOS.add(costCostingVO1);
+
+        CostCostingVO costCostingVO2 = new CostCostingVO();
+        costCostingVO2.setResponsibilityCode("zr1");
+        costCostingVO2.setResponsibilityName("责任中心1");
+        // 会计科目可能为空
+        costCostingVO2.setAccountCode("A11");
+        costCostingVO2.setAccountName("人事费用");
+        costCostingVO2.setAmount("800");
+        costCostingVO2.setTargetResponsibilityCode("tzr1");
+        costCostingVO2.setTargetResponsibilityName("目标责任中心1");
+        List<String> strings2 = new ArrayList<>();
+        strings2.add("1000");
+        strings2.add("2000");
+        costCostingVO2.setTargetShareMoneys(strings);
+        costCostingVO2.setShareParamName("医疗收入");
+        costCostingVO2.setShareValue("95");
+        costCostingVO2.setShareParamProportion("0.19");
+        costCostingVO2.setMoney("0.55");
+        costCostingVOS.add(costCostingVO2);
+
+        CostCostingVO costCostingVO3 = new CostCostingVO();
+        costCostingVO3.setResponsibilityCode("zr1");
+        costCostingVO3.setResponsibilityName("责任中心1");
+        costCostingVO3.setAccountCode("A12");
+        costCostingVO3.setAccountName("其他费用");
+        costCostingVO3.setAmount("600");
+        costCostingVO3.setAlias("设置的别名");
+        costCostingVO3.setTargetResponsibilityCode("tzr1");
+        costCostingVO3.setTargetResponsibilityName("目标责任中心1");
+        List<String> strings3 = new ArrayList<>();
+        strings3.add("1000");
+        strings3.add("2000");
+        costCostingVO3.setTargetShareMoneys(strings);
+        costCostingVO3.setShareParamName("医疗收入");
+        costCostingVO3.setShareValue("95");
+        costCostingVO3.setShareParamProportion("0.19");
+        costCostingVO3.setMoney("0.44");
+        costCostingVOS.add(costCostingVO3);
+
+        CostCostingVO costCostingVO5 = new CostCostingVO();
+        costCostingVO5.setResponsibilityCode("zr2");
+        costCostingVO5.setResponsibilityName("责任中心2");
+        costCostingVO5.setAccountCode("A13");
+        costCostingVO5.setAccountName("责任费用");
+        costCostingVO5.setAmount("900");
+        costCostingVO5.setTargetResponsibilityCode("tzr2");
+        costCostingVO5.setTargetResponsibilityName("目标责任中心2");
+        List<String> strings5 = new ArrayList<>();
+        strings5.add("1000");
+        strings5.add("2000");
+        costCostingVO5.setTargetShareMoneys(strings5);
+        costCostingVO5.setShareParamName("麻醉科收入");
+        costCostingVO5.setShareValue("100");
+        costCostingVO5.setShareParamProportion("0.188");
+        costCostingVO5.setMoney("1.11");
+        costCostingVOS.add(costCostingVO5);
+
+        CostCostingVO costCostingVO6 = new CostCostingVO();
+        costCostingVO6.setResponsibilityCode("zr1");
+        costCostingVO6.setResponsibilityName("责任中心1");
+        // 会计科目可能为空
+        costCostingVO6.setAccountCode("A11");
+        costCostingVO6.setAccountName("人事费用");
+        costCostingVO6.setAmount("800");
+        costCostingVO6.setTargetResponsibilityCode("tzr2");
+        costCostingVO6.setTargetResponsibilityName("目标责任中心2");
+        List<String> strings6 = new ArrayList<>();
+        strings6.add("1000");
+        strings6.add("2000");
+        costCostingVO6.setTargetShareMoneys(strings6);
+        costCostingVO6.setShareParamName("麻醉科收入");
+        costCostingVO6.setShareValue("100");
+        costCostingVO6.setShareParamProportion("0.18");
+        costCostingVO6.setMoney("0.33");
+        costCostingVOS.add(costCostingVO6);
+
+        CostCostingVO costCostingVO7 = new CostCostingVO();
+        costCostingVO7.setResponsibilityCode("zr1");
+        costCostingVO7.setResponsibilityName("责任中心1");
+        costCostingVO7.setAccountCode("A12");
+        costCostingVO7.setAccountName("其他费用");
+        costCostingVO7.setAmount("600");
+        costCostingVO7.setAlias("设置的别名");
+        costCostingVO7.setTargetResponsibilityCode("tzr2");
+        costCostingVO7.setTargetResponsibilityName("目标责任中心2");
+        List<String> strings7 = new ArrayList<>();
+        strings7.add("1000");
+        strings7.add("2000");
+        costCostingVO7.setTargetShareMoneys(strings);
+        costCostingVO7.setShareParamName("麻醉科收入");
+        costCostingVO7.setShareValue("100");
+        costCostingVO7.setShareParamProportion("0.18");
+        costCostingVO7.setMoney("0.22");
+        costCostingVOS.add(costCostingVO7);
+
+        CostCostingVO costCostingVO8 = new CostCostingVO();
+        costCostingVO8.setResponsibilityCode("zr2");
+        costCostingVO8.setResponsibilityName("责任中心2");
+        costCostingVO8.setAccountCode("A13");
+        costCostingVO8.setAccountName("责任费用");
+        costCostingVO8.setAmount("900");
+        costCostingVO8.setTargetResponsibilityCode("tzr1");
+        costCostingVO8.setTargetResponsibilityName("目标责任中心1");
+        List<String> strings8 = new ArrayList<>();
+        strings8.add("1000");
+        strings8.add("2000");
+        costCostingVO8.setTargetShareMoneys(strings8);
+        costCostingVO8.setShareParamName("员工数");
+        costCostingVO8.setShareValue("3");
+        costCostingVO8.setShareParamProportion("0.3344");
+        costCostingVO8.setMoney("1.55");
+        costCostingVOS.add(costCostingVO8);
+
+        CostCostingVO costCostingVO9 = new CostCostingVO();
+        costCostingVO9.setResponsibilityCode("zr2");
+        costCostingVO9.setResponsibilityName("责任中心2");
+        costCostingVO9.setAccountCode("A13");
+        costCostingVO9.setAccountName("责任费用");
+        costCostingVO9.setAmount("900");
+        costCostingVO9.setTargetResponsibilityCode("tzr1");
+        costCostingVO9.setTargetResponsibilityName("目标责任中心1");
+        List<String> strings9 = new ArrayList<>();
+        strings9.add("1000");
+        strings9.add("2000");
+        costCostingVO9.setTargetShareMoneys(strings9);
+        costCostingVO9.setShareParamName("医疗收入");
+        costCostingVO9.setShareValue("95");
+        costCostingVO9.setShareParamProportion("0.19");
+        costCostingVO9.setMoney("2.66");
+        costCostingVOS.add(costCostingVO9);
+        return costCostingVOS;
+    }
+    /**
+     * 科室
+     */
 }

+ 225 - 0
src/main/java/com/imed/costaccount/service/impl/CostDepartmentProfitServiceImpl.java

@@ -0,0 +1,225 @@
+package com.imed.costaccount.service.impl;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.imed.costaccount.common.util.BeanUtil;
+import com.imed.costaccount.common.util.DateUtils;
+import com.imed.costaccount.common.util.PageUtils;
+import com.imed.costaccount.constants.NumberConstant;
+import com.imed.costaccount.enums.DateStyleEnum;
+import com.imed.costaccount.mapper.CostDepartmentProfitMapper;
+import com.imed.costaccount.model.*;
+import com.imed.costaccount.model.vo.CostDepartmentProfitVO;
+import com.imed.costaccount.service.*;
+import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
+
+
+@Service("costDepartmentProfitService")
+public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentProfitMapper, CostDepartmentProfit> implements CostDepartmentProfitService {
+
+    private final ReportFormService reportFormService;
+
+    private final IncomeCollectionService incomeCollectionService;
+
+    private final CostShareLevelService costShareLevelService;
+
+    private final ResponsibilityService responsibilityService;
+
+    private final ReportRelationService reportRelationService;
+
+    private final AllocationService allocationService;
+
+
+    public CostDepartmentProfitServiceImpl(ReportFormService reportFormService, IncomeCollectionService incomeCollectionService, CostShareLevelService costShareLevelService, ResponsibilityService responsibilityService, ReportRelationService reportRelationService, AllocationService allocationService) {
+        this.reportFormService = reportFormService;
+        this.incomeCollectionService = incomeCollectionService;
+        this.costShareLevelService = costShareLevelService;
+        this.responsibilityService = responsibilityService;
+        this.reportRelationService = reportRelationService;
+        this.allocationService = allocationService;
+    }
+
+    /**
+     * 查询科室损益数据
+     *
+     * @param current
+     * @param pageSize
+     * @param responsibilityCode
+     * @param date
+     * @param hospId
+     * @return
+     */
+    @Override
+    public PageUtils queryList(Integer current, Integer pageSize, String responsibilityCode, String date, Long hospId) {
+        int year = 0;
+        int month = 0;
+        if (StrUtil.isNotBlank(date)) {
+            Date dateTime = DateUtils.StringToDate(date, DateStyleEnum.YYYY_MM_DD);
+            year = DateUtil.year(dateTime);
+            month = DateUtil.month(dateTime) + 1;
+        }
+        Page<CostDepartmentProfit> departmentProfitPage = new Page<>(current, pageSize);
+        Page<CostDepartmentProfit> pages = this.page(departmentProfitPage, new QueryWrapper<CostDepartmentProfit>().lambda()
+                .eq(CostDepartmentProfit::getHospId, hospId)
+                .eq(StrUtil.isNotBlank(responsibilityCode), CostDepartmentProfit::getResponsibilityCode, responsibilityCode)
+                .eq(StrUtil.isNotBlank(date), CostDepartmentProfit::getYear, year)
+                .eq(StrUtil.isNotBlank(date), CostDepartmentProfit::getMonth, month));
+        List<CostDepartmentProfit> records = pages.getRecords();
+        List<CostDepartmentProfitVO> costDepartmentProfitVOList = BeanUtil.convertList(records, CostDepartmentProfitVO.class);
+        PageUtils pageUtils = new PageUtils(pages);
+        pageUtils.setList(costDepartmentProfitVOList);
+        return pageUtils;
+    }
+
+    /**
+     * 科室损益计算
+     *
+     * @param date
+     * @param hospId
+     */
+    @Override
+    public void setDepartmentProfit(String date, Long hospId) {
+        int year = 0;
+        int month = 0;
+        if (StrUtil.isNotBlank(date)) {
+            Date dateTime = DateUtils.StringToDate(date, DateStyleEnum.YYYY_MM_DD);
+            year = DateUtil.year(dateTime);
+            month = DateUtil.month(dateTime) + 1;
+        }
+        // 先查询指定条件的报表数据
+        List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
+                .eq(ReportForm::getHospId, hospId)
+                .eq(ReportForm::getReportType, NumberConstant.ZERO).ne(ReportForm::getCalcType, NumberConstant.ZERO));
+        // 遍历报表数据根据报表数据计算方式进行计算
+        // 查询最后一个层级的责任中心
+        List<CostShareLevel> costShareLevelList = costShareLevelService.list(new QueryWrapper<CostShareLevel>().lambda()
+                .eq(CostShareLevel::getHospId, hospId).orderByDesc(CostShareLevel::getLeverSort));
+        Long id = costShareLevelList.get(0).getId();
+        // 查询责任中心里面是这个层级的所有的收益中心
+        List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
+                .eq(Responsibility::getHospId, hospId).eq(Responsibility::getResponsibilityType, NumberConstant.ONE));
+        // 归集前
+        List<IncomeCollection> incomeList = incomeCollectionService.list(new QueryWrapper<IncomeCollection>().lambda()
+                .eq(IncomeCollection::getHospId, hospId)
+                .eq(year > 0, IncomeCollection::getYear, year).eq(month > 0, IncomeCollection::getMonth, month));
+
+        Map<Long, List<ReportRelation>> reportRelationMap = reportRelationService.list(new QueryWrapper<ReportRelation>().lambda().eq(ReportRelation::getHospId, hospId)).stream().collect(Collectors.groupingBy(ReportRelation::getReportId));
+        // TODO 需要查询归集后的数据
+
+        // 查询分摊的报表数据
+        List<Allocation> allocationList = allocationService.list(new QueryWrapper<Allocation>().lambda().eq(Allocation::getHospId, hospId)
+                .eq(year > 0, Allocation::getDateYear, year).eq(month > 0, Allocation::getDateMonth, month));
+
+        // 查询所有指定类型的损益表
+        // 每个责任中心对应报表都要生成记录
+        // 封装需要设置的数据
+        List<CostDepartmentProfitVO> list = new ArrayList<>();
+        int finalYear = year;
+        int finalMonth = month;
+        responsibilityList.forEach(i->{
+            reportFormList.forEach(j->{
+                CostDepartmentProfitVO costDepartmentProfitVO = new CostDepartmentProfitVO();
+                costDepartmentProfitVO.setYear(finalYear);
+                costDepartmentProfitVO.setMonth(finalMonth);
+                costDepartmentProfitVO.setReportNum(j.getNum());
+                costDepartmentProfitVO.setCostType(j.getCalcType());
+                costDepartmentProfitVO.setReportName(j.getReportName());
+                costDepartmentProfitVO.setResponsibilityCode(i.getResponsibilityCode());
+                costDepartmentProfitVO.setResponsibilityName(i.getResponsibilityName());
+                costDepartmentProfitVO.setReportParentId(i.getParentId());
+                costDepartmentProfitVO.setCostType(NumberConstant.ONE);
+                costDepartmentProfitVO.setIncomeType(NumberConstant.ONE);
+                costDepartmentProfitVO.setHospId(hospId);
+                list.add(costDepartmentProfitVO);
+            });
+        });
+        list.forEach(i->{
+            Integer calcType = i.getCostType();
+            switch (calcType){
+                case 1:
+                    // TODO 按照会计科目进行计算
+                    setAccountReportData(i,incomeList,reportRelationMap);
+                    break;
+                case 2:
+                    // TODO 按照分摊层级进行计算
+                    break;
+                case 3:
+                   // TODO 按照小计进行计算
+                    break;
+                case 4:
+                    // TODO 按照计算公式进行计算
+                    break;
+                case 5:
+                    // TODO  按照责任中心进行计算
+                    break;
+                default:
+                    i.setAmount(new BigDecimal("0.0000"));
+                    break;
+            }
+
+        });
+    }
+
+    /**
+     * 按照会计科目进行计算
+     * @param i
+     */
+    private void setAccountReportData(CostDepartmentProfitVO i,List<IncomeCollection> list,Map<Long, List<ReportRelation>> reportRelationMap) {
+        // 在报表关联里面查询当前报表关联的
+        List<ReportRelation> reportRelationList = reportRelationMap.get(i.getId());
+        if (CollUtil.isNotEmpty(reportRelationList)){
+            // 获取对应的会计科目信息  筛选会计科目的Code
+            List<String> accountList = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
+            // 查找在归集数据里面当前责任中心对应的这些会计科目的金额
+            List<IncomeCollection> incomeCollectionList = list.stream().filter(income -> income.getResponsibilityCode().equals(i.getResponsibilityCode()) && accountList.contains(income.getAccountingCode())).collect(Collectors.toList());
+            AtomicReference<BigDecimal> sum= new AtomicReference<>(new BigDecimal("0.000"));
+            incomeCollectionList.forEach(m->{
+                sum.updateAndGet(v -> v.add(m.getAmount()));
+            });
+            // TODO 需要查询分摊后的表
+
+            i.setAmount(new BigDecimal(sum.toString()));
+        }else {
+            i.setAmount(new BigDecimal("0.0000"));
+        }
+
+    }
+    /**
+     * 按照分摊层级进行计算
+     */
+    private void setShareLevelReportData(CostDepartmentProfitVO i,Map<Long, List<ReportRelation>> reportRelationMap,List<Allocation> allocationList){
+        List<ReportRelation> reportRelationList = reportRelationMap.get(i.getId());
+        // 找到对应的分摊层级的Id
+        List<String> shareLevelIds = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
+        if (CollUtil.isNotEmpty(shareLevelIds)){
+            // 查询报表里面是当前分摊层级的数据
+            AtomicReference<BigDecimal> sum= new AtomicReference<>(new BigDecimal("0.000"));
+            List<Allocation> allocations = allocationList.stream().filter(m -> m.getLevelSort().equals(i.getLevelSort()) && m.getTargetResponsibilityCode().equals(i.getResponsibilityCode())).collect(Collectors.toList());
+            allocations.forEach(m->{
+                sum.updateAndGet(v -> v.add(m.getAmount()));
+            });
+            i.setAmount(new BigDecimal(sum.toString()));
+        }else {
+            i.setAmount(new BigDecimal("0.0000"));
+        }
+    }
+    /**
+     * 按照责任中心进行计算
+     * 原始责任中心是设置的责任中心   目标责任中心是报表的责任中心
+     */
+    public void setResponsibilityCode(){
+
+    }
+}

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

@@ -109,7 +109,7 @@ public class ReportRelationServiceImpl extends ServiceImpl<ReportRelationMapper,
             throw new CostException("选择的报表项目不存在");
         }
         Integer calcType = byId.getCalcType();
-        if (calcType != 1 && calcType != 2) {
+        if (calcType != 1 && calcType != 2 && calcType!=5) {
             throw new CostException("选择的报表项目不能绑定关联关系");
         }
 

+ 3 - 0
src/main/java/com/imed/costaccount/web/CostCostingCollectionController.java

@@ -74,5 +74,8 @@ public class CostCostingCollectionController {
 		costCostingCollectionService.removeByIds(Arrays.asList(ids));
         return Result.ok();
     }
+    /**
+     * 科室计算
+     */
 
 }

+ 18 - 6
src/main/java/com/imed/costaccount/web/CostCostingGroupController.java

@@ -4,6 +4,7 @@ import com.imed.costaccount.common.exception.CostException;
 import com.imed.costaccount.common.util.PageUtils;
 import com.imed.costaccount.common.util.Result;
 import com.imed.costaccount.model.dto.StartDTO;
+import com.imed.costaccount.model.vo.CollectDataFormVO;
 import com.imed.costaccount.service.AllocationService;
 import com.imed.costaccount.service.CostCostingGroupService;
 import io.swagger.annotations.Api;
@@ -13,6 +14,7 @@ import io.swagger.annotations.ApiOperation;
 import org.springframework.web.bind.annotation.*;
 
 import javax.validation.Valid;
+import java.util.List;
 
 
 /**
@@ -74,24 +76,34 @@ public class CostCostingGroupController extends AbstractController {
     @PostMapping("/startAllocation")
     public Result startAllocation(@RequestBody @Valid StartDTO startDTO) {
 //        costCostingGroupService.startAllocation(startDTO, getHospId());
-        allocationService.startAllocation(startDTO,getHospId());
+        allocationService.startAllocation(startDTO, getHospId());
         return Result.ok();
     }
 
     @ApiOperation("分摊后列表")
     @GetMapping("/queryAfterAllocation")
     @ApiImplicitParams({
-            @ApiImplicitParam(name = "year",value = "年月日(yyyy-MM-dd)"),
-            @ApiImplicitParam(name = "responsibilityCode",value = "责任中心代码"),
-            @ApiImplicitParam(name = "current",value = "每页数据大小"),
-            @ApiImplicitParam(name = "pageSize",value = "每页数据大小")
+            @ApiImplicitParam(name = "year", value = "年月日(yyyy-MM-dd)"),
+            @ApiImplicitParam(name = "responsibilityCode", value = "责任中心代码"),
+            @ApiImplicitParam(name = "current", value = "每页数据大小"),
+            @ApiImplicitParam(name = "pageSize", value = "每页数据大小")
     })
     public Result queryAfterAllocation(@RequestParam(value = "year", required = false) String year,
                                        @RequestParam(value = "responsibilityCode", required = false) String responsibilityCode,
                                        @RequestParam(value = "current", defaultValue = "1", required = false) Integer current,
                                        @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
-        PageUtils pageUtils = allocationService.queryAfterAllocation(year, responsibilityCode, current, pageSize,getHospId());
+        PageUtils pageUtils = allocationService.queryAfterAllocation(year, responsibilityCode, current, pageSize, getHospId());
         return Result.ok(pageUtils);
     }
 
+    @ApiOperation("分摊后报表")
+    @GetMapping("/queryAfterAllocationForm")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "year", value = "年月日(yyyy-MM-dd)"),
+            @ApiImplicitParam(name = "responsibilityCode", value = "责任中心代码")
+    })
+    public Result queryAfterAllocationForm(@RequestParam(value = "year", required = false) String year,
+                                           @RequestParam(value = "responsibilityCode", required = false) String responsibilityCode) {
+        return Result.ok(allocationService.queryAfterAllocationForm(year, responsibilityCode,getHospId()));
+    }
 }

+ 98 - 0
src/main/java/com/imed/costaccount/web/CostDepartmentProfitController.java

@@ -0,0 +1,98 @@
+package com.imed.costaccount.web;
+
+import com.imed.costaccount.common.util.PageUtils;
+import com.imed.costaccount.common.util.Result;
+import com.imed.costaccount.common.util.UserContext;
+import com.imed.costaccount.model.CostDepartmentProfit;
+import com.imed.costaccount.service.CostDepartmentProfitService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Arrays;
+
+
+/**
+ * 科室损益计算
+ *
+ * @author KCYG
+ * @date 2021-08-24 16:24:08
+ */
+@RestController
+@RequestMapping("/costAccount/costdepartmentprofit")
+@Api(tags = "科室损益计算")
+public class CostDepartmentProfitController {
+    @Autowired
+    private CostDepartmentProfitService costDepartmentProfitService;
+
+    /**
+     * 分页查询列表
+     * 查询的是
+     */
+    @GetMapping("/list")
+    @ApiOperation("科室损益计算查询")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "current", value = "当前页", required = true),
+            @ApiImplicitParam(name = "pageSize", value = "当前页大小", required = true),
+            @ApiImplicitParam(name = "responsibilityCode", value = "责任中心代码", required = false),
+            @ApiImplicitParam(name = "date", value = "年月yyyy-MM-dd")
+    })
+    public Result list(@RequestParam(value = "current", defaultValue = "1") Integer current,
+                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
+                       @RequestParam(value = "responsibilityCode") String responsibilityCode,
+                       @RequestParam(value = "date") String date){
+        Long hospId = UserContext.getHospId();
+        PageUtils pageUtils = costDepartmentProfitService.queryList(current,pageSize,responsibilityCode,date,hospId);
+        return Result.ok(pageUtils);
+    }
+
+    /**
+     * 科室损益计算
+     */
+    @PostMapping("/setDepartmentProfit")
+    @ApiOperation("进行科室损益计算")
+    public Result setDepartmentProfit(@RequestBody String date){
+        Long hospId = UserContext.getHospId();
+        costDepartmentProfitService.setDepartmentProfit(date,hospId);
+        return  Result.ok();
+    }
+
+    /**
+     * 信息
+     */
+    @RequestMapping("/info/{id}")
+    public Result info(@PathVariable("id") Long id){
+		CostDepartmentProfit costDepartmentProfit = costDepartmentProfitService.getById(id);
+        return Result.ok(costDepartmentProfit);
+    }
+
+    /**
+     * 保存
+     */
+    @RequestMapping("/save")
+    public Result save(@RequestBody CostDepartmentProfit costDepartmentProfit){
+		costDepartmentProfitService.save(costDepartmentProfit);
+        return Result.ok();
+    }
+
+    /**
+     * 修改
+     */
+    @RequestMapping("/update")
+    public Result update(@RequestBody CostDepartmentProfit costDepartmentProfit){
+		costDepartmentProfitService.updateById(costDepartmentProfit);
+        return Result.ok();
+    }
+
+    /**
+     * 删除
+     */
+    @RequestMapping("/delete")
+    public Result delete(@RequestBody Long[] ids){
+		costDepartmentProfitService.removeByIds(Arrays.asList(ids));
+        return Result.ok();
+    }
+}

+ 72 - 2
src/main/java/com/imed/costaccount/web/ExcelController.java

@@ -17,6 +17,8 @@ import com.imed.costaccount.service.*;
 import com.imed.costaccount.service.impl.DepartmentServiceImpl;
 import com.imed.costaccount.service.impl.ProductServiceImpl;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.poi.ss.usermodel.Sheet;
@@ -63,12 +65,15 @@ public class ExcelController extends AbstractController{
 
     private final ShareParamValueService shareParamValueService;
 
+    private final CostCostingCollectionService costCostingCollectionService;
+
+    private final AllocationService allocationServicel;
 
     public ExcelController(UserService userService, DepartmentServiceImpl departmentService, ProductServiceImpl productService,
                            AccountingService accountingService, AccountingProductService accountingProductService,
                            ResponsibilityDepartmentService responsibilityDepartmentService,
                            CostShareParamService costShareParamService, CostIncomeGroupService costIncomeGroupService, JwtUtil jwtUtil,
-                           CostCostingGroupService costCostingGroupService, ShareParamValueService shareParamValueService) {
+                           CostCostingGroupService costCostingGroupService, ShareParamValueService shareParamValueService, CostCostingCollectionService costCostingCollectionService, AllocationService allocationServicel) {
         this.userService = userService;
         this.departmentService = departmentService;
         this.productService = productService;
@@ -80,6 +85,8 @@ public class ExcelController extends AbstractController{
         this.jwtUtil = jwtUtil;
         this.costCostingGroupService = costCostingGroupService;
         this.shareParamValueService = shareParamValueService;
+        this.costCostingCollectionService = costCostingCollectionService;
+        this.allocationServicel = allocationServicel;
     }
 
     @ApiOperation("用户导出模板设置")
@@ -306,7 +313,6 @@ public class ExcelController extends AbstractController{
         writer.close();
         IoUtil.close(out);
     }
-
     /**
      * 成本数据导出模板
      */
@@ -361,7 +367,59 @@ public class ExcelController extends AbstractController{
         writer.close();
         IoUtil.close(out);
     }
+    /**
+     * 测试模板数据导出
+     */
+    @ApiOperation("分摊报表输出")
+    @GetMapping("/getShareReportTemplateTwo")
+    public void getShareReportTemplateTwo(HttpServletResponse response,Integer shareNumber,String token) throws IOException {
+        int userId = jwtUtil.getUserId(token);
+        User user = userService.getById(userId);
+        if (Objects.isNull(user)){
+            throw new CostException(500,"用户不存在");
+        }
+        Long hospId = user.getHospId();
+        String uuid = UUID.randomUUID().toString();
+        String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
+        FileUtil.del(FileUtil.file(url));
+        ExcelWriter writer = new ExcelWriter(url);
+        Sheet sheet = writer.getSheet();
+        // 第几次分摊
+        writer= costCostingCollectionService.getShareReportTemplate(writer,shareNumber,sheet);
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
+        response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
+        ServletOutputStream out = null;
+        out = response.getOutputStream();
+        writer.flush(out, true);
+        writer.close();
+        IoUtil.close(out);
 
+    }
+    @ApiOperation("分摊报表输出2.0")
+    @GetMapping("/getShareReportTemplate")
+    public void getTemplate(HttpServletResponse response,Integer levelSort,String token,Integer year,Integer month) throws IOException {
+        int userId = jwtUtil.getUserId(token);
+        User user = userService.getById(userId);
+        if (Objects.isNull(user)){
+            throw new CostException(500,"用户不存在");
+        }
+        Long hospId = user.getHospId();
+        String uuid = UUID.randomUUID().toString();
+        String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
+        FileUtil.del(FileUtil.file(url));
+        ExcelWriter writer = new ExcelWriter(url);
+        Sheet sheet = writer.getSheet();
+        // 第几次分摊
+        writer= allocationServicel.getShareReportTemplate(writer,levelSort,sheet,year,month);
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
+        response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
+        ServletOutputStream out = null;
+        out = response.getOutputStream();
+        writer.flush(out, true);
+        writer.close();
+        IoUtil.close(out);
+
+    }
     /**
      * 获取执行类型的成本项目
      *
@@ -479,6 +537,10 @@ public class ExcelController extends AbstractController{
      */
     @PostMapping("/importDataByFileType")
     @ApiOperation("收入数据导入")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "fileType", value = "文件类型 1 成本分摊参数值 2收入 3成本数据"),
+            @ApiImplicitParam(name = "dateTime", value = "年月yyyy-MM")}
+    )
     public Result importProductAccount(@RequestParam("file") MultipartFile file ,Integer fileType,String dateTime) {
         if (Objects.isNull(file)) {
             throw new CostException(500, "请选择文件");
@@ -501,6 +563,10 @@ public class ExcelController extends AbstractController{
     }
     @PostMapping("/importCostingGroup")
     @ApiOperation("成本数据导入")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "fileType", value = "文件类型 1 成本分摊参数值 2收入 3成本数据"),
+            @ApiImplicitParam(name = "dateTime", value = "年月yyyy-MM")}
+    )
     public Result importCostingGroup(@RequestParam("file") MultipartFile file ,Integer fileType,String dateTime) {
         if (Objects.isNull(file)) {
             throw new CostException(500, "请选择文件");
@@ -523,6 +589,10 @@ public class ExcelController extends AbstractController{
     }
     @PostMapping("/importShareParamGroup")
     @ApiOperation("成本分摊参数值导入")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "fileType", value = "文件类型 1 成本分摊参数值 2收入 3成本数据"),
+            @ApiImplicitParam(name = "dateTime", value = "年月yyyy-MM")}
+    )
     public Result importShareParamGroup(@RequestParam("file") MultipartFile file ,Integer fileType,String dateTime){
         if (Objects.isNull(file)) {
             throw new CostException(500, "请选择文件");

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

@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="com.imed.costaccount.mapper.AllocationQueryMapper">
+
+    <!-- 可根据自己的需求,是否要使用 -->
+    <resultMap type="com.imed.costaccount.model.AllocationQuery" id="allocationQueryMap">
+        <result property="id" column="id"/>
+        <result property="dateYear" column="date_year"/>
+        <result property="dateMonth" column="date_month"/>
+        <result property="hospId" column="hosp_id"/>
+        <result property="responsibilityCode" column="responsibility_code"/>
+        <result property="responsibilityName" column="responsibility_name"/>
+        <result property="originId" column="origin_id"/>
+        <result property="originType" column="origin_type"/>
+        <result property="amount" column="amount"/>
+        <result property="accountingCode" column="accounting_code"/>
+        <result property="accountingName" column="accounting_name"/>
+        <result property="createTime" column="create_time"/>
+        <result property="deleteTime" column="delete_time"/>
+    </resultMap>
+
+
+    <select id="getTotalMoney" resultType="java.math.BigDecimal">
+        select sum(amount)
+        from cost_allocation_query
+        where date_year = #{dateYear}
+          and date_month = #{month}
+          and hosp_id = #{hospId}
+    </select>
+    <select id="getRespCodeAndName" resultType="com.imed.costaccount.model.vo.CodeAndNameVO">
+        select responsibility_code as code, responsibility_name as name
+        from cost_allocation_query
+        where date_year = #{dateYear}
+          and date_month = #{month}
+          and hosp_id = #{hospId}
+    </select>
+    <select id="getAccountCodeAndName" resultType="com.imed.costaccount.model.vo.CodeAndNameVO">
+        select accounting_code as code, accounting_name as name
+        from cost_allocation_query
+        where date_year = #{dateYear}
+          and date_month = #{month}
+          and hosp_id = #{hospId}
+    </select>
+    <select id="getCountByRespAndAccounts" resultType="java.math.BigDecimal">
+        select IFNULL(sum(amount), 0)
+        from cost_allocation_query
+        where date_year = 2021
+        and date_month = 1
+        and hosp_id = 11
+        and responsibility_code = #{code}
+        and accounting_code in
+        <foreach collection="accountCodes" item="item" open="(" separator="," close=")">
+            #{item}
+        </foreach>
+
+    </select>
+    <select id="getTotalByAccountAndResps" resultType="java.math.BigDecimal">
+        select IFNULL(sum(amount), 0)
+        from cost_allocation_query
+        where date_year = 2021
+        and date_month = 1
+        and hosp_id = 11
+        and accounting_code = #{code}
+        and responsibility_code in
+        <foreach collection="respCodes" item="item" open="(" separator="," close=")">
+            #{item}
+        </foreach>
+    </select>
+    <select id="getTotalByAccountAndRespCode" resultType="java.math.BigDecimal">
+        select IFNULL(sum(amount), 0)
+        from cost_allocation_query
+        where date_year = 2021
+        and date_month = 1
+        and hosp_id = 11
+        and accounting_code =  #{accountCode}
+        and responsibility_code = #{responsibilityCode}
+    </select>
+
+
+</mapper>

+ 5 - 0
src/main/resources/mapper/CostCostingCollectionMapper.xml

@@ -26,6 +26,11 @@
 <!--        <result property="targetResponsibilityName" column="target_responsibility_name" />-->
         <result property="shareAmount" column="share_amount" />
     </resultMap>
+    <select id="selectGroupBy" resultType="com.imed.costaccount.model.vo.CostDepartmentProfitVO">
+        SELECT responsibility_code,responsibility_name, SUM(amount) as amount
+        from cost_costing_collection  where hosp_id=#{hospId}
+        GROUP BY responsibility_code
+    </select>
 
 
 </mapper>

+ 24 - 0
src/main/resources/mapper/CostDepartmentProfitMapper.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="com.imed.costaccount.mapper.CostDepartmentProfitMapper">
+
+	<!-- 可根据自己的需求,是否要使用 -->
+    <resultMap type="com.imed.costaccount.model.CostDepartmentProfit" id="costDepartmentProfitMap">
+        <result property="id" column="id"/>
+        <result property="year" column="year"/>
+        <result property="month" column="month"/>
+        <result property="reportNum" column="report_num"/>
+        <result property="reportName" column="report_name"/>
+        <result property="responsibilityCode" column="responsibility_code"/>
+        <result property="responsibilityName" column="responsibility_name"/>
+        <result property="costType" column="cost_type"/>
+        <result property="incomeType" column="income_type"/>
+        <result property="amount" column="amount"/>
+        <result property="hospId" column="hosp_id"/>
+        <result property="createTime" column="create_time"/>
+        <result property="deleteTime" column="delete_time"/>
+    </resultMap>
+
+
+</mapper>

+ 32 - 1
src/test/java/com/imed/costaccount/service/impl/AllocationServiceImplTest.java

@@ -1,6 +1,7 @@
 package com.imed.costaccount.service.impl;
 
 import com.imed.costaccount.common.util.PageUtils;
+import com.imed.costaccount.model.Allocation;
 import com.imed.costaccount.service.AccountingService;
 import com.imed.costaccount.service.AllocationService;
 import lombok.extern.slf4j.Slf4j;
@@ -10,6 +11,7 @@ import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.transaction.annotation.Transactional;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest
@@ -28,7 +30,36 @@ public class AllocationServiceImplTest {
         long hospId = 11L;
         PageUtils pageUtils = allocationService.queryAfterAllocation(year, responsibilityCode, current, pageSize, hospId);
         Assert.assertNotNull(pageUtils);
-        log.info("pageUtils={}",pageUtils);
+        log.info("pageUtils={}", pageUtils);
+    }
+
+    @Test
+    @Transactional
+    public void testTransaction1() {
+        Allocation before = allocationService.getById(40);
+//        allocationService.removeById(40);
+        try {
+            Thread.sleep(1000);
+        } catch (InterruptedException e) {
+            throw new RuntimeException("111");
+        }
+
+        Allocation after = allocationService.getById(40);
+        Assert.assertEquals(before, after);
+    }
+
+
+
+    @Test
+    public void test() {
+        new Thread(this::testTransaction1).start();
+        new Thread(this::testDel).start();
+    }
+
+    @Test
+    @Transactional
+    public void testDel() {
+        allocationService.removeById(40);
     }
 
 }