package com.ruoyi.doubledefense.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.doubledefense.domain.BasicSafetyCategory;
import com.ruoyi.doubledefense.domain.BasicSafetyDocument;
import com.ruoyi.doubledefense.dto.BasicSafetyCategoryDto;
import com.ruoyi.doubledefense.mapper.BasicSafetyCategoryMapper;
import com.ruoyi.doubledefense.mapper.BasicSafetyDocumentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.doubledefense.service.IBasicSafetyCategoryService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-03
 */
@Service
public class BasicSafetyCategoryServiceImpl extends ServiceImpl<BasicSafetyCategoryMapper, BasicSafetyCategory> implements IBasicSafetyCategoryService
{
    @Autowired
    private BasicSafetyDocumentMapper basicSafetyDocumentMapper;
    @Autowired
    private BasicSafetyCategoryMapper basicSafetyCategoryMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public BasicSafetyCategory selectBasicSafetyCategoryById(String id)
    {
        return basicSafetyCategoryMapper.selectBasicSafetyCategoryById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param basicSafetyCategory 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<BasicSafetyCategory> selectBasicSafetyCategoryList(BasicSafetyCategory basicSafetyCategory)
    {
        return basicSafetyCategoryMapper.selectBasicSafetyCategoryList(basicSafetyCategory);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param basicSafetyCategory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertBasicSafetyCategory(BasicSafetyCategory basicSafetyCategory)
    {
        return basicSafetyCategoryMapper.insertBasicSafetyCategory(basicSafetyCategory);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param basicSafetyCategory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateBasicSafetyCategory(BasicSafetyCategory basicSafetyCategory)
    {
        return basicSafetyCategoryMapper.updateBasicSafetyCategory(basicSafetyCategory);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBasicSafetyCategoryByIds(String[] ids)
    {
        List<BasicSafetyCategory> basicSafetyCategories = basicSafetyCategoryMapper.selectBatchIds(Arrays.asList(ids));
        boolean flag = true;
        for (BasicSafetyCategory x : basicSafetyCategories) {
            List<BasicSafetyCategory> basicSafetyCategoryList = basicSafetyCategoryMapper.selectList(Wrappers.lambdaQuery(BasicSafetyCategory.class)
                    .eq(BasicSafetyCategory::getParentId,x.getId())
            );
            if (!basicSafetyCategoryList.isEmpty())
                throw new RuntimeException("请删除子分类后重试");
            List<BasicSafetyDocument> basicSafetyDocuments = basicSafetyDocumentMapper.selectList(Wrappers.lambdaQuery(BasicSafetyDocument.class)
                    .eq(BasicSafetyDocument::getBasicSafetyCategoryId,x.getId())
            );
            if (!basicSafetyDocuments.isEmpty())
                throw new RuntimeException("请删除本分类下文件后重试");
        }
        return basicSafetyCategoryMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBasicSafetyCategoryById(String id)
    {
        return basicSafetyCategoryMapper.deleteBasicSafetyCategoryById(id);
    }

    @Override
    public List<BasicSafetyCategoryDto> getCategoryTree(String parentId) {
        List<BasicSafetyCategoryDto> dtos = new ArrayList<>();
        List<BasicSafetyCategory> basicSafetyCategories = basicSafetyCategoryMapper.selectList(Wrappers.lambdaQuery());
        if (!basicSafetyCategories.isEmpty()){
           dtos = basicSafetyCategories.stream().filter(d -> d.getParentId().equals("0")).map(d->{
                BasicSafetyCategoryDto dt = new BasicSafetyCategoryDto();
                BeanUtils.copyProperties(d,dt);
                dt.setCategoryList(getCategoryTree(dt,basicSafetyCategories));
                return dt;
            }).collect(Collectors.toList());
        }
        List<BasicSafetyCategoryDto> gdtos = new ArrayList<>();
        BasicSafetyCategoryDto dt = new BasicSafetyCategoryDto();
        dt.setCategoryList(dtos);
        dt.setId("0");
        dt.setCategoryName("根目录");
        gdtos.add(dt);
        return gdtos;
    }

    private List<BasicSafetyCategoryDto>  getCategoryTree(BasicSafetyCategoryDto  dto,List<BasicSafetyCategory> basicSafetyCategories){
       return basicSafetyCategories.stream().filter(d -> d.getParentId().equals(dto.getId())).map(d->{
            BasicSafetyCategoryDto dt = new BasicSafetyCategoryDto();
            BeanUtils.copyProperties(d,dt);
            Optional<BasicSafetyCategory> first = basicSafetyCategories.stream().filter(x -> x.getParentId().equals(d.getId())).findFirst();
            if (first.isPresent()){
                dt.setCategoryList(getCategoryTree(dt,basicSafetyCategories));
            }
            return dt;
        }).collect(Collectors.toList());
    }
}
