<?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.SysConfigMapper">

    <resultMap type="SysConfig" id="SysConfigResult" autoMapping="true"/>

    <sql id="selectConfigVo">
        select
            config_id,
            config_name,
            config_key,
            config_value,
            config_type,
            create_by,
            create_time,
            update_by,
            update_time,
            remark,
            permission
		from sys_config
    </sql>

    <!-- 查询条件 -->
	<sql id="sqlwhereSearch">
		<where>
			<if test="configId !=null">
				and config_id = #{configId}
			</if>
			<if test="configKey !=null and configKey != ''">
				and config_key = #{configKey}
			</if>
		</where>
	</sql>

    <select id="selectConfig" parameterType="SysConfig" resultMap="SysConfigResult">
        <include refid="selectConfigVo"/>
        <include refid="sqlwhereSearch"/>
    </select>

    <select id="selectConfigList" parameterType="SysConfigQuery" resultMap="SysConfigResult">
        <include refid="selectConfigVo"/>
        <where>
			<if test="configName != null and configName != ''">
				AND config_name like concat('%', #{configName}, '%')
			</if>
			<if test="configType != null and configType != ''">
				AND config_type = #{configType}
			</if>
			<if test="configKey != null and configKey != ''">
				AND config_key like concat('%', #{configKey}, '%')
			</if>
			<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
				and date_format(create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
			</if>
			<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
				and date_format(create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
			</if>
			<if test="configKeys != null and configKeys.size() > 0">
                AND config_key in
                <foreach collection="configKeys" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
			<if test="configIds != null and configIds.size() > 0">
                AND config_id in
                <foreach collection="configIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
		</where>
    </select>

    <select id="selectConfigById" parameterType="Long" resultMap="SysConfigResult">
        <include refid="selectConfigVo"/>
        where config_id = #{configId}
    </select>

    <select id="checkConfigKeyUnique" parameterType="String" resultMap="SysConfigResult">
        <include refid="selectConfigVo"/>
        where config_key = #{configKey} limit 1
    </select>

    <insert id="insertConfig" parameterType="SysConfig">
        insert into sys_config (
			<if test="configName != null and configName != '' ">config_name,</if>
			<if test="configKey != null and configKey != '' ">config_key,</if>
			<if test="configValue != null and configValue != '' ">config_value,</if>
			<if test="configType != null and configType != '' ">config_type,</if>
			<if test="createBy != null and createBy != ''">create_by,</if>
			<if test="remark != null and remark != ''">remark,</if>
			<if test="permission != null">permission,</if>
 			create_time
        )values(
			<if test="configName != null and configName != ''">#{configName},</if>
			<if test="configKey != null and configKey != ''">#{configKey},</if>
			<if test="configValue != null and configValue != ''">#{configValue},</if>
			<if test="configType != null and configType != ''">#{configType},</if>
			<if test="createBy != null and createBy != ''">#{createBy},</if>
			<if test="remark != null and remark != ''">#{remark},</if>
			<if test="permission != null">#{permission},</if>
 			sysdate()
		)
    </insert>

    <update id="updateConfig" parameterType="SysConfig">
        update sys_config
        <set>
            <if test="configName != null and configName != ''">config_name = #{configName},</if>
            <if test="configKey != null and configKey != ''">config_key = #{configKey},</if>
            <if test="configValue != null and configValue != ''">config_value = #{configValue},</if>
            <if test="configType != null and configType != ''">config_type = #{configType},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="permission != null">permission = #{permission},</if>
 			update_time = sysdate()
        </set>
        where config_id = #{configId}
    </update>

    <update id="batchUpdateValue">
        update sys_config
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="config_value = CASE config_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.configValue != null">
                        WHEN #{item.configId} THEN #{item.configValue}
                    </when>
                    <otherwise>
                        WHEN #{item.configId} THEN config_value
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        WHERE config_id IN
        <foreach open="(" collection="list" item="item" close=")" separator=",">
            #{item.configId}
        </foreach>
    </update>

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

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

</mapper>
