2
0

CostAccountShareServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 page
  47. * @param pageSize
  48. * @param name
  49. * @return
  50. */
  51. @Override
  52. public PageUtils queryList(Integer page, Integer pageSize, String name, Integer hospId) {
  53. Page<CostAccountShare> costAccountSharePage = new Page<>(page, 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(Integer 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<Integer, List<Responsibility>> resMap = list.stream().collect(Collectors.groupingBy(Responsibility::getId));
  71. Map<Integer, 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. Integer hospId = user.getHospId();
  102. // 检验输入的数据的合理性
  103. checkAccountShare(costAccountShareSaveDto, hospId);
  104. CostAccountShare costAccountShareRequest = BeanUtil.convertObj(costAccountShareSaveDto, CostAccountShare.class);
  105. costAccountShareRequest.setHospId(hospId);
  106. costAccountShareRequest.setCreateTime(System.currentTimeMillis());
  107. baseMapper.insert(costAccountShareRequest);
  108. }
  109. /**
  110. * 检验输入数据的合理性
  111. * @param costAccountShareSaveDto
  112. * @param hospId
  113. */
  114. private void checkAccountShare(CostAccountShareSaveDto costAccountShareSaveDto, Integer hospId) {
  115. Integer responsibilityId = costAccountShareSaveDto.getResponsibilityId();
  116. Responsibility responsibility = responsibilityService.getOne(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId,hospId).eq(Responsibility::getId, responsibilityId));
  117. if (Objects.isNull(responsibility)){
  118. throw new CostException(500,"输入的责任不存在");
  119. }
  120. costAccountShareSaveDto.setResponsibilityId(responsibilityId);
  121. costAccountShareSaveDto.setResponsibilityCode(responsibility.getResponsibilityCode());
  122. costAccountShareSaveDto.setResponsibilityName(responsibility.getResponsibilityName());
  123. costAccountShareSaveDto.setShareLevel(responsibility.getShareLevel());
  124. if (costAccountShareSaveDto.getAccountingId() > 0){
  125. // 如果输入成本科目的情况下
  126. Accounting accounting = accountingService.getOne(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId).eq(Accounting::getId, costAccountShareSaveDto.getAccountingId()));
  127. if (Objects.isNull(accounting)){
  128. throw new CostException(500,"输入的成本科目不存在");
  129. }else {
  130. costAccountShareSaveDto.setAccountingId(accounting.getId());
  131. costAccountShareSaveDto.setAccountingName(accounting.getAccountingName());
  132. costAccountShareSaveDto.setAccountingCode(accounting.getAccountingCode());
  133. }
  134. // 检验输入的责任中心与匹配的成本科目是否存在
  135. List<CostAccountShare> costAccountShareList = baseMapper.selectList(new QueryWrapper<CostAccountShare>().lambda().eq(CostAccountShare::getHospId,hospId));
  136. Map<String, List<CostAccountShare>> costAccountMap = costAccountShareList.stream().collect(Collectors.groupingBy(CostAccountShare::getResponsibilityCode));
  137. List<CostAccountShare> list = costAccountMap.get(costAccountShareSaveDto.getResponsibilityCode());
  138. if (!CollectionUtils.isEmpty(list)){
  139. String accountingCode = list.get(0).getAccountingCode();
  140. if (accountingCode.equals(costAccountShareSaveDto.getAccountingCode())){
  141. throw new CostException(500,"输入的责任中心对应的成本科目已存在");
  142. }
  143. }
  144. }
  145. // 检验输入的这个责任中心是否允许输入成本科目
  146. Integer shareLevel = responsibility.getShareLevel();
  147. CostShareLevel costShareLevel = costShareLevelService.getOne(new QueryWrapper<CostShareLevel>().lambda()
  148. .eq(CostShareLevel::getHospId, hospId).eq(CostShareLevel::getId, shareLevel));
  149. if (Objects.nonNull(costShareLevel)){
  150. if (costAccountShareSaveDto.getAccountingId()>0 && NumberConstant.ZERO.equals(costShareLevel.getCalcType())){
  151. throw new CostException(500,"合并计算不允许选择成本科目");
  152. }
  153. if (costAccountShareSaveDto.getAccountingId()<= 0 && NumberConstant.ONE.equals(costShareLevel.getCalcType())){
  154. throw new CostException(500,"分开计算的责任中心需要选择成本科目");
  155. }
  156. }else {
  157. throw new CostException(500,"对不起该责任中心没有对应分摊层级");
  158. }
  159. }
  160. /**
  161. * 修改成本中心责任对照表
  162. *
  163. * @param costAccountShareEditDto
  164. */
  165. @Override
  166. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  167. public void updateByCostAccountShare(CostAccountShareEditDto costAccountShareEditDto) {
  168. User user = (User) SecurityUtils.getSubject().getPrincipal();
  169. Integer hospId = user.getHospId();
  170. Integer id = costAccountShareEditDto.getId();
  171. CostAccountShare costAccountShare = baseMapper.selectById(id);
  172. if (Objects.isNull(costAccountShare)){
  173. throw new CostException(500,"责任中心成本数据不存在");
  174. }
  175. baseMapper.deleteById(id);
  176. // 新增责任中心成本对照数据
  177. CostAccountShareSaveDto costAccountShareSaveDto = BeanUtil.convertObj(costAccountShareEditDto, CostAccountShareSaveDto.class);
  178. // 检验输入的数据是否符合规则
  179. checkAccountShare(costAccountShareSaveDto,hospId);
  180. CostAccountShare costAccountShareRequest = BeanUtil.convertObj(costAccountShareEditDto, CostAccountShare.class);
  181. costAccountShareRequest.setId(null);
  182. costAccountShareRequest.setHospId(hospId);
  183. costAccountShareRequest.setParamList(costAccountShare.getParamList());
  184. costAccountShareRequest.setCreateTime(System.currentTimeMillis());
  185. baseMapper.insert(costAccountShareRequest);
  186. }
  187. /**
  188. * 修改成本分摊参数的设置
  189. *
  190. * @param shareParamEditDto
  191. */
  192. @Override
  193. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  194. public void updateShareParam(ShareParamEditDto shareParamEditDto) {
  195. User user = (User) SecurityUtils.getSubject().getPrincipal();
  196. Integer hospId = user.getHospId();
  197. Integer id = shareParamEditDto.getId();
  198. CostAccountShare costAccountShare = baseMapper.selectOne(new QueryWrapper<CostAccountShare>().lambda()
  199. .eq(CostAccountShare::getHospId, hospId)
  200. .eq(CostAccountShare::getId, id));
  201. if (Objects.isNull(costAccountShare)){
  202. throw new CostException(500,"责任中心成本数据不存在");
  203. }
  204. List<ShareParamProportionVO> shareParamProportionVOList = shareParamEditDto.getShareParamProportionVOList();
  205. // 检验输入的成本分摊参数是否存在
  206. List<CostShareParamVO> costShareParamServiceAll = costShareParamService.getAll(hospId);
  207. Map<Integer, List<CostShareParamVO>> listMap = costShareParamServiceAll.stream().collect(Collectors.groupingBy(CostShareParamVO::getId));
  208. shareParamProportionVOList.forEach(i->{
  209. if (CollectionUtils.isEmpty(listMap.get(i.getId()))){
  210. throw new CostException(500,"分摊名称:"+i.getShareParamName()+"未找到");
  211. }
  212. });
  213. // 检验输入的数据的和是否是100
  214. AtomicReference<Integer> sum= new AtomicReference<>(0);
  215. shareParamProportionVOList.forEach(i->{
  216. sum.updateAndGet(v -> v + i.getShareParamPopout());
  217. });
  218. int max = Integer.parseInt(sum.toString());
  219. if (!NumberConstant.ONE_HUNDRED.equals(max)){
  220. throw new CostException(500,"分摊比例的和不是100");
  221. }
  222. // 判断添加的分摊是否存在一样的
  223. HashMap<Integer, ShareParamProportionVO> hashMap = new HashMap<>();
  224. shareParamProportionVOList.forEach(i->{
  225. Integer paramId = i.getId();
  226. if (hashMap.containsKey(paramId)){
  227. throw new CostException(500,"不可以添加相同的分摊参数");
  228. }
  229. hashMap.put(paramId,i);
  230. });
  231. // TODO 暂时还考虑不到如何筛选选中的
  232. String paramList = JsonUtil.toJSONString(shareParamProportionVOList);
  233. costAccountShare.setParamList(paramList);
  234. baseMapper.updateById(costAccountShare);
  235. }
  236. /**
  237. * 获取责任中心成本表的分摊参数的集合
  238. *
  239. * @param id
  240. * @param hospId
  241. * @return
  242. */
  243. @Override
  244. public List<ShareParamProportionVO> selectShareParamById(Integer id, Integer hospId) {
  245. CostAccountShare costAccountShare = baseMapper.selectOne(new QueryWrapper<CostAccountShare>().lambda()
  246. .eq(CostAccountShare::getHospId, hospId).eq(CostAccountShare::getId, id));
  247. if (Objects.isNull(costAccountShare)){
  248. throw new CostException(500,"责任中心成本不存在");
  249. }
  250. String paramList = costAccountShare.getParamList();
  251. return JsonUtil.toList(paramList, ShareParamProportionVO.class);
  252. }
  253. /**
  254. * 成本分摊参数中被分摊参数对应选中的状态
  255. *
  256. * @param id
  257. * @param hospId
  258. * @return
  259. */
  260. @Override
  261. public List<CostShareParamStatusVO> getAllShareParamStatusById(Integer id, Integer hospId) {
  262. List<CostShareParamVO> costShareParamServiceAll = costShareParamService.getAll(hospId);
  263. CostAccountShare costAccountShare = baseMapper.selectById(id);
  264. if (Objects.isNull(costAccountShare)){
  265. throw new CostException(500,"成本分摊对应数据不存在");
  266. }
  267. String paramList = costAccountShare.getParamList();
  268. List<CostShareParamStatusVO> costShareParamStatusVOList = BeanUtil.convertList(costShareParamServiceAll, CostShareParamStatusVO.class);
  269. if (!StringUtils.isEmpty(paramList)) {
  270. List<ShareParamProportionVO> shareParamProportionVOList = JsonUtil.toList(paramList, ShareParamProportionVO.class);
  271. Map<Integer, List<ShareParamProportionVO>> map = shareParamProportionVOList.stream().collect(Collectors.groupingBy(ShareParamProportionVO::getId));
  272. costShareParamStatusVOList.forEach(i -> {
  273. Integer paramId = i.getId();
  274. if (!CollectionUtils.isEmpty(map.get(paramId))) {
  275. i.setShareParamStatus(NumberConstant.ONE);
  276. }
  277. });
  278. }
  279. return costShareParamStatusVOList;
  280. }
  281. }