<?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.qryord.project.business.query.mapper.TQueryMapper">
    
    <resultMap type="TQuery" id="TQueryResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="qryTime"    column="qry_time"    />
        <result property="queryType"    column="query_type"    />
        <result property="qryStatus"    column="qry_status"    />
        <result property="modelId"    column="model_id"    />
        <result property="modelName"    column="model_name"    />
        <result property="serialNo"    column="serial_no"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="userName"    column="user_name"    />
    </resultMap>

    <sql id="selectTQueryVo">
        select qry.id, qry.user_id, qry.qry_time, qry.query_type, qry.qry_status, qry.model_id, qry.model_name, qry.serial_no, qry.del_flag, qry.create_by, qry.create_time, qry.update_by, qry.update_time,
               su.user_name
        from t_query qry
        inner join sys_user su on qry.user_id = su.user_id
    </sql>

    <select id="selectTQueryList" parameterType="TQuery" resultMap="TQueryResult">
        <include refid="selectTQueryVo"/>
        <where>  
            <if test="userId != null "> and qry.user_id = #{userId}</if>
            <if test="params.beginTime != null and params.beginTime != ''"> and DATE_FORMAT(qry.qry_time,'%Y-%m-%d %H:%i:%S') <![CDATA[ >= ]]> DATE_FORMAT(#{params.beginTime},'%Y-%m-%d %H:%i:%S')</if>
            <if test="params.endTime != null and params.endTime != ''"> and DATE_FORMAT(qry.qry_time,'%Y-%m-%d %H:%i:%S') <![CDATA[ <= ]]> DATE_FORMAT(#{params.endTime},'%Y-%m-%d %H:%i:%S') </if>
            <if test="queryType != null  and queryType != ''"> and qry.query_type = #{queryType}</if>
            <if test="qryStatus != null  and qryStatus != ''"> and qry.qry_status = #{qryStatus}</if>
            <if test="modelId != null "> and qry.model_id = #{modelId}</if>
            <if test="modelName != null  and modelName != ''"> and qry.model_name like concat('%', #{modelName}, '%')</if>
            <if test="serialNo != null  and serialNo != ''"> and qry.serial_no = #{serialNo}</if>
            <if test="delFlag != null and delFlag != ''"> and qry.del_flag = #{delFlag}</if>
        </where>
        order by qry_time desc
    </select>
    
    <select id="selectTQueryById" parameterType="Long" resultMap="TQueryResult">
        <include refid="selectTQueryVo"/>
        where qry.id = #{id}
    </select>
        
    <insert id="insertTQuery" parameterType="TQuery" useGeneratedKeys="true" keyProperty="id">
        insert into t_query
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="qryTime != null">qry_time,</if>
            <if test="queryType != null">query_type,</if>
            <if test="qryStatus != null">qry_status,</if>
            <if test="modelId != null">model_id,</if>
            <if test="modelName != null">model_name,</if>
            <if test="serialNo != null">serial_no,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="qryTime != null">#{qryTime},</if>
            <if test="queryType != null">#{queryType},</if>
            <if test="qryStatus != null">#{qryStatus},</if>
            <if test="modelId != null">#{modelId},</if>
            <if test="modelName != null">#{modelName},</if>
            <if test="serialNo != null">#{serialNo},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateTQuery" parameterType="TQuery">
        update t_query
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="qryTime != null">qry_time = #{qryTime},</if>
            <if test="queryType != null">query_type = #{queryType},</if>
            <if test="qryStatus != null">qry_status = #{qryStatus},</if>
            <if test="modelId != null">model_id = #{modelId},</if>
            <if test="modelName != null">model_name = #{modelName},</if>
            <if test="serialNo != null">serial_no = #{serialNo},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTQueryById" parameterType="Long">
        update t_query
        set del_flag = '2'
        where id = #{id}
    </update>

    <update id="deleteTQueryByIds" parameterType="String">
        update t_query
        set del_flag = '2'
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>