package com.xhkjedu.sstudy.service.subjectbook;

import com.xhkjedu.sstudy.mapper.subjectbook.PointMapper;
import com.xhkjedu.sstudy.model.subjectbook.TPoint;
import com.xhkjedu.sstudy.utils.StudyUtil;
import com.xhkjedu.sstudy.vo.subjectbook.PointVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName PointService
 * Description TODO
 * Author WN
 * Date 2020/6/3 11:56
 **/
@Service
public class PointService {
    @Autowired
    private PointMapper pointMapper;

    /**
     *功能描述 新增
     * @author WN
     * @date 2020/6/3
     * @param  * @param point
     * @return java.lang.Integer
     */
    public Integer addPoint(TPoint point){
        String id = StudyUtil.getId();
        point.setPointid(id);
        if ("".equals(point.getPointpid())) {
            point.setPointpid(null);
        }

        point.setCreatetime(N_Utils.getSecondTimestamp());

        if (N_Utils.isEmpty(point.getPointpid())) {
            //说明是1级知识点
            point.setPointall(point.getPointname());
        }else{
            //非1级知识点，获取该父知识点的pointall字段值
            String pointall = pointMapper.getPointallByPointid(point.getPointpid());
            point.setPointall(pointall + ";" + point.getPointname());
        }

        return pointMapper.insertSelective(point);
    }

    /**
     *功能描述 修改
     * @author WN
     * @date 2020/6/3
     * @param  * @param point
     * @return java.lang.Integer
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer updatePoint(TPoint point){
        TPoint oldPoint = pointMapper.selectByPrimaryKey(point.getPointid());
        if(oldPoint.getPointlevel() == 1){
            //说明第一层
            point.setPointall(point.getPointname());
        }else{
            //设置本层pointall字段
            String pointall = oldPoint.getPointall().replaceAll(oldPoint.getPointname(),point.getPointname());
            point.setPointall(pointall);
        }
        pointMapper.updatePointAll(point.getPointid(),point.getPointall(),oldPoint.getPointlevel());
        point.setCreatetime(N_Utils.getSecondTimestamp());
        return pointMapper.updateByPrimaryKeySelective(point);
    }

    /**
     *功能描述  删除
     * @author WN
     * @date 2020/6/3
     * @param  * @param point
     * @return java.lang.Integer
     */
    public Integer delPoint(TPoint point){
        return pointMapper.deleteByPrimaryKey(point.getPointid());
    }

    //获取科目下知识点数
    public List<TPoint> listPoints(TPoint point){
        List<TPoint> lst = pointMapper.listPoints(point);
        String pointname = point.getPointname();
        if (N_Utils.isNotEmpty(pointname)) {
            List<TPoint> filterLst = lst.stream().filter(p -> p.getPointname().contains(pointname)).collect(Collectors.toList());
            if (N_Utils.isListEmpty(filterLst)) {
                return filterLst;
            }
            List<TPoint> parentPoints = new ArrayList<>();
            for (TPoint p : filterLst) {
                //查找所有父级知识点
                parentPoints.addAll(getParentPoints(p, lst));
            }
            parentPoints.addAll(filterLst);
            parentPoints = parentPoints.stream().distinct().collect(Collectors.toList());
            lst = parentPoints;
        }

        List<TPoint> points = new ArrayList<>();
        for (TPoint p:lst){
            if(p.getPointpid() == null){
                List<TPoint> childPoints = listChildePoint(p,lst);
                p.setChildren(childPoints);
                points.add(p);

            }
        }

        return points;
    }

    //获取父级知识点
    private List<TPoint> getParentPoints(TPoint p, List<TPoint> lst) {
        List<TPoint> parentPoints = new ArrayList<>();
        for (TPoint tPoint : lst) {
            String pointpid = p.getPointpid();
            if (N_Utils.isEmpty(pointpid)) continue;
            if (tPoint.getPointid().equals(pointpid)) {
                parentPoints.add(tPoint);
                List<TPoint> parentPoints1 = getParentPoints(tPoint, lst);
                parentPoints.addAll(parentPoints1);
            }
        }
        return parentPoints;
    }



//获取子知识点
    private List<TPoint> listChildePoint(TPoint point,List<TPoint> points){
        List<TPoint> rtnPoints = new ArrayList<>();
        for(TPoint p : points){
            if(point.getPointid().equals(p.getPointpid())){
                List<TPoint> childPoints = listChildePoint(p,points);
                p.setChildren(childPoints);
                rtnPoints.add(p);
            }
        }
        return rtnPoints;
    }

    //搜索知识点
    public List<PointVo> listPointsForSearch(TPoint point){
        return pointMapper.listPointsForSearch(point);
    }

    //查找使用知识点的模块
    public String findUseModule(String pointid) {
        String useModule = pointMapper.findUseModule(pointid);
        if (useModule.startsWith(",")) {
            useModule = useModule.substring(1);
        }
        return useModule;
    }

    //获取知识点详情
    public TPoint getPointInfoById(String pointid) {
        return pointMapper.getPointInfoById(pointid);
    }
}

