AccountingServiceImpl.java 13 KB

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