<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Api id="__4hvvZVpaPFFxpjLeGIt">
  <name>获取试卷</name>
  <parentId>__LgZds5MVHW3o08T7UcX</parentId>
  <applicationid>__LgZds5MVHW3o08T7UcX</applicationid>
  <status>published</status>
  <apiGroup></apiGroup>
  <encode>UTF-8</encode>
  <requestType>get</requestType>
  <requestUrl>/lesson/getPaper/{textId}</requestUrl>
  <tags></tags>
  <responseType>json</responseType>
  <responseScript><![CDATA[(function(){
  var textId=getParameter("textId");  
  var result = new Packages.java.util.HashMap(); //创建指定类对象
  try{
    var tracking = "";
    var isClearRedis=getParameter("isClearRedis");
    var generateExamPageCount=getParameter("generateExamPageCount");
    var redis_exam_pageKey = "v5.exam:page:"+textId;
    var redisConfigKey = "v5.exam:config:"+textId;
    var redisKey_single="v5.examId:single:questions:"+textId;
    var redisKey_multiple="v5.examId:multiple:questions:"+textId;
    var redisKey_judge="v5.examId:judge:questions:"+textId;

    if(getParameter("getSize")=="1")
    {
      var redis_exam_pageKeySize = getRedisSetSize(redis_exam_pageKey);
      result.put("size",redis_exam_pageKeySize);
      return result;
    }

    if(isClearRedis==1)
    {
      tracking+="clear redis;";
      removeFromRedis(redis_exam_pageKey);
      removeFromRedis(redisConfigKey);
      removeFromRedis(redisKey_single);
      removeFromRedis(redisKey_multiple);
      removeFromRedis(redisKey_judge);
      tracking+="clear redis;done";
      result.put("redismsg","redis清除成功");
      //return result
    }

    if(isClearRedis!="1" && !(isNotNull(generateExamPageCount)) )
    {
      var exam_pageSet = getRandomDataFromRedisSet(redis_exam_pageKey,1);
      if((exam_pageSet!=null && exam_pageSet.size()>0))
      {
        return exam_pageSet.get(0);
      }
    }

    var config = getDataFromRedis(redisConfigKey);
    if(config==null)
    {
      tracking+="get tlk_test_paper_base_info from db";
      var sqlr="select item_singleNum,item_multipleNum,item_judgeNum,item_singleScore,item_multipleScore,item_judgeScore,item_testTopicBank from tlk_test_paper_base_info where id='"+textId+"'";
      var datas = queryByDSName("organizationAuth",sqlr);
      if(datas!=null && datas!=[]){
        for(var iterator = datas.iterator();iterator.hasNext();){
          var map1 = iterator.next();//取值
          var singleNum= map1.get("item_singleNum");
          var multipleNum = map1.get("item_multipleNum");
          var judgeNum = map1.get("item_judgeNum");
          var singleScore = map1.get("item_singleScore");
          var multipleScore = map1.get("item_multipleScore");
          var judgeScore = map1.get("item_judgeScore");
          var testTopicBank = map1.get("item_testTopicBank");
          var singleNum= singleNum.intValue();
          var multipleNum= multipleNum.intValue();
          var judgeNum= judgeNum.intValue();
        }
        config = new Packages.java.util.HashMap();
        config.put("singleNum",singleNum);
        config.put("multipleNum",multipleNum);
        config.put("judgeNum",judgeNum);
        config.put("singleScore",singleScore);
        config.put("multipleScore",multipleScore);
        config.put("judgeScore",judgeScore);
        config.put("testTopicBank",testTopicBank);
        config.put("singleNum",singleNum);
        config.put("multipleNum",multipleNum);
        config.put("judgeNum",judgeNum);
        setDataToRedis(redisConfigKey,config,60000);
      }else
      {
        result.put("type","-1");
        result.put("msg","试卷配置信息无效");
        return result;
      }
    }
    println(config);
    var singleNum= config.get("singleNum");
    var multipleNum = config.get("multipleNum");
    var judgeNum = config.get("judgeNum");
    var singleScore = config.get("singleScore");
    var multipleScore = config.get("multipleScore");
    var judgeScore = config.get("judgeScore");
    var testTopicBank = config.get("testTopicBank");
    var singleNum= singleNum.intValue();
    var multipleNum= multipleNum.intValue();
    var judgeNum= judgeNum.intValue();

    var singleSet = getRandomDataFromRedisSet(redisKey_single,singleNum);
    var multipleSet = getRandomDataFromRedisSet(redisKey_multiple,multipleNum);
    var judgeSet = getRandomDataFromRedisSet(redisKey_judge,judgeNum);
    if((singleSet==null  || singleSet==[] || singleSet.size()==0) && 
       (multipleSet==null  || multipleSet==[] || multipleSet.size()==0)&& 
       (judgeSet==null  || judgeSet==[] || judgeSet.size()==0))
    {
      tracking+="get questions from db";
      var wheretopicBankId=" ";
      var where =" where 1=1 ";
      if(isNotNull(testTopicBank)){
        wheretopicBankId +=" and t3.ID = '"+testTopicBank+"' ";
      }
      //单选题sql
      var singleSql = 
          "SELECT'"+getDomainid()+"' as domainId,"+
          "	y.ID,"+
          "	y.ITEM_ID,"+
          "	y.ITEM_QUIZTOPIC,"+
          "	CASE y.ITEM_QUIZTYPE WHEN '0' THEN '单选题' WHEN '1' THEN '多选题' ELSE '判断题' END ITEM_QUIZTYPE,"+
          "	case y.ITEM_ANSWER when '对' then 'A' when '错' then 'B' else y.ITEM_ANSWER end ITEM_ANSWER,"+
          "	y.ITEM_QUIZANALYSIS,"+
          "	y.ITEM_QUIZCODE,"+
          "	y.ITEM_MYSCORE,"+
          "	y.ITEM_OPTION "+
          " FROM "+
          "	( "+
          " ( "+
          "	SELECT "+
          "		t.* "+
          "	FROM"+
          "		("+
          "		SELECT"+
          "			'"+getDomainid()+"' AS domainid,"+
          "			t4.ID,"+
          "			t4.ID AS ITEM_ID,"+
          "			t4.ITEM_QUIZTOPIC,"+
          "			t4.ITEM_QUIZTYPE,"+
          "			t4.ITEM_ANSWER,"+
          "'"+singleScore+"' AS ITEM_MYSCORE,"+
          "			t4.ITEM_QUIZCODE,"+
          "			t4.ITEM_QUIZANALYSIS,"+
          "			GROUP_CONCAT( IFNULL(t5.ITEM_OPTIONNAME,''), '#$#', IFNULL(t5.ITEM_OPTIONCONTENT,'') ,'#$#',IFNULL(t5.item_optionImage,'') ORDER BY t5.ITEM_OPTIONNAME , '.', t5.ITEM_OPTIONCONTENT Separator '$$') AS ITEM_OPTION,"+
          "			t5.ITEM_OPTIONIMAGE "+
          "		FROM"+
          "			organizationauth.tlk_topic_bank_base_info t3"+
          "			INNER JOIN organizationauth.tlk_test_quiz_base_info t4 ON t3.ID = t4.ITEM_TOPICBANKID"+
          "			LEFT JOIN organizationauth.tlk_test_quiz_option t5 ON t5.ITEM_QUIZID = t4.ID  where ITEM_QUIZTYPE = '0'"+wheretopicBankId+
          "		GROUP BY"+
          "			t4.ID "+
          " ORDER BY "+
          "			t4.ID  "+
          "		) t "+
          "	)  "+
          " ) y "+where;

      //多选题sql
      var sql2 = 
          "SELECT'"+getDomainid()+"' as domainId,"+
          "  y.ID,"+
          "  y.ITEM_ID,"+
          "  y.ITEM_QUIZTOPIC,"+
          "  CASE y.ITEM_QUIZTYPE WHEN '0' THEN '单选题' WHEN '1' THEN '多选题' ELSE '判断题' END ITEM_QUIZTYPE,"+
          "  case y.ITEM_ANSWER when '对' then 'A' when '错' then 'B' else y.ITEM_ANSWER end ITEM_ANSWER,"+
          "  y.ITEM_QUIZANALYSIS,"+
          "  y.ITEM_QUIZCODE,"+
          "  y.ITEM_MYSCORE,"+
          "  y.ITEM_OPTION "+
          " FROM "+
          "	( "+
          " ( "+
          "  SELECT "+
          "    t.* "+
          "  FROM"+
          "    ("+
          "    SELECT"+
          "      '"+getDomainid()+"' AS domainid,"+
          "      t4.ID,"+
          "      t4.ID AS ITEM_ID,"+
          "      t4.ITEM_QUIZTOPIC,"+
          "      t4.ITEM_QUIZTYPE,"+
          "      t4.ITEM_ANSWER,"+
          "'"+multipleScore+"' AS ITEM_MYSCORE,"+
          "      t4.ITEM_QUIZCODE,"+
          "      t4.ITEM_QUIZANALYSIS,"+
          "      GROUP_CONCAT( IFNULL(t5.ITEM_OPTIONNAME,''), '#$#', IFNULL(t5.ITEM_OPTIONCONTENT,'') ,'#$#',IFNULL(t5.item_optionImage,'') ORDER BY t5.ITEM_OPTIONNAME , '.', t5.ITEM_OPTIONCONTENT Separator '$$') AS ITEM_OPTION,"+
          "      t5.ITEM_OPTIONIMAGE "+
          "    FROM"+
          "      organizationauth.tlk_topic_bank_base_info t3"+
          "      INNER JOIN organizationauth.tlk_test_quiz_base_info t4 ON t3.ID = t4.ITEM_TOPICBANKID"+
          "      LEFT JOIN organizationauth.tlk_test_quiz_option t5 ON t5.ITEM_QUIZID = t4.ID  where ITEM_QUIZTYPE = '1'"+wheretopicBankId+
          "    GROUP BY"+
          "      t4.ID "+
          " ORDER BY "+
          "      t4.ID  "+
          "    ) t "+
          "  )  "+
          " ) y "+where;
      //判断题sql
      var sql3 = 
          "SELECT'"+getDomainid()+"' as domainId,"+
          "	y.ID,"+
          "	y.ITEM_ID,"+
          "	y.ITEM_QUIZTOPIC,"+
          "	CASE y.ITEM_QUIZTYPE WHEN '0' THEN '单选题' WHEN '1' THEN '多选题' ELSE '判断题' END ITEM_QUIZTYPE,"+
          "	case y.ITEM_ANSWER when '对' then 'A' when '错' then 'B' else y.ITEM_ANSWER end ITEM_ANSWER,"+
          "	y.ITEM_QUIZANALYSIS,"+
          "	y.ITEM_QUIZCODE,"+
          "	y.ITEM_MYSCORE,"+
          "	y.ITEM_OPTION "+
          " FROM "+
          "	( "+
          " ( "+
          "	SELECT "+
          "		t.* "+
          "	FROM"+
          "		("+
          "		SELECT"+
          "			'"+getDomainid()+"' AS domainid,"+
          "      t4.ID,"+
          "      t4.ID AS ITEM_ID,"+
          "      t4.ITEM_QUIZTOPIC,"+
          "      t4.ITEM_QUIZTYPE,"+
          "      t4.ITEM_ANSWER,"+
          "'"+judgeScore+"' AS ITEM_MYSCORE,"+
          "      t4.ITEM_QUIZCODE,"+
          "      t4.ITEM_QUIZANALYSIS,"+
          "      GROUP_CONCAT( IFNULL(t5.ITEM_OPTIONNAME,''), '#$#', IFNULL(t5.ITEM_OPTIONCONTENT,'') ,'#$#',IFNULL(t5.item_optionImage,'') ORDER BY t5.ITEM_OPTIONNAME , '.', t5.ITEM_OPTIONCONTENT Separator '$$') AS ITEM_OPTION,"+
          "      t5.ITEM_OPTIONIMAGE "+
          "    FROM"+
          "      organizationauth.tlk_topic_bank_base_info t3"+
          "      INNER JOIN organizationauth.tlk_test_quiz_base_info t4 ON t3.ID = t4.ITEM_TOPICBANKID"+
          "      LEFT JOIN organizationauth.tlk_test_quiz_option t5 ON t5.ITEM_QUIZID = t4.ID  where ITEM_QUIZTYPE = '2'"+wheretopicBankId+
          "    GROUP BY"+
          "      t4.ID "+
          " ORDER BY "+
          "      t4.ID  "+
          "    ) t "+
          "  )  "+
          " ) y "+where;


      var single_sqlDatas = queryByDSName("organizationAuth", singleSql);
      var multi_sqlDatas = queryByDSName("organizationAuth", sql2);
      var judge_sqlDatas = queryByDSName("organizationAuth", sql3);


      setDataToRedisForSet(redisKey_single,single_sqlDatas);
      setDataToRedisForSet(redisKey_multiple,multi_sqlDatas);
      setDataToRedisForSet(redisKey_judge,judge_sqlDatas);

      singleSet = getRandomDataFromRedisSet(redisKey_single,singleNum);
      multipleSet = getRandomDataFromRedisSet(redisKey_multiple,multipleNum);
      judgeSet = getRandomDataFromRedisSet(redisKey_judge,judgeNum);
    }

    singleSet = unionCollection(singleSet,multipleSet);
    singleSet = unionCollection(singleSet,judgeSet);
    if(tracking=="")
    {
      tracking="all from redis";
    }

    if(true)
    {
      addDataToRedisForSet(redis_exam_pageKey, singleSet);
      if (!isNotNull(generateExamPageCount)){
        generateExamPageCount = 100;
      }
      if(isNotNull(generateExamPageCount) && generateExamPageCount>1){ 
        for(var i=1;i<generateExamPageCount;i++)
        {
          var tmp_singleSet = getRandomDataFromRedisSet(redisKey_single,singleNum);
          var tmp_multipleSet = getRandomDataFromRedisSet(redisKey_multiple,multipleNum);
          var tmp_judgeSet = getRandomDataFromRedisSet(redisKey_judge,judgeNum);
          tmp_singleSet = unionCollection(tmp_singleSet,tmp_multipleSet);
          tmp_singleSet = unionCollection(tmp_singleSet,tmp_judgeSet);
          addDataToRedisForSet(redis_exam_pageKey, tmp_singleSet);
          // result.put(redis_exam_pageKey+":"+i, tmp_singleSet);
          // result.put("dynamicSize: " , getRedisSetSize(redis_exam_pageKey));
        }
        result.put("generateExamPageCount",generateExamPageCount);
        var redis_exam_pageKeySize = getRedisSetSize(redis_exam_pageKey);
        result.put("size",redis_exam_pageKeySize);
        return result;
      }
    }
    return singleSet;
  }catch(e){
    result.put("type","-1");
    result.put("exception",e);
    result.put("msg","请传入正确的试卷id");
    return result;
  }
})()
]]></responseScript>
</Api>