2
0

AccountingServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Propagation;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import java.util.*;
  21. import java.util.stream.Collectors;
  22. @Slf4j
  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. accountVOS.add(accountVO);
  62. for (AccountVO account : list) {
  63. // 如果是父子关系
  64. if (accountVO.getId().equals(account.getParentId())) {
  65. List<AccountVO> child = accountVO.getChildren();
  66. if (CollUtil.isEmpty(child)) {
  67. child = new ArrayList<>();
  68. }
  69. child.add(account);
  70. accountVO.setChildren(child);
  71. // 处理子节点
  72. this.getAccountTree(account, list);
  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. /**
  122. * 选择会计科目列表
  123. *
  124. * @param user
  125. * @return
  126. */
  127. @Override
  128. public List<SelectAccountingVO> selectAccounting(User user) {
  129. List<Accounting> list = this.list(
  130. new LambdaQueryWrapper<Accounting>().select(Accounting::getId,Accounting::getAccountingName,Accounting::getParentId,Accounting::getAllParentIds)
  131. .eq(Accounting::getHospId, user.getHospId())
  132. );
  133. if (CollUtil.isEmpty(list)) {
  134. return Collections.emptyList();
  135. }
  136. // 所有的
  137. List<SelectAccountingVO> all = list.stream().map(i ->{
  138. SelectAccountingVO vo = new SelectAccountingVO();
  139. vo.setValue(i.getId());
  140. vo.setLabel(i.getAccountingName());
  141. vo.setChildren(null);
  142. vo.setParentId(i.getParentId());
  143. vo.setAllParentIds(i.getAllParentIds());
  144. return vo;
  145. }).collect(Collectors.toList());
  146. // 顶层的
  147. List<SelectAccountingVO> parents = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
  148. List<SelectAccountingVO> accountVOS = new ArrayList<>();
  149. for (SelectAccountingVO parent : parents) {
  150. List<SelectAccountingVO> accountTree = this.getSelectAccountTree(parent, all);
  151. accountVOS.addAll(accountTree);
  152. }
  153. return accountVOS;
  154. }
  155. private List<SelectAccountingVO> getSelectAccountTree(SelectAccountingVO parent, List<SelectAccountingVO> all) {
  156. List<SelectAccountingVO> accountVOS = new ArrayList<>();
  157. accountVOS.add(parent);
  158. for (SelectAccountingVO account : all) {
  159. // 如果是父子关系
  160. if (parent.getValue().equals(account.getParentId())) {
  161. List<SelectAccountingVO> child = parent.getChildren();
  162. if (CollUtil.isEmpty(child)) {
  163. child = new ArrayList<>();
  164. }
  165. child.add(account);
  166. parent.setChildren(child);
  167. // 处理子节点
  168. this.getSelectAccountTree(account, all);
  169. }
  170. }
  171. return accountVOS;
  172. }
  173. /**
  174. * 编辑科目代码
  175. *
  176. * @param accountingEditDTO
  177. * @param user
  178. */
  179. @Override
  180. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  181. public void updateAccount(AccountingEditDTO accountingEditDTO, User user) {
  182. // TODO: 2021/7/28 追踪溯源需求不满足
  183. Integer id = accountingEditDTO.getId();
  184. // this.checkAccountingCode(accountingEditDTO.getAccountingCode(), user.getHospId());
  185. Accounting one = this.baseMapper.selectOne(
  186. new QueryWrapper<Accounting>().lambda()
  187. .eq(Accounting::getAccountingCode, accountingEditDTO.getAccountingCode())
  188. .eq(Accounting::getHospId, user.getHospId())
  189. .last("limit 1")
  190. );
  191. Accounting byId = this.getById(id);
  192. if (Objects.isNull(byId)) {
  193. throw new CostException(500, "当前选中会计科目已被移除");
  194. }
  195. if (Objects.nonNull(one) && !byId.getAccountingCode().equals(one.getAccountingCode())) {
  196. throw new CostException(500, "会计科目代码已存在,请重新生成");
  197. }
  198. // 直接修改
  199. byId.setAccountingCode(accountingEditDTO.getAccountingCode());
  200. byId.setAccountingName(accountingEditDTO.getAccountingName());
  201. byId.setCreateTime(System.currentTimeMillis());
  202. // byId.setAccountingType(accountingEditDTO.getAccountingType());
  203. this.updateById(byId);
  204. // this.removeById(id);
  205. // Accounting accounting = BeanUtil.convertObj(byId, Accounting.class);
  206. // saveAccount(accountingEditDTO, user, byId, accounting);
  207. // List<Accounting> list = new ArrayList<>();
  208. // this.getAndAllChild(Arrays.asList(id), user.getHospId(), list);
  209. // log.info("list:{}", list);
  210. // if (CollUtil.isEmpty(list)) {
  211. // return;
  212. // }
  213. // // 第一个子节点
  214. // List<Accounting> childList = list.stream().filter(i -> i.getParentId().equals(id)).collect(Collectors.toList());
  215. // if (CollUtil.isEmpty(childList)) {
  216. // throw new CostException(500, "数据异常");
  217. // }
  218. // childList.forEach(i -> {
  219. // i.setParentId(accounting.getId());
  220. // String allParentIds = setAllParentIds(byId);
  221. // i.setAllParentIds(allParentIds);
  222. // });
  223. // this.updateBatchById(childList);
  224. //
  225. //
  226. }
  227. // private String setAllParentIds(Accounting byId) {
  228. // Integer parentId = byId.getParentId();
  229. // // 不是顶层的
  230. // String allParentIds = "0";
  231. // if (parentId != 0) {
  232. // String oldParentIds = byId.getAllParentIds();
  233. // if ("0".equals(oldParentIds)) {
  234. // allParentIds = parentId + "";
  235. // } else {
  236. // allParentIds = oldParentIds + "-" + parentId;
  237. // }
  238. // }
  239. // return allParentIds;
  240. // }
  241. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  242. public void saveAccount(AccountingEditDTO accountingEditDTO, User user, Accounting byId, Accounting accounting) {
  243. this.checkAccountingCode(accountingEditDTO.getAccountingCode(), user.getHospId());
  244. accounting.setAccountingCode(accounting.getAccountingCode());
  245. accounting.setCreateTime(System.currentTimeMillis());
  246. // 新增逻辑
  247. Integer parentId = byId.getParentId();
  248. // 不是顶层的
  249. String allParentIds = "0";
  250. if (parentId != 0) {
  251. String oldParentIds = byId.getAllParentIds();
  252. if ("0".equals(oldParentIds)) {
  253. allParentIds = parentId + "";
  254. } else {
  255. allParentIds = oldParentIds + "-" + parentId;
  256. }
  257. }
  258. accounting.setHospId(user.getHospId());
  259. accounting.setParentId(parentId);
  260. accounting.setAllParentIds(allParentIds);
  261. accounting.setCreateTime(System.currentTimeMillis());
  262. this.save(accounting);
  263. }
  264. /**
  265. * 得到自己还有所有的子节点
  266. * @param ids
  267. * @param accounts
  268. * @return
  269. */
  270. private List<Accounting> getAndAllChild(List<Integer> ids, Integer hospId, List<Accounting> accounts) {
  271. List<Accounting> list = this.list(
  272. new LambdaQueryWrapper<Accounting>()
  273. .in(Accounting::getParentId, ids).eq(Accounting::getHospId,hospId)
  274. );
  275. if (CollUtil.isEmpty(list)) {
  276. return accounts;
  277. }
  278. accounts.addAll(list);
  279. this.getAndAllChild(list.stream().map(Accounting::getId).collect(Collectors.toList()), hospId, accounts);
  280. return accounts;
  281. }
  282. /**
  283. * 删除
  284. *
  285. * @param id
  286. * @param user
  287. */
  288. @Override
  289. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  290. public void deleteAccount(Integer id, User user) {
  291. List<Accounting> list = new ArrayList<>();
  292. List<Accounting> andAllChild = this.getAndAllChild(Arrays.asList(id), user.getHospId(), list);
  293. if (CollUtil.isEmpty(andAllChild)) {
  294. this.removeById(id);
  295. }
  296. List<Integer> collect = andAllChild.stream().map(Accounting::getId).collect(Collectors.toList());
  297. collect.add(id);
  298. this.removeByIds(collect);
  299. }
  300. }