<?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.hotent.uc.dao.UcGroupUserDao">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hotent.uc.model.UcGroupUser">
        <id column="ID_" property="id" />
        <result column="GROUP_ID_" property="groupId" />
        <result column="USER_ID" property="userId" />
        <result column="TENANT_ID_" property="tenantId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID_, GROUP_ID_, USER_ID, TENANT_ID_
    </sql>

    <select id="loadUserByGroupId" resultType="java.util.Map">
        SELECT uu.ID_        AS "id",
               uu.FULLNAME_  AS "fullname",
               uu.ACCOUNT_   AS "account",
               uo.NAME_      AS "orgname",
               uo.id_        AS "orgid",
               uo.PATH_NAME_ AS "pathname",
               uo.dem_id_    AS "demid",
               uop.POS_NAME_ AS "postname",
               uou.pos_id_   AS "postid",
               ug.GROUP_NAME   AS "groupname"
        FROM uc_user uu
                 RIGHT JOIN UC_GROUP_USER ugu
                            ON ugu.USER_ID = uu.ID_
                 LEFT JOIN uc_org_user uou
                           ON uou.USER_ID_ = uu.ID_ AND uou.IS_MASTER_ = '1' AND uou.IS_DELE_ = '0'
                                AND uou.DEM_ID_ = #{demId}
                 LEFT JOIN uc_org uo
                           ON uou.ORG_ID_ = uo.ID_
                            AND uo.DEM_ID_ = #{demId}
                 LEFT JOIN UC_ORG_POST uop
                           ON uou.pos_id_ = uop.id_
                 LEFT JOIN UC_GROUP ug ON ugu.GROUP_ID_=ug.ID_
        ${ew.customSqlSegment}
        <if test="@com.hotent.base.ognl.Ognl@isNotEmpty(ew.customSqlSegment)">
            and
        </if>
        <if test="@com.hotent.base.ognl.Ognl@isEmpty(ew.customSqlSegment)">
            where
        </if>
         uu.IS_DELE_ != '1'
          AND uu.STATUS_ = 1
          <if test="!groupIds.isEmpty()">
          AND GROUP_ID_ IN
          <foreach collection="groupIds" open="(" close=")" separator="," item="groupId">
              #{groupId}
          </foreach>
          </if>
          <if test="groupIds.isEmpty()">
              AND GROUP_ID_ IN (
              select ID_
              from uc_group where CREATE_BY_ = #{ew.paramNameValuePairs.userId}
              )
          </if>
        GROUP BY uu.ID_, uu.FULLNAME_, uu.ACCOUNT_, uo.NAME_, uo.id_, uo.PATH_NAME_, uo.dem_id_, uop.POS_NAME_, uou.pos_id_, ugu.SN_, ug.GROUP_NAME
        ORDER BY ugu.SN_
    </select>

</mapper>
