<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fibo.ddp.common.dao.enginex.risk.EngineResultSetMapper">

     <resultMap type="com.fibo.ddp.common.model.enginex.risk.EngineResultSet" id="engineresultsetMap">
	     <id column="id" property="id"/>
	     <result column="input" property="input"/>    
	     <result column="create_datetime" property="createDatetime"/>
	     <result column="result" property="result"/>    
	     <result column="engine_id" property="engineId"/>
	     <result column="uuid" property="uuid"/>
	     <result column="engine_version" property="engineVersion"/>
	     <result column="engine_name" property="engineName"/>
	     <result column="engine_code" property="engineCode"/>
	     <result column="type" property="type"/>
	     <result column="sub_version" property="subVersion"/>
	     <result column="scorecardscore" property="scorecardscore"/>
	     <result column="batch_no" property="batchNo"/>
	     <result column="datilResult" property="datilResult"/>
	     <result column="start_time" property="startTime"/>
	     <result column="cost_time" property="costTime"/>
		 <result column="hbase_row_key" property="hbaseRowKey"/>
     </resultMap>

    <select id="getResultSetDetailsById" parameterType="Long" resultMap="engineresultsetMap">
		select *
		from t_resultset 
		where id = #{resultSetId} 
	</select>   
     
     <!-- 新增引擎并返回ID -->
     <insert id="insertResultSet" parameterType="com.fibo.ddp.common.model.enginex.risk.EngineResultSet"  useGeneratedKeys="true" keyProperty="id">
	   INSERT INTO 
	       t_resultset
	    <trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="uid != null">
				uid,
			</if>
			<if test="pid != null">
				pid,
			</if>
			<if test="input != null">
	           input,
	        </if>
			<if test="output != null">
				output,
			</if>
			<if test="result != null">
	           result,
	        </if>
	        <if test="engineId != null">
	           engine_id,
	        </if>
	        <if test="uuid != null">
	           uuid,
	        </if>	       
	        <if test="engineVersion != null">
	           engine_version,
	        </if>
	        <if test="engineName != null">
	           engine_name,
	        </if>
	          <if test="engineCode != null">
	           engine_code,
	        </if>
	        <if test="type != null">
	           type,
	        </if>
	        <if test="subVersion != null">
	           sub_version,
	        </if>
	        <if test="scorecardscore != null">
	           scorecardscore,
	        </if>
	        <if test="batchNo != null">
	           batch_no,
	        </if>        
	        <if test="datilResult != null">
	           datilResult,
	        </if>        
	    </trim>
	    values
	     <trim prefix="(" suffix=")" suffixOverrides=",">
	        <if test="uid != null">
	           #{uid},
	        </if>
			 <if test="pid != null">
				 #{pid},
			 </if>
			 <if test="input != null">
				 #{input},
			 </if>
			 <if test="output != null">
				 #{output},
			 </if>
			 <if test="result != null">
	           #{result},
	        </if>
	        <if test="engineId != null">
	           #{engineId},
	        </if>
	        <if test="uuid != null">
	           #{uuid},
	        </if>
	        <if test="engineVersion != null">
	           #{engineVersion},
	        </if>
	        <if test="engineName != null">
	           #{engineName},
	        </if>
	        <if test="engineCode != null">
	           #{engineCode},
	        </if>
	        <if test="type != null">
	           #{type},
	        </if>
	        <if test="subVersion != null">
	            #{subVersion},
	        </if>
	        <if test="scorecardscore != null">
	            #{scorecardscore},
	        </if>
	        <if test="batchNo != null">
	            #{batchNo},
	        </if>	    	         
	        <if test="datilResult != null">
	            #{datilResult},
	        </if>	    	         
	    </trim>
	</insert>
	<select id="getResultSetByList"   parameterType="com.fibo.ddp.common.model.enginex.risk.EngineResultSet" resultMap="engineresultsetMap" >
       SELECT 
            *
         FROM  
            t_engine 
         where 
                status>-1  and
                organ_id =#{organId}
               
		 <if test="startDate != null and startDate !=''">	
		 <![CDATA[ and create_datetime  >=  #{startDate}  ]]>	    
     	 
		 </if>
		 <if test="endDate != null and endDate !=''">		    
     		 <![CDATA[ and create_datetime  <=  #{endDate}  ]]>
		 </if>	
		 order by create_datetime desc		
	</select>
	
	<select id="getEngineResultSetBySegment"   parameterType="map" resultMap="engineresultsetMap" >
       SELECT 
            *
         FROM  
            t_resultset 
         where
		  1=1
		 <if test="engineId != null">
		 <![CDATA[ and engine_id = #{engineId}  ]]>
     	 
		 </if>
		<if test="businessId != null and businessId !=''">
			<![CDATA[ and pid  like  concat('%',#{businessId},'%')  ]]>

		</if>
		<if test="startDate != null and startDate !=''">
			<![CDATA[ and create_datetime  >=  #{startDate}  ]]>

		</if>
		 <if test="endDate != null and endDate !=''">		    
     		 <![CDATA[ and create_datetime <= DATE_ADD(STR_TO_DATE(#{endDate},  '%Y-%m-%d'),interval 1 DAY)  ]]>

		 </if>	
		 order by create_datetime desc		
	</select>
	
	<select id="getResultSetById" parameterType="com.fibo.ddp.common.model.enginex.risk.EngineResultSet" resultMap="engineresultsetMap">
     select  * from t_resultset where  id = #{id} 
  </select>	
  
  	<select id="getBatchTestResultSetByEngineId" parameterType="map" resultMap="engineresultsetMap">
  	    select batch_no, engine_id, engine_name, startTime, costTime
        from(
    	 select rs.batch_no, e.id as engine_id, e.name as engine_name
    	        , min(rs.create_datetime) as start_time
    	        , TIMESTAMPDIFF(second,min(rs.create_datetime),max(rs.create_datetime)) as cost_time
    	   from t_resultset rs, t_engine e
    	  where rs.engine_id = e.id
    	        and ( batch_no is not null and batch_no != '' )
    	        and e.organ_id = ( select organ_id from t_user where user_id = #{userId} )
    	        and rs.engine_id = #{engineId}
    	  group by rs.batch_no, e.id, e.name
    	)x
    	order by x.startTime desc
  	</select>
  	
  	<select id="getBatchTestResultSetByBatchNo" parameterType="map" resultMap="engineresultsetMap">
		select rs.*
          from t_resultset rs, t_engine e
         where rs.engine_id = e.id
               and e.organ_id = ( select organ_id from t_user where user_id = #{userId} )
               and rs.batch_no = #{batchNo}
	</select>

	<update id="updateResultOutput" parameterType="com.fibo.ddp.common.model.enginex.risk.EngineResultSet">
		update t_resultset t set t.`output` = #{output} where t.`id` = #{id}
	</update>

	<select id="countDecisionCallNum" resultType="com.fibo.ddp.common.model.analyse.AnalyseDecisionResult">
		select
	</select>

	<update id="updateResultById">
		UPDATE t_resultset
		set hbase_row_key = #{rowKeyStr}
		where id = #{resultId}
	</update>
</mapper>