package com.xhkjedu.sstudy.controller.subjectbook;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.sstudy.model.subjectbook.TSubject;
import com.xhkjedu.sstudy.service.subjectbook.SubjectService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sstudy.vo.subjectbook.SubjectVo;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
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 SubjectController
 * Description TODO
 * Author WN
 * Date 2020/6/1 12:03
 **/
@Slf4j
@RestController
@RequestMapping("/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    /*
     * @Description 科目管理列表
     * @Date 2023/11/28 10:29:09
     * @Author WN
     * @Param [subject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_manage")
    public ResultVo listManage(@RequestBody TSubject subject){
        List<TSubject> lst = subjectService.listSubjects(subject);
        return new ResultVo(0,"查询科目集合成功",lst);
    }

    /**
     *功能描述  获取阶段下所有科目信息
     * @author WN
     * @date 2020/6/1
     * @param  * @param subject
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/list")
    public ResultVo listSubjects(@RequestBody TSubject subject){
        List<TSubject> lst = subjectService.listSubjects(subject);
        return new ResultVo(0,"查询科目集合成功",lst);
    }

    @PostMapping("/teacher_list")
    public ResultVo listTeacherSubjects(@RequestBody TSubject subject){
        Integer teacherid = subject.getCreateid();
        N_Utils.validation(new Object[]{teacherid,"教师id",1});
        List<TSubject> lst = subjectService.listTeacherSubjects(teacherid);
        return new ResultVo(0,"查询教师科目集合成功",lst);
    }

    /**
     *功能描述 用户选择学科
     * @author WN
     * @date 2020/6/2
     * @param  * @param subject
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/listsel")
    public ResultVo listSubjectsForSel(@RequestBody TSubject subject){
        List<SubjectVo> lst = subjectService.listSubjectsForSel(subject);
        return new ResultVo(0,"查询科目集合成功",lst);
    }

    /**
     *功能描述  添加科目
     * @author WN
     * @date 2020/6/1
     * @param  * @param subject
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/add")
    @Action("科目管理-增加")
    public ResultVo add(@RequestBody TSubject subject){
        N_Utils.validation(new Object[]{subject.getSubjectname(),"科目名称",2,subject.getLevel(),"阶段",1});
        int result = subjectService.addSubject(subject);
        if (result > 0) {
            return new ResultVo(0, "保存成功");
        }else{
            return new ResultVo(1, "保存失败");
        }
    }

    /**
     *功能描述  修改科目
     * @author WN
     * @date 2020/6/1
     * @param  * @param subject
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/update")
    @Action("科目管理-修改")
    public ResultVo update(@RequestBody TSubject subject){
        N_Utils.validation(new Object[]{subject.getSubjectid(),"科目id",2,subject.getSubjectname(),"科目名称",2});
        int result = subjectService.updateSubject(subject);
        if (result > 0) return new ResultVo(0,"修改成功");
        else return new ResultVo(1,"修改失败");
    }

    /**
     *功能描述 删除科目
     * @author WN
     * @date 2020/6/1
     * @param  * @param subject
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/del")
    @Action("科目管理-删除")
    public ResultVo del(@RequestBody TSubject subject) {
        if (N_Utils.isEmpty(subject.getSubjectid()))
            return new ResultVo(1,"未选择删除的科目");
        String useModule = subjectService.findUseModule(subject.getSubjectid());
        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 = subjectService.delSubject(subject);
        if (result > 0) return new ResultVo(0,"删除成功");
        else return new ResultVo(1,"删除失败");
    }


}
