AccountingServiceImpl.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.AccountingEditDTO;
  11. import com.imed.costaccount.model.dto.AccountingSaveDTO;
  12. import com.imed.costaccount.model.vo.AccountVO;
  13. import com.imed.costaccount.model.vo.SelectAccountingVO;
  14. import com.imed.costaccount.service.AccountingService;
  15. import com.imed.costaccount.utils.BeanUtil;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.beanutils.BeanUtilsBean;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Propagation;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import java.util.*;
  22. import java.util.stream.Collectors;
  23. @Slf4j
  24. @Service("accountingService")
  25. public class AccountingServiceImpl extends ServiceImpl<AccountingMapper, Accounting> implements AccountingService {
  26. /**
  27. * 获取会计科目列表按收入支出类型
  28. *
  29. * @param accountType 会计科目类型1.收入,2.支出
  30. * @param user
  31. * @return
  32. */
  33. @Override
  34. public List<AccountVO> getListByAccountType(Integer accountType, User user) {
  35. // 1. 得到所有的会计科目
  36. List<Accounting> list = this.list(
  37. new LambdaQueryWrapper<Accounting>()
  38. .eq(Accounting::getHospId, user.getHospId())
  39. );
  40. if (CollUtil.isEmpty(list)) {
  41. return Collections.emptyList();
  42. }
  43. // 所有的
  44. List<AccountVO> all = list.stream().map(i -> BeanUtil.convertObj(i, AccountVO.class)).collect(Collectors.toList());
  45. // 顶层的
  46. List<AccountVO> parents = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
  47. List<AccountVO> accountVOS = new ArrayList<>();
  48. for (AccountVO parent : parents) {
  49. List<AccountVO> accountTree = this.getAccountTree(parent, all);
  50. accountVOS.addAll(accountTree);
  51. }
  52. return accountVOS;
  53. }
  54. /**
  55. * 递归处理
  56. * @param accountVO
  57. * @param list
  58. * @return
  59. */
  60. private List<AccountVO> getAccountTree(AccountVO accountVO, List<AccountVO> list) {
  61. List<AccountVO> accountVOS = new ArrayList<>();
  62. for (AccountVO account : list) {
  63. // 如果是父子关系
  64. if (accountVO.getId().equals(account.getParentId())) {
  65. List<AccountVO> child = accountVO.getChild();
  66. if (CollUtil.isEmpty(child)) {
  67. child = new ArrayList<>();
  68. }
  69. child.add(account);
  70. accountVO.setChild(child);
  71. // 处理子节点
  72. this.getAccountTree(account, list);
  73. accountVOS.add(accountVO);
  74. }
  75. }
  76. return accountVOS;
  77. }
  78. /**
  79. * 保存会计科目
  80. *
  81. * @param accountingSaveDTO
  82. * @param user
  83. */
  84. @Override
  85. @Transactional(propagation = Propagation.REQUIRED)
  86. public void saveAccounting(AccountingSaveDTO accountingSaveDTO, User user) {
  87. // 校验会计科目代码
  88. this.checkAccountingCode(accountingSaveDTO.getAccountingCode(), user.getHospId());
  89. // 新增逻辑
  90. Integer parentId = accountingSaveDTO.getId();
  91. // 不是顶层的
  92. String allParentIds = "0";
  93. if (parentId != 0) {
  94. Accounting byId = this.getById(parentId);
  95. if (Objects.isNull(byId)) {
  96. throw new CostException(500, "上层级别会计科目已被移除");
  97. }
  98. String oldParentIds = byId.getAllParentIds();
  99. if ("0".equals(oldParentIds)) {
  100. allParentIds = parentId + "";
  101. } else {
  102. allParentIds = oldParentIds + "-" + parentId;
  103. }
  104. }
  105. Accounting accounting = BeanUtil.convertObj(accountingSaveDTO, Accounting.class);
  106. accounting.setHospId(user.getHospId());
  107. accounting.setParentId(parentId);
  108. accounting.setAllParentIds(allParentIds);
  109. accounting.setCreateTime(System.currentTimeMillis());
  110. this.save(accounting);
  111. }
  112. private void checkAccountingCode(String code, Integer hospId) {
  113. List<Accounting> list = this.baseMapper.selectList(
  114. new QueryWrapper<Accounting>().lambda().select(Accounting::getId)
  115. .eq(Accounting::getAccountingCode, code)
  116. .eq(Accounting::getHospId, hospId)
  117. );
  118. if (CollUtil.isNotEmpty(list)) {
  119. throw new CostException(500, "会计科目代码已存在,请重新生成");
  120. }
  121. }
  122. /**
  123. * 选择会计科目列表
  124. *
  125. * @param user
  126. * @return
  127. */
  128. @Override
  129. public List<SelectAccountingVO> selectAccounting(User user) {
  130. List<Accounting> list = this.list(
  131. new LambdaQueryWrapper<Accounting>().select(Accounting::getId,Accounting::getAccountingName,Accounting::getParentId,Accounting::getAllParentIds)
  132. .eq(Accounting::getHospId, user.getHospId())
  133. );
  134. if (CollUtil.isEmpty(list)) {
  135. return Collections.emptyList();
  136. }
  137. // 所有的
  138. List<SelectAccountingVO> all = list.stream().map(i -> BeanUtil.convertObj(i, SelectAccountingVO.class)).collect(Collectors.toList());
  139. // 顶层的
  140. List<SelectAccountingVO> parents = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
  141. List<SelectAccountingVO> accountVOS = new ArrayList<>();
  142. for (SelectAccountingVO parent : parents) {
  143. List<SelectAccountingVO> accountTree = this.getSelectAccountTree(parent, all);
  144. accountVOS.addAll(accountTree);
  145. }
  146. return accountVOS;
  147. }
  148. private List<SelectAccountingVO> getSelectAccountTree(SelectAccountingVO parent, List<SelectAccountingVO> all) {
  149. List<SelectAccountingVO> accountVOS = new ArrayList<>();
  150. for (SelectAccountingVO account : all) {
  151. // 如果是父子关系
  152. if (parent.getId().equals(account.getParentId())) {
  153. List<SelectAccountingVO> child = parent.getChild();
  154. if (CollUtil.isEmpty(child)) {
  155. child = new ArrayList<>();
  156. }
  157. child.add(account);
  158. parent.setChild(child);
  159. // 处理子节点
  160. this.getSelectAccountTree(account, all);
  161. accountVOS.add(parent);
  162. }
  163. }
  164. return accountVOS;
  165. }
  166. /**
  167. * 编辑科目代码
  168. *
  169. * @param accountingEditDTO
  170. * @param user
  171. */
  172. @Override
  173. public void updateAccount(AccountingEditDTO accountingEditDTO, User user) {
  174. Integer id = accountingEditDTO.getId();
  175. List<Accounting> accounts = new ArrayList<>();
  176. Accounting byId = this.getById(id);
  177. // TODO: 2021/7/28 校验
  178. accounts.add(byId);
  179. // 如果是顶层的修改
  180. getAndAllChild(Arrays.asList(id), user.getHospId(), accounts);
  181. log.info("得到所有的这个下面的列表:{}", accounts);
  182. Map<Integer, List<Accounting>> map = accounts.stream().collect(Collectors.groupingBy(Accounting::getId));
  183. // 删除所有的列表
  184. // this.remove
  185. }
  186. /**
  187. * 得到自己还有所有的子节点
  188. * @param ids
  189. * @param accounts
  190. * @return
  191. */
  192. private List<Accounting> getAndAllChild(List<Integer> ids, Integer hospId, List<Accounting> accounts) {
  193. List<Accounting> list = this.list(
  194. new LambdaQueryWrapper<Accounting>()
  195. .in(Accounting::getParentId, ids).eq(Accounting::getHospId,hospId)
  196. );
  197. if (CollUtil.isEmpty(list)) {
  198. return accounts;
  199. }
  200. accounts.addAll(list);
  201. this.getAndAllChild(list.stream().map(Accounting::getId).collect(Collectors.toList()), hospId, accounts);
  202. return accounts;
  203. }
  204. }