package com.xhkjedu.sstudy.service.subjectbook;


import com.xhkjedu.sstudy.mapper.subjectbook.DirectorMapper;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.utils.StudyUtil;
import com.xhkjedu.sstudy.vo.subjectbook.DirectorVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @ClassName DirectorService
 * Description TODO
 * Author WN
 * Date 2020/6/3 15:59
 **/
@Service
public class DirectorService {
    @Autowired
    private DirectorMapper directorMapper;

    /**
     * 功能描述 新增
     *
     * @param * @param director
     * @return java.lang.Integer
     * @author WN
     * @date 2020/6/3
     */
    public Integer addDirector(TDirector director) {
        String id = StudyUtil.getId();
        director.setDirectorid(id);
        if ("".equals(director.getDirectorpid())) {
            director.setDirectorpid(null);
        }
        director.setCreatetime(N_Utils.getSecondTimestamp());
        return directorMapper.insertSelective(director);
    }

    /**
     * 添加并返回主键
     *
     * @return java.lang.Integer
     * @Param [director]
     * @Author ywx
     * @Date 2020/7/6 15:00
     **/
    public String addAndReturnKey(TDirector director) {
        String id = StudyUtil.getId();
        director.setDirectorid(id);
        director.setCreatetime(N_Utils.getSecondTimestamp());
        directorMapper.insertSelective(director);
        return id;
    }

    /**
     * 功能描述 修改
     *
     * @param * @param director
     * @return java.lang.Integer
     * @author WN
     * @date 2020/6/3
     */
    public Integer updateDirector(TDirector director) {
        director.setCreatetime(N_Utils.getSecondTimestamp());
        return directorMapper.updateByPrimaryKeySelective(director);
    }

    /**
     * 功能描述  删除
     *
     * @param * @param director
     * @return java.lang.Integer
     * @author WN
     * @date 2020/6/3
     */
    public Integer delDirector(TDirector director) {
        return directorMapper.deleteById(director.getDirectorid());
    }

    /**
     * 功能描述  章节树形结构
     *
     * @param * @param director
     * @return java.util.List<com.xhkjedu.model.subjectbook.TDirector>
     * @author WN
     * @date 2020/6/3
     */
    public List<TDirector> lisTDirectors(TDirector director) {
        List<TDirector> lst = directorMapper.listDirectors(director);

        List<TDirector> directors = new ArrayList<>();
        for (TDirector d : lst) {
            if (d.getDirectorpid() == null) {
                List<TDirector> childDirectors = listChildedirector(d, lst);
                d.setChildren(childDirectors);
                directors.add(d);
            }
        }

        return directors;
    }

    //获取子章节
    private List<TDirector> listChildedirector(TDirector director, List<TDirector> directors) {
        List<TDirector> rtndirectors = new ArrayList<>();
        for (TDirector d : directors) {
            if (director.getDirectorid().equals(d.getDirectorpid())) {
                List<TDirector> childDirectors = listChildedirector(d, directors);
                d.setChildren(childDirectors);
                rtndirectors.add(d);
            }
        }
        return rtndirectors;
    }

    //2022-03-16使用stream递归
    public List<TDirector> lisTDirectors2(TDirector director) {
        List<TDirector> lst = directorMapper.listDirectors(director);

        List<TDirector> collect = lst.stream().filter(d -> d.getDirectorpid() == null).map(
                (d) -> {
                    d.setChildren(getChildrens(d, lst));
                    return d;
                }
        ).collect(Collectors.toList());
        return collect;
    }

    //递归查询子节点
    private List<TDirector> getChildrens(TDirector root, List<TDirector> all) {
        List<TDirector> children = all.stream().filter(d -> {
            return Objects.equals(d.getDirectorpid(), root.getDirectorid());
        }).map(
                (d) -> {
                    d.setChildren(getChildrens(d, all));
                    return d;
                }
        ).collect(Collectors.toList());
        return children;
    }

    /**
     * 教师收藏教材下的章节数量
     *
     * @return java.lang.Integer
     * @Param [teacherid, lsbid]
     * @Author ywx
     * @Date 2020/7/8 14:49
     **/
    public Integer countByTeacherIdLsbId(Integer teacherid, String lsbid) {
        return directorMapper.countByTeacherIdLsbId(teacherid, lsbid);
    }

    //查找使用章节的模块
    public String findUseModule(String directorid) {
        String useModule = directorMapper.findUseModule(directorid);
        if (useModule.startsWith(",")) {
            useModule = useModule.substring(1);
        }
        return useModule;
    }

    /**
     * 修改层级章节
     *
     * @return void
     * @Param [directorid, directorname]
     * @Author ywx
     * @Date 2022/3/14 15:14
     **/
    public void updateDirectorAll(String directorid) {
        DirectorVo vo = directorMapper.getNameAndLevelById(directorid);
        Integer level = vo.getLevel();
        if (level.equals(4)) return;
        directorMapper.updateDirectorAll(directorid, vo.getDirectorname(), level);
    }

    //获取章节信息
    public TDirector getDirectorById(String directorid) {
        return directorMapper.selectByPrimaryKey(directorid);
    }
}
