package com.xhkjedu.sstudy.controller.subjectbook;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.sstudy.model.subjectbook.TSubjectBook;
import com.xhkjedu.sstudy.service.subjectbook.SubjectBookService;
import com.xhkjedu.sstudy.vo.subjectbook.LsbSubjectVo;
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;
import java.util.Map;

/**
 * @ClassName SubjectBookController
 * Description 教材
 * Author WN
 * Date 2020/6/2 18:10
 **/
@RestController
@RequestMapping("lsb")
public class SubjectBookController {
    @Autowired
    private SubjectBookService subjectBookService;

    /**
     *功能描述 添加教材
     * @author WN
     * @date 2020/6/2
     * @param  * @param subjectBook
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/add")
    @Action("教材管理-增加")
    public ResultVo add(@RequestBody TSubjectBook subjectBook){
        N_Utils.validation(new Object[]{subjectBook.getLevel(),"阶段",1,subjectBook.getSubjectid(),"科目",1,subjectBook.getVersionid(),"版本",1});
        int result = subjectBookService.addSubjectBook(subjectBook);
        if (result > 0) {
            return new ResultVo(0, "保存成功");
        }else{
            return new ResultVo(1, "保存失败");
        }
    }

   /**
    *功能描述 教材
    * @author WN
    * @date 2020/6/2
    * @param  * @param subjectBook
    * @return com.xhkjedu.vo.ResultVo
    */
    @PostMapping("/update")
    @Action("教材管理-修改")
    public ResultVo update(@RequestBody TSubjectBook subjectBook){
        N_Utils.validation(new Object[]{subjectBook.getLsbid(),"教材id",1});
        return subjectBookService.updateSubjectBook(subjectBook);
    }

    /**
     *功能描述 教材--删除
     * @author WN
     * @date 2020/6/17
     * @param  * @param subjectBook
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/del")
    @Action("教材管理-删除")
    public ResultVo del(@RequestBody TSubjectBook subjectBook){
        N_Utils.validation(new Object[]{subjectBook.getLsbid(),"教材id",1});
        String useModule = subjectBookService.findUseModule(subjectBook.getLsbid());
        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 = subjectBookService.delSubjectBook(subjectBook);
        if (result > 0) return new ResultVo(0,"删除成功");
        else return new ResultVo(1,"删除失败");
    }

    /**
     *功能描述  教材--树形结构
     * @author WN
     * @date 2020/6/3
     * @param  * @param subjectBook
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/tree")
    public ResultVo listSubjectBooks(@RequestBody TSubjectBook subjectBook){
        N_Utils.validation(new Object[]{
                subjectBook.getLevel(),"阶段",1,
        });
        List<LsbSubjectVo> lst = subjectBookService.listSubjectBooksForTree(subjectBook);
        return new ResultVo(0,"获取教材树形结构成功",lst);
    }

    /**
     *功能描述  教材管理列表
     * @author WN
     * @date 2020/6/7
     * @param  * @param subjectBook
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/list_manage")
    public ResultVo listManage(@RequestBody TSubjectBook subjectBook){
        N_Utils.validation(new Object[]{
                subjectBook.getLevel(),"阶段",1
        });
        List<TSubjectBook> lst = subjectBookService.listSubjectBooks(subjectBook);
        return new ResultVo(0,"获取所有教材集合",lst);
    }

    /**
     *功能描述  获取教材集合
     * @author WN
     * @date 2020/6/7
     * @param  * @param subjectBook
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/list")
    public ResultVo list(@RequestBody TSubjectBook subjectBook){
        N_Utils.validation(new Object[]{
                subjectBook.getLevel(),"阶段",1
        });
        List<TSubjectBook> lst = subjectBookService.listSubjectBooks(subjectBook);
        return new ResultVo(0,"获取所有教材集合",lst);
    }

    @PostMapping("/list_sel")
    public ResultVo listForSel() {
        List<Map> list = subjectBookService.listForSel();
        return new ResultVo(0,"获取教材选择列表成功",list);
    }
}
