package com.sqx.modules.industry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.industry.entity.Industry;
import com.sqx.modules.industry.dao.IndustryDao;
import com.sqx.modules.industry.service.IndustryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.resumes.entity.RuleClassify;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author www.javacoder.top
 * @since 2023-01-14
 */
@Service
public class IndustryServiceImpl extends ServiceImpl<IndustryDao, Industry> implements IndustryService {

    @Override
    public Result addIndustry(Industry industry) {
        if (industry.getParentId() == null) {
            Industry ind = baseMapper.selectById(industry.getParentId());
            if (ind == null) {
                return Result.error("上级不存在");
            }
        }
        industry.setCreateTime(LocalDateTime.now());
        return Result.upStatus(baseMapper.insert(industry));
    }

    @Override
    public Result updateIndustry(Industry industry) {
        if (industry.getParentId() == null) {
            Industry ind = baseMapper.selectById(industry.getParentId());
            if (ind == null) {
                return Result.error("上级不存在");
            }
        }
        return Result.upStatus(baseMapper.updateById(industry));

    }

    @Override
    public List<Industry> getIndustryList(Industry industry) {
        return getIndustryTree(baseMapper.selectList(new QueryWrapper<>(industry)));

    }

    @Override
    public Result deleteIndustry(Long industryId) {
        Industry industry = baseMapper.selectById(industryId);

        Integer count = baseMapper.selectCount(new QueryWrapper<Industry>().eq("parent_id", industry.getIndustryId()));
        if (count > 0) {
            return Result.error("请先删除下级行业");
        }
        return Result.upStatus(baseMapper.deleteById(industryId));

    }

    @Override
    public IPage<Industry> adminGetIndustryList(Integer page, Integer limit, Industry industry) {
        Page<Industry> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        if(industry.getParentId()==null){
            industry.setParentId(0L);
        }
        return baseMapper.selectPage(pages, new QueryWrapper<>(industry).orderByAsc("sort"));
    }

    private List<Industry> getIndustryTree(List<Industry> records) {
        return records.stream()
                .filter(e -> e.getParentId() == 0)
                .peek((menu) -> menu.setChildrenList(getChildrenList(menu, records)))
                .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());
    }

    //递归查找所有菜单的子菜单
    private List<Industry> getChildrenList(Industry root, List<Industry> all) {
        return all.stream().filter(categoryEntity -> categoryEntity.getParentId().equals(root.getIndustryId())).peek(categoryEntity -> {
            //1、找到子菜单(递归)
            categoryEntity.setChildrenList(getChildrenList(categoryEntity, all));
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
    }
}
