AccountingServiceImpl.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.imed.costaccount.common.exception.CostException;
  7. import com.imed.costaccount.mapper.AccountingMapper;
  8. import com.imed.costaccount.model.Accounting;
  9. import com.imed.costaccount.model.User;
  10. import com.imed.costaccount.model.dto.AccountingSaveDTO;
  11. import com.imed.costaccount.model.vo.AccountVO;
  12. import com.imed.costaccount.service.AccountingService;
  13. import com.imed.costaccount.utils.BeanUtil;
  14. import org.apache.commons.beanutils.BeanUtilsBean;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Propagation;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.Objects;
  22. import java.util.stream.Collectors;
  23. @Service("accountingService")
  24. public class AccountingServiceImpl extends ServiceImpl<AccountingMapper, Accounting> implements AccountingService {
  25. /**
  26. * 获取会计科目列表按收入支出类型
  27. *
  28. * @param accountType 会计科目类型1.收入,2.支出
  29. * @param user
  30. * @return
  31. */
  32. @Override
  33. public List<AccountVO> getListByAccountType(Integer accountType, User user) {
  34. // 1. 得到所有的会计科目
  35. List<Accounting> list = this.list(
  36. new LambdaQueryWrapper<Accounting>()
  37. .eq(Accounting::getHospId, user.getHospId())
  38. );
  39. if (CollUtil.isEmpty(list)) {
  40. return Collections.emptyList();
  41. }
  42. // 所有的
  43. List<AccountVO> all = list.stream().map(i -> BeanUtil.convertObj(i, AccountVO.class)).collect(Collectors.toList());
  44. // 顶层的
  45. List<AccountVO> parents = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
  46. List<AccountVO> accountVOS = new ArrayList<>();
  47. for (AccountVO parent : parents) {
  48. List<AccountVO> accountTree = this.getAccountTree(parent, all);
  49. accountVOS.addAll(accountTree);
  50. }
  51. return accountVOS;
  52. }
  53. /**
  54. * 递归处理
  55. * @param accountVO
  56. * @param list
  57. * @return
  58. */
  59. private List<AccountVO> getAccountTree(AccountVO accountVO, List<AccountVO> list) {
  60. List<AccountVO> accountVOS = new ArrayList<>();
  61. for (AccountVO account : list) {
  62. // 如果是父子关系
  63. if (accountVO.getId().equals(account.getParentId())) {
  64. List<AccountVO> child = accountVO.getChild();
  65. if (CollUtil.isEmpty(child)) {
  66. child = new ArrayList<>();
  67. }
  68. child.add(account);
  69. accountVO.setChild(child);
  70. // 处理子节点
  71. this.getAccountTree(account, list);
  72. accountVOS.add(accountVO);
  73. }
  74. }
  75. return accountVOS;
  76. }
  77. /**
  78. * 保存会计科目
  79. *
  80. * @param accountingSaveDTO
  81. * @param user
  82. */
  83. @Override
  84. @Transactional(propagation = Propagation.REQUIRED)
  85. public void saveAccounting(AccountingSaveDTO accountingSaveDTO, User user) {
  86. // 校验会计科目代码
  87. this.checkAccountingCode(accountingSaveDTO.getAccountingCode(), user.getHospId());
  88. // 新增逻辑
  89. Integer parentId = accountingSaveDTO.getId();
  90. // 不是顶层的
  91. String allParentIds = "0";
  92. if (parentId != 0) {
  93. Accounting byId = this.getById(parentId);
  94. if (Objects.isNull(byId)) {
  95. throw new CostException(500, "上层级别会计科目已被移除");
  96. }
  97. String oldParentIds = byId.getAllParentIds();
  98. if ("0".equals(oldParentIds)) {
  99. allParentIds = parentId + "";
  100. } else {
  101. allParentIds = oldParentIds + "-" + parentId;
  102. }
  103. }
  104. Accounting accounting = BeanUtil.convertObj(accountingSaveDTO, Accounting.class);
  105. accounting.setHospId(user.getHospId());
  106. accounting.setParentId(parentId);
  107. accounting.setAllParentIds(allParentIds);
  108. accounting.setCreateTime(System.currentTimeMillis());
  109. this.save(accounting);
  110. }
  111. private void checkAccountingCode(String code, Integer hospId) {
  112. List<Accounting> list = this.baseMapper.selectList(
  113. new QueryWrapper<Accounting>().lambda().select(Accounting::getId)
  114. .eq(Accounting::getAccountingCode, code)
  115. .eq(Accounting::getHospId, hospId)
  116. );
  117. if (CollUtil.isNotEmpty(list)) {
  118. throw new CostException(500, "会计科目代码已存在,请重新生成");
  119. }
  120. }
  121. }