package com.xhkjedu.sstudy.controller.bk;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.constant.PlatformConstant;
import com.xhkjedu.sstudy.model.bk.TBk;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.service.bk.BkService;
import com.xhkjedu.sstudy.service.subjectbook.DirectorService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description：课堂备课
 * @Author: WN
 * @Date: 2024/6/5 10:26:42
 **/
@RestController
@RequestMapping("/bk")
public class BkController {
    @Resource
    private BkService bkService;
    @Resource
    private DirectorService directorService;

    /*
     * @Description 我的备课列表
     * @Date 2024/6/5 10:29:00
     * @Author WN
     * @Param [bk]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("list")
    public ResultVo list(@RequestBody TBk bk, HttpServletRequest request) {
        Integer createid = request.getIntHeader(PlatformConstant.XH_USER_ID);
        N_Utils.validation(new Object[]{
                bk.getPage(), "显示页码", 1, bk.getPageSize(), "显示条数", 1,bk.getLsbid(),"教材",2});
        bk.setCreateid(createid);
        //获取所选章节信息
        if(N_Utils.isNotEmpty(bk.getDirectorid()) && !"0".equals(bk.getDirectorid())){
            TDirector director = directorService.getDirectorById(bk.getDirectorid());
            bk.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                bk.setDirectorall(director.getDirectorall());
            }
        }

        PageHelper.startPage(bk.getPage(), bk.getPageSize());
        List<Map<String,Object>> list = bkService.listBks(bk);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取列表成功", pageResult);
    }

    /*
     * @Description 我的备课添加
     * @Date 2024/6/5 10:49:38
     * @Author WN
     * @Param [bk]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/save")
    @Action("我的备课-增加")
    public ResultVo save(@RequestBody TBk bk){
        N_Utils.validation(new Object[]{
                bk.getBktitle(),"备课标题",2,
                bk.getBktype(),"备课类型",1,bk.getLsbid(),"备课教材",2
        });
        bkService.save(bk);
        return new ResultVo(0,"保存成功");
    }

    /*
     * @Description 修改备课
     * @Date 2024/6/5 11:06:24
     * @Author WN
     * @Param [bk]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/update")
    @Action("我的备课-修改")
    public ResultVo update(@RequestBody TBk bk){
        N_Utils.validation(new Object[]{
                bk.getBkid(),"备课id",1,
                bk.getBktitle(),"备课标题",2,
                bk.getBktype(),"备课类型",1,bk.getLsbid(),"备课教材",2
        });
        bkService.update(bk);
        return new ResultVo(0,"保存成功");
    }

    /*
     * @Description 删除备课
     * @Date 2024/6/5 11:07:56
     * @Author WN
     * @Param [bk]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/delete")
    @Action("我的备课-删除")
    public ResultVo delete(@RequestBody TBk bk){
        N_Utils.validation(new Object[]{ bk.getBkid(),"备课id",1 });
        bkService.delete(bk.getBkid());
        return new ResultVo(0,"删除成功");
    }

    /*
     * @Description 获取备课详情
     * @Date 2024/6/5 11:07:56
     * @Author WN
     * @Param [bk]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TBk bk){
        N_Utils.validation(new Object[]{ bk.getBkid(),"备课id",1 });
        TBk rtnBk = bkService.detail(bk.getBkid());
        return new ResultVo(0,"获取成功",rtnBk);
    }

    /*
     * @Description 我的备课列表_课堂端
     * @Date 2024/6/12 9:27:53
     * @Author WN
     * @Param [bk]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("list_client")
    public ResultVo listClient(@RequestBody TBk bk, HttpServletRequest request) {
        N_Utils.validation(new Object[]{
                bk.getLsbid(),"教材",2});
        Integer createid = request.getIntHeader(PlatformConstant.XH_USER_ID);
        bk.setCreateid(createid);

        //获取所选章节信息
        if(N_Utils.isNotEmpty(bk.getDirectorid()) && !"0".equals(bk.getDirectorid())){
            TDirector director = directorService.getDirectorById(bk.getDirectorid());
            bk.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                bk.setDirectorall(director.getDirectorall());
            }
        }

        List<Map<String,Object>> list = bkService.listBks(bk);
        return new ResultVo(0, "获取列表成功", list);
    }
}
