package com.xhkjedu.sstudy.controller.subjectbook;

import com.xhkjedu.annotation.Action;
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 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 SubjectQuestiontypeController
 * Description 题型
 * Author WN
 * Date 2020/6/2 14:19
 **/
@RestController
@RequestMapping("qtype")
public class SubjectQuestiontypeController {
    @Autowired
    private SubjectQuestiontypeService subjectQuestiontypeService;

    /*
     * @Description 题型管理列表
     * @Date 2023/11/28 10:29:43
     * @Author WN
     * @Param [qtype]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_manage")
    public ResultVo listManage(@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 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<Map> lst = subjectQuestiontypeService.listQuestiontypesForSel(qtype);
        return new ResultVo(0,"成功获取题型",lst);
    }

    /**
     * @Description 获取题型，英语学科添加听力题
     * @Param [qtype]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/8/17 10:22
     **/
    @PostMapping("/qtypesel")
    public ResultVo listTypesForSearchQuestion(@RequestBody TSubjectQuestiontype qtype){
        N_Utils.validation(new Object[]{qtype.getSubjectid(),"科目",1});
        List<Map> lst = subjectQuestiontypeService.listTypesForSearchQuestion(qtype);
        return new ResultVo(0,"成功获取题型",lst);
    }

    /**
     *功能描述 添加题型
     * @author WN
     * @date 2020/6/2
     * @param  * @param subject
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/add")
    @Action("题型管理-增加")
    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")
    @Action("题型管理-修改")
    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")
    @Action("题型管理-删除")
    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,"删除失败");
    }
}
