<?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.ruoyi.system.mapper.QueryConfigMapper">

    <resultMap type="QueryConfig" id="QueryConfigResult">
        <result property="configId" column="config_id"/>
        <result property="queryNumber" column="query_number"/>
        <result property="qrUnit" column="qr_unit"/>
        <result property="qrTime" column="qr_time"/>
        <result property="queryType" column="query_type"/>
        <result property="queryMode" column="query_mode"/>
        <result property="status" column="status"/>
        <result property="isDel" column="is_del"/>
        <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="remark" column="remark"/>
    </resultMap>

    <sql id="selectQueryConfigVo">
        select config_id, query_number, qr_unit, qr_time, query_type, query_mode, status, is_del, create_by, create_time, update_by, update_time, remark from query_config
    </sql>

    <select id="selectQueryConfigList" parameterType="QueryConfig" resultMap="QueryConfigResult">
        <include refid="selectQueryConfigVo"/>
        <where>
            is_del = 'N'
            <if test="qrTime != null  and qrTime != ''">and qr_time = #{qrTime}</if>
            <if test="queryType != null  and queryType != ''">and query_type = #{queryType}</if>
            <if test="queryMode != null  and queryMode != ''">and query_mode = #{queryMode}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
        </where>
    </select>

    <select id="selectQueryConfig" parameterType="QueryConfig" resultMap="QueryConfigResult">
        <include refid="selectQueryConfigVo"/>
        <where>
            status = 'N'
            and is_del = 'N'
            <if test="queryType != null  and queryType != ''">and query_type = #{queryType}</if>
            <if test="queryMode != null  and queryMode != ''">and query_mode = #{queryMode}</if>
        </where>
        order by create_time DESC
    </select>

    <select id="selectQueryConfigByConfigId" parameterType="Long" resultMap="QueryConfigResult">
        <include refid="selectQueryConfigVo"/>
        where config_id = #{configId} and is_del = 'N'
    </select>

    <insert id="insertQueryConfig" parameterType="QueryConfig" useGeneratedKeys="true" keyProperty="configId">
        insert into query_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="queryNumber != null">query_number,</if>
            <if test="qrUnit != null and qrUnit != ''">qr_unit,</if>
            <if test="qrTime != null and qrTime != ''">qr_time,</if>
            <if test="queryType != null">query_type,</if>
            <if test="queryMode != null">query_mode,</if>
            <if test="status != null">status,</if>
            <if test="isDel != null">is_del,</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>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="queryNumber != null">#{queryNumber},</if>
            <if test="qrUnit != null and qrUnit != ''">#{qrUnit},</if>
            <if test="qrTime != null and qrTime != ''">#{qrTime},</if>
            <if test="queryType != null">#{queryType},</if>
            <if test="queryMode != null">#{queryMode},</if>
            <if test="status != null">#{status},</if>
            <if test="isDel != null">#{isDel},</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>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateQueryConfig" parameterType="QueryConfig">
        update query_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="queryNumber != null">query_number = #{queryNumber},</if>
            <if test="qrUnit != null and qrUnit != ''">qr_unit = #{qrUnit},</if>
            <if test="qrTime != null and qrTime != ''">qr_time = #{qrTime},</if>
            <if test="queryType != null">query_type = #{queryType},</if>
            <if test="queryMode != null">query_mode = #{queryMode},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isDel != null">is_del = #{isDel},</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>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where config_id = #{configId}
    </update>

    <delete id="deleteQueryConfigByConfigId" parameterType="Long">
        delete from query_config where config_id = #{configId}
    </delete>

    <delete id="deleteQueryConfigByConfigIds" parameterType="String">
        delete from query_config where config_id in
        <foreach item="configId" collection="array" open="(" separator="," close=")">
            #{configId}
        </foreach>
    </delete>

    <select id="selectQueryConfigByTypeAndMode" parameterType="QueryConfig" resultType="int">
        select count(1) from query_config
        <where>query_type = #{queryType} and query_mode = #{queryMode} and is_del = 'N'
            <if test="configId != null">and config_id not in (#{configId})</if>
        </where>
    </select>
</mapper>