AccountingServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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.service.CostIncomeGroupSetService;
  16. import com.imed.costaccount.service.CostShareParamService;
  17. import com.imed.costaccount.utils.BeanUtil;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Propagation;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import java.util.*;
  23. import java.util.stream.Collectors;
  24. @Slf4j
  25. @Service("accountingService")
  26. public class AccountingServiceImpl extends ServiceImpl<AccountingMapper, Accounting> implements AccountingService {
  27. private final CostShareParamService shareParamService;
  28. private final CostIncomeGroupSetService costIncomeGroupSetService;
  29. public AccountingServiceImpl(CostShareParamService shareParamService, CostIncomeGroupSetService costIncomeGroupSetService) {
  30. this.shareParamService = shareParamService;
  31. this.costIncomeGroupSetService = costIncomeGroupSetService;
  32. }
  33. /**
  34. * 获取会计科目列表按收入支出类型
  35. *
  36. * @param accountType 会计科目类型1.收入,2.支出
  37. * @param user
  38. * @param shareParamId
  39. * @return
  40. */
  41. @Override
  42. public List<AccountVO> getListByAccountType(Integer accountType, User user, Integer shareParamId, Integer incomeGroutSetId) {
  43. // 1. 得到所有的会计科目
  44. List<Accounting> list = this.list(
  45. new LambdaQueryWrapper<Accounting>()
  46. .eq(Accounting::getHospId, user.getHospId())
  47. .eq(Accounting::getAccountingType, accountType)
  48. .orderByDesc(Accounting::getCreateTime)
  49. );
  50. if (CollUtil.isEmpty(list)) {
  51. return Collections.emptyList();
  52. }
  53. // 所有的
  54. // List<AccountVO> all = list.stream().map(i -> BeanUtil.con(i, AccountVO.class)).collect(Collectors.toList());
  55. List<AccountVO> all = BeanUtil.convertList(list, AccountVO.class);
  56. // 查询所有的已绑定的分摊参数
  57. if (Objects.nonNull(shareParamId)) {
  58. List<Integer> ids = shareParamService.selectIsSelect(shareParamId);
  59. if (CollUtil.isNotEmpty(ids)) {
  60. all.forEach(i -> {
  61. if (ids.contains(i.getId())) {
  62. i.setIsShareParamSelect(true);
  63. }
  64. });
  65. }
  66. }
  67. // 查询所有已绑定的 收入归集设置
  68. if (Objects.nonNull(incomeGroutSetId)){
  69. List<String> codes= costIncomeGroupSetService.selectIsSelect(incomeGroutSetId);
  70. if (CollUtil.isNotEmpty(codes)){
  71. all.forEach(i->{
  72. if (codes.contains(i.getAccountingCode())){
  73. i.setIsShareParamSelect(true);
  74. }
  75. });
  76. }
  77. }
  78. // 顶层的
  79. List<AccountVO> parents = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
  80. List<AccountVO> accountVOS = new ArrayList<>();
  81. for (AccountVO parent : parents) {
  82. List<AccountVO> accountTree = this.getAccountTree(parent, all);
  83. accountVOS.addAll(accountTree);
  84. }
  85. return accountVOS;
  86. }
  87. /**
  88. * 递归处理
  89. *
  90. * @param accountVO
  91. * @param list
  92. * @return
  93. */
  94. private List<AccountVO> getAccountTree(AccountVO accountVO, List<AccountVO> list) {
  95. List<AccountVO> accountVOS = new ArrayList<>();
  96. accountVOS.add(accountVO);
  97. for (AccountVO account : list) {
  98. // 如果是父子关系
  99. if (accountVO.getId().equals(account.getParentId())) {
  100. List<AccountVO> child = accountVO.getChildren();
  101. if (CollUtil.isEmpty(child)) {
  102. child = new ArrayList<>();
  103. }
  104. child.add(account);
  105. accountVO.setChildren(child);
  106. // 处理子节点
  107. this.getAccountTree(account, list);
  108. }
  109. }
  110. return accountVOS;
  111. }
  112. /**
  113. * 保存会计科目
  114. *
  115. * @param accountingSaveDTO
  116. * @param user
  117. */
  118. @Override
  119. @Transactional(propagation = Propagation.REQUIRED)
  120. public void saveAccounting(AccountingSaveDTO accountingSaveDTO, User user) {
  121. // 校验会计科目代码
  122. this.checkAccountingCode(accountingSaveDTO.getAccountingCode(), user.getHospId());
  123. // 新增逻辑
  124. Long parentId = accountingSaveDTO.getId();
  125. // 不是顶层的
  126. String allParentIds = "0";
  127. if (parentId != 0) {
  128. Accounting byId = this.getById(parentId);
  129. if (Objects.isNull(byId)) {
  130. throw new CostException(500, "上层级别会计科目已被移除");
  131. }
  132. // 如果父子节点的会计科目类型不一致,退出
  133. if (!byId.getAccountingType().equals(accountingSaveDTO.getAccountingType())) {
  134. throw new CostException(500, "会计科目类型要与上层一致");
  135. }
  136. String oldParentIds = byId.getAllParentIds();
  137. if ("0".equals(oldParentIds)) {
  138. allParentIds = parentId + "";
  139. } else {
  140. allParentIds = oldParentIds + "-" + parentId;
  141. }
  142. }
  143. Accounting accounting = BeanUtil.convertObj(accountingSaveDTO, Accounting.class);
  144. accounting.setHospId(user.getHospId());
  145. accounting.setParentId(parentId);
  146. accounting.setAllParentIds(allParentIds);
  147. accounting.setCreateTime(System.currentTimeMillis());
  148. // 如果是支出类型
  149. if (accountingSaveDTO.getAccountingType().equals(2)) {
  150. Integer isBaseCode = accountingSaveDTO.getIsBaseCode();
  151. if (Objects.isNull(isBaseCode)) {
  152. throw new CostException(500, "支出类型需要选择是否固定支出成本");
  153. }
  154. accounting.setIsBaseCost(isBaseCode);
  155. }
  156. this.save(accounting);
  157. }
  158. private void checkAccountingCode(String code, Long hospId) {
  159. List<Accounting> list = this.baseMapper.selectList(
  160. new QueryWrapper<Accounting>().lambda().select(Accounting::getId)
  161. .eq(Accounting::getAccountingCode, code)
  162. .eq(Accounting::getHospId, hospId)
  163. );
  164. if (CollUtil.isNotEmpty(list)) {
  165. throw new CostException(500, "会计科目代码已存在,请重新生成");
  166. }
  167. }
  168. /**
  169. * 选择会计科目列表
  170. *
  171. * @param user
  172. * @return
  173. */
  174. @Override
  175. public List<SelectAccountingVO> selectAccounting(User user) {
  176. List<Accounting> list = this.list(
  177. new LambdaQueryWrapper<Accounting>().select(Accounting::getId, Accounting::getAccountingName, Accounting::getParentId, Accounting::getAllParentIds)
  178. .eq(Accounting::getHospId, user.getHospId())
  179. );
  180. if (CollUtil.isEmpty(list)) {
  181. return Collections.emptyList();
  182. }
  183. // 所有的
  184. List<SelectAccountingVO> all = list.stream().map(i -> {
  185. SelectAccountingVO vo = new SelectAccountingVO();
  186. vo.setValue(i.getId());
  187. vo.setLabel(i.getAccountingName());
  188. vo.setChildren(null);
  189. vo.setParentId(i.getParentId());
  190. vo.setAllParentIds(i.getAllParentIds());
  191. return vo;
  192. }).collect(Collectors.toList());
  193. // 顶层的
  194. List<SelectAccountingVO> parents = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
  195. List<SelectAccountingVO> accountVOS = new ArrayList<>();
  196. for (SelectAccountingVO parent : parents) {
  197. List<SelectAccountingVO> accountTree = this.getSelectAccountTree(parent, all);
  198. accountVOS.addAll(accountTree);
  199. }
  200. return accountVOS;
  201. }
  202. private List<SelectAccountingVO> getSelectAccountTree(SelectAccountingVO parent, List<SelectAccountingVO> all) {
  203. List<SelectAccountingVO> accountVOS = new ArrayList<>();
  204. accountVOS.add(parent);
  205. for (SelectAccountingVO account : all) {
  206. // 如果是父子关系
  207. if (parent.getValue().equals(account.getParentId())) {
  208. List<SelectAccountingVO> child = parent.getChildren();
  209. if (CollUtil.isEmpty(child)) {
  210. child = new ArrayList<>();
  211. }
  212. child.add(account);
  213. parent.setChildren(child);
  214. // 处理子节点
  215. this.getSelectAccountTree(account, all);
  216. }
  217. }
  218. return accountVOS;
  219. }
  220. /**
  221. * 编辑科目代码
  222. *
  223. * @param accountingEditDTO
  224. * @param user
  225. */
  226. @Override
  227. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  228. public void updateAccount(AccountingEditDTO accountingEditDTO, User user) {
  229. // TODO: 2021/7/28 追踪溯源需求不满足
  230. Long id = accountingEditDTO.getId();
  231. // this.checkAccountingCode(accountingEditDTO.getAccountingCode(), user.getHospId());
  232. Accounting one = this.baseMapper.selectOne(
  233. new QueryWrapper<Accounting>().lambda()
  234. .eq(Accounting::getAccountingCode, accountingEditDTO.getAccountingCode())
  235. .eq(Accounting::getHospId, user.getHospId())
  236. .last("limit 1")
  237. );
  238. Accounting byId = this.getById(id);
  239. if (Objects.isNull(byId)) {
  240. throw new CostException(500, "当前选中会计科目已被移除");
  241. }
  242. if (Objects.nonNull(one) && !byId.getAccountingCode().equals(one.getAccountingCode())) {
  243. throw new CostException(500, "会计科目代码已存在,请重新生成");
  244. }
  245. // 直接修改
  246. byId.setAccountingCode(accountingEditDTO.getAccountingCode());
  247. byId.setAccountingName(accountingEditDTO.getAccountingName());
  248. byId.setCreateTime(System.currentTimeMillis());
  249. // byId.setAccountingType(accountingEditDTO.getAccountingType());
  250. this.updateById(byId);
  251. // this.removeById(id);
  252. // Accounting accounting = BeanUtil.convertObj(byId, Accounting.class);
  253. // saveAccount(accountingEditDTO, user, byId, accounting);
  254. // List<Accounting> list = new ArrayList<>();
  255. // this.getAndAllChild(Arrays.asList(id), user.getHospId(), list);
  256. // log.info("list:{}", list);
  257. // if (CollUtil.isEmpty(list)) {
  258. // return;
  259. // }
  260. // // 第一个子节点
  261. // List<Accounting> childList = list.stream().filter(i -> i.getParentId().equals(id)).collect(Collectors.toList());
  262. // if (CollUtil.isEmpty(childList)) {
  263. // throw new CostException(500, "数据异常");
  264. // }
  265. // childList.forEach(i -> {
  266. // i.setParentId(accounting.getId());
  267. // String allParentIds = setAllParentIds(byId);
  268. // i.setAllParentIds(allParentIds);
  269. // });
  270. // this.updateBatchById(childList);
  271. //
  272. //
  273. }
  274. // private String setAllParentIds(Accounting byId) {
  275. // Integer parentId = byId.getParentId();
  276. // // 不是顶层的
  277. // String allParentIds = "0";
  278. // if (parentId != 0) {
  279. // String oldParentIds = byId.getAllParentIds();
  280. // if ("0".equals(oldParentIds)) {
  281. // allParentIds = parentId + "";
  282. // } else {
  283. // allParentIds = oldParentIds + "-" + parentId;
  284. // }
  285. // }
  286. // return allParentIds;
  287. // }
  288. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  289. public void saveAccount(AccountingEditDTO accountingEditDTO, User user, Accounting byId, Accounting accounting) {
  290. this.checkAccountingCode(accountingEditDTO.getAccountingCode(), user.getHospId());
  291. accounting.setAccountingCode(accounting.getAccountingCode());
  292. accounting.setCreateTime(System.currentTimeMillis());
  293. // 新增逻辑
  294. Long parentId = byId.getParentId();
  295. // 不是顶层的
  296. String allParentIds = "0";
  297. if (parentId != 0) {
  298. String oldParentIds = byId.getAllParentIds();
  299. if ("0".equals(oldParentIds)) {
  300. allParentIds = parentId + "";
  301. } else {
  302. allParentIds = oldParentIds + "-" + parentId;
  303. }
  304. }
  305. accounting.setHospId(user.getHospId());
  306. accounting.setParentId(parentId);
  307. accounting.setAllParentIds(allParentIds);
  308. accounting.setCreateTime(System.currentTimeMillis());
  309. this.save(accounting);
  310. }
  311. /**
  312. * 得到自己还有所有的子节点
  313. *
  314. * @param ids
  315. * @param accounts
  316. * @return
  317. */
  318. private List<Accounting> getAndAllChild(List<Long> ids, Long hospId, List<Accounting> accounts) {
  319. List<Accounting> list = this.list(
  320. new LambdaQueryWrapper<Accounting>()
  321. .in(Accounting::getParentId, ids).eq(Accounting::getHospId, hospId)
  322. );
  323. if (CollUtil.isEmpty(list)) {
  324. return accounts;
  325. }
  326. accounts.addAll(list);
  327. this.getAndAllChild(list.stream().map(Accounting::getId).collect(Collectors.toList()), hospId, accounts);
  328. return accounts;
  329. }
  330. /**
  331. * 删除
  332. *
  333. * @param id
  334. * @param user
  335. */
  336. @Override
  337. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  338. public void deleteAccount(Long id, User user) {
  339. List<Accounting> list = new ArrayList<>();
  340. List<Accounting> andAllChild = this.getAndAllChild(Arrays.asList(id), user.getHospId(), list);
  341. if (CollUtil.isEmpty(andAllChild)) {
  342. this.removeById(id);
  343. }
  344. List<Long> collect = andAllChild.stream().map(Accounting::getId).collect(Collectors.toList());
  345. collect.add(id);
  346. this.removeByIds(collect);
  347. }
  348. }