package com.xhkjedu.sstudy.controller.subjectbook;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.service.subjectbook.DirectorService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
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 DirectorController
 * Description 章节
 * Author WN
 * Date 2020/6/3 11:58
 **/
@RestController
@RequestMapping("director")
public class DirectorController {
    @Autowired
    private DirectorService directorService;

    /*
     * @Description 章节管理列表
     * @Date 2023/11/28 10:23:52
     * @Author WN
     * @Param [director]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_manage")
    public ResultVo listManage(@RequestBody TDirector director) {
        N_Utils.validation(new Object[]{director.getLsbid(), "教材id", 1});
        List<TDirector> lst = directorService.lisTDirectors(director);
        return new ResultVo(0, "获取教材下章节成功", lst);
    }

    /**
     * 功能描述  教材下章节树形结构
     *
     * @param * @param director
     * @return com.xhkjedu.vo.ResultVo
     * @author WN
     * @date 2020/6/3
     */
    @PostMapping("/list")
    public ResultVo list(@RequestBody TDirector director) {
        N_Utils.validation(new Object[]{director.getLsbid(), "教材id", 1});
        List<TDirector> lst = directorService.lisTDirectors(director);
        return new ResultVo(0, "获取教材下章节成功", lst);
    }


    /**
     * 功能描述  添加
     *
     * @param * @param director
     * @return com.xhkjedu.vo.ResultVo
     * @author WN
     * @date 2020/6/3
     */
    @PostMapping("/add")
    @Action("章节管理-增加")
    public ResultVo add(@RequestBody TDirector director) {
        N_Utils.validation(new Object[]{director.getDirectorname(), "章节名称", 2, director.getLsbid(), "教材id", 1,
                director.getBelong(), "章节归属", 1, director.getDirectorlevel(), "归属层级", 1});
        if (director.getDirectorlevel() > 4) {
            return new ResultVo(1, "超过层级限制");
        }
        int result = directorService.addDirector(director);
        if (result > 0) {
            return new ResultVo(0, "保存成功");
        } else {
            return new ResultVo(1, "保存失败");
        }
    }

    /**
     * 功能描述  修改章节
     *
     * @param * @param director
     * @return com.xhkjedu.vo.ResultVo
     * @author WN
     * @date 2020/6/1
     */
    @PostMapping("/update")
    @Action("章节管理-修改")
    public ResultVo update(@RequestBody TDirector director) {
        N_Utils.validation(new Object[]{director.getDirectorid(), "章节id", 2, director.getDirectorname(), "章节名称", 2});
        int result = directorService.updateDirector(director);
        if (result > 0) {
            directorService.updateDirectorAll(director.getDirectorid());
            return new ResultVo(0, "修改成功");
        } else return new ResultVo(1, "修改失败");
    }

    /**
     * 功能描述 删除
     *
     * @param * @param director
     * @return com.xhkjedu.vo.ResultVo
     * @author WN
     * @date 2020/6/3
     */
    @PostMapping("/del")
    @Action("章节管理-删除")
    public ResultVo del(@RequestBody TDirector director) {
        N_Utils.validation(new Object[]{director.getDirectorid(), "章节id", 2});
        String useModule = directorService.findUseModule(director.getDirectorid());
        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 = directorService.delDirector(director);
        if (result > 0) return new ResultVo(0, "删除成功");
        else return new ResultVo(1, "删除失败");
    }
}
