package com.youlai.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.youlai.boot.system.mapper.MineDemandMapper;
import com.youlai.boot.system.mapper.OMRecordMapper;
import com.youlai.boot.system.mapper.RecordMapper;
import com.youlai.boot.system.mapper.SysFolderMapper;
import com.youlai.boot.system.model.entity.MineDemand;
import com.youlai.boot.system.model.entity.OMRecord;
import com.youlai.boot.system.model.entity.Record;
import com.youlai.boot.system.model.entity.SysFolder;
import com.youlai.boot.system.service.SysFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service
public class SysFolderServiceImpl extends ServiceImpl<SysFolderMapper, SysFolder> implements SysFolderService {


    @Autowired
    MineDemandMapper mineDemandMapper;

    @Autowired
    OMRecordMapper omRecordMapper;

    @Autowired
    RecordMapper recordMapper;



    @Override
    public List<SysFolder> getFolderTree(String type) {
        LambdaQueryWrapper<SysFolder> queryWrapper = new LambdaQueryWrapper<SysFolder>().eq(SysFolder::getType, type);
        List<SysFolder> allFolders = baseMapper.selectList(queryWrapper);
        return buildTree(allFolders, 0);
    }

    @Override
    public List<SysFolder> getSubFolderTree(Integer parentId) {
        List<SysFolder> subFolders = baseMapper.selectByParentId(parentId);
        return buildTree(subFolders, parentId);
    }

    @Override
    @Transactional
    public boolean createFolder(SysFolder folder) {
        folder.setCreateTime(new Date());
        folder.setUpdateTime(new Date());
        
        // 获取父文件夹信息
        SysFolder parentFolder = getById(folder.getParentId());
        if (parentFolder != null) {
            // 构建祖先路径
            folder.setAncestorIds(parentFolder.getAncestorIds() == null ? 
                parentFolder.getId().toString() : 
                parentFolder.getAncestorIds() + "," + parentFolder.getId());
        } else {
            // 根文件夹
            folder.setParentId(0);
            folder.setAncestorIds(null);
        }
        
        return save(folder);
    }

    @Override
    @Transactional
    public boolean deleteFolder(Integer folderId) {
        // 检查是否存在子文件夹
        LambdaQueryWrapper<SysFolder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysFolder::getParentId, folderId);
        long count = count(queryWrapper);
        
        if (count > 0) {
            return false; // 存在子文件夹，不删除
        }

        LambdaQueryWrapper<MineDemand> dqw = new LambdaQueryWrapper<>();
        dqw.eq(MineDemand::getFolderId, folderId);
        Long demandCount = mineDemandMapper.selectCount(dqw);

        LambdaQueryWrapper<OMRecord> omrqw = new LambdaQueryWrapper<>();
        omrqw.eq(OMRecord::getFolderId, folderId);
        Long omrCount = omRecordMapper.selectCount(omrqw);

        LambdaQueryWrapper<Record> relqw = new LambdaQueryWrapper<>();
        relqw.eq(Record::getFolderId, folderId);
        Long reCount = recordMapper.selectCount(relqw);

        if (demandCount > 0||omrCount>0||reCount>0) {
            return false; // 存在子文件夹，不删除
        }

        return removeById(folderId);
    }

    @Override
    @Transactional
    public boolean renameFolder(Integer folderId, String newName) {
        SysFolder folder = getById(folderId);
        if (folder == null) {
            return false;
        }
        
        folder.setName(newName);
        folder.setFolderName(newName); // 根据业务需求调整
        folder.setUpdateTime(new Date());
        
        return updateById(folder);
    }

    private List<SysFolder> buildTree(List<SysFolder> allFolders, Integer parentId) {
        List<SysFolder> tree = new ArrayList<>();
        
        for (SysFolder folder : allFolders) {
            if (Objects.equals(folder.getParentId(), parentId)) {
                List<SysFolder> children = buildTree(allFolders, folder.getId());
                // 这里可以设置children属性，需要在实体类中添加该属性
                folder.setChildren(children);
                tree.add(folder);
            }
        }
        
        return tree;
    }

    @Override
    public List<Integer> getSubFolderIdsRecursively(Integer folderId) {
        List<Integer> subFolderIds = new ArrayList<>();
        collectSubFolderIds(folderId, subFolderIds);
        subFolderIds.add(folderId);
        return subFolderIds;
    }

    // 递归收集子文件夹ID
    private void collectSubFolderIds(Integer folderId, List<Integer> subFolderIds) {
        List<SysFolder> subFolders = this.baseMapper.selectList(
                Wrappers.<SysFolder>lambdaQuery().eq(SysFolder::getParentId, folderId));
        for (SysFolder folder : subFolders) {
            subFolderIds.add(folder.getId());
            collectSubFolderIds(folder.getId(), subFolderIds);
        }
    }

}    