<?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=".dao.SysRoleStoreDao">

    <resultMap type=".entity.SysRoleStore" id="SysRoleStoreMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="storeId" column="store_id" jdbcType="INTEGER"/>
        <result property="storeNum" column="store_num" jdbcType="VARCHAR"/>
        <result property="storeName" column="store_name" jdbcType="VARCHAR"/>
        <result property="roleId" column="role_id" jdbcType="INTEGER"/>
        <result property="roleNum" column="role_num" jdbcType="VARCHAR"/>
        <result property="roleName" column="role_name" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="SysRoleStoreMap">
        select
          id, store_id, store_num, store_name, role_id, role_num, role_name
        from sys_role_store
        where id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="SysRoleStoreMap">
        select
          id, store_id, store_num, store_name, role_id, role_num, role_name
        from sys_role_store
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="storeId != null">
                and store_id = #{storeId}
            </if>
            <if test="storeNum != null and storeNum != ''">
                and store_num = #{storeNum}
            </if>
            <if test="storeName != null and storeName != ''">
                and store_name = #{storeName}
            </if>
            <if test="roleId != null">
                and role_id = #{roleId}
            </if>
            <if test="roleNum != null and roleNum != ''">
                and role_num = #{roleNum}
            </if>
            <if test="roleName != null and roleName != ''">
                and role_name = #{roleName}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from sys_role_store
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="storeId != null">
                and store_id = #{storeId}
            </if>
            <if test="storeNum != null and storeNum != ''">
                and store_num = #{storeNum}
            </if>
            <if test="storeName != null and storeName != ''">
                and store_name = #{storeName}
            </if>
            <if test="roleId != null">
                and role_id = #{roleId}
            </if>
            <if test="roleNum != null and roleNum != ''">
                and role_num = #{roleNum}
            </if>
            <if test="roleName != null and roleName != ''">
                and role_name = #{roleName}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into sys_role_store(store_id, store_num, store_name, role_id, role_num, role_name)
        values (#{storeId}, #{storeNum}, #{storeName}, #{roleId}, #{roleNum}, #{roleName})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into sys_role_store(store_id, store_num, store_name, role_id, role_num, role_name)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#{entity.storeId}, #{entity.storeNum}, #{entity.storeName}, #{entity.roleId}, #{entity.roleNum}, #{entity.roleName})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into sys_role_store(store_id, store_num, store_name, role_id, role_num, role_name)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.storeId}, #{entity.storeNum}, #{entity.storeName}, #{entity.roleId}, #{entity.roleNum}, #{entity.roleName})
        </foreach>
        on duplicate key update
        store_id = values(store_id),
        store_num = values(store_num),
        store_name = values(store_name),
        role_id = values(role_id),
        role_num = values(role_num),
        role_name = values(role_name)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update sys_role_store
        <set>
            <if test="storeId != null">
                store_id = #{storeId},
            </if>
            <if test="storeNum != null and storeNum != ''">
                store_num = #{storeNum},
            </if>
            <if test="storeName != null and storeName != ''">
                store_name = #{storeName},
            </if>
            <if test="roleId != null">
                role_id = #{roleId},
            </if>
            <if test="roleNum != null and roleNum != ''">
                role_num = #{roleNum},
            </if>
            <if test="roleName != null and roleName != ''">
                role_name = #{roleName},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from sys_role_store where id = #{id}
    </delete>

</mapper>

