ReportRelationServiceImpl.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.imed.costaccount.common.exception.CostException;
  7. import com.imed.costaccount.mapper.ReportFormMapper;
  8. import com.imed.costaccount.mapper.ReportRelationMapper;
  9. import com.imed.costaccount.model.ReportForm;
  10. import com.imed.costaccount.model.ReportRelation;
  11. import com.imed.costaccount.model.dto.ReportRelationDTO;
  12. import com.imed.costaccount.model.vo.RelationVO;
  13. import com.imed.costaccount.service.ReportRelationService;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Propagation;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Objects;
  21. import java.util.stream.Collectors;
  22. @Slf4j
  23. @Service("reportRelationService")
  24. public class ReportRelationServiceImpl extends ServiceImpl<ReportRelationMapper, ReportRelation> implements ReportRelationService {
  25. private final ReportFormMapper reportFormMapper;
  26. public ReportRelationServiceImpl(ReportFormMapper reportFormMapper) {
  27. this.reportFormMapper = reportFormMapper;
  28. }
  29. /**
  30. * 报表项目关联的会计科目对象
  31. *
  32. * @param reportId 报表项目id
  33. * @param hospId 医院id
  34. * @return {@link RelationVO}
  35. */
  36. @Override
  37. public List<RelationVO> getAccountRelation(Long reportId, Long hospId) {
  38. return baseMapper.getAccountRelation(reportId, hospId);
  39. }
  40. /**
  41. * 报表项目关联的分摊层级对象
  42. *
  43. * @param reportId 报表项目id
  44. * @param hospId 医院id
  45. * @return {@link RelationVO}
  46. */
  47. @Override
  48. public List<RelationVO> getShareLevel(Long reportId, Long hospId) {
  49. return baseMapper.getShareParam(reportId, hospId);
  50. }
  51. /**
  52. * 根据关系类型,获取可绑定的关系数据(包含回显)
  53. *
  54. * @param reportId 报表项目id
  55. * @param relation 1.对应会计科目设置,2.对应分摊参数设置,根据列表中showAddRelation字段是否存在判断
  56. * @param hospId 医院id
  57. * @return 返回所有列表 并便是已选择的元素
  58. */
  59. @Override
  60. public List<RelationVO> getRelationList(Long reportId, Integer relation, Long hospId) {
  61. List<RelationVO> list = new ArrayList<>();
  62. if (relation == 1) {
  63. list = this.getAccountRelation(reportId, hospId);
  64. // List<Accounting> accounts = accountingService.list(new LambdaQueryWrapper<Accounting>().select(Accounting::getAccountingCode, Accounting::getAccountingName).eq(Accounting::getHospId, hospId));
  65. // if (accounts.isEmpty()) {
  66. // return list;
  67. // }
  68. // list = accounts.stream().map(i -> {
  69. // RelationVO relationVO = new RelationVO();
  70. // relationVO.setIsSelect(false);
  71. // relationVO.setRelation(1);
  72. // relationVO.setCode(i.getAccountingCode());
  73. // relationVO.setName(i.getAccountingName());
  74. // return relationVO;
  75. // }).collect(Collectors.toList());
  76. // for (RelationVO relationVO : accountRelation) {
  77. // for (RelationVO vo : list) {
  78. // if (vo.getCode().equals(relationVO.getCode())) {
  79. // vo.setIsSelect(true);
  80. // }
  81. // }
  82. // }
  83. } else if (relation == 2) {
  84. list = this.getShareLevel(reportId, hospId);
  85. // List<CostShareParam> accounts = shareParamService.list(new LambdaQueryWrapper<CostShareParam>().select(CostShareParam::getShareParamCode, CostShareParam::getShareParamName).eq(CostShareParam::getHospId, hospId));
  86. // if (accounts.isEmpty()) {
  87. // return list;
  88. // }
  89. // list = accounts.stream().map(i -> {
  90. // RelationVO relationVO = new RelationVO();
  91. // relationVO.setIsSelect(false);
  92. // relationVO.setRelation(1);
  93. // relationVO.setCode(i.getShareParamCode());
  94. // relationVO.setName(i.getShareParamName());
  95. // return relationVO;
  96. // }).collect(Collectors.toList());
  97. // for (RelationVO relationVO : accountRelation) {
  98. // for (RelationVO vo : list) {
  99. // if (vo.getCode().equals(relationVO.getCode())) {
  100. // vo.setIsSelect(true);
  101. // }
  102. // }
  103. // }
  104. }
  105. return list;
  106. }
  107. /**
  108. * 编辑相关关系
  109. *
  110. * @param reportRelationDTO {@link ReportRelationDTO}
  111. * @param hospId
  112. */
  113. @Override
  114. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  115. public void saveReportRelation(ReportRelationDTO reportRelationDTO, Long hospId) {
  116. Integer relation = reportRelationDTO.getRelation();
  117. List<String> relationCodes = reportRelationDTO.getRelationCodes();
  118. Long reportId = reportRelationDTO.getReportId();
  119. // 校验这个report是否能绑定
  120. ReportForm byId = reportFormMapper.selectById(reportId);
  121. if (Objects.isNull(byId)) {
  122. throw new CostException("选择的报表项目不存在");
  123. }
  124. Integer calcType = byId.getCalcType();
  125. if (calcType != 1 && calcType != 2) {
  126. throw new CostException("选择的报表项目不能绑定关联关系");
  127. }
  128. List<ReportRelation> list = this.list(
  129. new LambdaQueryWrapper<ReportRelation>().eq(ReportRelation::getReportId, reportId).eq(ReportRelation::getHospId, hospId)
  130. );
  131. if (!list.isEmpty()) {
  132. baseMapper.deleteBatchIds(list.stream().map(ReportRelation::getId).collect(Collectors.toList()));
  133. }
  134. List<ReportRelation> reportRelations = relationCodes.stream().map(code -> {
  135. ReportRelation reportRelation = new ReportRelation();
  136. reportRelation.setRelation(relation);
  137. reportRelation.setReportId(reportId);
  138. reportRelation.setHospId(hospId);
  139. reportRelation.setRelationCode(code);
  140. reportRelation.setCreateTime(System.currentTimeMillis());
  141. return reportRelation;
  142. }).collect(Collectors.toList());
  143. // 校验 关联关系的id 是否已绑定过其他的报表项目
  144. checkIfExist(hospId, relation, relationCodes);
  145. this.saveBatch(reportRelations);
  146. }
  147. /**
  148. * 校验 关联关系的id 是否已绑定过其他的报表项目
  149. *
  150. * @param hospId 医院id
  151. * @param relation 关系
  152. * @param relationCodes 所有需要绑定的code 列表
  153. */
  154. private void checkIfExist(Long hospId, Integer relation, List<String> relationCodes) {
  155. List<ReportRelation> relations = this.list(
  156. new LambdaQueryWrapper<ReportRelation>().select(ReportRelation::getRelationCode)
  157. .in(ReportRelation::getRelationCode, relationCodes)
  158. .eq(ReportRelation::getRelation, relation)
  159. .eq(ReportRelation::getHospId, hospId)
  160. );
  161. if (CollUtil.isNotEmpty(relations)) {
  162. throw new CostException(500, "编码为:" +
  163. StrUtil.join(StrUtil.COMMA, relations.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList())) +
  164. "已被其他项目代码绑定");
  165. }
  166. }
  167. }