Quellcode durchsuchen

Merge branch 'master' of huangrui/CostAccount into dev

lijiaxi vor 4 Jahren
Ursprung
Commit
940f1bbecc

+ 7 - 0
src/main/java/com/imed/costaccount/model/Responsibility.java

@@ -1,5 +1,6 @@
 package com.imed.costaccount.model;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableLogic;
 import com.baomidou.mybatisplus.annotation.TableName;
@@ -7,8 +8,10 @@ import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.experimental.Accessors;
+import org.springframework.data.annotation.Transient;
 
 import java.io.Serializable;
+import java.util.List;
 
 /**
  * 责任中心表
@@ -81,4 +84,8 @@ public class Responsibility implements Serializable {
 	@TableLogic(value = "0",delval = "UNIX_TIMESTAMP(NOW()) * 1000")
 	private Long deleteTime;
 
+	@TableField(exist = false)
+	@Transient
+	private List<Responsibility> children;
+
 }

+ 8 - 0
src/main/java/com/imed/costaccount/service/DepartmentProfitFileService.java

@@ -22,5 +22,13 @@ public interface DepartmentProfitFileService extends IService<DepartmentProfitFi
      * @return
      */
     PageUtils queryList(Integer current, Integer pageSize, String date, String reportName, Long hospId);
+
+    /**
+     * 添加科室损益记录
+     * @param date 时间
+     * @param reportType 报表类型
+     * @param hospId 医院ID
+     */
+    void addDepartmentProfitFile(String date, Integer reportType, Long hospId);
 }
 

+ 16 - 6
src/main/java/com/imed/costaccount/service/impl/CostDepartmentProfitServiceImpl.java

@@ -120,6 +120,7 @@ public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentP
         if (CollUtil.isEmpty(costShareLevelList)) {
             throw new CostException(500, "分摊层级未设置");
         }
+        // TODO 可能多个
         Long id = costShareLevelList.get(0).getId();
         // 查询责任中心里面是这个层级的所有的收益中心
         List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
@@ -132,7 +133,7 @@ public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentP
             throw new CostException(500, "归集后数据不存在");
         }
         Map<Long, List<ReportRelation>> reportRelationMap = reportRelationService.list(new QueryWrapper<ReportRelation>().lambda().eq(ReportRelation::getHospId, hospId)).stream().collect(Collectors.groupingBy(ReportRelation::getReportId));
-        // 分摊后的数据getOriginType等于2说明是分摊后的数据
+        // 分摊后的数据说明是分摊后的数据
         List<AllocationQuery> allocationQueryList = allocationQueryService.list(new QueryWrapper<AllocationQuery>().lambda().eq(AllocationQuery::getHospId, hospId)
                 .eq(year > 0, AllocationQuery::getDateYear, year)
                 .eq(month > 0, AllocationQuery::getDateMonth, month)
@@ -376,6 +377,13 @@ public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentP
                 writer.merge(jj, i + 1, 0, 0, cellValue1, false);
             }
         }
+        // 设置列宽
+        for (int i = 0; i < 50; i++) {
+            // 调整每一列宽度
+            sheet.autoSizeColumn((short) i);
+            // 解决自动设置列宽中文失效的问题
+            sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 14 / 10);
+        }
     }
 
     /**
@@ -457,9 +465,11 @@ public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentP
                 // 查询报表里面是当前分摊层级的数据
                 List<Allocation> allocations = allocationList.stream().filter(i -> i.getTargetResponsibilityCode().equals(costDepartmentProfitVO.getResponsibilityCode()) && responsibilityCodes.contains(i.getResponsibilityCode())).collect(Collectors.toList());
                 if (CollUtil.isNotEmpty(allocations)) {
-                    allocations.forEach(m -> {
-                        sum.updateAndGet(v -> v.add(m.getAmount()));
-                    });
+                    BigDecimal reduce = allocations.stream().map(Allocation::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
+                    sum.set(reduce);
+//                    allocations.forEach(m -> {
+//                        sum.updateAndGet(v -> v.add(m.getAmount()));
+//                    });
                 }
             }
         }
@@ -483,7 +493,7 @@ public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentP
         Long reportParentId = costDepartmentProfitVO.getReportParentId();
         String responsibilityCode = costDepartmentProfitVO.getResponsibilityCode();
         // 查询报表里面在当前父层级下的并且不是自己的报表项目
-        List<CostDepartmentProfitVO> costDepartmentProfitVOS = profitVOS.stream().filter(i -> i.getReportParentId().equals(reportParentId) && i.getResponsibilityCode().equals(responsibilityCode) && !NumberConstant.THREE.equals(i.getCalcType())).collect(Collectors.toList());
+        List<CostDepartmentProfitVO> costDepartmentProfitVOS = allList.stream().filter(i -> i.getReportParentId().equals(reportParentId) && i.getResponsibilityCode().equals(responsibilityCode) && !NumberConstant.THREE.equals(i.getCalcType())).collect(Collectors.toList());
         AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
         // 遍历数据 之前的已经经过了计算
         costDepartmentProfitVOS.forEach(i -> {
@@ -493,7 +503,7 @@ public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentP
                 throw new CostException(500, "报表未找到");
             }
             BigDecimal amount = getAmount(costDepartmentProfitVOS1, costShareLevelList, responsibilityCode, list, allocationQueryReportVOList, reportRelationMap, allocationList, listMap,allList);
-            sum.updateAndGet(v -> v.add(amount));
+            sum.set(amount.add(sum.get()));;
         });
 //        costDepartmentProfitVO.setAmount(new BigDecimal(sum.toString()));
         return sum.get();

+ 34 - 3
src/main/java/com/imed/costaccount/service/impl/DepartmentProfitFileServiceImpl.java

@@ -13,6 +13,8 @@ import com.imed.costaccount.model.DepartmentProfitFile;
 import com.imed.costaccount.model.vo.DepartmentProfitFileVO;
 import com.imed.costaccount.service.DepartmentProfitFileService;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.List;
 
@@ -33,9 +35,14 @@ public class DepartmentProfitFileServiceImpl extends ServiceImpl<DepartmentProfi
      */
     @Override
     public PageUtils queryList(Integer current, Integer pageSize, String date, String reportName, Long hospId) {
-        DateTime dateTime = DateUtil.parse(date);
-        int dateYear = DateUtil.year(dateTime);
-        int month=DateUtil.month(dateTime)+1;
+        DateTime dateTime;
+        int dateYear = 0;
+        int month=0;
+        if (StrUtil.isNotBlank(date)){
+            dateTime = DateUtil.parse(date);
+            dateYear = DateUtil.year(dateTime);
+            month=DateUtil.month(dateTime)+1;
+        }
         Page<DepartmentProfitFile> profitFilePage = new Page<>(current, pageSize);
         Page<DepartmentProfitFile> pages = this.page(profitFilePage, new QueryWrapper<DepartmentProfitFile>()
                 .lambda()
@@ -51,4 +58,28 @@ public class DepartmentProfitFileServiceImpl extends ServiceImpl<DepartmentProfi
         return pageUtils;
     }
 
+    /**
+     * 添加科室损益记录
+     *
+     * @param date       时间
+     * @param reportType 报表类型
+     * @param hospId     医院ID
+     */
+    @Override
+    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
+    public void addDepartmentProfitFile(String date, Integer reportType, Long hospId) {
+        DepartmentProfitFile departmentProfitFile = new DepartmentProfitFile();
+        DateTime dateTime = DateUtil.parse(date);
+        int year = DateUtil.year(dateTime);
+        int month = DateUtil.month(dateTime)+1;
+        departmentProfitFile.setHospId(hospId);
+        departmentProfitFile.setYear(year);
+        departmentProfitFile.setMonth(month);
+        departmentProfitFile.setReportType(reportType);
+        String reportName=reportType==1?year+"年"+month+"月-损益表.xlsx":year+"年"+month+"月-其他表.xlsx";
+        departmentProfitFile.setReportName(reportName);
+        departmentProfitFile.setCreateTime(System.currentTimeMillis());
+        this.save(departmentProfitFile);
+    }
+
 }

+ 56 - 6
src/main/java/com/imed/costaccount/service/impl/HospProfitAndLossServiceImpl.java

@@ -8,6 +8,7 @@ import cn.hutool.core.util.ReUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.poi.excel.ExcelUtil;
 import cn.hutool.poi.excel.ExcelWriter;
+import cn.hutool.poi.excel.StyleSet;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.imed.costaccount.common.enums.CalcTypeEnum;
@@ -21,6 +22,9 @@ import com.imed.costaccount.model.vo.RelationVO;
 import com.imed.costaccount.model.vo.ReportFormVO;
 import com.imed.costaccount.service.*;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
 import org.springframework.stereotype.Service;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
@@ -390,24 +394,54 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
         // 得到所有责任中心的子节点
         List<Responsibility> leafResp = responsibilityService.getLeafResp(hospId);
 
+        // 得到上一层的title子节点
         ExcelWriter writer = ExcelUtil.getWriter();
-        List<String> secondTitleList = leafResp.stream().map(Responsibility::getResponsibilityName).collect(Collectors.toList());
+        List<String> secondTitleListCode = leafResp.stream().map(Responsibility::getResponsibilityCode).collect(Collectors.toList());
         writer.merge(leafResp.size() + 1, "全院损益计算导出 \n" + "制表时间:" + DateUtil.now());
+        // 得到两层结构
         writer.setColumnWidth(-1, 20);
         writer.passCurrentRow();
-        writer.merge(2, 2, 0, 1, "项目", false);
-        for (int i = 0; i < secondTitleList.size(); i++) {
-            String str = secondTitleList.get(i);
+        writer.merge(2, 2, 0, 1, "项目", true);
+        int oldSize = 2;
+        Map<Long, Object> map = new HashMap<>();
+        for (int i = 0; i < leafResp.size(); i++) {
+            Responsibility responsibility = leafResp.get(i);
+            String str = responsibility.getResponsibilityName();
             writer.writeCellValue(i + 2, 2, str);
+            // 写父亲层级
+            Long parentId = responsibility.getParentId();
+            if (map.get(parentId) != null) {
+                // 如果parentId = 0
+                if (parentId != 0L) {
+                    continue;
+                }
+            }
+            Responsibility byId = responsibilityService.getById(parentId);
+            String name = "";
+            if (byId != null) {
+                name = byId.getResponsibilityName();
+            }
+            int count = (int) leafResp.stream().filter(o -> o.getParentId().equals(parentId)).count();
+
+
+            if (count == 1) {
+                writer.writeCellValue(oldSize, 1, name);
+            } else {
+                writer.merge(1, 1, oldSize, oldSize + count - 1, name, false);
+            }
+            oldSize = oldSize + count;
+            map.put(parentId, oldSize);
+
         }
         // 得到全院损益报表处理 树状结构(only 2层)
         List<ReportFormVO> allHospList = reportFormService.getAllHospList(hospId);
         if (allHospList.isEmpty()) {
-            return;
+            throw new CostException("请先设置全院损益报表");
         }
         // 查询所有的全院损益数据  内存溢出问题
         List<HospProfitAndLoss> list = getAllDataByDate(year, month, hospId);
         int lastRow = 3;
+        Sheet sheet = writer.getSheet();
         for (int i = 0; i < allHospList.size(); i++) {
             ReportFormVO parentFormVO = allHospList.get(i);
             List<ReportFormVO> children = parentFormVO.getChildren();
@@ -420,15 +454,25 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
             for (int j = 0; j < size; j++) {
                 // todo  可以抽取出单独方法
                 ReportFormVO childFormVO = children.get(j);
+
                 writer.writeCellValue(1, lastRow + j, childFormVO.getReportName());
                 // 单独计每个数据的责任中心对应的金额
+                for (int k = 0; k < secondTitleListCode.size(); k++) {
+
+                    String responsibilityCode = secondTitleListCode.get(k);
+                    Integer num = childFormVO.getNum();
+                    HospProfitAndLoss loss = list.stream().filter(o -> o.getReportNum().equals(num) && o.getResponsibilityCode().equals(responsibilityCode)).findAny()
+                            .orElse(null);
+                    BigDecimal bigDecimal = Objects.isNull(loss) ? BigDecimal.ZERO : loss.getAmount();
+                    writer.writeCellValue(k + 2, lastRow + j, bigDecimal);
+                }
 
             }
             lastRow = lastRow + size;
         }
 
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
-        response.setHeader("Content-Disposition", "attachment;filename=" + year + month + ".xls");
+        response.setHeader("Content-Disposition", "attachment;filename=" + "全院损益列表" + ".xls");
         ServletOutputStream out = null;
 
         try {
@@ -443,6 +487,12 @@ public class HospProfitAndLossServiceImpl extends ServiceImpl<HospProfitAndLossM
 
     }
 
+//    private List<String> getLeafRespOnlyOne(Long hospId, List<Responsibility> leafResp) {
+//        List<Long> parentIds = leafResp.stream().map(Responsibility::getParentId).collect(Collectors.toList());
+//        responsibilityService.getById(pare)
+//
+//    }
+
     private List<HospProfitAndLoss> getAllDataByDate(int year, int month, Long hospId) {
         return this.list(
                 new LambdaQueryWrapper<HospProfitAndLoss>()

+ 10 - 1
src/main/java/com/imed/costaccount/web/DepartmentProfitFileController.java

@@ -49,7 +49,16 @@ public class DepartmentProfitFileController {
         return Result.ok(pageUtils);
     }
 
-
+    /**
+     * 计算科室损益记录
+     */
+    @PostMapping("addDepartmentProfitFile")
+    @ApiOperation("添加科室损益记录")
+    public Result addDepartmentProfitFile(String date,Integer reportType){
+        Long hospId = UserContext.getHospId();
+        departmentProfitFileService.addDepartmentProfitFile(date,reportType,hospId);
+        return Result.ok();
+    }
     /**
      * 信息
      */