CostDepartmentProfitServiceImpl.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.poi.excel.ExcelWriter;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  10. import com.imed.costaccount.common.enums.DateStyleEnum;
  11. import com.imed.costaccount.common.exception.CostException;
  12. import com.imed.costaccount.common.util.BeanUtil;
  13. import com.imed.costaccount.common.util.DateUtils;
  14. import com.imed.costaccount.common.util.PageUtils;
  15. import com.imed.costaccount.common.util.UserContext;
  16. import com.imed.costaccount.constants.NumberConstant;
  17. import com.imed.costaccount.mapper.CostDepartmentProfitMapper;
  18. import com.imed.costaccount.model.*;
  19. import com.imed.costaccount.model.vo.AllocationQueryReportVO;
  20. import com.imed.costaccount.model.vo.CostDepartmentProfitVO;
  21. import com.imed.costaccount.service.*;
  22. import org.apache.poi.ss.usermodel.Sheet;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Propagation;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import java.math.BigDecimal;
  27. import java.util.*;
  28. import java.util.concurrent.atomic.AtomicReference;
  29. import java.util.stream.Collectors;
  30. @Service("costDepartmentProfitService")
  31. public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentProfitMapper, CostDepartmentProfit> implements CostDepartmentProfitService {
  32. private final ReportFormService reportFormService;
  33. private final IncomeCollectionService incomeCollectionService;
  34. private final CostShareLevelService costShareLevelService;
  35. private final ResponsibilityService responsibilityService;
  36. private final ReportRelationService reportRelationService;
  37. private final AllocationService allocationService;
  38. private final AllocationQueryService allocationQueryService;
  39. public CostDepartmentProfitServiceImpl(ReportFormService reportFormService, IncomeCollectionService incomeCollectionService, CostShareLevelService costShareLevelService, ResponsibilityService responsibilityService, ReportRelationService reportRelationService, AllocationService allocationService, AllocationQueryService allocationQueryService) {
  40. this.reportFormService = reportFormService;
  41. this.incomeCollectionService = incomeCollectionService;
  42. this.costShareLevelService = costShareLevelService;
  43. this.responsibilityService = responsibilityService;
  44. this.reportRelationService = reportRelationService;
  45. this.allocationService = allocationService;
  46. this.allocationQueryService = allocationQueryService;
  47. }
  48. /**
  49. * 查询科室损益数据
  50. *
  51. * @param current
  52. * @param pageSize
  53. * @param responsibilityCode
  54. * @param date
  55. * @param hospId
  56. * @return
  57. */
  58. @Override
  59. public PageUtils queryList(Integer current, Integer pageSize, String responsibilityCode, String date, Long hospId) {
  60. int year = 0;
  61. int month = 0;
  62. if (StrUtil.isNotBlank(date)) {
  63. Date dateTime = DateUtils.StringToDate(date, DateStyleEnum.YYYY_MM_DD);
  64. year = DateUtil.year(dateTime);
  65. month = DateUtil.month(dateTime) + 1;
  66. }
  67. Page<CostDepartmentProfit> departmentProfitPage = new Page<>(current, pageSize);
  68. // 查询的时候过过滤那些计算方式是不设置的数据
  69. Page<CostDepartmentProfit> pages = this.page(departmentProfitPage, new QueryWrapper<CostDepartmentProfit>().lambda()
  70. .eq(CostDepartmentProfit::getHospId, hospId)
  71. .eq(StrUtil.isNotBlank(responsibilityCode), CostDepartmentProfit::getResponsibilityCode, responsibilityCode)
  72. .eq(StrUtil.isNotBlank(date), CostDepartmentProfit::getYear, year)
  73. .eq(StrUtil.isNotBlank(date), CostDepartmentProfit::getMonth, month)
  74. .ne(CostDepartmentProfit::getCalcType, NumberConstant.ZERO).orderByDesc(CostDepartmentProfit::getAmount));
  75. List<CostDepartmentProfit> records = pages.getRecords();
  76. List<CostDepartmentProfitVO> costDepartmentProfitVOList = BeanUtil.convertList(records, CostDepartmentProfitVO.class);
  77. PageUtils pageUtils = new PageUtils(pages);
  78. pageUtils.setList(costDepartmentProfitVOList);
  79. return pageUtils;
  80. }
  81. /**
  82. * 科室损益计算
  83. *
  84. * @param date
  85. * @param hospId
  86. */
  87. @Override
  88. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  89. public void setDepartmentProfit(String date, Long hospId) {
  90. DateTime parse = DateUtil.parse(date);
  91. int year = DateUtil.year(parse);
  92. int month = DateUtil.month(parse) + 1;
  93. // 先查询指定条件的报表数据 查询损益表的数据
  94. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  95. .eq(ReportForm::getHospId, hospId)
  96. .eq(ReportForm::getReportType, NumberConstant.ZERO));
  97. if (CollUtil.isEmpty(reportFormList)) {
  98. throw new CostException(500, "损益表未找到");
  99. }
  100. // 遍历报表数据根据报表数据计算方式进行计算
  101. // 查询最后一个层级的责任中心
  102. List<CostShareLevel> costShareLevelList = costShareLevelService.list(new QueryWrapper<CostShareLevel>().lambda()
  103. .eq(CostShareLevel::getHospId, hospId).orderByDesc(CostShareLevel::getLeverSort));
  104. if (CollUtil.isEmpty(costShareLevelList)) {
  105. throw new CostException(500, "分摊层级未设置");
  106. }
  107. Long id = costShareLevelList.get(0).getId();
  108. // 查询责任中心里面是这个层级的所有的收益中心
  109. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  110. .eq(Responsibility::getHospId, hospId).eq(Responsibility::getResponsibilityType, NumberConstant.ONE).eq(Responsibility::getShareId, id));
  111. // 归集后
  112. List<IncomeCollection> incomeList = incomeCollectionService.list(new QueryWrapper<IncomeCollection>().lambda()
  113. .eq(IncomeCollection::getHospId, hospId)
  114. .eq(year > 0, IncomeCollection::getYear, year).eq(month > 0, IncomeCollection::getMonth, month));
  115. if (CollUtil.isEmpty(incomeList)) {
  116. throw new CostException(500, "归集后数据不存在");
  117. }
  118. Map<Long, List<ReportRelation>> reportRelationMap = reportRelationService.list(new QueryWrapper<ReportRelation>().lambda().eq(ReportRelation::getHospId, hospId)).stream().collect(Collectors.groupingBy(ReportRelation::getReportId));
  119. // 分摊后的数据getOriginType等于2说明是分摊后的数据
  120. List<AllocationQuery> allocationQueryList = allocationQueryService.list(new QueryWrapper<AllocationQuery>().lambda().eq(AllocationQuery::getHospId, hospId)
  121. .eq(year > 0, AllocationQuery::getDateYear, year)
  122. .eq(month > 0, AllocationQuery::getDateMonth, month)
  123. .eq(AllocationQuery::getOriginType, NumberConstant.TWO));
  124. if (CollUtil.isEmpty(allocationQueryList)) {
  125. throw new CostException(500, "分摊后数据不存在");
  126. }
  127. // 封装数据
  128. List<AllocationQueryReportVO> allocationQueryReportVOList = BeanUtil.convertList(allocationQueryList, AllocationQueryReportVO.class);
  129. allocationQueryReportVOList.forEach(i -> {
  130. i.setAccountingCodes(Arrays.asList(i.getAccountingCode().split(StrUtil.COMMA)));
  131. i.setAccountingNames(Arrays.asList(i.getAccountingName().split(StrUtil.COMMA)));
  132. });
  133. // 查询分摊的报表数据 后面的计算方式需要使用 小计等需要使用
  134. List<Allocation> allocationList = allocationService.list(new QueryWrapper<Allocation>().lambda().eq(Allocation::getHospId, hospId)
  135. .eq(year > 0, Allocation::getDateYear, year).eq(month > 0, Allocation::getDateMonth, month));
  136. if (CollUtil.isEmpty(allocationList)) {
  137. throw new CostException(500, "分摊报表数据不存在");
  138. }
  139. // 查询所有指定类型的损益表
  140. // 每个责任中心对应报表都要生成记录
  141. // 封装需要设置的数据
  142. List<CostDepartmentProfitVO> list = new ArrayList<>();
  143. int finalYear = year;
  144. int finalMonth = month;
  145. responsibilityList.forEach(i -> {
  146. reportFormList.forEach(j -> {
  147. CostDepartmentProfitVO costDepartmentProfitVO = new CostDepartmentProfitVO();
  148. costDepartmentProfitVO.setYear(finalYear);
  149. costDepartmentProfitVO.setMonth(finalMonth);
  150. costDepartmentProfitVO.setReportId(j.getId());
  151. costDepartmentProfitVO.setReportNum(j.getNum());
  152. costDepartmentProfitVO.setCalcType(j.getCalcType());
  153. costDepartmentProfitVO.setReportName(j.getReportName());
  154. costDepartmentProfitVO.setCalcFormula(j.getCalcFormula());
  155. costDepartmentProfitVO.setReportParentId(j.getParentId());
  156. costDepartmentProfitVO.setResponsibilityCode(i.getResponsibilityCode());
  157. costDepartmentProfitVO.setResponsibilityName(i.getResponsibilityName());
  158. costDepartmentProfitVO.setCostType(NumberConstant.ONE);
  159. costDepartmentProfitVO.setIncomeType(NumberConstant.ONE);
  160. costDepartmentProfitVO.setHospId(hospId);
  161. list.add(costDepartmentProfitVO);
  162. });
  163. });
  164. Map<Long, List<CostDepartmentProfitVO>> listMap = list.stream().collect(Collectors.groupingBy(CostDepartmentProfitVO::getReportId));
  165. // 记录每一次计算的钱
  166. list.forEach(i -> {
  167. Integer calcType = i.getCalcType();
  168. if (NumberConstant.ONE.equals(calcType)){
  169. // TODO 按照会计科目进行计算
  170. i.setAmount(setAccountReportData(i, incomeList, allocationQueryReportVOList, reportRelationMap));
  171. }else if (NumberConstant.TWO.equals(calcType)){
  172. // TODO 按照分摊层级进行计算
  173. i.setAmount(setShareLevelReportData(i, costShareLevelList, reportRelationMap, allocationList));
  174. }else if (NumberConstant.THREE.equals(calcType)){
  175. // TODO 按照小计进行计算
  176. i.setAmount(setSubtotal(i, costShareLevelList, listMap, list, incomeList, allocationQueryReportVOList, reportRelationMap, allocationList));
  177. }else if (NumberConstant.FOUR.equals(calcType)){
  178. // TODO 按照计算公式进行计算
  179. i.setAmount(setCalculation(i, list, costShareLevelList, listMap, incomeList, allocationQueryReportVOList, reportRelationMap, allocationList));
  180. }else if (NumberConstant.FIVE.equals(calcType)){
  181. // TODO 按照责任中心进行计算
  182. i.setAmount(setResponsibilityCode(i, reportRelationMap, allocationList));
  183. }else {
  184. i.setAmount(new BigDecimal("0.0000"));
  185. }
  186. // switch (calcType) {
  187. // case 1:
  188. //
  189. // break;
  190. // case 2:
  191. //
  192. // break;
  193. // case 3:
  194. //
  195. // break;
  196. // case 4:
  197. //
  198. // break;
  199. // case 5:
  200. //
  201. // break;
  202. // default:
  203. // break;
  204. // }
  205. });
  206. // 删除这个年月的数据
  207. this.remove(new QueryWrapper<CostDepartmentProfit>().lambda().eq(CostDepartmentProfit::getHospId, hospId)
  208. .eq(CostDepartmentProfit::getYear, year).eq(CostDepartmentProfit::getMonth, month));
  209. // 添加数据
  210. List<CostDepartmentProfit> costDepartmentProfits = BeanUtil.convertList(list, CostDepartmentProfit.class);
  211. long l = System.currentTimeMillis();
  212. costDepartmentProfits.forEach(i -> {
  213. i.setCreateTime(l);
  214. });
  215. this.saveBatch(costDepartmentProfits);
  216. }
  217. /**
  218. * 科室损益计算导出
  219. *
  220. * @param writer
  221. * @param sheet
  222. * @param date
  223. */
  224. @Override
  225. public void getDepartmentProfit(ExcelWriter writer, Sheet sheet, String date) {
  226. int year = 0;
  227. int month = 0;
  228. if (StrUtil.isNotBlank(date)) {
  229. Date dateTime = DateUtils.StringToDate(date, DateStyleEnum.YYYY_MM_DD);
  230. year = DateUtil.year(dateTime);
  231. month = DateUtil.month(dateTime) + 1;
  232. }
  233. Long hospId = UserContext.getHospId();
  234. List<CostDepartmentProfit> costDepartmentProfitList = this.list(new QueryWrapper<CostDepartmentProfit>().lambda().eq(CostDepartmentProfit::getHospId, hospId).eq(StrUtil.isNotBlank(date), CostDepartmentProfit::getYear, year).eq(StrUtil.isNotBlank(date), CostDepartmentProfit::getMonth, month));
  235. // 16个责任中心名称
  236. List<String> responsibilityNameList = costDepartmentProfitList.stream().map(CostDepartmentProfit::getResponsibilityName).distinct().sorted().collect(Collectors.toList());
  237. Map<String, List<CostDepartmentProfit>> responsibilityNameMap = costDepartmentProfitList.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getResponsibilityName));
  238. for (int i = 0; i < responsibilityNameList.size(); i++) {
  239. String responsibilityName = responsibilityNameList.get(i);
  240. List<CostDepartmentProfit> departmentProfits = responsibilityNameMap.get(responsibilityName);
  241. writer.merge(0, 2, 0, departmentProfits.size() + 2, responsibilityName, false);
  242. }
  243. System.out.println(responsibilityNameList);
  244. }
  245. /**
  246. * 按照会计科目进行计算
  247. *
  248. * @param i
  249. */
  250. private BigDecimal setAccountReportData(CostDepartmentProfitVO i, List<IncomeCollection> list, List<AllocationQueryReportVO> allocationQueryReportVOList, Map<Long, List<ReportRelation>> reportRelationMap) {
  251. // 在报表关联里面查询当前报表关联的
  252. Long reportId = i.getReportId();
  253. List<ReportRelation> reportRelationList = reportRelationMap.get(i.getReportId());
  254. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.000"));
  255. if (CollUtil.isNotEmpty(reportRelationList)) {
  256. // 获取对应的会计科目信息 筛选会计科目的Code
  257. List<String> accountList = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  258. // 查找在归集数据里面当前责任中心对应的这些会计科目的金额
  259. List<IncomeCollection> incomeCollectionList = list.stream().filter(income -> income.getResponsibilityCode().equals(i.getResponsibilityCode()) && accountList.contains(income.getAccountingCode())).collect(Collectors.toList());
  260. // 需要查询分摊后的表
  261. // 归集加
  262. List<AllocationQueryReportVO> reportVOList = allocationQueryReportVOList.stream().filter(m -> m.getResponsibilityCode().equals(i.getResponsibilityCode()) && !Collections.disjoint(accountList, m.getAccountingCodes())).collect(Collectors.toList());
  263. if (CollUtil.isNotEmpty(incomeCollectionList)) {
  264. incomeCollectionList.forEach(m -> {
  265. sum.updateAndGet(v -> v.add(m.getAmount()));
  266. });
  267. }
  268. // 成本减
  269. if (CollUtil.isNotEmpty(reportVOList)) {
  270. reportVOList.forEach(m -> {
  271. sum.updateAndGet(v -> v.add(m.getAmount()));
  272. });
  273. }
  274. }
  275. i.setAmount(new BigDecimal(sum.toString()));
  276. // numMap.put(i.getReportNum()+i.getResponsibilityCode(),new BigDecimal(sum.toString()));
  277. return sum.get();
  278. }
  279. /**
  280. * 按照分摊层级进行计算
  281. * 按照分摊层级计算 报表分摊层级是当前的层级 并且目标责任中心失败当前责任中心
  282. */
  283. private BigDecimal setShareLevelReportData(CostDepartmentProfitVO i, List<CostShareLevel> costShareLevelList, Map<Long, List<ReportRelation>> reportRelationMap, List<Allocation> allocationList) {
  284. List<ReportRelation> reportRelationList = reportRelationMap.get(i.getReportId());
  285. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.000"));
  286. if (CollUtil.isNotEmpty(reportRelationList)) {
  287. // 找到对应的分摊层级的Id 但是分摊报表里面存的是分摊层级的序号
  288. List<Long> shareLevelIds = reportRelationList.stream().map(ReportRelation::getRelationCode).map(Long::valueOf).collect(Collectors.toList());
  289. List<Integer> levelShortList = costShareLevelList.stream().filter(m -> shareLevelIds.contains(m.getId())).map(CostShareLevel::getLeverSort).collect(Collectors.toList());
  290. if (CollUtil.isNotEmpty(levelShortList)) {
  291. // 查询报表里面是当前分摊层级的数据
  292. List<Allocation> allocations = allocationList.stream().filter(m -> levelShortList.contains(m.getLevelSort()) && m.getTargetResponsibilityCode().equals(i.getResponsibilityCode())).collect(Collectors.toList());
  293. if (CollUtil.isNotEmpty(allocations)) {
  294. allocations.forEach(m -> {
  295. sum.updateAndGet(v -> v.add(m.getAmount()));
  296. });
  297. }
  298. }
  299. }
  300. i.setAmount(new BigDecimal(sum.toString()));
  301. return sum.get();
  302. }
  303. /**
  304. * 按照责任中心进行计算
  305. * 原始责任中心是设置的责任中心 目标责任中心是报表的责任中心
  306. * 查询分摊报表里面目标责任中心是当前责任中心 报表责任中心是当前设置的责任中心
  307. */
  308. public BigDecimal setResponsibilityCode(CostDepartmentProfitVO costDepartmentProfitVO, Map<Long, List<ReportRelation>> reportRelationMap, List<Allocation> allocationList) {
  309. // 获取当前报表对应的责任中心
  310. List<ReportRelation> reportRelationList = reportRelationMap.get(costDepartmentProfitVO.getReportId());
  311. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.000"));
  312. if (CollUtil.isNotEmpty(reportRelationList)) {
  313. // 获取对应的责任中心的Code集合 这个是设置的责任中心
  314. List<String> responsibilityCodes = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  315. if (CollUtil.isNotEmpty(responsibilityCodes)) {
  316. // 查询报表里面是当前分摊层级的数据
  317. List<Allocation> allocations = allocationList.stream().filter(i -> i.getTargetResponsibilityCode().equals(costDepartmentProfitVO.getResponsibilityCode()) && responsibilityCodes.contains(i.getResponsibilityCode())).collect(Collectors.toList());
  318. if (CollUtil.isNotEmpty(allocations)) {
  319. allocations.forEach(m -> {
  320. sum.updateAndGet(v -> v.add(m.getAmount()));
  321. });
  322. }
  323. }
  324. }
  325. costDepartmentProfitVO.setAmount(new BigDecimal(sum.toString()));
  326. return sum.get();
  327. }
  328. /**
  329. * 按照小计的计算方式
  330. * 同一个目录下相同的其他金额的和
  331. */
  332. public BigDecimal setSubtotal(CostDepartmentProfitVO costDepartmentProfitVO,
  333. List<CostShareLevel> costShareLevelList,
  334. Map<Long, List<CostDepartmentProfitVO>> listMap,
  335. List<CostDepartmentProfitVO> profitVOS,
  336. List<IncomeCollection> list,
  337. List<AllocationQueryReportVO> allocationQueryReportVOList,
  338. Map<Long, List<ReportRelation>> reportRelationMap, List<Allocation> allocationList) {
  339. // 因为报表是按照报表项目的升序进行排序的 前面的报表是已经进行计算了的
  340. // 查询当前报表的父层级id
  341. Long reportParentId = costDepartmentProfitVO.getReportParentId();
  342. String responsibilityCode = costDepartmentProfitVO.getResponsibilityCode();
  343. // 查询报表里面在当前父层级下的并且不是自己的报表项目
  344. List<CostDepartmentProfitVO> costDepartmentProfitVOS = profitVOS.stream().filter(i -> i.getReportParentId().equals(reportParentId) && i.getResponsibilityCode().equals(responsibilityCode) && !NumberConstant.THREE.equals(i.getCostType())).collect(Collectors.toList());
  345. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  346. // 遍历数据 之前的已经经过了计算
  347. costDepartmentProfitVOS.forEach(i -> {
  348. Long reportId = i.getReportId();
  349. List<CostDepartmentProfitVO> costDepartmentProfitVOS1 = listMap.get(reportId);
  350. if (CollUtil.isEmpty(costDepartmentProfitVOS1)) {
  351. throw new CostException(500, "报表未找到");
  352. }
  353. BigDecimal amount = getAmount(profitVOS, costShareLevelList, responsibilityCode, list, allocationQueryReportVOList, reportRelationMap, allocationList, listMap);
  354. sum.updateAndGet(v -> v.add(amount));
  355. });
  356. costDepartmentProfitVO.setAmount(new BigDecimal(sum.toString()));
  357. return sum.get();
  358. }
  359. /***
  360. * 按照计算方式进行计算
  361. */
  362. public BigDecimal setCalculation(CostDepartmentProfitVO costDepartmentProfitVO, List<CostDepartmentProfitVO> costDepartmentProfitVOList, List<CostShareLevel> costShareLevelList, Map<Long, List<CostDepartmentProfitVO>> listMap, List<IncomeCollection> list, List<AllocationQueryReportVO> allocationQueryReportVOList, Map<Long, List<ReportRelation>> reportRelationMap, List<Allocation> allocationList) {
  363. // 获取当前报表的计算方式 [1]+[2]类型/ [1]-[2]
  364. String calcFormula = costDepartmentProfitVO.getCalcFormula();
  365. String responsibilityCode = costDepartmentProfitVO.getResponsibilityCode();
  366. String replace = calcFormula.replace("[", "").replace("]", "").replace("+", ",").replace("-", ",-");
  367. List<Integer> calcFormulaList = Arrays.stream(replace.split(StrUtil.COMMA)).map(Integer::valueOf).collect(Collectors.toList());
  368. // 查询这个编号集合的报表
  369. AtomicReference<BigDecimal> bigDecimal = new AtomicReference<>(new BigDecimal("0.0000"));
  370. calcFormulaList.forEach(calc -> {
  371. Integer calcNum=Math.abs(calc);
  372. List<Long> reportIdList = costDepartmentProfitVOList.stream().filter(i -> i.getResponsibilityCode().equals(responsibilityCode) && calcNum.equals(i.getReportNum())).map(CostDepartmentProfitVO::getReportId).collect(Collectors.toList());
  373. if (CollUtil.isNotEmpty(reportIdList)) {
  374. reportIdList.forEach(i -> {
  375. List<CostDepartmentProfitVO> profitVOS = listMap.get(i);
  376. if (CollUtil.isEmpty(profitVOS)) {
  377. throw new CostException(500, "报表未找到");
  378. }
  379. BigDecimal amount = getAmount(profitVOS, costShareLevelList, responsibilityCode, list, allocationQueryReportVOList, reportRelationMap, allocationList, listMap);
  380. if (calc>0){
  381. bigDecimal.updateAndGet(v -> v.add(amount));
  382. }else {
  383. bigDecimal.updateAndGet(v -> v.subtract(amount));
  384. }
  385. });
  386. }
  387. });
  388. return bigDecimal.get();
  389. }
  390. /**
  391. * 判断是那种计算方式 调用方法进行计算
  392. */
  393. public BigDecimal getAmount(List<CostDepartmentProfitVO> profitVOS,
  394. List<CostShareLevel> costShareLevelList,
  395. String responsibilityCode, List<IncomeCollection> list,
  396. List<AllocationQueryReportVO> allocationQueryReportVOList,
  397. Map<Long, List<ReportRelation>> reportRelationMap, List<Allocation> allocationList,
  398. Map<Long, List<CostDepartmentProfitVO>> listMap) {
  399. BigDecimal bigDecimal = new BigDecimal("0.0000");
  400. // 在对这个报表进行过滤
  401. List<CostDepartmentProfitVO> costDepartmentProfitVOS = profitVOS.stream().filter(i -> i.getResponsibilityCode().equals(responsibilityCode)).collect(Collectors.toList());
  402. // 都一个就是
  403. CostDepartmentProfitVO costDepartmentProfitVO = costDepartmentProfitVOS.get(0);
  404. Integer costType = costDepartmentProfitVO.getCostType();
  405. if (NumberConstant.ONE.equals(costType)) {
  406. // 调用计算的方法 按照会计科目
  407. bigDecimal = setAccountReportData(costDepartmentProfitVO, list, allocationQueryReportVOList, reportRelationMap);
  408. } else if (NumberConstant.TWO.equals(costType)) {
  409. // 按照分摊层级
  410. bigDecimal = setShareLevelReportData(costDepartmentProfitVO, costShareLevelList, reportRelationMap, allocationList);
  411. } else if (NumberConstant.THREE.equals(costType)) {
  412. // 小计
  413. bigDecimal = setSubtotal(costDepartmentProfitVO, costShareLevelList, listMap, profitVOS, list, allocationQueryReportVOList, reportRelationMap, allocationList);
  414. } else if (NumberConstant.FIVE.equals(costType)) {
  415. // 按照责任中心
  416. bigDecimal = setResponsibilityCode(costDepartmentProfitVO, reportRelationMap, allocationList);
  417. }
  418. return bigDecimal;
  419. }
  420. }