package com.xhkjedu.sstudy.service.subjectbook;

import com.xhkjedu.sstudy.mapper.question.QuestionMapper;
import com.xhkjedu.sstudy.mapper.subjectbook.SubjectMapper;
import com.xhkjedu.sstudy.mapper.subjectbook.SubjectQuestiontypeMapper;
import com.xhkjedu.sstudy.model.subjectbook.TSubjectQuestiontype;
import com.xhkjedu.sstudy.utils.StudyUtil;
import com.xhkjedu.utils.N_Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SubjectQuestiontypeService
 * Description TODO
 * Author WN
 * Date 2020/6/2 11:57
 **/
@Service
public class SubjectQuestiontypeService {
    @Autowired
    private SubjectQuestiontypeMapper subjectQuestiontypeMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Resource
    private QuestionMapper questionMapper;

    /**
     *功能描述 获取科目下所有题型
     * @author WN
     * @date 2020/6/2
     * @param  * @param sqtype
     * @return java.util.List<com.xhkjedu.model.subjectbook.TSubjectQuestiontype>
     */
    public List<TSubjectQuestiontype> listQuestiontypes(TSubjectQuestiontype sqtype){
        return subjectQuestiontypeMapper.listQuestiontypes(sqtype);
    }

    /**
     *功能描述 获取科目下所有题型用于选择数据
     * @author WN
     * @date 2020/6/2
     * @param  * @param sqtype
     * @return java.util.List<com.xhkjedu.vo.subjectbook.SubjectQuestiontypeVo>
     */
    public List<Map> listQuestiontypesForSel(TSubjectQuestiontype sqtype){
        return subjectQuestiontypeMapper.listQuestiontypesForSel(sqtype);
    }

    /**
     *功能描述 添加题型
     * @author WN
     * @date 2020/6/2
     * @param  * @param sqtype
     * @return java.lang.Integer
     */
    public Integer addQuestiontype(TSubjectQuestiontype sqtype){
        String id = StudyUtil.getId();
        sqtype.setDeleted(0);
        sqtype.setQtypeid(id);
        sqtype.setCreatetime(N_Utils.getSecondTimestamp());
        return subjectQuestiontypeMapper.insertSelective(sqtype);
    }

   /**
    *功能描述 修改题型
    * @author WN
    * @date 2020/6/2
    * @param  * @param sqtype
    * @return java.lang.Integer
    */
    public Integer updateQuestiontype(TSubjectQuestiontype sqtype){
        sqtype.setCreatetime(N_Utils.getSecondTimestamp());
        return subjectQuestiontypeMapper.updateByPrimaryKeySelective(sqtype);
    }

    /**
     *功能描述 修改题型
     * @author WN
     * @date 2020/6/2
     * @param  * @param sqtype
     * @return java.lang.Integer
     */
    public Integer delQuestiontype(TSubjectQuestiontype sqtype){
        int result = subjectQuestiontypeMapper.deleteByPrimaryKey(sqtype.getQtypeid());
        return result;
    }

    //查找使用题型的模块
    public String findUseModule(String qtypeid) {
        String useModule = subjectQuestiontypeMapper.findUseModule(qtypeid);
        if (useModule.startsWith(",")) {
            useModule = useModule.substring(1);
        }
        return useModule;
    }

    /**
     * @Description 获取题型，英语学科添加听力题
     * @Param [sqtype]
     * @Return java.util.List<java.util.Map>
     * @Author wn
     * @Date 2022/8/17 10:21
     **/
    public List<Map> listTypesForSearchQuestion(TSubjectQuestiontype sqtype) {
        List<Map> list = subjectQuestiontypeMapper.listQuestiontypesForSel(sqtype);
        //根据科目id获取科目名称
        String  subjectname = subjectMapper.getSubjectnameById(sqtype.getSubjectid());
        if(subjectname.contains("英语")){
            //如果是英语题获取英语题对应试题包含听力题的题型
//            List<Map> heartypes = subjectQuestiontypeMapper.listQtypesHasHear(sqtype);//获取有听力题的题型
            Integer hearnum = questionMapper.getQuestionHasHearNum(sqtype.getSubjectid());
            if(hearnum!=null && hearnum >0){
                Map tlmap = new HashMap();
                tlmap.put("qtypename","听力题");
                tlmap.put("qtypeid","tlt");
                list.add(0,tlmap);
            }
        }
        return list;
    }
}
