package com.xhkjedu.sstudy.controller.subjectbook;

import com.xhkjedu.sstudy.model.subjectbook.TSubjectQuestiontype;
import com.xhkjedu.sstudy.service.subjectbook.SubjectQuestiontypeService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.sstudy.vo.subjectbook.SubjectQuestiontypeVo;
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 SubjectQuestiontypeController
 * Description 题型
 * Author WN
 * Date 2020/6/2 14:19
 **/
@RestController
@RequestMapping("qtype")
public class SubjectQuestiontypeController {
    @Autowired
    private SubjectQuestiontypeService subjectQuestiontypeService;

    /**
     *功能描述 科目下题型列表
     * @author WN
     * @date 2020/6/2
     * @param  * @param qtype
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/list")
    public ResultVo listSubjects(@RequestBody TSubjectQuestiontype qtype){
        N_Utils.validation(new Object[]{qtype.getSubjectid(),"科目",1});
        List<TSubjectQuestiontype> lst = subjectQuestiontypeService.listQuestiontypes(qtype);
        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 TSubjectQuestiontype qtype){
        N_Utils.validation(new Object[]{qtype.getSubjectid(),"科目",1});
        List<SubjectQuestiontypeVo> lst = subjectQuestiontypeService.listQuestiontypesForSel(qtype);
        return new ResultVo(0,"查询科目集合成功",lst);
    }

    /**
     *功能描述 添加题型
     * @author WN
     * @date 2020/6/2
     * @param  * @param subject
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/add")
    public ResultVo add(@RequestBody TSubjectQuestiontype qtype){
        N_Utils.validation(new Object[]{qtype.getSubjectid(),"科目",1,qtype.getQtypename(),"题型名称",2});

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

   /**
    *功能描述  修改题型
    * @author WN
    * @date 2020/6/2
    * @param  * @param qtype
    * @return com.xhkjedu.vo.ResultVo
    */
    @PostMapping("/update")
    public ResultVo update(@RequestBody TSubjectQuestiontype qtype){
        N_Utils.validation(new Object[]{qtype.getQtypeid(),"题型id",2,qtype.getQtypename(),"题型名称",2});
        int result = subjectQuestiontypeService.updateQuestiontype(qtype);
        if (result > 0) return new ResultVo(0,"修改成功");
        else return new ResultVo(1,"修改失败");
    }

    /**
     *功能描述  删除题型
     * @author WN
     * @date 2020/6/2
     * @param  * @param qtype
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/del")
    public ResultVo del(@RequestBody TSubjectQuestiontype qtype){
        if (N_Utils.isEmpty(qtype.getQtypeid()))
            return new ResultVo(1,"未选择删除的题型");
        String useModule = subjectQuestiontypeService.findUseModule(qtype.getQtypeid());
        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 = subjectQuestiontypeService.delQuestiontype(qtype);
        if (result > 0) return new ResultVo(0,"删除成功");
        else return new ResultVo(1,"删除失败");
    }
}
