AllocationServiceImpl.java 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. package com.kcim.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.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  10. import com.kcim.common.constants.NumberConstant;
  11. import com.kcim.common.constants.SQLParameter;
  12. import com.kcim.common.enums.CustomSqlTypeEnum;
  13. import com.kcim.common.exception.CostException;
  14. import com.kcim.common.util.BeanUtil;
  15. import com.kcim.common.util.JacksonUtil;
  16. import com.kcim.common.util.PageUtils;
  17. import com.kcim.common.util.UserContext;
  18. import com.kcim.dao.mapper.AllocationMapper;
  19. import com.kcim.dao.model.*;
  20. import com.kcim.dao.model.dto.StartDTO;
  21. import com.kcim.dao.repository.ResponsibilityRepository;
  22. import com.kcim.service.*;
  23. import com.kcim.vo.*;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.apache.poi.ss.usermodel.Sheet;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.transaction.annotation.Propagation;
  28. import org.springframework.transaction.annotation.Transactional;
  29. import org.springframework.util.CollectionUtils;
  30. import java.math.BigDecimal;
  31. import java.math.RoundingMode;
  32. import java.util.*;
  33. import java.util.concurrent.atomic.AtomicReference;
  34. import java.util.stream.Collectors;
  35. @Slf4j
  36. @Service("allocationService")
  37. public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocation> implements AllocationService {
  38. // @Value("${file.serverPath}")
  39. // private String fileTempPath;
  40. //
  41. // @Value("${file.serverUrl}")
  42. // private String FILE_PATH;
  43. private final CostCostingGroupService costCostingGroupService;
  44. private final CostShareLevelService shareLevelService;
  45. private final ResponsibilityService responsibilityService;
  46. private final CostAccountShareService accountShareService;
  47. private final ShareParamValueService shareParamValueService;
  48. private final CostShareParamService shareParamService;
  49. private final AllocationQueryService allocationQueryService;
  50. private final ResponsibilityRepository responsibilityRepository;
  51. private final CenterService centerService;
  52. private final SqlService sqlService;
  53. public AllocationServiceImpl(CostCostingGroupService costCostingGroupService, CostShareLevelService shareLevelService, ResponsibilityService responsibilityService, CostAccountShareService accountShareService, ShareParamValueService shareParamValueService, CostShareParamService shareParamService, AllocationQueryService allocationQueryService, ResponsibilityRepository responsibilityRepository, CenterService centerService, SqlService sqlService) {
  54. this.costCostingGroupService = costCostingGroupService;
  55. this.shareLevelService = shareLevelService;
  56. this.responsibilityService = responsibilityService;
  57. this.accountShareService = accountShareService;
  58. this.shareParamValueService = shareParamValueService;
  59. this.shareParamService = shareParamService;
  60. this.allocationQueryService = allocationQueryService;
  61. this.responsibilityRepository = responsibilityRepository;
  62. this.centerService = centerService;
  63. this.sqlService = sqlService;
  64. }
  65. /**
  66. * 分摊成本数据
  67. *
  68. * @param startDTO {@link StartDTO}
  69. * @param hospId 医院id
  70. */
  71. @Override
  72. @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
  73. public void startAllocation(StartDTO startDTO, Long hospId) {
  74. long timeMillis = System.currentTimeMillis();
  75. // 得到这个月的所有导入的成本数据
  76. List<CostCostingGroup> costingGroups = costCostingGroupService.getByYearAndDate(startDTO.getYear(), startDTO.getMonth(), hospId);
  77. // 没有重新导入
  78. if (costingGroups.isEmpty()) {
  79. throw new CostException("本月分摊数据未导入");
  80. }
  81. // 导入数据按责任中心归类
  82. Map<String, List<CostCostingGroup>> responsibilityCodeMap = costingGroups.stream().collect(Collectors.groupingBy(CostCostingGroup::getResponsibilityCode));
  83. // 得到这个月导入的成本分摊参数值列表
  84. List<ShareParamValue> shareParamValues = shareParamValueService.getListByYearAndMonth(startDTO.getYear(), startDTO.getMonth(), hospId);
  85. if (shareParamValues.isEmpty()) {
  86. throw new CostException("本月成本分摊参数值未导入");
  87. }
  88. Map<String, List<ShareParamValue>> paramValueRespCodeMap = shareParamValues.stream().collect(Collectors.groupingBy(ShareParamValue::getResponsibilityCode));
  89. // 删除该年月已经分摊过的数据
  90. removeData(startDTO, hospId);
  91. // 得到这个医院所有的分摊层级列表排序
  92. List<CostShareLevelVO> shareLevelVOs = shareLevelService.getAll(hospId);
  93. if (CollUtil.isEmpty(shareLevelVOs)) {
  94. throw new CostException("请先设置医院分摊层级");
  95. }
  96. //优化循环调用 责任中心 分摊层级对应
  97. //成本分摊字典
  98. List<CostShareParamVO> costShareParamList = shareParamService.getAll(hospId);
  99. if (CollUtil.isEmpty(costShareParamList)) {
  100. throw new CostException("请先设置分摊参数字典");
  101. }
  102. Map<String,String> ShareParamNameMap = costShareParamList.stream().collect(Collectors.toMap(CostShareParamVO::getShareParamCode, CostShareParamVO::getShareParamName, (a, b) -> b));
  103. //责任中心
  104. List<Responsibility> responsibilityList = responsibilityRepository.getList(hospId);
  105. if (CollUtil.isEmpty(responsibilityList)) {
  106. throw new CostException("请先设置责任中心");
  107. }
  108. //按shareId分组
  109. Map<Long, List<Responsibility>> responsibilityShareIdMap = responsibilityList.stream().collect(Collectors.groupingBy(Responsibility::getShareId));
  110. Map<String,String> responsibilityNameMap = responsibilityList.stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, Responsibility::getResponsibilityName, (a, b) -> b));
  111. Map<String,Long> responsibilityShareIdDictMap = responsibilityList.stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, Responsibility::getShareId, (a, b) -> b));
  112. //分摊参数对应
  113. List<CostAccountShare> accountShareList = accountShareService.getAll();
  114. if (CollUtil.isEmpty(accountShareList)) {
  115. throw new CostException("请先设置责任中心分摊参数对应");
  116. }
  117. Map<String, List<CostAccountShare>> accountShareResponsibilityMap = accountShareList.stream().collect(Collectors.groupingBy(CostAccountShare::getResponsibilityCode));
  118. List<Allocation> allocations = new LinkedList<>();
  119. // key 责任中心代码, value 分摊过来的钱
  120. // Map<String, BigDecimal> costMap = new ConcurrentReaderHashMap();
  121. List<Allocation> costList = new ArrayList<>();
  122. for (CostShareLevelVO shareLevelVO : shareLevelVOs) {
  123. // 分摊层级id
  124. Long levelId = shareLevelVO.getId();
  125. // 目标分摊层级,可能不存在
  126. String targetLevel = shareLevelVO.getTargetLevel();
  127. if (StrUtil.isBlank(targetLevel)) {
  128. throw new CostException("未设置目标层级");
  129. }
  130. // 计算方式 0是合并计算 1是分开计算
  131. Integer calcType = shareLevelVO.getCalcType();
  132. // 得到该分摊层级下责任中心列表,如果不存在,下一个
  133. List<Responsibility> responsibilities = responsibilityShareIdMap.get(levelId);
  134. // List<Responsibility> responsibilities = responsibilityService.getLevelIdByCode(levelId, hospId);
  135. // if (responsibilities.isEmpty()) {
  136. // continue;
  137. // }
  138. if(CollectionUtils.isEmpty(responsibilities)){
  139. continue;
  140. }
  141. // 优化
  142. // 遍历责任中心得到对应的分摊参数对应
  143. for (Responsibility responsibility : responsibilities) {
  144. // String responsibilityCode = responsibility.getResponsibilityCode();
  145. // 得到分摊参数对应记录,不存在,下一个
  146. List<CostAccountShare> accountShares = accountShareResponsibilityMap.get(responsibility.getResponsibilityCode());
  147. // List<CostAccountShare> accountShares = accountShareService.getByResponsibility(responsibilityCode, hospId);
  148. if (CollectionUtils.isEmpty(accountShares)) {
  149. continue;
  150. }
  151. // 遍历分摊参数对应记录
  152. for (CostAccountShare accountShare : accountShares) {
  153. Long accountShareId = accountShare.getId();
  154. String paramList = accountShare.getParamList();
  155. // 如果分摊比例未设置直接报错
  156. if (StrUtil.isBlank(paramList)) {
  157. throw new CostException("责任中心:" + accountShare.getResponsibilityName() + ";会计科目为:" + accountShare.getAccountingNames() + ";未设置分摊参数比例");
  158. }
  159. List<AccountShareVO> accountShareVOs = JacksonUtil.str2ObjList(paramList, List.class, AccountShareVO.class);
  160. // List<CostCostingGroup> groups = responsibilityCodeMap.get(responsibilityCode);
  161. // if (CollUtil.isEmpty(groups)) {
  162. // continue;
  163. // }
  164. // 计算本次分摊的钱
  165. BigDecimal totalAmount = this.getCostAmount(accountShare, calcType, responsibilityCodeMap, costList);
  166. if (totalAmount.equals(BigDecimal.ZERO)) {
  167. continue;
  168. }
  169. // 相关的分摊参数比例
  170. if (!CollectionUtils.isEmpty(accountShareVOs)) {
  171. for (AccountShareVO accountShareVO : accountShareVOs) {
  172. // String paramCode = accountShareVO.getShareParamCode();
  173. String shareParamPopout = accountShareVO.getShareParamPopout();
  174. BigDecimal bigDecimal = new BigDecimal(shareParamPopout);
  175. BigDecimal rate = bigDecimal.divide(new BigDecimal(100), 4, RoundingMode.HALF_UP);
  176. // BigDecimal rate = new BigDecimal("1");
  177. // if (!"100".equals(shareParamPopout)) {
  178. // rate = new BigDecimal("0." + shareParamPopout);
  179. // }
  180. // 本次的分摊比例计算
  181. BigDecimal thisAmount = rate.multiply(totalAmount);
  182. // 得到目标层级责任中心列表
  183. List<Responsibility> targetResponsibilities = this.getTargetResponsibility(targetLevel, hospId, shareLevelVO.getLeverSort());
  184. if (targetResponsibilities.isEmpty()) {
  185. throw new CostException("找不到目标责任中心");
  186. }
  187. // 目标责任中心得到对应
  188. List<ShareParamValue> targetShareParamValue = getTarget(targetResponsibilities, accountShareVO, paramValueRespCodeMap);
  189. if (CollUtil.isEmpty(targetShareParamValue)) {
  190. throw new CostException("找不到目标责任中心对应的分摊参数【"+accountShareVO.getShareParamCode()+"】值");
  191. }
  192. // 分母
  193. BigDecimal reduce = targetShareParamValue.stream().map(ShareParamValue::getValueNum).reduce(BigDecimal.ZERO, BigDecimal::add);
  194. for (ShareParamValue paramValue : targetShareParamValue) {
  195. // 分子
  196. BigDecimal numerator = paramValue.getValueNum();
  197. BigDecimal targetAmount = reduce.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : thisAmount.multiply(numerator).divide(reduce, RoundingMode.HALF_UP);
  198. BigDecimal percent = reduce.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : numerator.divide(reduce, RoundingMode.HALF_UP);
  199. Allocation targetAllocation = new Allocation();
  200. String valueResponsibilityCode = paramValue.getResponsibilityCode();
  201. //优化循环调用
  202. String targetRespName = responsibilityNameMap.get(valueResponsibilityCode);
  203. // String targetRespName = responsibilityService.getByCode(valueResponsibilityCode, hospId);
  204. Long targetShareLevelId = responsibilityShareIdDictMap.get(valueResponsibilityCode);
  205. // Long targetShareLevelId = responsibilityService.getLevelIdByCode(valueResponsibilityCode, hospId);
  206. if (Objects.isNull(targetShareLevelId)) {
  207. throw new CostException("目标责任中心分摊层级异常");
  208. }
  209. String shareParamName = ShareParamNameMap.get(paramValue.getShareParamCode());
  210. // String shareParamName = shareParamService.getByCode(paramValue.getShareParamCode(), hospId);
  211. targetAllocation.setDateMonth(startDTO.getMonth()).setDateYear(startDTO.getYear()).setLevelSort(shareLevelVO.getLeverSort())
  212. .setLevelName(shareLevelVO.getShareName()).setHospId(hospId).setResponsibilityCode(responsibility.getResponsibilityCode())
  213. .setResponsibilityName(responsibility.getResponsibilityName()).setAccountShareId(accountShareId).setAmount(targetAmount)
  214. .setCreateTime(timeMillis).setTargetResponsibilityCode(valueResponsibilityCode).setTargetResponsibilityName(targetRespName)
  215. .setShareParamCode(paramValue.getShareParamCode()).setShareParamName(shareParamName).setTotalAmount(totalAmount).setShareParamValueNum(paramValue.getValueNum())
  216. .setShareParamRate(percent).setShareLevelId(levelId).setTargetShareLevelId(targetShareLevelId);
  217. // todo 目标分摊层级责任中心 就是当前列个表中的责任中心
  218. allocations.add(targetAllocation);
  219. costList.add(targetAllocation);
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. this.saveBatch(allocations);
  227. List<Allocation> list = this.list(
  228. new LambdaQueryWrapper<Allocation>()
  229. .eq(Allocation::getHospId, hospId).eq(Allocation::getDateYear, startDTO.getYear()).eq(Allocation::getDateMonth, startDTO.getMonth())
  230. );
  231. if (list.isEmpty()) {
  232. log.error("未分摊到数据......");
  233. return;
  234. }
  235. // 入cost_allocation_query 表 便于后续操作
  236. this.saveAllocationQuery(list, hospId, startDTO.getYear(), startDTO.getMonth(),shareLevelVOs,accountShareList);
  237. Map<String,String> sqlParameter = new HashMap<>();
  238. if(startDTO.getMonth()<10){
  239. sqlParameter.put(SQLParameter.COMPUTE_DATE_CODE,startDTO.getYear()+"-0"+startDTO.getMonth());
  240. }else{
  241. sqlParameter.put(SQLParameter.COMPUTE_DATE_CODE,startDTO.getYear()+"-"+startDTO.getMonth());
  242. }
  243. sqlService.autoExecuteSql(CustomSqlTypeEnum.COST_ALLOCATION_CALC.getCode(),sqlParameter);
  244. }
  245. private void removeData(StartDTO startDTO, Long hospId) {
  246. this.remove(
  247. new LambdaQueryWrapper<Allocation>()
  248. .eq(Allocation::getDateYear, startDTO.getYear())
  249. .eq(Allocation::getDateMonth, startDTO.getMonth())
  250. .eq(Allocation::getHospId, hospId)
  251. );
  252. allocationQueryService.remove(
  253. new LambdaQueryWrapper<AllocationQuery>()
  254. .eq(AllocationQuery::getDateYear, startDTO.getYear())
  255. .eq(AllocationQuery::getDateMonth, startDTO.getMonth())
  256. .eq(AllocationQuery::getHospId, hospId)
  257. );
  258. }
  259. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  260. public void saveAllocationQuery(List<Allocation> list, Long hospId,
  261. Integer year, Integer month, List<CostShareLevelVO> shareLevelVOs, List<CostAccountShare> accountShareList) {
  262. List<AllocationQuery> saveList = new ArrayList<>();
  263. List<Long> maxId = shareLevelService.getMaxId(hospId);
  264. // List<Allocation> maxList = list.stream().filter(i -> maxId.contains(i.getTargetShareLevelId())).collect(Collectors.toList());
  265. Map<Long, List<Allocation>> collect = list.stream().collect(Collectors.groupingBy(Allocation::getTargetShareLevelId));
  266. //优化 去除循环调用
  267. // List<CostAccountShare> all = accountShareService.getAll();
  268. Map<Long,CostAccountShare> accountShareMap = accountShareList.stream().collect(Collectors.toMap(CostAccountShare::getId, costAccountShare -> costAccountShare, (a, b) -> b));
  269. // 得到这个月的所有导入的成本数据用责任中心关联取得shareId 优化 去除循环调用
  270. List<CostCostingGroup> costingGroups = costCostingGroupService.getRealData(CollUtil.newArrayList(), hospId, year, month);
  271. Map<Long, List<CostCostingGroup>> costShareIdGroup = costingGroups.stream().collect(Collectors.groupingBy(CostCostingGroup::getShareId));
  272. for (CostShareLevelVO shareLevelVO : shareLevelVOs) {
  273. List<Allocation> allocations = collect.get(shareLevelVO.getId());
  274. if(!CollectionUtils.isEmpty(allocations)){
  275. setAfterAllocation(allocations, hospId, saveList,accountShareMap);
  276. }
  277. if(!CollectionUtils.isEmpty(costShareIdGroup.get(shareLevelVO.getId()))){
  278. setPreAllocation(hospId,saveList,shareLevelVO,costShareIdGroup.get(shareLevelVO.getId()));
  279. }
  280. }
  281. allocationQueryService.saveBatch(saveList);
  282. }
  283. private void setPreAllocation(Long hospId, List<AllocationQuery> saveList,
  284. CostShareLevelVO shareLevelVO, List<CostCostingGroup> costingGroups) {
  285. // List<CostCostingGroup> costingGroups = costCostingGroupService.getRealData(CollUtil.newArrayList(maxId), hospId, year, month);
  286. costingGroups.forEach(i -> {
  287. AllocationQuery allocationQuery = new AllocationQuery();
  288. allocationQuery.setDateYear(i.getDateYear()).setDateMonth(i.getDateMonth())
  289. .setHospId(hospId).setResponsibilityCode(i.getResponsibilityCode()).setResponsibilityName(i.getResponsibilityName())
  290. .setOriginId(i.getId()).setOriginType(1L).setAmount(i.getAmount())
  291. .setAccountingCode(i.getAccountCode()).setAccountingName(i.getAccountName())
  292. .setCreateTime(System.currentTimeMillis())
  293. .setLevelSort(shareLevelVO.getLeverSort()).setLevelName(shareLevelVO.getShareName())
  294. .setTargetResponsibilityCode(i.getResponsibilityCode())
  295. .setTargetResponsibilityName(i.getResponsibilityName())
  296. .setShareLevelId(0L);
  297. saveList.add(allocationQuery);
  298. });
  299. }
  300. private void setAfterAllocation(List<Allocation> list, Long hospId, List<AllocationQuery> saveList, Map<Long,
  301. CostAccountShare> accountShareMap) {
  302. list.forEach(i -> {
  303. Long accountShareId = i.getAccountShareId();
  304. CostAccountShare byId = accountShareMap.get(accountShareId);
  305. if (Objects.isNull(byId)) {
  306. return;
  307. }
  308. String accountingCodes = byId.getAccountingCodes();
  309. if (StrUtil.isBlank(accountingCodes)) {
  310. return;
  311. }
  312. String accountingNames = byId.getAccountingNames();
  313. String alias = byId.getAlias();
  314. if (StrUtil.isNotBlank(alias)) {
  315. accountingNames = alias;
  316. }
  317. AllocationQuery allocationQuery = new AllocationQuery();
  318. allocationQuery.setDateYear(i.getDateYear()).setDateMonth(i.getDateMonth())
  319. .setHospId(hospId).setResponsibilityCode(i.getResponsibilityCode())
  320. .setResponsibilityName(i.getResponsibilityName())
  321. .setOriginId(i.getId()).setOriginType(2L).setAmount(i.getAmount())
  322. .setAccountingCode(accountingCodes).setAccountingName(accountingNames)
  323. .setCreateTime(System.currentTimeMillis())
  324. .setLevelSort(i.getLevelSort()).setLevelName(i.getLevelName())
  325. .setTargetResponsibilityCode(i.getTargetResponsibilityCode())
  326. .setTargetResponsibilityName(i.getTargetResponsibilityName())
  327. .setShareLevelId(i.getShareLevelId());
  328. saveList.add(allocationQuery);
  329. });
  330. }
  331. /**
  332. * 得到目标月成本分摊参数值数据
  333. *
  334. * @param targetResponsibilities
  335. * @param map
  336. * @return
  337. */
  338. private List<ShareParamValue> getTarget(List<Responsibility> targetResponsibilities, AccountShareVO accountShareVO, Map<String, List<ShareParamValue>> map) {
  339. // 目标的责任中心
  340. List<ShareParamValue> shareParamValues = map.entrySet().stream().map(Map.Entry::getValue).flatMap(Collection::stream).collect(Collectors.toList());
  341. List<String> originRespCodes = targetResponsibilities.stream().map(Responsibility::getResponsibilityCode).collect(Collectors.toList());
  342. return shareParamValues.stream().filter(j -> originRespCodes.contains(j.getResponsibilityCode()))
  343. .filter(i -> i.getShareParamCode().equals(accountShareVO.getShareParamCode())).collect(Collectors.toList());
  344. }
  345. /**
  346. * 计算本次的成本金额
  347. *
  348. * @return
  349. */
  350. private BigDecimal getCostAmount(CostAccountShare accountShare, Integer calcType, Map<String, List<CostCostingGroup>> map, List<Allocation> costList) {
  351. // 是否包含分摊成本 0不包含 1 包含
  352. Integer isShareCost = accountShare.getIsShareCost();
  353. String accountingCodes = accountShare.getAccountingCodes();
  354. String responsibilityCode = accountShare.getResponsibilityCode();
  355. List<CostCostingGroup> costingGroups = map.get(responsibilityCode);
  356. if (CollUtil.isEmpty(costingGroups)) {
  357. List<Allocation> all = costList.stream().filter(i -> i.getTargetResponsibilityCode().equals(responsibilityCode)).collect(Collectors.toList());
  358. if (CollUtil.isEmpty(all) || isShareCost == 0) {
  359. return BigDecimal.ZERO;
  360. }
  361. BigDecimal reduce = all.stream().map(Allocation::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
  362. return reduce;
  363. } else {
  364. BigDecimal costAmount = BigDecimal.ZERO;
  365. List<Allocation> all = new ArrayList<>();
  366. if (!costList.isEmpty()) {
  367. all = costList.stream().filter(i -> i.getTargetResponsibilityCode().equals(responsibilityCode)).collect(Collectors.toList());
  368. }
  369. // 计算方式 0是合并计算 1是分开计算
  370. if (calcType == 0) {
  371. costAmount = costingGroups.stream().map(CostCostingGroup::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
  372. if (!all.isEmpty()) {
  373. BigDecimal bigDecimal = all.stream()
  374. .map(Allocation::getAmount)
  375. .reduce(BigDecimal.ZERO, BigDecimal::add);
  376. if (Objects.nonNull(bigDecimal)) {
  377. costAmount = costAmount.add(bigDecimal);
  378. }
  379. }
  380. } else {
  381. if (StrUtil.isBlank(accountingCodes)) {
  382. return BigDecimal.ZERO;
  383. }
  384. ArrayList<String> accountCodes = CollUtil.newArrayList(accountingCodes.split(StrUtil.COMMA));
  385. List<CostCostingGroup> costGroups = costingGroups.stream().filter(i -> accountCodes.contains(i.getAccountCode())).collect(Collectors.toList());
  386. costAmount = costGroups.stream().map(CostCostingGroup::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
  387. if (isShareCost == 1) {
  388. if (!all.isEmpty()) {
  389. BigDecimal bigDecimal = all.stream()
  390. .map(Allocation::getAmount)
  391. .reduce(BigDecimal.ZERO, BigDecimal::add);
  392. if (Objects.nonNull(bigDecimal)) {
  393. costAmount = costAmount.add(bigDecimal);
  394. }
  395. }
  396. }
  397. }
  398. return costAmount;
  399. }
  400. }
  401. /**
  402. * 通过目标层级获取目标责任中心
  403. *
  404. * @param targetLevel 目标层级 2,3,4
  405. * @param leverSort
  406. * @return List
  407. */
  408. private List<Responsibility> getTargetResponsibility(String targetLevel, Long hospId, Integer leverSort) {
  409. ArrayList<String> targetLevelList = CollUtil.newArrayList(StrUtil.split(targetLevel, StrUtil.COMMA));
  410. if (targetLevelList.size() == 1) {
  411. if (leverSort.equals(Integer.parseInt(targetLevelList.get(0)))) {
  412. return new ArrayList<>();
  413. }
  414. }
  415. List<CostShareLevel> shareLevels = shareLevelService.getListByLevelSort(targetLevelList, hospId);
  416. if (shareLevels.isEmpty()) {
  417. throw new CostException("请重新设置分摊层级");
  418. }
  419. List<Long> shareLevelIds = shareLevels.stream().map(CostShareLevel::getId).collect(Collectors.toList());
  420. return responsibilityService.getByLevelIds(shareLevelIds, hospId);
  421. }
  422. /**
  423. * 分摊后查询列表
  424. *
  425. * @param year 年月 (yyyy-MM-dd)
  426. * @param responsibilityCode 责任中心代码
  427. * @param current 当前页
  428. * @param pageSize 当前页展示的数据大小
  429. * @param hospId 医院id
  430. * @return PageUtils
  431. */
  432. @Override
  433. public PageUtils queryAfterAllocation(String year, String responsibilityCode, Integer current, Integer pageSize, Long hospId) {
  434. Integer dateYear = null;
  435. Integer dateMonth = null;
  436. if (StrUtil.isNotBlank(year)) {
  437. DateTime dateTime = DateUtil.parseDate(year);
  438. dateYear = DateUtil.year(dateTime);
  439. dateMonth = DateUtil.month(dateTime) + 1;
  440. }
  441. Integer startIndex = (current - 1) * pageSize;
  442. List<AfterAllocationVO> list = baseMapper.queryAfterAllocationList(dateYear, dateMonth, responsibilityCode, startIndex, pageSize, hospId);
  443. int totalCount = baseMapper.queryAfterAllocationListCount(dateYear, dateMonth, responsibilityCode, hospId);
  444. BigDecimal sum = baseMapper.queryAfterAllocationListSum(dateYear, dateMonth, responsibilityCode, hospId);
  445. return new PageUtils(list, totalCount, pageSize, current, sum);
  446. }
  447. /**
  448. * 分摊报表导出
  449. *
  450. * @param writer {@link ExcelWriter}
  451. * @param levelSort 分摊层级 就是第几次分摊
  452. * @param sheet 报表
  453. * @param year 年
  454. * @param month 月
  455. * @param shareLevelId
  456. * @return
  457. */
  458. @Override
  459. public ExcelWriter getShareReportTemplate(ExcelWriter writer, Integer levelSort, Sheet sheet, Integer year, Integer month, Long shareLevelId) {
  460. // 获取数据
  461. Map<String,List<String>> targetTotalMoneys = new HashMap<>();
  462. List<AllocationReportVO> allocationReportVOList = getAllocationReportVOS(levelSort, year, month, shareLevelId,targetTotalMoneys);
  463. // 设置导出
  464. Map<String, List<AllocationReportVO>> responsibilityMap = allocationReportVOList.stream().collect(Collectors.groupingBy(AllocationReportVO::getResponsibilityCode));
  465. Map<String, List<AllocationReportVO>> targetResponsibilityMap = allocationReportVOList.stream().collect(Collectors.groupingBy(AllocationReportVO::getTargetResponsibilityCode));
  466. // 以会计科目查
  467. Map<String, AllocationReportVO> allAccMap = allocationReportVOList.stream().collect(Collectors.toMap(k -> k.getResponsibilityName() + k.getAccountName() + k.getTargetResponsibilityName() + k.getShareParamName(), synOne -> synOne));
  468. // 用别名查 过滤别名不为空的
  469. Map<String, AllocationReportVO> allAliMap = allocationReportVOList.stream().filter(i -> StrUtil.isNotBlank(i.getAlias())).collect(Collectors.toMap(k -> k.getResponsibilityName() + k.getAlias() + k.getTargetResponsibilityName() + k.getShareParamName(), synOne -> synOne));
  470. // Map<String, AllocationReportVO> allAliMap = allocationReportVOList.stream().collect(Collectors.toMap(k -> k.getResponsibilityName() + k.getAlias() + k.getTargetResponsibilityName() + k.getShareParamName(), synOne -> synOne));
  471. // 当前责任中心下面有几个会计科目 后面进行合并使用
  472. final int[] numResponsibility = new int[1];
  473. levelSort=levelSort+1;
  474. // // 从第几列开始编写数据
  475. final int[] column = {levelSort + 3};
  476. responsibilityMap.forEach((key,value)->{
  477. Map<String, AllocationReportVO> linkedHashMap = new LinkedHashMap<>();
  478. value.forEach(i -> {
  479. String s = i.getResponsibilityCode() + i.getAccountCode();
  480. if (!linkedHashMap.containsKey(s)) {
  481. linkedHashMap.put(s, i);
  482. }
  483. });
  484. numResponsibility[0] = linkedHashMap.size();
  485. if (numResponsibility[0] >= NumberConstant.TWO) {
  486. Set<String> strings = linkedHashMap.keySet();
  487. for (String s : strings) {
  488. AllocationReportVO allocationReportVO = linkedHashMap.get(s);
  489. if (StrUtil.isBlank(allocationReportVO.getAlias())) {
  490. writer.writeCellValue(column[0], 0, allocationReportVO.getResponsibilityName());
  491. // 别名不存在
  492. writer.writeCellValue(column[0], 1, allocationReportVO.getAccountName());
  493. } else {
  494. // 不为空 设置别名
  495. writer.writeCellValue(column[0], 0, allocationReportVO.getResponsibilityName());
  496. writer.writeCellValue(column[0], 1, allocationReportVO.getAlias());
  497. }
  498. writer.writeCellValue(column[0], 2, allocationReportVO.getTotalAmounts());
  499. column[0]++;
  500. }
  501. } else {
  502. // 不需要合并单元格
  503. writer.writeCellValue(column[0], 0, value.get(0).getResponsibilityName());
  504. if (StrUtil.isNotBlank(value.get(0).getAlias())) {
  505. writer.writeCellValue(column[0], 1, value.get(0).getAlias());
  506. } else {
  507. writer.writeCellValue(column[0], 1, value.get(0).getAccountName());
  508. }
  509. writer.writeCellValue(column[0], 2, value.get(0).getTotalAmounts());
  510. column[0]++;
  511. }
  512. });
  513. // for (String key : keySet) {
  514. // List<AllocationReportVO> allocationReportVOS = responsibilityMap.get(key);
  515. // Map<String, AllocationReportVO> linkedHashMap = new LinkedHashMap<>();
  516. // allocationReportVOS.forEach(i -> {
  517. // String s = i.getResponsibilityCode() + i.getAccountCode();
  518. // if (!linkedHashMap.containsKey(s)) {
  519. // linkedHashMap.put(s, i);
  520. // }
  521. // });
  522. // numResponsibility[0] = linkedHashMap.size();
  523. // if (numResponsibility[0] >= NumberConstant.TWO) {
  524. // Set<String> strings = linkedHashMap.keySet();
  525. // for (String s : strings) {
  526. // AllocationReportVO allocationReportVO = linkedHashMap.get(s);
  527. // if (StrUtil.isBlank(allocationReportVO.getAlias())) {
  528. // writer.writeCellValue(column[0], 0, allocationReportVO.getResponsibilityName());
  529. // // 别名不存在
  530. // writer.writeCellValue(column[0], 1, allocationReportVO.getAccountName());
  531. // } else {
  532. // // 不为空 设置别名
  533. // writer.writeCellValue(column[0], 0, allocationReportVO.getResponsibilityName());
  534. // writer.writeCellValue(column[0], 1, allocationReportVO.getAlias());
  535. // }
  536. // writer.writeCellValue(column[0], 2, allocationReportVO.getTotalAmounts());
  537. // column[0]++;
  538. // }
  539. // } else {
  540. // // 不需要合并单元格
  541. // writer.writeCellValue(column[0], 0, allocationReportVOS.get(0).getResponsibilityName());
  542. // if (StrUtil.isNotBlank(allocationReportVOS.get(0).getAlias())) {
  543. // writer.writeCellValue(column[0], 1, allocationReportVOS.get(0).getAlias());
  544. // } else {
  545. // writer.writeCellValue(column[0], 1, allocationReportVOS.get(0).getAccountName());
  546. // }
  547. // writer.writeCellValue(column[0], 2, allocationReportVOS.get(0).getTotalAmounts());
  548. // column[0]++;
  549. // }
  550. // }
  551. // 设置单元格合并
  552. for (int j = 1; j < levelSort; j++) {
  553. writer.merge(0, 1, j, j, "第" + j + "次分摊", false);
  554. }
  555. // 目标责任集合
  556. writer.passCurrentRow();
  557. // 从第三行开始
  558. int num = 3;
  559. Set<String> targetSet = targetResponsibilityMap.keySet();
  560. int t = 0;
  561. for (String target : targetSet) {
  562. List<AllocationReportVO> allocationReportVOS = targetResponsibilityMap.get(target);
  563. Map<String, AllocationReportVO> linkedHashMap = new LinkedHashMap<>();
  564. allocationReportVOS.forEach(i -> {
  565. String s = i.getTargetResponsibilityCode() + i.getShareParamName();
  566. if (!linkedHashMap.containsKey(s)) {
  567. linkedHashMap.put(s, i);
  568. }
  569. });
  570. int shareParamSize = linkedHashMap.size();
  571. if (shareParamSize >= NumberConstant.TWO) {
  572. // 责任中心
  573. AllocationReportVO costCostingVO = allocationReportVOS.get(0);
  574. // 设置第几次分摊的值
  575. List<String> targetShareMoneyList = targetTotalMoneys.get(target);
  576. // for (int k = 0; k < levelSort - 1; k++) {
  577. // writer.merge(num, num + shareParamSize - 1, k + 1, k + 1, costCostingVO.getTargetShareMoneys().get(t++), false);
  578. //
  579. // }
  580. for (int k = 0; k < levelSort - 1; k++) {
  581. Object allocationAmount = getAllocationAmount(levelSort, k, allocationReportVOS, targetShareMoneyList);
  582. writer.merge(num, num + shareParamSize - 1, k + 1, k + 1, allocationAmount, false);
  583. }
  584. // if(!CollectionUtils.isEmpty(targetShareMoneyList)){
  585. // for (int k = 0; k < levelSort - 1; k++) {
  586. // if(k==levelSort-2){
  587. // BigDecimal totalAmount = allocationReportVOS.stream().map(AllocationReportVO::getAmount)
  588. // .reduce(BigDecimal.ZERO, BigDecimal::add);
  589. // writer.merge(num, num + shareParamSize - 1, k + 1, k + 1, totalAmount, false);
  590. // }else {
  591. // writer.merge(num, num + shareParamSize - 1, k + 1, k + 1, targetShareMoneyList.get(k), false);
  592. // }
  593. // }
  594. // }else {
  595. // for (int k = 0; k < levelSort - 1; k++) {
  596. // if(k==levelSort-2){
  597. // BigDecimal totalAmount = allocationReportVOS.stream().map(AllocationReportVO::getAmount)
  598. // .reduce(BigDecimal.ZERO, BigDecimal::add);
  599. // writer.merge(num, num + shareParamSize - 1, k + 1, k + 1, totalAmount, false);
  600. // }else {
  601. // writer.merge(num, num + shareParamSize - 1, k + 1, k + 1, "0.0000", false);
  602. // }
  603. // }
  604. // }
  605. // 设置对应的分摊参数值
  606. Set<String> strings = linkedHashMap.keySet();
  607. for (String s : strings) {
  608. AllocationReportVO allocationReportVO = linkedHashMap.get(s);
  609. writer.writeCellValue(0, num, allocationReportVO.getTargetResponsibilityName());
  610. writer.writeCellValue(levelSort, num, allocationReportVO.getShareParamName());
  611. writer.writeCellValue(levelSort + 1, num, allocationReportVO.getShareParamValueNums());
  612. writer.writeCellValue(levelSort + 2, num, allocationReportVO.getShareParamRates());
  613. for (int m = levelSort + 3; m < column[0]; m++) {
  614. // x是m y是num
  615. // 获取当前这一列对应的责任中心 以及会计科目
  616. // 第一行责任中心
  617. String responsibilityName = sheet.getRow(0).getCell(m).getStringCellValue();
  618. // 第二行的会计科目或者
  619. String accountNameOrAlias = sheet.getRow(1).getCell(m).getStringCellValue();
  620. // 这一行的目标责任中心
  621. String otherResponsibilityName = sheet.getRow(num).getCell(0).getStringCellValue();
  622. // 分摊参数
  623. String shareName = sheet.getRow(num).getCell(levelSort).getStringCellValue();
  624. String sss = responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName;
  625. AllocationReportVO vo = allAliMap.get(sss);
  626. AllocationReportVO allocationReportVO2 = allAccMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
  627. AllocationReportVO allocationReportVO3 = allAliMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
  628. if (Objects.nonNull(allocationReportVO2)) {
  629. writer.writeCellValue(m, num, allocationReportVO2.getAmounts());
  630. } else if (Objects.nonNull(allocationReportVO3)) {
  631. writer.writeCellValue(m, num, allocationReportVO3.getAmounts());
  632. } else {
  633. writer.writeCellValue(m, num, 0);
  634. }
  635. }
  636. num++;
  637. }
  638. }
  639. if (shareParamSize < NumberConstant.TWO) {
  640. writer.writeCellValue(0, num, allocationReportVOS.get(0).getTargetResponsibilityName());
  641. // for (int k = 0; k < levelSort - 1; k++) {
  642. // writer.writeCellValue(k + 1, num, allocationReportVOS.get(0).getTargetShareMoneys().get(k));
  643. // }
  644. List<String> targetShareMoneyList = targetTotalMoneys.get(target);
  645. for (int k = 0; k < levelSort - 1; k++) {
  646. Object allocationAmount = getAllocationAmount(levelSort, k, allocationReportVOS, targetShareMoneyList);
  647. writer.writeCellValue(k + 1, num, allocationAmount);
  648. }
  649. // if(!CollectionUtils.isEmpty(targetShareMoneyList)){
  650. // for (int k = 0; k < levelSort - 1; k++) {
  651. // if(k==levelSort-2){
  652. // BigDecimal totalAmount = allocationReportVOS.stream().map(AllocationReportVO::getAmount)
  653. // .reduce(BigDecimal.ZERO, BigDecimal::add);
  654. // writer.writeCellValue(k + 1, num, totalAmount);
  655. // }else {
  656. // writer.writeCellValue(k + 1, num, targetShareMoneyList.get(k));
  657. // }
  658. // }
  659. // }else {
  660. // for (int k = 0; k < levelSort - 1; k++) {
  661. // if(k==levelSort-2){
  662. // BigDecimal totalAmount = allocationReportVOS.stream().map(AllocationReportVO::getAmount)
  663. // .reduce(BigDecimal.ZERO, BigDecimal::add);
  664. // writer.writeCellValue(k + 1, num, totalAmount);
  665. // }else {
  666. // writer.writeCellValue(k + 1, num, "0.0000");
  667. // }
  668. // }
  669. // }
  670. writer.writeCellValue(levelSort, num, allocationReportVOS.get(0).getShareParamName());
  671. writer.writeCellValue(levelSort + 1, num, allocationReportVOS.get(0).getShareParamValueNums());
  672. writer.writeCellValue(levelSort + 2, num, allocationReportVOS.get(0).getShareParamRates());
  673. for (int m = levelSort + 3; m < column[0]; m++) {
  674. // x是m y是num
  675. // 获取当前这一列对应的责任中心 以及会计科目
  676. // 第一行责任中心
  677. String responsibilityName = sheet.getRow(0).getCell(m).getStringCellValue();
  678. // 第二行的会计科目或者
  679. String accountNameOrAlias = sheet.getRow(1).getCell(m).getStringCellValue();
  680. // 这一行的目标责任中心
  681. String otherResponsibilityName = sheet.getRow(num).getCell(0).getStringCellValue();
  682. // 分摊参数
  683. String shareName = sheet.getRow(num).getCell(levelSort).getStringCellValue();
  684. AllocationReportVO allocationReportVO2 = allAccMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
  685. AllocationReportVO allocationReportVO3 = allAliMap.get(responsibilityName + accountNameOrAlias + otherResponsibilityName + shareName);
  686. if (Objects.nonNull(allocationReportVO2)) {
  687. writer.writeCellValue(m, num, allocationReportVO2.getAmounts());
  688. } else if (Objects.nonNull(allocationReportVO3)) {
  689. writer.writeCellValue(m, num, allocationReportVO3.getAmounts());
  690. } else {
  691. writer.writeCellValue(m, num, 0);
  692. }
  693. }
  694. num++;
  695. }
  696. }
  697. Map<String, Integer> rowMap = new HashMap<>();
  698. int cc = levelSort + 3;
  699. // 合并行
  700. boolean otherData = false;
  701. for (int m = levelSort + 3; m < column[0] - 1; m++) {
  702. // String cellValue = sheet.getRow(0).getCell(m).getStringCellValue();
  703. // if (!rowMap.containsKey(cellValue)){
  704. // rowMap.put(cellValue,m);
  705. // Integer integer = rowMap.get(sheet.getRow(0).getCell(m).getStringCellValue());
  706. // String value = sheet.getRow(0).getCell(m ).getStringCellValue();
  707. // writer.merge(0, 0, rowMap.get(sheet.getRow(0).getCell(m-1).getStringCellValue()), m-1, sheet.getRow(0).getCell(m-1).getStringCellValue(), false);
  708. // }
  709. String cellValue1 = sheet.getRow(0).getCell(m).getStringCellValue();
  710. String cellValue2 = sheet.getRow(0).getCell(m + 1).getStringCellValue();
  711. if (!cellValue1.equals(cellValue2)) {
  712. if (cc != m) {
  713. writer.merge(0, 0, cc, m, cellValue1, false);
  714. } else {
  715. writer.writeCellValue(cc, 0, cellValue1);
  716. }
  717. cc = m + 1;
  718. } else if (m == column[0] - 2) {
  719. writer.merge(0, 0, cc, m + 1, sheet.getRow(0).getCell(m + 1).getStringCellValue(), false);
  720. }
  721. }
  722. // 合并列
  723. int jj = 3;
  724. for (int i = 3; i < num - 1; i++) {
  725. String cellValue1 = sheet.getRow(i).getCell(0).getStringCellValue();
  726. String cellValue2 = sheet.getRow(i + 1).getCell(0).getStringCellValue();
  727. if (!cellValue1.equals(cellValue2)) {
  728. if (jj != i) {
  729. writer.merge(jj, i, 0, 0, cellValue1, false);
  730. } else {
  731. writer.writeCellValue(0, jj, cellValue1);
  732. }
  733. jj = i + 1;
  734. } else if (i == num - 2) {
  735. writer.merge(jj, i + 1, 0, 0, cellValue1, false);
  736. }
  737. }
  738. for (int i = 0; i < 30; i++) {
  739. // 调整每一列宽度
  740. sheet.autoSizeColumn((short) i);
  741. // 解决自动设置列宽中文失效的问题
  742. sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 14 / 10);
  743. }
  744. return writer;
  745. }
  746. /**
  747. * 获取责任中心对应层级的分摊金额
  748. * @param levelSort
  749. * @param index
  750. * @param allocationReportVOS
  751. * @param targetShareMoneyList
  752. * @return
  753. */
  754. public Object getAllocationAmount(int levelSort,int index,List<AllocationReportVO> allocationReportVOS,List<String> targetShareMoneyList){
  755. //当前层级取分摊金额加总
  756. if(index==levelSort-2){
  757. BigDecimal totalAmount = allocationReportVOS.stream().map(AllocationReportVO::getAmount)
  758. .reduce(BigDecimal.ZERO, BigDecimal::add);
  759. return totalAmount;
  760. }
  761. //取不到对应层级的分摊总金额
  762. if(CollectionUtils.isEmpty(targetShareMoneyList)||targetShareMoneyList.size()<=index){
  763. return BigDecimal.valueOf(NumberConstant.ZERO,NumberConstant.FOUR);
  764. }
  765. //取对应层级的分摊总金额
  766. return targetShareMoneyList.get(index);
  767. }
  768. /**
  769. * 查询数据
  770. *
  771. * @param levelSort
  772. * @param year
  773. * @param month
  774. * @param targetTotalMoneys
  775. * @return
  776. */
  777. private List<AllocationReportVO> getAllocationReportVOS(Integer levelSort, Integer year, Integer month, Long shareLevelId,
  778. Map<String, List<String>> targetTotalMoneys) {
  779. Long hospId = UserContext.getCurrentLoginHospId();
  780. if (Objects.isNull(levelSort) || Objects.isNull(year) || Objects.isNull(month)) {
  781. throw new CostException(500, "参数异常");
  782. }
  783. // 处理第几次分摊计算值
  784. List<Allocation> allocationList = baseMapper.selectList(new QueryWrapper<Allocation>().lambda()
  785. .eq(Allocation::getHospId, hospId).eq(Allocation::getLevelSort, levelSort).eq(Allocation::getShareLevelId, shareLevelId).eq(Allocation::getDateYear, year)
  786. .eq(Allocation::getDateMonth, month));
  787. // 找会计科室的时候使用的
  788. List<Allocation> allocations = baseMapper.selectList(new QueryWrapper<Allocation>().lambda()
  789. .eq(Allocation::getHospId, hospId).eq(Allocation::getDateYear, year)
  790. .eq(Allocation::getDateMonth, month));
  791. Map<String, List<Allocation>> accrepMap = allocations.stream().collect(Collectors.groupingBy(k -> k.getLevelSort() + "cost" + k.getTargetResponsibilityCode()));
  792. LinkedList<String> shareMoney = new LinkedList<>();
  793. List<CostAccountShare> costAccountShareList = accountShareService.list(new QueryWrapper<CostAccountShare>().lambda()
  794. .eq(CostAccountShare::getHospId, hospId));
  795. Map<Long, CostAccountShare> accountShareMap = costAccountShareList.stream().collect(Collectors.toMap(CostAccountShare::getId, synOne -> synOne));
  796. List<AllocationReportVO> allocationReportVOList = BeanUtil.convertList(allocationList, AllocationReportVO.class);
  797. List<CostShareParamVO> shareParamVOList = shareParamService.getAll(hospId);
  798. Map<String,Integer> shareParamStatusMap = shareParamVOList.stream().collect(Collectors.toMap(CostShareParamVO::getShareParamCode, paramVO -> paramVO.getStatus() == null ? NumberConstant.ONE : paramVO.getStatus(), (a, b) -> b));
  799. List<AllocationReportVO> removeList = new ArrayList<>();
  800. // 设置会计科目的
  801. allocationReportVOList.forEach(i -> {
  802. Long accountShareId = i.getAccountShareId();
  803. CostAccountShare costAccountShare = accountShareMap.get(accountShareId);
  804. if (Objects.isNull(costAccountShare)) {
  805. throw new CostException(500, "成本参数参数设置对应不存在");
  806. }
  807. i.setAccountCode(costAccountShare.getAccountingCodes());
  808. i.setAccountName(costAccountShare.getAccountingNames());
  809. i.setAlias(costAccountShare.getAlias());
  810. if (levelSort > 1) {
  811. List<String> targetMoney = new ArrayList<>();
  812. for (int j = 1; j < levelSort; j++) {
  813. // 每一次计算要设置为0
  814. AtomicReference<BigDecimal> money = new AtomicReference<>(new BigDecimal("0.0000"));
  815. List<Allocation> allocations1 = accrepMap.get(j + "cost" + i.getTargetResponsibilityCode());
  816. if (CollUtil.isNotEmpty(allocations1)) {
  817. allocations1.forEach(m -> {
  818. money.updateAndGet(v -> v.add(m.getAmount()));
  819. // System.out.println(m.getAmount());
  820. });
  821. } else {
  822. // TODO 封装测试数据
  823. shareMoney.add("0");
  824. }
  825. // System.out.println("第"+j+"次"+money);
  826. shareMoney.add(money.toString());
  827. targetMoney.add(money.toString());
  828. }
  829. if(!CollectionUtils.isEmpty(targetTotalMoneys)){
  830. List<String> strings = targetTotalMoneys.get(i.getTargetResponsibilityCode());
  831. if(CollectionUtils.isEmpty(strings)){
  832. targetTotalMoneys.put(i.getTargetResponsibilityCode(),targetMoney);
  833. }
  834. }else {
  835. targetTotalMoneys.put(i.getTargetResponsibilityCode(),targetMoney);
  836. }
  837. }
  838. i.setTargetShareMoneys(shareMoney);
  839. // 设置字符串类型数据
  840. i.setTotalAmounts(i.getTotalAmount().toString());
  841. i.setShareParamValueNums(i.getShareParamValueNum().toString());
  842. i.setShareParamRates(i.getShareParamRate().toString());
  843. i.setAmounts(i.getAmount().toString());
  844. //校验是否有分摊参数未启用
  845. Integer status = shareParamStatusMap.get(i.getShareParamCode());
  846. if(status != null&&status.equals(NumberConstant.ZERO)){
  847. removeList.add(i);
  848. }
  849. });
  850. //校验是否有分摊参数未启用 未启用分摊参数 移除
  851. if(!CollectionUtils.isEmpty(removeList)){
  852. allocationReportVOList.removeAll(removeList);
  853. }
  854. return allocationReportVOList;
  855. }
  856. /**
  857. * 分摊后报表
  858. *
  859. * @param year 年月(yyyy-MM-dd)
  860. * @param responsibilityCode 责任中心代码
  861. * @param hospId 医院id
  862. * @return List
  863. */
  864. @Override
  865. public CollectDataFormVO queryAfterAllocationForm(String year, String responsibilityCode, Long hospId) {
  866. DateTime dateTime = DateUtil.parseDate(year);
  867. int dateYear = DateUtil.year(dateTime);
  868. int month = DateUtil.month(dateTime) + 1;
  869. List<CodeAndNameVO> responsibilityCodeAndNames = allocationQueryService.getRespCodeAndName(hospId, dateYear, month);
  870. List<CodeAndNameVO> accountCodeAndNames = allocationQueryService.getAccountCodeAndName(hospId, dateYear, month);
  871. // todo 校验两个List是否为空
  872. // 填充
  873. responsibilityCodeAndNames.add(0, new CodeAndNameVO("#", "#"));
  874. responsibilityCodeAndNames.add(responsibilityCodeAndNames.size(), new CodeAndNameVO("合计", "合计"));
  875. List<String> titleData = responsibilityCodeAndNames.stream().map(CodeAndNameVO::getName).collect(Collectors.toList());
  876. List<String> respCodes = responsibilityCodeAndNames.stream().map(CodeAndNameVO::getCode).collect(Collectors.toList());
  877. Map<Integer, String> titleMap = new HashMap<>();
  878. for (int i = 0; i < titleData.size(); i++) {
  879. titleMap.put(i + 1, titleData.get(i));
  880. }
  881. List<Map<Integer, Object>> realDatas = new ArrayList<>();
  882. List<String> accountCodes = accountCodeAndNames.stream().map(CodeAndNameVO::getCode).collect(Collectors.toList());
  883. for (CodeAndNameVO account : accountCodeAndNames) {
  884. Map<Integer, Object> map = new HashMap<>();
  885. for (int i = 0; i < responsibilityCodeAndNames.size(); i++) {
  886. if (i == 0) {
  887. map.put(i + 1, account.getName());
  888. continue;
  889. } else if (i == responsibilityCodeAndNames.size() - 1) {
  890. // todo 计算最右侧合计的钱
  891. BigDecimal amount = allocationQueryService.getTotalByAccountAndResps(hospId, dateYear, month, account.getCode(), respCodes);
  892. map.put(i + 1, amount);
  893. continue;
  894. }
  895. // TODO: 2021/8/26 计算 中间的钱
  896. BigDecimal amount = allocationQueryService.getTotalByAccountAndRespCode(hospId, dateYear, month, account.getCode(), respCodes.get(i));
  897. map.put(i + 1, amount);
  898. }
  899. realDatas.add(map);
  900. }
  901. // 尾栏计算
  902. Map<Integer, Object> map = new HashMap<>();
  903. for (int i = 0; i < titleData.size(); i++) {
  904. if (i == 0) {
  905. map.put(i + 1, "合计");
  906. continue;
  907. } else if (i == titleData.size() - 1) {
  908. // TODO: 2021/8/26 计算
  909. BigDecimal bigDecimal = allocationQueryService.getTotalMoney(dateYear, month, hospId);
  910. map.put(i + 1, bigDecimal);
  911. continue;
  912. }
  913. BigDecimal bigDecimal = allocationQueryService.getCountByRespAndAccounts(hospId, dateYear, month, responsibilityCodeAndNames.get(i).getCode(), accountCodes);
  914. map.put(i + 1, bigDecimal);
  915. }
  916. return new CollectDataFormVO(titleMap, realDatas, map);
  917. }
  918. /**
  919. * 分摊后报表输出
  920. *
  921. * @param date yyyy-MM-dd
  922. * @param hospId 医院id
  923. * @return List
  924. */
  925. @Override
  926. public List<AfterAllocationFormVO> afterAllocationFormList(String date, Long hospId) {
  927. DateTime parse = DateUtil.parse(date);
  928. int year = DateUtil.year(parse);
  929. int month = DateUtil.month(parse) + 1;
  930. // 得到这个月的分摊过的分摊层级
  931. List<Allocation> list = baseMapper.getAllSortLevel(hospId, year, month);
  932. Map<Long, CostShareLevel> map = shareLevelService.list(new QueryWrapper<CostShareLevel>().lambda().eq(CostShareLevel::getHospId, hospId)).stream().collect(Collectors.toMap(CostShareLevel::getId, synOne -> synOne));
  933. // list.
  934. List<AfterAllocationFormVO> vos = list.stream().map(i -> {
  935. AfterAllocationFormVO vo = new AfterAllocationFormVO();
  936. vo.setId(i.getId());
  937. vo.setYear(i.getDateYear());
  938. vo.setMonth(i.getDateMonth());
  939. vo.setShareLevel(i.getLevelSort());
  940. vo.setShareReportName(map.get(i.getShareLevelId()).getShareName() + "分摊");
  941. vo.setShareTime(DateUtil.date(i.getCreateTime()));
  942. vo.setShareLevelId(i.getShareLevelId());
  943. return vo;
  944. }).collect(Collectors.toList());
  945. return vos;
  946. }
  947. /**
  948. * 按时间计算分摊数据
  949. *
  950. * @param year 年月
  951. * @param month 月
  952. * @param hospId
  953. * @return
  954. */
  955. @Override
  956. public List<Allocation> getByDate(int year, int month, Long hospId) {
  957. return this.list(
  958. new LambdaQueryWrapper<Allocation>()
  959. .eq(Allocation::getDateYear, year)
  960. .eq(Allocation::getDateMonth, month)
  961. .eq(Allocation::getHospId, hospId)
  962. );
  963. }
  964. }