|
@@ -0,0 +1,135 @@
|
|
|
+package com.imed.costaccount.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.imed.costaccount.common.exception.CostException;
|
|
|
+import com.imed.costaccount.mapper.AccountingMapper;
|
|
|
+import com.imed.costaccount.model.Accounting;
|
|
|
+import com.imed.costaccount.model.User;
|
|
|
+import com.imed.costaccount.model.dto.AccountingSaveDTO;
|
|
|
+import com.imed.costaccount.model.vo.AccountVO;
|
|
|
+import com.imed.costaccount.service.AccountingService;
|
|
|
+import com.imed.costaccount.utils.BeanUtil;
|
|
|
+import org.apache.commons.beanutils.BeanUtilsBean;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+@Service("accountingService")
|
|
|
+public class AccountingServiceImpl extends ServiceImpl<AccountingMapper, Accounting> implements AccountingService {
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取会计科目列表按收入支出类型
|
|
|
+ *
|
|
|
+ * @param accountType 会计科目类型1.收入,2.支出
|
|
|
+ * @param user
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<AccountVO> getListByAccountType(Integer accountType, User user) {
|
|
|
+ // 1. 得到所有的会计科目
|
|
|
+ List<Accounting> list = this.list(
|
|
|
+ new LambdaQueryWrapper<Accounting>()
|
|
|
+ .eq(Accounting::getHospId, user.getHospId())
|
|
|
+ );
|
|
|
+ if (CollUtil.isEmpty(list)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ // 所有的
|
|
|
+ List<AccountVO> all = list.stream().map(i -> BeanUtil.convertObj(i, AccountVO.class)).collect(Collectors.toList());
|
|
|
+ // 顶层的
|
|
|
+ List<AccountVO> parents = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
|
|
|
+ List<AccountVO> accountVOS = new ArrayList<>();
|
|
|
+ for (AccountVO parent : parents) {
|
|
|
+ List<AccountVO> accountTree = this.getAccountTree(parent, all);
|
|
|
+ accountVOS.addAll(accountTree);
|
|
|
+ }
|
|
|
+ return accountVOS;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归处理
|
|
|
+ * @param accountVO
|
|
|
+ * @param list
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<AccountVO> getAccountTree(AccountVO accountVO, List<AccountVO> list) {
|
|
|
+ List<AccountVO> accountVOS = new ArrayList<>();
|
|
|
+ for (AccountVO account : list) {
|
|
|
+ // 如果是父子关系
|
|
|
+ if (accountVO.getId().equals(account.getParentId())) {
|
|
|
+ List<AccountVO> child = accountVO.getChild();
|
|
|
+ if (CollUtil.isEmpty(child)) {
|
|
|
+ child = new ArrayList<>();
|
|
|
+ }
|
|
|
+ child.add(account);
|
|
|
+ accountVO.setChild(child);
|
|
|
+ // 处理子节点
|
|
|
+ this.getAccountTree(account, list);
|
|
|
+
|
|
|
+ accountVOS.add(accountVO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return accountVOS;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存会计科目
|
|
|
+ *
|
|
|
+ * @param accountingSaveDTO
|
|
|
+ * @param user
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED)
|
|
|
+ public void saveAccounting(AccountingSaveDTO accountingSaveDTO, User user) {
|
|
|
+ // 校验会计科目代码
|
|
|
+ this.checkAccountingCode(accountingSaveDTO.getAccountingCode(), user.getHospId());
|
|
|
+
|
|
|
+ // 新增逻辑
|
|
|
+ Integer parentId = accountingSaveDTO.getId();
|
|
|
+ // 不是顶层的
|
|
|
+ String allParentIds = "0";
|
|
|
+ if (parentId != 0) {
|
|
|
+ Accounting byId = this.getById(parentId);
|
|
|
+ if (Objects.isNull(byId)) {
|
|
|
+ throw new CostException(500, "上层级别会计科目已被移除");
|
|
|
+ }
|
|
|
+ String oldParentIds = byId.getAllParentIds();
|
|
|
+ if ("0".equals(oldParentIds)) {
|
|
|
+ allParentIds = parentId + "";
|
|
|
+ } else {
|
|
|
+ allParentIds = oldParentIds + "-" + parentId;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Accounting accounting = BeanUtil.convertObj(accountingSaveDTO, Accounting.class);
|
|
|
+ accounting.setHospId(user.getHospId());
|
|
|
+ accounting.setParentId(parentId);
|
|
|
+ accounting.setAllParentIds(allParentIds);
|
|
|
+ accounting.setCreateTime(System.currentTimeMillis());
|
|
|
+ this.save(accounting);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void checkAccountingCode(String code, Integer hospId) {
|
|
|
+ List<Accounting> list = this.baseMapper.selectList(
|
|
|
+ new QueryWrapper<Accounting>().lambda().select(Accounting::getId)
|
|
|
+ .eq(Accounting::getAccountingCode, code)
|
|
|
+ .eq(Accounting::getHospId, hospId)
|
|
|
+ );
|
|
|
+ if (CollUtil.isNotEmpty(list)) {
|
|
|
+ throw new CostException(500, "会计科目代码已存在,请重新生成");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|