<?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.zcxk.dao.RoleOrgMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.entity.RoleOrg" id="RoleOrgMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="roleId" column="ROLE_ID" jdbcType="INTEGER"/>
        <result property="orgId" column="ORG_ID" jdbcType="INTEGER"/>
        <result property="status" column="STATUS" jdbcType="INTEGER"/>
        <result property="dateCreate" column="DATE_CREATE" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="CREATE_BY" jdbcType="VARCHAR"/>
        <result property="dateUpdate" column="DATE_UPDATE" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="UPDATE_BY" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        ID, ROLE_ID, ORG_ID, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY    </sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="RoleOrgMap">
        select
        <include refid="Base_Column_List"/>
        from uims_role_org
        where ID = #{id}
    </select>


    <!-- 查询全部 -->
    <select id="selectAll" resultMap="RoleOrgMap">
        select
        <include refid="Base_Column_List"/>
        from uims_role_org
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="RoleOrgMap">
        select
        <include refid="Base_Column_List"/>
        from uims_role_org
        <where>
            and status != 0
            <if test="id != null">
                and ID = #{id}
            </if>
            <if test="roleId != null">
                and ROLE_ID = #{roleId}
            </if>
            <if test="orgId != null">
                and ORG_ID = #{orgId}
            </if>

            <if test="dateCreate != null">
                and DATE_CREATE = #{dateCreate}
            </if>
            <if test="createBy != null and createBy != ''">
                and CREATE_BY = #{createBy}
            </if>
            <if test="dateUpdate != null">
                and DATE_UPDATE = #{dateUpdate}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and UPDATE_BY = #{updateBy}
            </if>
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into uims_role_org(ID, ROLE_ID, ORG_ID, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY)
        values ( #{id}, #{roleId}, #{orgId}, #{status}, #{dateCreate}, #{createBy}, #{dateUpdate}, #{updateBy})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into uims_role_org(ID, ROLE_ID, ORG_ID, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY)
        values
        <foreach collection="roleOrgs" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.roleId}, #{item.orgId}, #{item.status}, #{item.dateCreate}, #{item.createBy},
            #{item.dateUpdate}, #{item.updateBy} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update uims.uims_role_org
        <set>
            <if test="roleId != null">
                ROLE_ID = #{roleId},
            </if>
            <if test="orgId != null">
                ORG_ID = #{orgId},
            </if>
            <if test="status != null">
                STATUS = #{status},
            </if>
            <if test="dateCreate != null">
                DATE_CREATE = #{dateCreate},
            </if>
            <if test="createBy != null and createBy != ''">
                CREATE_BY = #{createBy},
            </if>
            <if test="dateUpdate != null">
                DATE_UPDATE = #{dateUpdate},
            </if>
            <if test="updateBy != null and updateBy != ''">
                UPDATE_BY = #{updateBy},
            </if>
        </set>
        where ID = #{id}
    </update>

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


    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from uims_role_org
    </select>
    <select id="selectPage" resultMap="RoleOrgMap">
        select
        <include refid="Base_Column_List"/>
        from uims_role_org
        <where>
            <if test="roleOrg.id != null">
                and ID = #{roleOrg.id}
            </if>
            <if test="roleOrg.roleId != null">
                and ROLE_ID = #{roleOrg.roleId}
            </if>
            <if test="roleOrg.orgId != null">
                and ORG_ID = #{roleOrg.orgId}
            </if>
            <if test="roleOrg.status != null">
                and STATUS = #{roleOrg.status}
            </if>
            <if test="roleOrg.dateCreate != null">
                and DATE_CREATE = #{roleOrg.dateCreate}
            </if>
            <if test="roleOrg.createBy != null and roleOrg.createBy != ''">
                and CREATE_BY = #{roleOrg.createBy}
            </if>
            <if test="roleOrg.dateUpdate != null">
                and DATE_UPDATE = #{roleOrg.dateUpdate}
            </if>
            <if test="roleOrg.updateBy != null and roleOrg.updateBy != ''">
                and UPDATE_BY = #{roleOrg.updateBy}
            </if>
        </where>
    </select>
    <update id="deleteOrgRole">
        update uims_role_org set STATUS=0 where
        ROLE_ID=#{id}
    </update>
</mapper>