AccountingServiceImpl.java 13 KB

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