CostAccountShareServiceImpl.java 16 KB

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