CostShareParamServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  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.exception.CostException;
  9. import com.imed.costaccount.common.util.BeanUtil;
  10. import com.imed.costaccount.common.util.PageUtils;
  11. import com.imed.costaccount.common.util.UserContext;
  12. import com.imed.costaccount.constants.NumberConstant;
  13. import com.imed.costaccount.mapper.AccountingMapper;
  14. import com.imed.costaccount.mapper.CostShareParamMapper;
  15. import com.imed.costaccount.model.Accounting;
  16. import com.imed.costaccount.model.CostShareParam;
  17. import com.imed.costaccount.model.dto.CostShareParamAccountDto;
  18. import com.imed.costaccount.model.dto.CostShareParamEditDto;
  19. import com.imed.costaccount.model.dto.CostShareParamSaveDto;
  20. import com.imed.costaccount.model.vo.CostShareParamVO;
  21. import com.imed.costaccount.service.CostShareParamService;
  22. import io.swagger.annotations.ApiOperation;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Propagation;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import org.springframework.util.CollectionUtils;
  27. import org.springframework.util.StringUtils;
  28. import java.util.*;
  29. import java.util.stream.Collectors;
  30. import static com.imed.costaccount.common.constants.Constant.LIMIT;
  31. @Service("costShareParamService")
  32. public class CostShareParamServiceImpl extends ServiceImpl<CostShareParamMapper, CostShareParam> implements CostShareParamService {
  33. private final AccountingMapper accountingMapper;
  34. public CostShareParamServiceImpl(AccountingMapper accountingMapper) {
  35. this.accountingMapper = accountingMapper;
  36. }
  37. /**
  38. * 分页查询相关的分摊参数数据
  39. *
  40. * @param current
  41. * @param pageSize
  42. * @param name
  43. * @return
  44. */
  45. @Override
  46. public PageUtils queryList(Integer current, Integer pageSize, String name, Long hospId) {
  47. Page<CostShareParam> costShareParamPage = new Page<>(current, pageSize);
  48. Page<CostShareParam> pages = this.page(costShareParamPage, new QueryWrapper<CostShareParam>().lambda()
  49. .eq(!StringUtils.isEmpty(hospId), CostShareParam::getHospId, hospId)
  50. .like(!StringUtils.isEmpty(name), CostShareParam::getShareParamName, name).orderByDesc(CostShareParam::getCreateTime));
  51. List<CostShareParam> records = pages.getRecords();
  52. List<CostShareParamVO> costShareParamVOList = BeanUtil.convertList(records, CostShareParamVO.class);
  53. // 封装会计科目Id的集合
  54. costShareParamVOList.forEach(i -> {
  55. String accountingId = i.getAccountingId();
  56. if (StrUtil.isNotBlank(accountingId)) {
  57. i.setAccountingIds(Arrays.asList(accountingId.split(StrUtil.COMMA)));
  58. } else {
  59. i.setAccountingIds(null);
  60. }
  61. });
  62. PageUtils pageUtils = new PageUtils(pages);
  63. pageUtils.setList(costShareParamVOList);
  64. return pageUtils;
  65. }
  66. /**
  67. * 保存分摊参数
  68. * 保存的时候需要校验分摊参数
  69. *
  70. * @param costShareParamSaveDto
  71. */
  72. @Override
  73. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  74. public void addCostShareParam(CostShareParamSaveDto costShareParamSaveDto) {
  75. //检验分摊参数是存在
  76. getCostShareParamByCode(costShareParamSaveDto);
  77. CostShareParam costShareParam = BeanUtil.convertObj(costShareParamSaveDto, CostShareParam.class);
  78. costShareParam.setCreateTime(System.currentTimeMillis());
  79. baseMapper.insert(costShareParam);
  80. }
  81. /**
  82. * 检验分摊参数是存在
  83. */
  84. private void getCostShareParamByCode(CostShareParamSaveDto costShareParamSaveDto) {
  85. List<CostShareParam> costShareParamList = baseMapper.selectList(new QueryWrapper<CostShareParam>().lambda()
  86. .eq(CostShareParam::getHospId, costShareParamSaveDto.getHospId()));
  87. // 检验添加的分摊参数是否存在
  88. if (!CollectionUtils.isEmpty(costShareParamList)) {
  89. Map<String, List<CostShareParam>> costShareMap = costShareParamList.stream().collect(Collectors.groupingBy(CostShareParam::getShareParamCode));
  90. if (!CollectionUtils.isEmpty(costShareMap.get(costShareParamSaveDto.getShareParamCode()))) {
  91. throw new CostException(500, "分摊参数已存在");
  92. }
  93. }
  94. }
  95. /**
  96. * 修改分摊参数
  97. *
  98. * @param costShareParamEditDto
  99. */
  100. @Override
  101. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  102. public void updateCostShareParam(CostShareParamEditDto costShareParamEditDto) {
  103. Long id = costShareParamEditDto.getId();
  104. CostShareParam costShareParam = baseMapper.selectById(id);
  105. if (Objects.isNull(costShareParam)) {
  106. throw new CostException(500, "对不起分摊参数不存在");
  107. }
  108. baseMapper.deleteById(id);
  109. // 判断当前输入的Code以最初的Code是否一样
  110. List<CostShareParam> costShareParamList = baseMapper.selectList(new QueryWrapper<CostShareParam>().lambda().select(CostShareParam::getShareParamCode).eq(CostShareParam::getHospId, costShareParamEditDto.getHospId()));
  111. Map<String, List<CostShareParam>> map = costShareParamList.stream().collect(Collectors.groupingBy(CostShareParam::getShareParamCode));
  112. if (!CollectionUtils.isEmpty(map.get(costShareParamEditDto.getShareParamCode()))) {
  113. throw new CostException(500, "对不起分摊参数代码已存在");
  114. }
  115. CostShareParam costShareParamRequest = BeanUtil.convertObj(costShareParamEditDto, CostShareParam.class);
  116. costShareParamRequest.setId(null);
  117. costShareParamRequest.setCreateTime(System.currentTimeMillis());
  118. if (NumberConstant.ONE.equals(costShareParamEditDto.getShareParamCode()) && NumberConstant.ONE.equals(costShareParamEditDto.getShareParamCode())) {
  119. costShareParamRequest.setAccountingId(costShareParam.getAccountingId());
  120. }
  121. baseMapper.insert(costShareParamRequest);
  122. }
  123. /**
  124. * 为按照科目的计算方式添加成本科目
  125. *
  126. * @param costShareParamAccountDto
  127. * @return
  128. */
  129. @Override
  130. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  131. public CostShareParam updateCostShareParamByAccountId(CostShareParamAccountDto costShareParamAccountDto) {
  132. Long hospId = UserContext.getHospId();
  133. Long costShareParamId = costShareParamAccountDto.getCostShareParamId();
  134. CostShareParam costShareParam = baseMapper.selectById(costShareParamId);
  135. List<String> accoutingCodes = new LinkedList<>();
  136. Map<Long, String> accountingMap = accountingMapper.selectList(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId)).stream().collect(Collectors.toMap(Accounting::getId, Accounting::getAccountingCode));
  137. // 判断当前操作的分层参数的计算方式是不是按科目计算
  138. if (NumberConstant.TWO.equals(costShareParam.getCalcType())) {
  139. String[] accountIds = costShareParamAccountDto.getAccountIds();
  140. List<String> accountList = Arrays.asList(accountIds);
  141. if (!CollectionUtil.isEmpty(accountList)) {
  142. String accountingIds = accountList.stream().map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
  143. costShareParam.setAccountingId(accountingIds);
  144. accountList.forEach(i -> {
  145. String accountCode = accountingMap.get(Long.parseLong(i));
  146. accoutingCodes.add(accountCode);
  147. });
  148. costShareParam.setAccountingCodes(accoutingCodes.stream().map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA)));
  149. } else {
  150. costShareParam.setAccountingId(null);
  151. }
  152. baseMapper.updateById(costShareParam);
  153. } else {
  154. throw new CostException(500, "计算方式不是按会计科目计算");
  155. }
  156. return null;
  157. }
  158. /**
  159. * 获取所有分摊参数的集合信息
  160. *
  161. * @param hospId
  162. * @return
  163. */
  164. @Override
  165. @ApiOperation("获取所有的分摊参数的集合数据")
  166. public List<CostShareParamVO> getAll(Long hospId) {
  167. List<CostShareParam> costShareParamList = baseMapper.selectList(new QueryWrapper<CostShareParam>().lambda()
  168. .eq(CostShareParam::getHospId, hospId));
  169. List<CostShareParamVO> costShareParamVOList = BeanUtil.convertList(costShareParamList, CostShareParamVO.class);
  170. return costShareParamVOList;
  171. }
  172. /**
  173. * 根据医院的Id分摊参数的ID获取对应的分摊参数的数据
  174. *
  175. * @param id
  176. * @param hospId
  177. * @return
  178. */
  179. @Override
  180. public CostShareParamVO getByHospIdAndAPramId(Integer id, Long hospId) {
  181. CostShareParam costShareParam = baseMapper.selectOne(new QueryWrapper<CostShareParam>().lambda()
  182. .eq(CostShareParam::getHospId, hospId)
  183. .eq(CostShareParam::getId, id));
  184. CostShareParamVO costShareParamVO = BeanUtil.convertObj(costShareParam, CostShareParamVO.class);
  185. return costShareParamVO;
  186. }
  187. @Override
  188. public List<Long> selectIsSelect(Integer shareParamId) {
  189. CostShareParam byId = this.getById(shareParamId);
  190. if (Objects.isNull(byId)) {
  191. return Collections.emptyList();
  192. }
  193. String accountingId = byId.getAccountingId();
  194. if (!StringUtils.isEmpty(accountingId)) {
  195. String[] split = accountingId.split(StrUtil.COMMA);
  196. List<Long> accountingIdList = Arrays.stream(split).map(Long::valueOf).collect(Collectors.toList());
  197. return accountingIdList;
  198. } else {
  199. return Collections.emptyList();
  200. }
  201. }
  202. /**
  203. * 通过计算方式和id获取是否存在会计科目的成本分摊参数
  204. *
  205. * @param shareParamId 主键id
  206. * @return CostShareParam
  207. */
  208. @Override
  209. public CostShareParam getByIdAndCalcType(Long shareParamId) {
  210. return this.getOne(
  211. new LambdaQueryWrapper<CostShareParam>().eq(CostShareParam::getId, shareParamId).eq(CostShareParam::getCalcType, 2)
  212. );
  213. }
  214. /**
  215. * 通过code得到name
  216. *
  217. * @param shareParamCode
  218. * @param hospId
  219. * @return
  220. */
  221. @Override
  222. public String getByCode(String shareParamCode, Long hospId) {
  223. CostShareParam one = this.getOne(
  224. new LambdaQueryWrapper<CostShareParam>().eq(CostShareParam::getShareParamCode, shareParamCode)
  225. .eq(CostShareParam::getHospId, hospId).last(LIMIT)
  226. );
  227. if (Objects.isNull(one)) {
  228. throw new CostException("数据异常");
  229. }
  230. return one.getShareParamName();
  231. }
  232. /**
  233. * 批量删除分摊参数
  234. *
  235. * @param asList 分摊参数的Id集合
  236. */
  237. @Override
  238. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  239. public void deleteByIds(List<Integer> asList) {
  240. this.removeByIds(asList);
  241. }
  242. }