ServiceFileTreeServiceImpl.java 4.95 KB
package com.subsidy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.subsidy.common.exception.HttpException;
import com.subsidy.model.ServiceFileTreeDO;
import com.subsidy.mapper.ServiceFileTreeMapper;
import com.subsidy.service.ServiceFileTreeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import com.subsidy.vo.tree.TreesVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文件目录树 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2024-04-08
 */
@Service
public class ServiceFileTreeServiceImpl extends ServiceImpl<ServiceFileTreeMapper, ServiceFileTreeDO> implements ServiceFileTreeService {


    public List<TreesVO> trees(ServiceFileTreeDO serviceFileTreeDO) {

        List<TreesVO> result = new ArrayList<>();

        List<ServiceFileTreeDO> serviceFileTreeDOS = this.baseMapper.selectList(new QueryWrapper<ServiceFileTreeDO>()
                .lambda()
                .eq(ServiceFileTreeDO::getCompanyId, serviceFileTreeDO.getCompanyId())
                .eq(ServiceFileTreeDO::getServiceId, serviceFileTreeDO.getServiceId())
                .isNull(ServiceFileTreeDO::getParentId));

        for (ServiceFileTreeDO sftd : serviceFileTreeDOS) {
            TreesVO treesVO = new TreesVO();
            treesVO.setId(sftd.getId());
            treesVO.setTreeName(sftd.getTreeName());
            List<ServiceFileTreeDO> serviceFileTreeDOList = this.baseMapper.selectList(new QueryWrapper<ServiceFileTreeDO>()
                    .lambda()
                    .eq(ServiceFileTreeDO::getParentId, sftd.getId()));

            List<TreesVO> resultTmp = new ArrayList<>();
            for (ServiceFileTreeDO sftd2 : serviceFileTreeDOList) {
                TreesVO treesVO1 = new TreesVO();
                treesVO1.setId(sftd2.getId());
                treesVO1.setTreeName(sftd2.getTreeName());
                treesVO1.setParentId(sftd.getId());
                resultTmp.add(treesVO1);
            }
            treesVO.setTreesVOS(resultTmp);
            result.add(treesVO);
        }
        return result;
    }

    public String addTree(ServiceFileTreeDO serviceFileTreeDO) {

        //查看当前节点下有没有重名的
        int count = this.baseMapper.selectCount(new QueryWrapper<ServiceFileTreeDO>()
                .lambda()
                .eq(ServiceFileTreeDO::getCompanyId, serviceFileTreeDO.getCompanyId())
                .eq(ServiceFileTreeDO::getParentId, serviceFileTreeDO.getParentId())
                .eq(ServiceFileTreeDO::getTreeName, serviceFileTreeDO.getTreeName())
                .eq(ServiceFileTreeDO::getServiceId, serviceFileTreeDO.getServiceId()));

        if (count > 0) {
            throw new HttpException(40001);
        }

        this.baseMapper.insert(serviceFileTreeDO);
        return ConstantUtils.ADD_SUCCESS;
    }

    public String updateTree(ServiceFileTreeDO serviceFileTreeDO) {

        ServiceFileTreeDO serviceFileTreeDO1 = this.baseMapper.selectById(serviceFileTreeDO.getId());
        //查看当前节点下有没有重名的
        int count = this.baseMapper.selectCount(new QueryWrapper<ServiceFileTreeDO>()
                .lambda()
                .eq(ServiceFileTreeDO::getCompanyId, serviceFileTreeDO1.getCompanyId())
                .eq(ServiceFileTreeDO::getParentId, serviceFileTreeDO1.getParentId())
                .eq(ServiceFileTreeDO::getTreeName, serviceFileTreeDO.getTreeName())
                .eq(ServiceFileTreeDO::getServiceId, serviceFileTreeDO1.getServiceId())
                .ne(ServiceFileTreeDO::getId, serviceFileTreeDO.getId()));

        if (count > 0) {
            throw new HttpException(40001);
        }

        this.baseMapper.updateById(serviceFileTreeDO);
        return ConstantUtils.SUCCESS_UPDATE;
    }

    public String deleteTree(ServiceFileTreeDO serviceFileTreeDO) {

        ServiceFileTreeDO serviceFileTreeDO1 = this.baseMapper.selectById(serviceFileTreeDO.getId());
        this.baseMapper.deleteById(serviceFileTreeDO.getId());

        List<Long> deleteIds = new ArrayList<>();
        deleteIds.add(serviceFileTreeDO.getId());

        //如果是一级目录的话,要删除对应的二级目录
        if (null == serviceFileTreeDO1.getParentId()) {

            //找出二级节点
            List<ServiceFileTreeDO> serviceFileTreeDOS = this.baseMapper.selectList(new QueryWrapper<ServiceFileTreeDO>()
                    .lambda()
                    .eq(ServiceFileTreeDO::getParentId, serviceFileTreeDO.getId()));
            for (ServiceFileTreeDO sftd : serviceFileTreeDOS) {
                deleteIds.add(sftd.getId());
            }

            // 删除该目录下及子目录下的文件
            this.baseMapper.delete(new QueryWrapper<ServiceFileTreeDO>()
                    .lambda()
                    .in(ServiceFileTreeDO::getId, deleteIds));
        }
        return ConstantUtils.DELETE_SUCCESS;
    }


}