CostDepartmentProfitServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.imed.costaccount.common.util.BeanUtil;
  9. import com.imed.costaccount.common.util.DateUtils;
  10. import com.imed.costaccount.common.util.PageUtils;
  11. import com.imed.costaccount.constants.NumberConstant;
  12. import com.imed.costaccount.enums.DateStyleEnum;
  13. import com.imed.costaccount.mapper.CostDepartmentProfitMapper;
  14. import com.imed.costaccount.model.*;
  15. import com.imed.costaccount.model.vo.CostDepartmentProfitVO;
  16. import com.imed.costaccount.service.*;
  17. import org.springframework.stereotype.Service;
  18. import java.math.BigDecimal;
  19. import java.util.ArrayList;
  20. import java.util.Date;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.concurrent.atomic.AtomicReference;
  24. import java.util.stream.Collectors;
  25. @Service("costDepartmentProfitService")
  26. public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentProfitMapper, CostDepartmentProfit> implements CostDepartmentProfitService {
  27. private final ReportFormService reportFormService;
  28. private final IncomeCollectionService incomeCollectionService;
  29. private final CostShareLevelService costShareLevelService;
  30. private final ResponsibilityService responsibilityService;
  31. private final ReportRelationService reportRelationService;
  32. private final AllocationService allocationService;
  33. public CostDepartmentProfitServiceImpl(ReportFormService reportFormService, IncomeCollectionService incomeCollectionService, CostShareLevelService costShareLevelService, ResponsibilityService responsibilityService, ReportRelationService reportRelationService, AllocationService allocationService) {
  34. this.reportFormService = reportFormService;
  35. this.incomeCollectionService = incomeCollectionService;
  36. this.costShareLevelService = costShareLevelService;
  37. this.responsibilityService = responsibilityService;
  38. this.reportRelationService = reportRelationService;
  39. this.allocationService = allocationService;
  40. }
  41. /**
  42. * 查询科室损益数据
  43. *
  44. * @param current
  45. * @param pageSize
  46. * @param responsibilityCode
  47. * @param date
  48. * @param hospId
  49. * @return
  50. */
  51. @Override
  52. public PageUtils queryList(Integer current, Integer pageSize, String responsibilityCode, String date, Long hospId) {
  53. int year = 0;
  54. int month = 0;
  55. if (StrUtil.isNotBlank(date)) {
  56. Date dateTime = DateUtils.StringToDate(date, DateStyleEnum.YYYY_MM_DD);
  57. year = DateUtil.year(dateTime);
  58. month = DateUtil.month(dateTime) + 1;
  59. }
  60. Page<CostDepartmentProfit> departmentProfitPage = new Page<>(current, pageSize);
  61. Page<CostDepartmentProfit> pages = this.page(departmentProfitPage, new QueryWrapper<CostDepartmentProfit>().lambda()
  62. .eq(CostDepartmentProfit::getHospId, hospId)
  63. .eq(StrUtil.isNotBlank(responsibilityCode), CostDepartmentProfit::getResponsibilityCode, responsibilityCode)
  64. .eq(StrUtil.isNotBlank(date), CostDepartmentProfit::getYear, year)
  65. .eq(StrUtil.isNotBlank(date), CostDepartmentProfit::getMonth, month));
  66. List<CostDepartmentProfit> records = pages.getRecords();
  67. List<CostDepartmentProfitVO> costDepartmentProfitVOList = BeanUtil.convertList(records, CostDepartmentProfitVO.class);
  68. PageUtils pageUtils = new PageUtils(pages);
  69. pageUtils.setList(costDepartmentProfitVOList);
  70. return pageUtils;
  71. }
  72. /**
  73. * 科室损益计算
  74. *
  75. * @param date
  76. * @param hospId
  77. */
  78. @Override
  79. public void setDepartmentProfit(String date, Long hospId) {
  80. int year = 0;
  81. int month = 0;
  82. if (StrUtil.isNotBlank(date)) {
  83. Date dateTime = DateUtils.StringToDate(date, DateStyleEnum.YYYY_MM_DD);
  84. year = DateUtil.year(dateTime);
  85. month = DateUtil.month(dateTime) + 1;
  86. }
  87. // 先查询指定条件的报表数据
  88. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  89. .eq(ReportForm::getHospId, hospId)
  90. .eq(ReportForm::getReportType, NumberConstant.ZERO).ne(ReportForm::getCalcType, NumberConstant.ZERO));
  91. // 遍历报表数据根据报表数据计算方式进行计算
  92. // 查询最后一个层级的责任中心
  93. List<CostShareLevel> costShareLevelList = costShareLevelService.list(new QueryWrapper<CostShareLevel>().lambda()
  94. .eq(CostShareLevel::getHospId, hospId).orderByDesc(CostShareLevel::getLeverSort));
  95. Long id = costShareLevelList.get(0).getId();
  96. // 查询责任中心里面是这个层级的所有的收益中心
  97. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  98. .eq(Responsibility::getHospId, hospId).eq(Responsibility::getResponsibilityType, NumberConstant.ONE));
  99. // 归集前
  100. List<IncomeCollection> incomeList = incomeCollectionService.list(new QueryWrapper<IncomeCollection>().lambda()
  101. .eq(IncomeCollection::getHospId, hospId)
  102. .eq(year > 0, IncomeCollection::getYear, year).eq(month > 0, IncomeCollection::getMonth, month));
  103. Map<Long, List<ReportRelation>> reportRelationMap = reportRelationService.list(new QueryWrapper<ReportRelation>().lambda().eq(ReportRelation::getHospId, hospId)).stream().collect(Collectors.groupingBy(ReportRelation::getReportId));
  104. // TODO 需要查询归集后的数据
  105. // 查询分摊的报表数据
  106. List<Allocation> allocationList = allocationService.list(new QueryWrapper<Allocation>().lambda().eq(Allocation::getHospId, hospId)
  107. .eq(year > 0, Allocation::getDateYear, year).eq(month > 0, Allocation::getDateMonth, month));
  108. // 查询所有指定类型的损益表
  109. // 每个责任中心对应报表都要生成记录
  110. // 封装需要设置的数据
  111. List<CostDepartmentProfitVO> list = new ArrayList<>();
  112. int finalYear = year;
  113. int finalMonth = month;
  114. responsibilityList.forEach(i->{
  115. reportFormList.forEach(j->{
  116. CostDepartmentProfitVO costDepartmentProfitVO = new CostDepartmentProfitVO();
  117. costDepartmentProfitVO.setYear(finalYear);
  118. costDepartmentProfitVO.setMonth(finalMonth);
  119. costDepartmentProfitVO.setReportNum(j.getNum());
  120. costDepartmentProfitVO.setCostType(j.getCalcType());
  121. costDepartmentProfitVO.setReportName(j.getReportName());
  122. costDepartmentProfitVO.setResponsibilityCode(i.getResponsibilityCode());
  123. costDepartmentProfitVO.setResponsibilityName(i.getResponsibilityName());
  124. costDepartmentProfitVO.setReportParentId(i.getParentId());
  125. costDepartmentProfitVO.setCostType(NumberConstant.ONE);
  126. costDepartmentProfitVO.setIncomeType(NumberConstant.ONE);
  127. costDepartmentProfitVO.setHospId(hospId);
  128. list.add(costDepartmentProfitVO);
  129. });
  130. });
  131. list.forEach(i->{
  132. Integer calcType = i.getCostType();
  133. switch (calcType){
  134. case 1:
  135. // TODO 按照会计科目进行计算
  136. setAccountReportData(i,incomeList,reportRelationMap);
  137. break;
  138. case 2:
  139. // TODO 按照分摊层级进行计算
  140. break;
  141. case 3:
  142. // TODO 按照小计进行计算
  143. break;
  144. case 4:
  145. // TODO 按照计算公式进行计算
  146. break;
  147. case 5:
  148. // TODO 按照责任中心进行计算
  149. break;
  150. default:
  151. i.setAmount(new BigDecimal("0.0000"));
  152. break;
  153. }
  154. });
  155. }
  156. /**
  157. * 按照会计科目进行计算
  158. * @param i
  159. */
  160. private void setAccountReportData(CostDepartmentProfitVO i,List<IncomeCollection> list,Map<Long, List<ReportRelation>> reportRelationMap) {
  161. // 在报表关联里面查询当前报表关联的
  162. List<ReportRelation> reportRelationList = reportRelationMap.get(i.getId());
  163. if (CollUtil.isNotEmpty(reportRelationList)){
  164. // 获取对应的会计科目信息 筛选会计科目的Code
  165. List<String> accountList = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  166. // 查找在归集数据里面当前责任中心对应的这些会计科目的金额
  167. List<IncomeCollection> incomeCollectionList = list.stream().filter(income -> income.getResponsibilityCode().equals(i.getResponsibilityCode()) && accountList.contains(income.getAccountingCode())).collect(Collectors.toList());
  168. AtomicReference<BigDecimal> sum= new AtomicReference<>(new BigDecimal("0.000"));
  169. incomeCollectionList.forEach(m->{
  170. sum.updateAndGet(v -> v.add(m.getAmount()));
  171. });
  172. // TODO 需要查询分摊后的表
  173. i.setAmount(new BigDecimal(sum.toString()));
  174. }else {
  175. i.setAmount(new BigDecimal("0.0000"));
  176. }
  177. }
  178. /**
  179. * 按照分摊层级进行计算
  180. */
  181. private void setShareLevelReportData(CostDepartmentProfitVO i,Map<Long, List<ReportRelation>> reportRelationMap,List<Allocation> allocationList){
  182. List<ReportRelation> reportRelationList = reportRelationMap.get(i.getId());
  183. // 找到对应的分摊层级的Id
  184. List<String> shareLevelIds = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  185. if (CollUtil.isNotEmpty(shareLevelIds)){
  186. // 查询报表里面是当前分摊层级的数据
  187. AtomicReference<BigDecimal> sum= new AtomicReference<>(new BigDecimal("0.000"));
  188. List<Allocation> allocations = allocationList.stream().filter(m -> m.getLevelSort().equals(i.getLevelSort()) && m.getTargetResponsibilityCode().equals(i.getResponsibilityCode())).collect(Collectors.toList());
  189. allocations.forEach(m->{
  190. sum.updateAndGet(v -> v.add(m.getAmount()));
  191. });
  192. i.setAmount(new BigDecimal(sum.toString()));
  193. }else {
  194. i.setAmount(new BigDecimal("0.0000"));
  195. }
  196. }
  197. /**
  198. * 按照责任中心进行计算
  199. * 原始责任中心是设置的责任中心 目标责任中心是报表的责任中心
  200. */
  201. public void setResponsibilityCode(){
  202. }
  203. }