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.AccountingEditDTO; import com.imed.costaccount.model.dto.AccountingSaveDTO; import com.imed.costaccount.model.vo.AccountVO; import com.imed.costaccount.model.vo.SelectAccountingVO; import com.imed.costaccount.service.AccountingService; import com.imed.costaccount.utils.BeanUtil; import lombok.extern.slf4j.Slf4j; 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.*; import java.util.stream.Collectors; @Slf4j @Service("accountingService") public class AccountingServiceImpl extends ServiceImpl implements AccountingService { /** * 获取会计科目列表按收入支出类型 * * @param accountType 会计科目类型1.收入,2.支出 * @param user * @return */ @Override public List getListByAccountType(Integer accountType, User user) { // 1. 得到所有的会计科目 List list = this.list( new LambdaQueryWrapper() .eq(Accounting::getHospId, user.getHospId()) ); if (CollUtil.isEmpty(list)) { return Collections.emptyList(); } // 所有的 List all = list.stream().map(i -> BeanUtil.convertObj(i, AccountVO.class)).collect(Collectors.toList()); // 顶层的 List parents = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList()); List accountVOS = new ArrayList<>(); for (AccountVO parent : parents) { List accountTree = this.getAccountTree(parent, all); accountVOS.addAll(accountTree); } return accountVOS; } /** * 递归处理 * @param accountVO * @param list * @return */ private List getAccountTree(AccountVO accountVO, List list) { List accountVOS = new ArrayList<>(); for (AccountVO account : list) { // 如果是父子关系 if (accountVO.getId().equals(account.getParentId())) { List 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 list = this.baseMapper.selectList( new QueryWrapper().lambda().select(Accounting::getId) .eq(Accounting::getAccountingCode, code) .eq(Accounting::getHospId, hospId) ); if (CollUtil.isNotEmpty(list)) { throw new CostException(500, "会计科目代码已存在,请重新生成"); } } /** * 选择会计科目列表 * * @param user * @return */ @Override public List selectAccounting(User user) { List list = this.list( new LambdaQueryWrapper().select(Accounting::getId,Accounting::getAccountingName,Accounting::getParentId,Accounting::getAllParentIds) .eq(Accounting::getHospId, user.getHospId()) ); if (CollUtil.isEmpty(list)) { return Collections.emptyList(); } // 所有的 List all = list.stream().map(i -> BeanUtil.convertObj(i, SelectAccountingVO.class)).collect(Collectors.toList()); // 顶层的 List parents = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList()); List accountVOS = new ArrayList<>(); for (SelectAccountingVO parent : parents) { List accountTree = this.getSelectAccountTree(parent, all); accountVOS.addAll(accountTree); } return accountVOS; } private List getSelectAccountTree(SelectAccountingVO parent, List all) { List accountVOS = new ArrayList<>(); for (SelectAccountingVO account : all) { // 如果是父子关系 if (parent.getId().equals(account.getParentId())) { List child = parent.getChild(); if (CollUtil.isEmpty(child)) { child = new ArrayList<>(); } child.add(account); parent.setChild(child); // 处理子节点 this.getSelectAccountTree(account, all); accountVOS.add(parent); } } return accountVOS; } /** * 编辑科目代码 * * @param accountingEditDTO * @param user */ @Override public void updateAccount(AccountingEditDTO accountingEditDTO, User user) { Integer id = accountingEditDTO.getId(); List accounts = new ArrayList<>(); Accounting byId = this.getById(id); // TODO: 2021/7/28 校验 accounts.add(byId); // 如果是顶层的修改 getAndAllChild(Arrays.asList(id), user.getHospId(), accounts); log.info("得到所有的这个下面的列表:{}", accounts); Map> map = accounts.stream().collect(Collectors.groupingBy(Accounting::getId)); // 删除所有的列表 // this.remove } /** * 得到自己还有所有的子节点 * @param ids * @param accounts * @return */ private List getAndAllChild(List ids, Integer hospId, List accounts) { List list = this.list( new LambdaQueryWrapper() .in(Accounting::getParentId, ids).eq(Accounting::getHospId,hospId) ); if (CollUtil.isEmpty(list)) { return accounts; } accounts.addAll(list); this.getAndAllChild(list.stream().map(Accounting::getId).collect(Collectors.toList()), hospId, accounts); return accounts; } }