<?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.RoleMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.entity.Role" id="RoleMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="tenantId" column="TENANT_ID" jdbcType="VARCHAR"/>
        <result property="roleName" column="ROLE_NAME" jdbcType="VARCHAR"/>
        <result property="description" column="DESCRIPTION" jdbcType="VARCHAR"/>
        <result property="roleState" column="ROLE_STATE" jdbcType="INTEGER"/>
        <result property="permissionType" column="PERMISSION_TYPE" jdbcType="INTEGER"/>
        <result property="remark" column="REMARK" jdbcType="VARCHAR"/>
        <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"/>
        <result property="orgId" column="ORG_ID" jdbcType="VARCHAR"/>
    </resultMap>
    <resultMap type="com.zcxk.dto.RoleDto" id="RoleDtoMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>

        <result property="roleName" column="ROLE_NAME" jdbcType="VARCHAR"/>

        <result property="users" column="users" jdbcType="INTEGER"/>
        <result property="roleState" column="ROLE_STATE" jdbcType="INTEGER"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        ID, TENANT_ID, ROLE_NAME, DESCRIPTION, ROLE_STATE, PERMISSION_TYPE, REMARK, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY    </sql>

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


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

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="RoleMap">
        select
        <include refid="Base_Column_List"/>
        from uims_role
        <where>
            and status =1
            <if test="items != null">
                and (org_id in
                <foreach item="item" index="index" collection="items" open="(" separator="," close=")">
                    <if test="item.orgCompanyId!= null">

                        #{item.orgCompanyId}
                    </if>

                </foreach>
                or org_id in
                <foreach item="item" index="index" collection="items" open="(" separator="," close=")">
                    <if test="item.orgDeparmtmentId!= null">

                        #{item.orgDeparmtmentId}
                    </if>

                </foreach>
                )
            </if>
            <if test="id != null">
                and ID = #{id}
            </if>
            <if test="tenantId != null and tenantId != ''">
                and TENANT_ID = #{tenantId}
            </if>
            <if test="roleName != null and roleName != ''">
                and ROLE_NAME = #{roleName}
            </if>
            <if test="description != null and description != ''">
                and DESCRIPTION = #{description}
            </if>
            <if test="roleState != null">
                and ROLE_STATE = #{roleState}
            </if>
            <if test="permissionType != null">
                and PERMISSION_TYPE = #{permissionType}
            </if>
            <if test="remark != null and remark != ''">
                and REMARK = #{remark}
            </if>
            <if test="status != null">
                and STATUS = #{status}
            </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(ID, TENANT_ID, ROLE_NAME, DESCRIPTION, ROLE_STATE, PERMISSION_TYPE, REMARK, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY,ORG_ID)
        values ( #{id}, #{tenantId}, #{roleName}, #{description}, #{roleState}, #{permissionType}, #{remark}, #{status}, #{dateCreate}, #{createBy}, #{dateUpdate}, #{updateBy},#{orgId})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into uims_role(ID, TENANT_ID, ROLE_NAME, DESCRIPTION, ROLE_STATE, PERMISSION_TYPE, REMARK, STATUS,
        DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY)
        values
        <foreach collection="roles" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.tenantId}, #{item.roleName}, #{item.description}, #{item.roleState},
            #{item.permissionType}, #{item.remark}, #{item.status}, #{item.dateCreate}, #{item.createBy},
            #{item.dateUpdate}, #{item.updateBy} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update uims.uims_role
        <set>
            <if test="tenantId != null and tenantId != ''">
                TENANT_ID = #{tenantId},
            </if>
            <if test="roleName != null and roleName != ''">
                ROLE_NAME = #{roleName},
            </if>
            <if test="description != null and description != ''">
                DESCRIPTION = #{description},
            </if>
            <if test="roleState != null">
                ROLE_STATE = #{roleState},
            </if>
            <if test="permissionType != null">
                PERMISSION_TYPE = #{permissionType},
            </if>
            <if test="remark != null and remark != ''">
                REMARK = #{remark},
            </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 where ID = #{id}
    </delete>



    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from uims_role
    </select>
    <select id="selectPage" resultMap="RoleDtoMap">
        select
        <include refid="Base_Column_List"/>,users
        from uims_role a left join
        (select count(*) users ,ROLE_ID from uims_user_role b
        <where>
            and status!=0
       </where>
       group by b.ROLE_ID ) b
        ON a.id=b.ROLE_ID
        <where>
            and status =1
            <if test="role.items != null">
                and (org_id in
                <foreach item="item" index="index" collection="role.items" open="(" separator="," close=")">
                    <if test="item.orgCompanyId!= null">

                        #{item.orgCompanyId}
                    </if>

                </foreach>
                or org_id in
                <foreach item="item" index="index" collection="role.items" open="(" separator="," close=")">
                    <if test="item.orgDeparmtmentId!= null">

                        #{item.orgDeparmtmentId}
                    </if>

                </foreach>
                )
            </if>

            <if test="role.id != null">
                and ID = #{role.id}
            </if>
            <if test="role.tenantId != null and role.tenantId != ''">
                and TENANT_ID = #{role.tenantId}
            </if>
            <if test="role.roleName != null and role.roleName != ''">
                and ROLE_NAME  like concat('%', #{role.roleName}, '%')
            </if>
            <if test="role.description != null and role.description != ''">
                and DESCRIPTION = #{role.description}
            </if>
            <if test="role.roleState != null">
                and ROLE_STATE = #{role.roleState}
            </if>
            <if test="role.permissionType != null">
                and PERMISSION_TYPE = #{role.permissionType}
            </if>
            <if test="role.remark != null and role.remark != ''">
                and REMARK = #{role.remark}
            </if>
            <if test="role.status != null">
                and STATUS = #{role.status}
            </if>
            <if test="role.dateCreate != null">
                and DATE_CREATE = #{role.dateCreate}
            </if>
            <if test="role.createBy != null and role.createBy != ''">
                and CREATE_BY = #{role.createBy}
            </if>
            <if test="role.dateUpdate != null">
                and DATE_UPDATE = #{role.dateUpdate}
            </if>
            <if test="role.updateBy != null and role.updateBy != ''">
                and UPDATE_BY = #{role.updateBy}
            </if>
        </where>
    </select>
    <select id="findRoleUser" resultType="com.zcxk.entity.Role">
        select a.id,b.ROLE_NAME  from uims_user_role a join uims_role b on a.ROLE_ID=b.ID
         where a.ROLE_ID=#{id} and a.status!=0 limit 1
    </select>
    <sql id="sysAreaJoins">
          left join uims_org com ON com.id = a.company_org_id
          left join uims_org dep ON dep.id = a.dept_org_id
          left join uims_user_role userrole ON userrole.USER_ID=a.id
     </sql>
    <sql id="userColumns">
         a.id as "id" ,
         a.tenant_id as "tenantId" ,
         a.username as "username" ,
         a.phone as "phone" ,
         a.company_org_id as "companyOrgId" ,
         a.dept_org_id as "deptOrgId" ,
         a.photo as "photo" ,
         a.user_type as "userType" ,
         a.enable_state as "enableState" ,
         a.remark as "remark" ,
         a.email as "email" ,
         a.status as "status" ,
         a.date_create as "dateCreate" ,
         a.create_by as "createBy" ,
         a.date_update as "dateUpdate" ,
         a.update_by as "updateBy",
         com.org_name as "companyOrgName" ,
         dep.org_name as "deptOrgName",
         userrole.role_id as roleId
     </sql>
    <select id="findUsersByRole" resultType="com.zcxk.entity.UserEntity">
        select 	<include refid="userColumns"/>
         from  uims_user a
        <include refid="sysAreaJoins"/>
        where a.tenant_id=#{role.tenantId}
          and userrole.role_id=#{role.id}
          and userrole.status=1 and a.status=1
    </select>
    <update id="deleteRoleUser">
        update uims_user_role set status=0 where ROLE_ID=#{id}
    </update>
    <update id="deleteRoleMenu">
        update uims_role_menu set status=0 where ROLE_ID=#{id}
    </update>
</mapper>