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 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 splitLevelVOLinkedList = new LinkedList<>(); // // 目标层级责任中心集合 // LinkedList targetSplitLevelVOLinkedList = new LinkedList<>(); // setMockData(splitLevelVOLinkedList, targetSplitLevelVOLinkedList); List costCostingVOS = setMockDa(); Map> responsibilityMap = costCostingVOS.stream().collect(Collectors.groupingBy(CostCostingVO::getResponsibilityCode)); // Map collect = costCostingVOS.stream().collect(Collectors.toMap(CostCostingVO::getResponsibilityCode, CostCostingVO::getAccountCode)); Map> targetResponsibilityMap = costCostingVOS.stream().collect(Collectors.groupingBy(CostCostingVO::getTargetResponsibilityCode)); Map allAccMap = costCostingVOS.stream().collect(Collectors.toMap(k -> k.getResponsibilityName()+k.getAccountName()+k.getTargetResponsibilityName()+k.getShareParamName(), synOne -> synOne)); Map 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 keySet = responsibilityMap.keySet(); for (String key : keySet) { List costCostingVOS1 = responsibilityMap.get(key); Map 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 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 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 targetSet = targetResponsibilityMap.keySet(); for (String target : targetSet) { List costCostingVOS1 = targetResponsibilityMap.get(target); Map 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 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 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 splitLevelVOLinkedList, LinkedList targetSplitLevelVOLinkedList) { // // 设置分摊层级责任中心数据 // SplitLevelVO splitLevelVO = new SplitLevelVO(); // splitLevelVO.setResponsibilityName("责任中心1"); // splitLevelVO.setResponsibilityCode("zr1"); // LinkedList 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 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 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 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 setMockDa() { List 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 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 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 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 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 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 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 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 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 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; } /** * 科室 */ }