<?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.UserGroupMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.entity.UserGroup" id="UserGroupMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="tenantId" column="TENANT_ID" jdbcType="VARCHAR"/>
        <result property="groupName" column="GROUP_NAME" jdbcType="VARCHAR"/>
        <result property="groupState" column="GROUP_STATE" 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="roleId" column="ROLE_ID" jdbcType="INTEGER"/>
    </resultMap>

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

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


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

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="UserGroupMap">
        select
        <include refid="Base_Column_List"/>
        from uims_user_group
        <where>
            <if test="id != null">
                and ID = #{id}
            </if>
            <if test="tenantId != null and tenantId != ''">
                and TENANT_ID = #{tenantId}
            </if>
            <if test="groupName != null and groupName != ''">
                and GROUP_NAME = #{groupName}
            </if>
            <if test="groupState != null">
                and GROUP_STATE = #{groupState}
            </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>
            <if test="roleId != null">
                and ROLE_ID = #{roleId}
            </if>
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into uims_user_group(ID, TENANT_ID, GROUP_NAME, GROUP_STATE, REMARK, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY, ROLE_ID)
        values ( #{id}, #{tenantId}, #{groupName}, #{groupState}, #{remark}, #{status}, #{dateCreate}, #{createBy}, #{dateUpdate}, #{updateBy}, #{roleId})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into uims_user_group(ID, TENANT_ID, GROUP_NAME, GROUP_STATE, REMARK, STATUS, DATE_CREATE, CREATE_BY,
        DATE_UPDATE, UPDATE_BY, ROLE_ID)
        values
        <foreach collection="userGroups" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.tenantId}, #{item.groupName}, #{item.groupState}, #{item.remark}, #{item.status},
            #{item.dateCreate}, #{item.createBy}, #{item.dateUpdate}, #{item.updateBy}, #{item.roleId} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update uims.uims_user_group
        <set>
            <if test="tenantId != null and tenantId != ''">
                TENANT_ID = #{tenantId},
            </if>
            <if test="groupName != null and groupName != ''">
                GROUP_NAME = #{groupName},
            </if>
            <if test="groupState != null">
                GROUP_STATE = #{groupState},
            </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>
            <if test="roleId != null">
                ROLE_ID = #{roleId},
            </if>
        </set>
        where ID = #{id}
    </update>

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


    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from uims_user_group
    </select>
    <select id="selectPage" resultMap="UserGroupMap">
        select
        <include refid="Base_Column_List"/>
        from uims_user_group
        <where>
             STATUS!=0
            <if test="userGroup.id != null">
                and ID = #{userGroup.id}
            </if>
            <if test="userGroup.tenantId != null and userGroup.tenantId != ''">
                and TENANT_ID = #{userGroup.tenantId}
            </if>
            <if test="userGroup.groupName != null and userGroup.groupName != ''">
                and GROUP_NAME  like concat('%', #{userGroup.groupName}, '%')
            </if>
            <if test="userGroup.groupState != null">
                and GROUP_STATE = #{userGroup.groupState}
            </if>
            <if test="userGroup.remark != null and userGroup.remark != ''">
                and REMARK = #{userGroup.remark}
            </if>
            <if test="userGroup.status != null">
                and STATUS = #{userGroup.status}
            </if>
            <if test="userGroup.dateCreate != null">
                and DATE_CREATE = #{userGroup.dateCreate}
            </if>
            <if test="userGroup.createBy != null and userGroup.createBy != ''">
                and CREATE_BY = #{userGroup.createBy}
            </if>
            <if test="userGroup.dateUpdate != null">
                and DATE_UPDATE = #{userGroup.dateUpdate}
            </if>
            <if test="userGroup.updateBy != null and userGroup.updateBy != ''">
                and UPDATE_BY = #{userGroup.updateBy}
            </if>
            <if test="userGroup.roleId != null">
                and ROLE_ID = #{userGroup.roleId}
            </if>
        </where>
    </select>
    <select id="findHasUserGroupUsers" resultType="com.zcxk.entity.UserGroup">
        select  a.id,b.GROUP_NAME groupName from uims_group_user a join uims_user_group b on a.GROUP_ID=b.ID
        where a.GROUP_ID=#{id} and a.status!=0 limit 1
    </select>
    <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.id as roleId
     </sql>
    <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>
    <select id="findUsersByUserGroup" resultType="com.zcxk.entity.UserEntity">
        select distinct a.username as "username" , a.id as "id"
        from uims_user a left join uims_group_user b on a.id=b.USER_ID

        where  a.status!=0
        <if test="id != -1 ">
           and  b.GROUP_ID=#{id} and b.status!=0
        </if>
        <if test="id == -1 ">
            and (b.GROUP_ID is null or b.status =0 )
        </if>
        <if test="tenantId != null and tenantId != ''">
            and a.TENANT_ID = #{tenantId}
        </if>
    </select>
    <update id="deleteUserGroupRelations">
        update uims_group_user set STATUS=0 where STATUS !=0 and GROUP_ID=#{id}
    </update>
    <update id="deleteUserRoles">
       update uims_user_role set STATUS=0 where STATUS!=0 and USER_ID in
        <foreach collection="userGroupDto.userIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>

    </update>
    <!-- 批量新增 -->
    <insert id="batchInsertRoles">
        insert into uims_user_role(ID, USER_ID, ROLE_ID, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY)
        values
        <foreach collection="uimsUserRoles" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.userId}, #{item.roleId}, #{item.status}, #{item.dateCreate}, #{item.createBy},
            #{item.dateUpdate}, #{item.updateBy} )
        </foreach>
    </insert>
</mapper>