package com.sqx.modules.resumes.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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.resumes.dao.PostDetailsDao;
import com.sqx.modules.resumes.dao.RuleClassifyDao;
import com.sqx.modules.resumes.entity.PostDetails;
import com.sqx.modules.resumes.entity.RuleClassify;
import com.sqx.modules.resumes.service.PostDetailsService;
import com.sqx.modules.resumes.service.RuleClassifyService;
import org.junit.Rule;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 岗位分类
 */
@Service
public class RuleClassifyServiceImpl extends ServiceImpl<RuleClassifyDao, RuleClassify> implements RuleClassifyService {


    @Override
    public Result addClassify(RuleClassify ruleClassify) {
        if (ruleClassify.getParentId() != 0) {
            RuleClassify rule = baseMapper.selectById(ruleClassify.getParentId());
            if (rule == null) {
                return Result.error("一级菜单不存在");
            }
        }
        return Result.upStatus(baseMapper.insert(ruleClassify));
    }

    @Override
    public List<RuleClassify> getClassifyList(RuleClassify classify) {
        List<RuleClassify> records = baseMapper.selectList(new QueryWrapper<>(classify).eq("status", 1));
        return getClassifyTree(records);
    }


    @Override
    public IPage<RuleClassify> adminGetClassifyList(Integer page, Integer limit, RuleClassify classify) {
        QueryWrapper<RuleClassify> wrapper = new QueryWrapper<>(classify);
        Page<RuleClassify> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        if (classify.getParentId() == null) {
            wrapper.eq("parent_id", 0);
        }
        wrapper.orderByAsc("sort");
        return baseMapper.selectPage(pages, wrapper);
    }

    @Override
    public IPage<RuleClassify> adminGetParentId(Integer page, Integer limit, RuleClassify classify) {
        Page<RuleClassify> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<RuleClassify> selectPage = baseMapper.selectPage(pages, new QueryWrapper<>(classify).orderByAsc("sort"));
        for (RuleClassify record : selectPage.getRecords()) {
            record.setChildrens(baseMapper.selectList(new QueryWrapper<RuleClassify>().eq("parent_id", record.getRuleClassifyId()).orderByAsc("sort")));
        }
        return selectPage;
    }

    @Override
    public Result updateClassify(RuleClassify ruleClassify) {
        if (ruleClassify.getParentId() != 0) {
            RuleClassify classify = baseMapper.selectById(ruleClassify.getParentId());
            if (classify == null) {
                return Result.error("上级不存在");
            }
        }
        return Result.upStatus(baseMapper.updateById(ruleClassify));
    }

    @Override
    public Result deleteClassify(Long intentionId) {
        Integer count = baseMapper.selectCount(new QueryWrapper<RuleClassify>().eq("parent_id", intentionId));
        if (count > 0) {
            return Result.error("请先删除下级岗位");
        } else {
            return Result.upStatus(baseMapper.deleteById(intentionId));
        }


    }

    @Override
    public List<RuleClassify> userGetClassify(RuleClassify classify) {

        List<RuleClassify> selectList = baseMapper.selectList(new QueryWrapper<>(classify).eq("status", 1).ne("parent_id", 0));
        for (RuleClassify ruleClassify : selectList) {

            RuleClassify titleInfo = baseMapper.selectById(ruleClassify.getParentId());
            ruleClassify.setTitle(titleInfo.getTitle());
        }
        return selectList;
    }

    @Override
    public List<String> getClassifyHasTitle(Long ruleClassifyId) {
        return baseMapper.getClassifyHasTitle(ruleClassifyId);
    }


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

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