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

    <resultMap type="SysTableSet" id="SysTableSetResult">
        <result property="id" column="id"/>
        <result property="tableName" column="table_name"/>
        <result property="userId" column="user_id"/>
        <result property="surface" column="surface"/>
        <result property="label" column="label"/>
        <result property="name" column="name"/>
        <result property="checked" column="checked"/>
        <result property="width" column="width"/>
        <result property="fixed" column="fixed"/>
    </resultMap>

    <sql id="selectSysTableSetVo">
        select id, table_name, user_id, surface, label, fixed, name, checked, width from sys_table_set
    </sql>

    <select id="selectSysTableSetList" parameterType="SysTableSet" resultMap="SysTableSetResult">
        <include refid="selectSysTableSetVo"/>
        <where>
            <if test="tableName != null  and tableName != ''">and table_name = #{tableName}</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="surface != null ">and surface = #{surface}</if>
            <if test="label != null  and label != ''">and label = #{label}</if>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="checked != null ">and checked = #{checked}</if>
            <if test="width != null ">and width = #{width}</if>
            <if test="fixed != null ">and fixed = #{fixed}</if>
        </where>
    </select>

    <select id="selectSysTableSetById" parameterType="Long" resultMap="SysTableSetResult">
        <include refid="selectSysTableSetVo"/>
        where id = #{id}
    </select>

    <select id="selectMaxId" resultType="long">
        SELECT if(MAX(table_id) IS NULL,0,table_id)  AS a FROM sys_table_set
    </select>
    <select id="selectMessage" resultType="com.ruoyi.system.domain.SysTableSet">
        SELECT
	        id,surface,label,name,checked,width
        FROM
	        `sys_table_set`
        WHERE
	        user_id = #{userId}
	    AND table_name = #{tableName}
    </select>

    <insert id="insertSysTableSet" parameterType="SysTableSet" useGeneratedKeys="true" keyProperty="id">
        insert into sys_table_set
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tableName != null">table_name,</if>
            <if test="userId != null">user_id,</if>
            <if test="surface != null">surface,</if>
            <if test="label != null">label,</if>
            <if test="name != null">name,</if>
            <if test="checked != null">checked,</if>
            <if test="width != null">width,</if>
            <if test="fixed != null">fixed,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tableName != null">#{tableName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="surface != null">#{surface},</if>
            <if test="label != null">#{label},</if>
            <if test="name != null">#{name},</if>
            <if test="checked != null">#{checked},</if>
            <if test="width != null">#{width},</if>
            <if test="fixed != null">#{fixed},</if>
        </trim>
    </insert>

    <update id="updateSysTableSet" parameterType="SysTableSet">
        update sys_table_set
        <trim prefix="SET" suffixOverrides=",">
            <if test="tableName != null">table_name = #{tableName},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="surface != null">surface = #{surface},</if>
            <if test="label != null">label = #{label},</if>
            <if test="name != null">name = #{name},</if>
            <if test="checked != null">checked = #{checked},</if>
            <if test="width != null">width = #{width},</if>
            <if test="fixed != null">width = #{fixed},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateTableSetMessage" parameterType="object">
        UPDATE sys_table_set
        <set>
            <if test="table.surface != null">
                surface = #{table.surface},
            </if>
            <if test="table.label != '' || table.label != null">
                label = #{table.label},
            </if>
            <if test="table.name != '' || table.name != null">
                name = #{table.name},
            </if>
            <if test="table.checked != null">
                checked = #{table.checked},
            </if>
            <if test="table.width != null">
                width = #{table.width},
            </if>
        </set>
        WHERE
        user_id = #{table.userId}
        AND table_name = #{table.tableName}
    </update>

    <delete id="deleteSysTableSet" parameterType="SysTableSet">
        delete from sys_table_set where user_id = #{userId} and table_name = #{tableName}
    </delete>

    <delete id="deleteSysTableSetById" parameterType="Long">
        delete from sys_table_set where id = #{id}
    </delete>

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

</mapper>