CostShareParamServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package com.kcim.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.kcim.common.exception.CostException;
  9. import com.kcim.common.util.BeanUtil;
  10. import com.kcim.common.util.PageUtils;
  11. import com.kcim.common.util.UserContext;
  12. import com.kcim.common.constants.NumberConstant;
  13. import com.kcim.dao.mapper.AccountingMapper;
  14. import com.kcim.dao.mapper.CostShareParamMapper;
  15. import com.kcim.dao.model.Accounting;
  16. import com.kcim.dao.model.CostShareParam;
  17. import com.kcim.dao.model.dto.CostShareParamAccountDto;
  18. import com.kcim.dao.model.dto.CostShareParamEditDto;
  19. import com.kcim.dao.model.dto.CostShareParamSaveDto;
  20. import com.kcim.vo.CostShareParamVO;
  21. import com.kcim.service.CostShareParamService;
  22. import com.kcim.common.constants.Constant;
  23. import io.swagger.annotations.ApiOperation;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Propagation;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import org.springframework.util.CollectionUtils;
  28. import org.springframework.util.StringUtils;
  29. import java.util.*;
  30. import java.util.stream.Collectors;
  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. costShareParamVOList.sort(Comparator.comparing(CostShareParamVO::getShareParamCode));
  64. pageUtils.setList(costShareParamVOList);
  65. return pageUtils;
  66. }
  67. /**
  68. * 保存分摊参数
  69. * 保存的时候需要校验分摊参数
  70. *
  71. * @param costShareParamSaveDto
  72. */
  73. @Override
  74. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  75. public void addCostShareParam(CostShareParamSaveDto costShareParamSaveDto) {
  76. //检验分摊参数是存在
  77. getCostShareParamByCode(costShareParamSaveDto);
  78. CostShareParam costShareParam = BeanUtil.convertObj(costShareParamSaveDto, CostShareParam.class);
  79. costShareParam.setCreateTime(System.currentTimeMillis());
  80. baseMapper.insert(costShareParam);
  81. }
  82. /**
  83. * 检验分摊参数是存在
  84. */
  85. private void getCostShareParamByCode(CostShareParamSaveDto costShareParamSaveDto) {
  86. List<CostShareParam> costShareParamList = baseMapper.selectList(new QueryWrapper<CostShareParam>().lambda()
  87. .eq(CostShareParam::getHospId, costShareParamSaveDto.getHospId()));
  88. // 检验添加的分摊参数是否存在
  89. if (!CollectionUtils.isEmpty(costShareParamList)) {
  90. Map<String, List<CostShareParam>> costShareMap = costShareParamList.stream().collect(Collectors.groupingBy(CostShareParam::getShareParamCode));
  91. if (!CollectionUtils.isEmpty(costShareMap.get(costShareParamSaveDto.getShareParamCode()))) {
  92. throw new CostException(500, "分摊参数已存在");
  93. }
  94. }
  95. }
  96. /**
  97. * 修改分摊参数
  98. *
  99. * @param costShareParamEditDto
  100. */
  101. @Override
  102. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  103. public void updateCostShareParam(CostShareParamEditDto costShareParamEditDto) {
  104. Long id = costShareParamEditDto.getId();
  105. CostShareParam costShareParam = baseMapper.selectById(id);
  106. if (Objects.isNull(costShareParam)) {
  107. throw new CostException(500, "对不起分摊参数不存在");
  108. }
  109. baseMapper.deleteById(id);
  110. // 判断当前输入的Code以最初的Code是否一样
  111. List<CostShareParam> costShareParamList = baseMapper.selectList(new QueryWrapper<CostShareParam>().lambda().select(CostShareParam::getShareParamCode).eq(CostShareParam::getHospId, costShareParamEditDto.getHospId()));
  112. Map<String, List<CostShareParam>> map = costShareParamList.stream().collect(Collectors.groupingBy(CostShareParam::getShareParamCode));
  113. if (!CollectionUtils.isEmpty(map.get(costShareParamEditDto.getShareParamCode()))) {
  114. throw new CostException(500, "对不起分摊参数代码已存在");
  115. }
  116. CostShareParam costShareParamRequest = BeanUtil.convertObj(costShareParamEditDto, CostShareParam.class);
  117. costShareParamRequest.setId(null);
  118. costShareParamRequest.setCreateTime(System.currentTimeMillis());
  119. if (NumberConstant.ONE.equals(costShareParamEditDto.getShareParamCode()) && NumberConstant.ONE.equals(costShareParamEditDto.getShareParamCode())) {
  120. costShareParamRequest.setAccountingId(costShareParam.getAccountingId());
  121. }
  122. baseMapper.insert(costShareParamRequest);
  123. }
  124. /**
  125. * 为按照科目的计算方式添加成本科目
  126. *
  127. * @param costShareParamAccountDto
  128. * @return
  129. */
  130. @Override
  131. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  132. public CostShareParam updateCostShareParamByAccountId(CostShareParamAccountDto costShareParamAccountDto) {
  133. Long hospId = UserContext.getHospId();
  134. Long costShareParamId = costShareParamAccountDto.getCostShareParamId();
  135. CostShareParam costShareParam = baseMapper.selectById(costShareParamId);
  136. List<String> accoutingCodes = new LinkedList<>();
  137. Map<Long, String> accountingMap = accountingMapper.selectList(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId)).stream().collect(Collectors.toMap(Accounting::getId, Accounting::getAccountingCode));
  138. // 判断当前操作的分层参数的计算方式是不是按科目计算
  139. if (NumberConstant.TWO.equals(costShareParam.getCalcType())) {
  140. String[] accountIds = costShareParamAccountDto.getAccountIds();
  141. List<String> accountList = Arrays.asList(accountIds);
  142. if (!CollectionUtil.isEmpty(accountList)) {
  143. String accountingIds = accountList.stream().map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
  144. costShareParam.setAccountingId(accountingIds);
  145. accountList.forEach(i -> {
  146. String accountCode = accountingMap.get(Long.parseLong(i));
  147. accoutingCodes.add(accountCode);
  148. });
  149. costShareParam.setAccountingCodes(accoutingCodes.stream().map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA)));
  150. } else {
  151. costShareParam.setAccountingId(null);
  152. }
  153. baseMapper.updateById(costShareParam);
  154. } else {
  155. throw new CostException(500, "计算方式不是按会计科目计算");
  156. }
  157. return null;
  158. }
  159. /**
  160. * 获取所有分摊参数的集合信息
  161. *
  162. * @param hospId
  163. * @return
  164. */
  165. @Override
  166. @ApiOperation("获取所有的分摊参数的集合数据")
  167. public List<CostShareParamVO> getAll(Long hospId) {
  168. List<CostShareParam> costShareParamList = baseMapper.selectList(new QueryWrapper<CostShareParam>().lambda()
  169. .eq(CostShareParam::getHospId, hospId));
  170. return BeanUtil.convertList(costShareParamList, CostShareParamVO.class);
  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(Constant.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. }