CostAccountShareServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package com.imed.costaccount.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.imed.costaccount.common.exception.CostException;
  6. import com.imed.costaccount.common.util.JsonUtil;
  7. import com.imed.costaccount.common.util.PageUtils;
  8. import com.imed.costaccount.constants.NumberConstant;
  9. import com.imed.costaccount.mapper.CostAccountShareMapper;
  10. import com.imed.costaccount.model.*;
  11. import com.imed.costaccount.model.dto.CostAccountShareEditDto;
  12. import com.imed.costaccount.model.dto.CostAccountShareSaveDto;
  13. import com.imed.costaccount.model.dto.ShareParamEditDto;
  14. import com.imed.costaccount.model.vo.CostAccountShareVO;
  15. import com.imed.costaccount.model.vo.CostShareParamStatusVO;
  16. import com.imed.costaccount.model.vo.CostShareParamVO;
  17. import com.imed.costaccount.model.vo.ShareParamProportionVO;
  18. import com.imed.costaccount.service.*;
  19. import com.imed.costaccount.common.util.BeanUtil;
  20. import org.apache.shiro.SecurityUtils;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Propagation;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import org.springframework.util.CollectionUtils;
  25. import org.springframework.util.StringUtils;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Objects;
  30. import java.util.concurrent.atomic.AtomicReference;
  31. import java.util.stream.Collectors;
  32. @Service("costAccountShareService")
  33. public class CostAccountShareServiceImpl extends ServiceImpl<CostAccountShareMapper, CostAccountShare> implements CostAccountShareService {
  34. private final ResponsibilityService responsibilityService;
  35. private final AccountingService accountingService;
  36. private final CostShareLevelService costShareLevelService;
  37. private final CostShareParamService costShareParamService;
  38. public CostAccountShareServiceImpl(ResponsibilityService responsibilityService, AccountingService accountingService, CostShareLevelService costShareLevelService, CostShareParamService costShareParamService) {
  39. this.responsibilityService = responsibilityService;
  40. this.accountingService = accountingService;
  41. this.costShareLevelService = costShareLevelService;
  42. this.costShareParamService = costShareParamService;
  43. }
  44. /**
  45. * 分页查询责任中心成本对照相关数据
  46. *
  47. * @param current
  48. * @param pageSize
  49. * @param name
  50. * @return
  51. */
  52. @Override
  53. public PageUtils queryList(Integer current, Integer pageSize, String name, Long hospId) {
  54. Page<CostAccountShare> costAccountSharePage = new Page<>(current, pageSize);
  55. Page<CostAccountShare> pages = this.page(costAccountSharePage, new QueryWrapper<CostAccountShare>().lambda()
  56. .eq(!StringUtils.isEmpty(hospId), CostAccountShare::getHospId, hospId)
  57. .like(!StringUtils.isEmpty(name), CostAccountShare::getResponsibilityName, name)
  58. .orderByAsc(CostAccountShare::getShareLevel));
  59. List<CostAccountShare> costAccountShareList = pages.getRecords();
  60. List<CostAccountShareVO> costAccountShareVOList = BeanUtil.convertList(costAccountShareList, CostAccountShareVO.class);
  61. getMessage(hospId, costAccountShareVOList);
  62. //
  63. PageUtils pageUtils = new PageUtils(pages);
  64. pageUtils.setList(costAccountShareVOList);
  65. return pageUtils;
  66. }
  67. private void getMessage(Long hospId, List<CostAccountShareVO> costAccountShareList) {
  68. // 设置责任中心的数据 与 会计科目的数据从对应的id里面获取
  69. List<Responsibility> list = responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId));
  70. List<Accounting> accountingList = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId));
  71. Map<Long, List<Responsibility>> resMap = list.stream().collect(Collectors.groupingBy(Responsibility::getId));
  72. Map<Long, List<Accounting>> accountMap = accountingList.stream().collect(Collectors.groupingBy(Accounting::getId));
  73. costAccountShareList.forEach(i->{
  74. Long id = i.getResponsibilityId();
  75. List<Responsibility> responsibilities = resMap.get(id);
  76. if (!CollectionUtils.isEmpty(responsibilities)){
  77. i.setResponsibilityId(id);
  78. i.setResponsibilityName(responsibilities.get(0).getResponsibilityName());
  79. i.setResponsibilityCode(responsibilities.get(0).getResponsibilityCode());
  80. }
  81. Long accountingId = i.getAccountingId();
  82. if (accountingId>0){
  83. List<Accounting> accountingList1 = accountMap.get(accountingId);
  84. if (!CollectionUtils.isEmpty(accountingList1)){
  85. i.setAccountingId(accountingId);
  86. i.setAccountingName(accountingList1.get(0).getAccountingName());
  87. i.setAccountingCode(accountingList1.get(0).getAccountingCode());
  88. i.setAllParentIds(accountingList1.get(0).getAllParentIds());
  89. }
  90. }
  91. });
  92. }
  93. /**
  94. * 保存成本参数对应成本对照表
  95. *
  96. * @param costAccountShareSaveDto
  97. */
  98. @Override
  99. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  100. public void addCostAccountShare(CostAccountShareSaveDto costAccountShareSaveDto) {
  101. User user = (User) SecurityUtils.getSubject().getPrincipal();
  102. Long hospId = user.getHospId();
  103. // 检验输入的数据的合理性
  104. checkAccountShare(costAccountShareSaveDto, hospId);
  105. // 检验输入的责任中心是否存在
  106. List<CostAccountShare> costAccountShareList = baseMapper.selectList(new QueryWrapper<CostAccountShare>().lambda()
  107. .eq(CostAccountShare::getHospId, hospId));
  108. Map<Long, CostAccountShare> map = costAccountShareList.stream().collect(Collectors.toMap(CostAccountShare::getResponsibilityId,synOe->synOe));
  109. if (Objects.nonNull(map.get(costAccountShareSaveDto.getResponsibilityId()))){
  110. throw new CostException(500,"添加的责任中心已存在");
  111. }
  112. CostAccountShare costAccountShareRequest = BeanUtil.convertObj(costAccountShareSaveDto, CostAccountShare.class);
  113. costAccountShareRequest.setHospId(hospId);
  114. costAccountShareRequest.setCreateTime(System.currentTimeMillis());
  115. baseMapper.insert(costAccountShareRequest);
  116. }
  117. /**
  118. * 检验输入数据的合理性
  119. * @param costAccountShareSaveDto
  120. * @param hospId
  121. */
  122. private void checkAccountShare(CostAccountShareSaveDto costAccountShareSaveDto, Long hospId) {
  123. Long responsibilityId = costAccountShareSaveDto.getResponsibilityId();
  124. Responsibility responsibility = responsibilityService.getOne(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId,hospId).eq(Responsibility::getId, responsibilityId));
  125. if (Objects.isNull(responsibility)){
  126. throw new CostException(500,"输入的责任不存在");
  127. }
  128. costAccountShareSaveDto.setResponsibilityId(responsibilityId);
  129. costAccountShareSaveDto.setResponsibilityCode(responsibility.getResponsibilityCode());
  130. costAccountShareSaveDto.setResponsibilityName(responsibility.getResponsibilityName());
  131. costAccountShareSaveDto.setShareLevel(responsibility.getShareLevel());
  132. if (!StringUtils.isEmpty(costAccountShareSaveDto.getAccountingId()) && costAccountShareSaveDto.getAccountingId() > 0){
  133. // 如果输入成本科目的情况下
  134. Accounting accounting = accountingService.getOne(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId).eq(Accounting::getId, costAccountShareSaveDto.getAccountingId()));
  135. if (Objects.isNull(accounting)){
  136. throw new CostException(500,"输入的成本科目不存在");
  137. }else {
  138. costAccountShareSaveDto.setAccountingId(accounting.getId());
  139. costAccountShareSaveDto.setAccountingName(accounting.getAccountingName());
  140. costAccountShareSaveDto.setAccountingCode(accounting.getAccountingCode());
  141. }
  142. // 检验输入的责任中心与匹配的成本科目是否存在
  143. List<CostAccountShare> costAccountShareList = baseMapper.selectList(new QueryWrapper<CostAccountShare>().lambda().eq(CostAccountShare::getHospId,hospId));
  144. Map<String, List<CostAccountShare>> costAccountMap = costAccountShareList.stream().collect(Collectors.groupingBy(CostAccountShare::getResponsibilityCode));
  145. List<CostAccountShare> list = costAccountMap.get(costAccountShareSaveDto.getResponsibilityCode());
  146. if (!CollectionUtils.isEmpty(list)){
  147. String accountingCode = list.get(0).getAccountingCode();
  148. if (accountingCode.equals(costAccountShareSaveDto.getAccountingCode())){
  149. throw new CostException(500,"输入的责任中心对应的成本科目已存在");
  150. }
  151. }
  152. }
  153. // 检验输入的这个责任中心是否允许输入成本科目
  154. Long shareId = responsibility.getShareId();
  155. CostShareLevel costShareLevel = costShareLevelService.getOne(new QueryWrapper<CostShareLevel>().lambda()
  156. .eq(CostShareLevel::getHospId, hospId).eq(CostShareLevel::getId, shareId));
  157. if (Objects.nonNull(costShareLevel)){
  158. if ((!StringUtils.isEmpty(costAccountShareSaveDto.getAccountingId()) && costAccountShareSaveDto.getAccountingId() > 0) && NumberConstant.ZERO.equals(costShareLevel.getCalcType())){
  159. throw new CostException(500,"合并计算不允许选择成本科目");
  160. }
  161. if ((!StringUtils.isEmpty(costAccountShareSaveDto.getAccountingId()) && costAccountShareSaveDto.getAccountingId() <= 0) && NumberConstant.ONE.equals(costShareLevel.getCalcType())){
  162. throw new CostException(500,"分开计算的责任中心需要选择成本科目");
  163. }
  164. }else {
  165. throw new CostException(500,"对不起该责任中心没有对应分摊层级");
  166. }
  167. }
  168. /**
  169. * 修改成本中心责任对照表
  170. *
  171. * @param costAccountShareEditDto
  172. */
  173. @Override
  174. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  175. public void updateByCostAccountShare(CostAccountShareEditDto costAccountShareEditDto) {
  176. User user = (User) SecurityUtils.getSubject().getPrincipal();
  177. Long hospId = user.getHospId();
  178. Long id = costAccountShareEditDto.getId();
  179. CostAccountShare costAccountShare = baseMapper.selectById(id);
  180. if (Objects.isNull(costAccountShare)){
  181. throw new CostException(500,"责任中心成本数据不存在");
  182. }
  183. baseMapper.deleteById(id);
  184. // 新增责任中心成本对照数据
  185. CostAccountShareSaveDto costAccountShareSaveDto = BeanUtil.convertObj(costAccountShareEditDto, CostAccountShareSaveDto.class);
  186. // 检验输入的数据是否符合规则
  187. checkAccountShare(costAccountShareSaveDto,hospId);
  188. CostAccountShareEditDto accountShareEditDto = BeanUtil.convertObj(costAccountShareSaveDto, CostAccountShareEditDto.class);
  189. CostAccountShare costAccountShareRequest = BeanUtil.convertObj(accountShareEditDto, CostAccountShare.class);
  190. costAccountShareRequest.setId(null);
  191. costAccountShareRequest.setHospId(hospId);
  192. costAccountShareRequest.setParamList(costAccountShare.getParamList());
  193. costAccountShareRequest.setCreateTime(System.currentTimeMillis());
  194. baseMapper.insert(costAccountShareRequest);
  195. }
  196. /**
  197. * 修改成本分摊参数的设置
  198. *
  199. * @param shareParamEditDto
  200. */
  201. @Override
  202. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  203. public void updateShareParam(ShareParamEditDto shareParamEditDto) {
  204. List<ShareParamProportionVO> shareParamProportionVOList1 = shareParamEditDto.getShareParamProportionVOList();
  205. User user = (User) SecurityUtils.getSubject().getPrincipal();
  206. Long hospId = user.getHospId();
  207. Long id = shareParamEditDto.getId();
  208. CostAccountShare costAccountShare = baseMapper.selectOne(new QueryWrapper<CostAccountShare>().lambda()
  209. .eq(CostAccountShare::getHospId, hospId)
  210. .eq(CostAccountShare::getId, id));
  211. if (Objects.isNull(costAccountShare)){
  212. throw new CostException(500,"分摊参数对应数据不存在");
  213. }
  214. if (!CollectionUtils.isEmpty(shareParamProportionVOList1)){
  215. List<ShareParamProportionVO> shareParamProportionVOList = shareParamEditDto.getShareParamProportionVOList();
  216. // 检验输入的成本分摊参数是否存在
  217. List<CostShareParamVO> costShareParamServiceAll = costShareParamService.getAll(hospId);
  218. Map<Long, List<CostShareParamVO>> listMap = costShareParamServiceAll.stream().collect(Collectors.groupingBy(CostShareParamVO::getId));
  219. shareParamProportionVOList.forEach(i->{
  220. if (CollectionUtils.isEmpty(listMap.get(i.getId()))){
  221. throw new CostException(500,"分摊名称:"+i.getShareParamName()+"未找到");
  222. }
  223. });
  224. // 检验输入的数据的和是否是100
  225. AtomicReference<Integer> sum= new AtomicReference<>(0);
  226. shareParamProportionVOList.forEach(i->{
  227. sum.updateAndGet(v -> v + i.getShareParamPopout());
  228. });
  229. int max = Integer.parseInt(sum.toString());
  230. if (!NumberConstant.ONE_HUNDRED.equals(max)){
  231. throw new CostException(500,"分摊比例的和不是100");
  232. }
  233. // 判断添加的分摊是否存在一样的
  234. HashMap<Long, ShareParamProportionVO> hashMap = new HashMap<>();
  235. shareParamProportionVOList.forEach(i->{
  236. Long paramId = i.getId();
  237. if (hashMap.containsKey(paramId)){
  238. throw new CostException(500,"不可以添加相同的分摊参数");
  239. }
  240. hashMap.put(paramId,i);
  241. });
  242. //
  243. String paramList = JsonUtil.toJSONString(shareParamProportionVOList);
  244. costAccountShare.setParamList(paramList);
  245. }else{
  246. costAccountShare.setParamList(null);
  247. }
  248. baseMapper.updateById(costAccountShare);
  249. }
  250. /**
  251. * 获取责任中心成本表的分摊参数的集合
  252. *
  253. * @param id
  254. * @param hospId
  255. * @return
  256. */
  257. @Override
  258. public List<ShareParamProportionVO> selectShareParamById(Integer id, Long hospId) {
  259. CostAccountShare costAccountShare = baseMapper.selectOne(new QueryWrapper<CostAccountShare>().lambda()
  260. .eq(CostAccountShare::getHospId, hospId).eq(CostAccountShare::getId, id));
  261. if (Objects.isNull(costAccountShare)){
  262. throw new CostException(500,"责任中心成本不存在");
  263. }
  264. String paramList = costAccountShare.getParamList();
  265. return JsonUtil.toList(paramList, ShareParamProportionVO.class);
  266. }
  267. /**
  268. * 成本分摊参数中被分摊参数对应选中的状态
  269. *
  270. * @param id
  271. * @param hospId
  272. * @return
  273. */
  274. @Override
  275. public List<CostShareParamStatusVO> getAllShareParamStatusById(Integer id, Long hospId) {
  276. List<CostShareParamVO> costShareParamServiceAll = costShareParamService.getAll(hospId);
  277. CostAccountShare costAccountShare = baseMapper.selectById(id);
  278. if (Objects.isNull(costAccountShare)){
  279. throw new CostException(500,"成本分摊对应数据不存在");
  280. }
  281. String paramList = costAccountShare.getParamList();
  282. List<CostShareParamStatusVO> costShareParamStatusVOList = BeanUtil.convertList(costShareParamServiceAll, CostShareParamStatusVO.class);
  283. if (!StringUtils.isEmpty(paramList)) {
  284. List<ShareParamProportionVO> shareParamProportionVOList = JsonUtil.toList(paramList, ShareParamProportionVO.class);
  285. Map<Long, List<ShareParamProportionVO>> map = shareParamProportionVOList.stream().collect(Collectors.groupingBy(ShareParamProportionVO::getId));
  286. costShareParamStatusVOList.forEach(i -> {
  287. Long paramId = i.getId();
  288. if (!CollectionUtils.isEmpty(map.get(paramId))) {
  289. i.setShareParamStatus(NumberConstant.ONE);
  290. }
  291. });
  292. }
  293. return costShareParamStatusVOList;
  294. }
  295. }