Ver Fonte

分摊报表输出

ljx há 4 anos atrás
pai
commit
16c0133227

+ 232 - 2
src/main/java/com/imed/costaccount/service/impl/AllocationServiceImpl.java

@@ -12,14 +12,14 @@ 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.UserContext;
 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.AllocationReportVO;
 import com.imed.costaccount.model.vo.AfterAllocationVO;
+import com.imed.costaccount.model.vo.AllocationReportVO;
 import com.imed.costaccount.model.vo.CostShareLevelVO;
 import com.imed.costaccount.service.*;
 import org.apache.poi.ss.usermodel.Sheet;
@@ -30,6 +30,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
 
 
@@ -313,4 +314,233 @@ 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
+     */
+    @NotNull
+    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;
+    }
+
 }