package com.xhkjedu.sstudy.controller.subjectbook;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.sstudy.model.subjectbook.TPoint;
import com.xhkjedu.sstudy.service.subjectbook.PointService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.sstudy.vo.subjectbook.PointVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @ClassName PointController
 * Description 知识点
 * Author WN
 * Date 2020/6/3 11:58
 **/
@RestController
@RequestMapping("point")
public class PointController {
    @Autowired
    private PointService pointService;

    /*
     * @Description 知识点管理列表
     * @Date 2023/11/28 10:25:01
     * @Author WN
     * @Param [point]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_manage")
    public ResultVo listManage(@RequestBody TPoint point){
        N_Utils.validation(new Object[]{point.getSubjectid(),"科目id",2});
        List<TPoint> lst = pointService.listPoints(point);
        return new ResultVo(0,"获取科目下知识点成功",lst);
    }
    /**
     *功能描述  科目下知识点列表
     * @author WN
     * @date 2020/6/3
     * @param  * @param point
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/list")
    public ResultVo list(@RequestBody TPoint point){
        N_Utils.validation(new Object[]{point.getSubjectid(),"科目id",2});
        List<TPoint> lst = pointService.listPoints(point);
        return new ResultVo(0,"获取科目下知识点成功",lst);
    }

    /**
     *功能描述  添加
     * @author WN
     * @date 2020/6/3
     * @param  * @param point
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/add")
    @Action("知识点管理-增加")
    public ResultVo add(@RequestBody TPoint point){
        N_Utils.validation(new Object[]{point.getPointname(),"知识点名称",2,point.getSubjectid(),"科目",1
        ,point.getPointlevel(),"归属层级",1});
        if (point.getPointlevel() > 5) {
            return new ResultVo(1,"超过层级限制");
        }

        int result = pointService.addPoint(point);
        if (result > 0) {
            return new ResultVo(0, "保存成功");
        }else{
            return new ResultVo(1, "保存失败");
        }
    }

    /**
     *功能描述  修改知识点
     * @author WN
     * @date 2020/6/1
     * @param  * @param point
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/update")
    @Action("知识点管理-修改")
    public ResultVo update(@RequestBody TPoint point){
        N_Utils.validation(new Object[]{point.getPointid(),"知识点id",2,point.getPointname(),"知识点名称",2});
        int result = pointService.updatePoint(point);
        if (result > 0) return new ResultVo(0,"修改成功");
        else return new ResultVo(1,"修改失败");
    }

    /**
     *功能描述 删除
     * @author WN
     * @date 2020/6/3
     * @param  * @param point
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/del")
    @Action("知识点管理-删除")
    public ResultVo del(@RequestBody TPoint point){
        N_Utils.validation(new Object[]{point.getPointid(),"知识点id",2});
        String useModule = pointService.findUseModule(point.getPointid());
        if (N_Utils.isNotEmpty(useModule)) {
//            return new ResultVo(1,"使用知识点模块【"+useModule+"】，禁止删除");
            String[] strs = useModule.split(",");
            for (String str : strs) {
                if (N_Utils.isNotEmpty(str)){
                    useModule = str;
                    break;
                }
            }
            return new ResultVo(1,"此知识点下有"+useModule+"，禁止删除");
        }
        int result = pointService.delPoint(point);
        if (result > 0) return new ResultVo(0,"删除成功");
        else return new ResultVo(1,"删除失败");
    }

    /**
     *功能描述  搜索知识点
     * @author WN
     * @date 2020/6/19
     * @param  * @param point
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/search")
    public ResultVo search(@RequestBody TPoint point){
        N_Utils.validation(new Object[]{point.getSubjectid(),"科目id",2});
        List<PointVo> lst = pointService.listPointsForSearch(point);
        return new ResultVo(0,"搜索科目下知识点成功",lst);
    }


}
