package com.miaxis.three.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.common.exception.CustomException;
import com.miaxis.question.domain.TAppExamRule;
import com.miaxis.question.service.ITAppExamRuleService;
import com.miaxis.three.domain.ThreeForce;
import com.miaxis.three.mapper.ThreeForceMapper;
import com.miaxis.three.service.IThreeForceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 三力测试Service业务层处理
 *
 * @author miaxis
 * @date 2023-04-03
 */
@Service
public class ThreeForceServiceImpl extends ServiceImpl<ThreeForceMapper, ThreeForce> implements IThreeForceService {
    @Autowired
    private ThreeForceMapper threeForceMapper;

    @Autowired
    private ITAppExamRuleService appExamRuleService;

    /**
     * 查询三力测试列表
     *
     * @param threeForce 三力测试
     * @return 三力测试
     */
    @Override
    public List<ThreeForce> selectThreeForceList(ThreeForce threeForce) {
        return threeForceMapper.selectThreeForceList(threeForce);
    }

    @Override
    public List<ThreeForce> selectTestQuestionInfoList() {

        List<ThreeForce> allList = new ArrayList<ThreeForce>();
        TAppExamRule tAppExamRule = new TAppExamRule();
        tAppExamRule.setGs("test");
        tAppExamRule.setAreacode(0);

        List<TAppExamRule> list = appExamRuleService.selectTAppExamRuleList(tAppExamRule);
        if (list.size() == 0) {
            return allList;
        }
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT * FROM (");
        TAppExamRule er = list.get(0);
        int easyJudge = er.getJudgeCount(); //判断
        int easyChoice = er.getChoiceCount(); //选择
        int total = +easyJudge + easyChoice; //总题数
        //判断题
        sql.append(" (SELECT * FROM three_force WHERE Type=1  ORDER BY RAND() LIMIT " + easyJudge + ") ");
        sql.append(" union ");
        //选择题
        sql.append(" (SELECT * FROM three_force WHERE Type=2 ORDER BY RAND() LIMIT " + easyChoice + ") ");
        sql.append(" ) as db");
        System.out.println(sql.toString());
        allList = threeForceMapper.selectTestQuestionInfoList(sql.toString());
        if (allList.size() == total) {
            return allList;
        } else {
            new CustomException("模拟考试取题出错，请重试。");
            return null;
        }
    }
}
