<?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.github.wxiaoqi.security.admin.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.github.wxiaoqi.security.admin.entity.User">
    <!--
      WARNING - @mbggenerated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="birthday" jdbcType="VARCHAR" property="birthday" />
    <result column="address" jdbcType="VARCHAR" property="address" />
    <result column="mobile_phone" jdbcType="VARCHAR" property="mobilePhone" />
    <result column="tel_phone" jdbcType="VARCHAR" property="telPhone" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="sex" jdbcType="CHAR" property="sex" />
    <result column="type" jdbcType="CHAR" property="type" />
    <result column="status" jdbcType="SMALLINT" property="status" />
    <result column="description" jdbcType="VARCHAR" property="description" />
    <result column="crt_time" jdbcType="DATE" property="crtTime" />
    <result column="crt_user" jdbcType="VARCHAR" property="crtUser" />
    <result column="crt_name" jdbcType="VARCHAR" property="crtName" />
    <result column="crt_host" jdbcType="VARCHAR" property="crtHost" />
    <result column="upd_time" jdbcType="DATE" property="updTime" />
    <result column="upd_user" jdbcType="VARCHAR" property="updUser" />
    <result column="upd_name" jdbcType="VARCHAR" property="updName" />
    <result column="upd_host" jdbcType="VARCHAR" property="updHost" />
    <result column="data_all" jdbcType="SMALLINT" property="dataAll" />
    <result column="attr1" jdbcType="VARCHAR" property="attr1" />
    <result column="attr2" jdbcType="VARCHAR" property="attr2" />
    <result column="attr3" jdbcType="VARCHAR" property="attr3" />
    <result column="attr4" jdbcType="VARCHAR" property="attr4" />
    <result column="attr5" jdbcType="VARCHAR" property="attr5" />
    <result column="attr6" jdbcType="VARCHAR" property="attr6" />
    <result column="attr7" jdbcType="VARCHAR" property="attr7" />
    <result column="attr8" jdbcType="VARCHAR" property="attr8" />
  </resultMap>
  
  <select id="selectMemberByGroupId" resultMap="BaseResultMap">
    select u.* from base_user u
    left join  base_group_member gm
    on gm.user_id = u.id where gm.group_id = #{groupId}
  </select>

  <select id="selectLeaderByGroupId" resultMap="BaseResultMap">
    select u.* from base_user u
    left join  base_group_leader gm
    on gm.user_id = u.id where gm.group_id = #{groupId}
  </select>

  <select id="getUserByUid" resultMap="BaseResultMap">
    select u.* from base_user u
    left join  base_user_relation r
    on r.user_id = u.id where r.to_uid = #{to_uid} limit 1
  </select>

<!--  <select id="userPage" resultMap="BaseResultMap">-->
<!--    select u.* from base_user u-->
<!--                      left join  base_group_leader gm-->
<!--                                 on gm.user_id = u.id where gm.group_id = #{groupId}-->
<!--  </select>-->
  <select id="selectList" parameterType="com.github.wxiaoqi.security.admin.dto.BaseUserDTO" resultType="com.github.wxiaoqi.security.admin.vo.BaseUserVo">
    SELECT
    u.*,
    c.`name` as companyName,
    IFNULL(i.`name`,ci.`name`) as corporationName,
    if(i.id > 0,i.id,u.corporation_id)  as corporationId
    FROM base_user  u
    LEFT JOIN  branch_company  c ON u.company_id=c.id
    LEFT JOIN company_info i ON c.company_id=i.id
    LEFT JOIN company_info ci ON u.corporation_id=ci.id
    <where>
        u.is_del = 0
      <if test="status != null">
        and u.status = #{status}
      </if>
      <if test="bizType != null">
        and u.biz_type = #{bizType}
      </if>
      <if test="companyId != null and companyId > 0">
        and u.company_id = #{companyId}
      </if>
      <if test="corporationId != null  and corporationId > 0 ">
        and i.id = #{corporationId}
      </if>
      <if test="name != null and name != '' ">
        and ( u.name like  concat('%',#{name},'%') or  u.username like  concat('%',#{name},'%') )
      </if>
      <if test="dataCompanyIds != null and dataCompanyIds.size > 0">
        and u.company_id in
        <foreach collection="dataCompanyIds" item="id" open="(" separator="," close=")">
          #{id}
        </foreach>
      </if>
      <if test="dataCorporationIds != null and dataCorporationIds.size > 0">
        and ( i.id  in
        <foreach collection="dataCorporationIds" item="id" open="(" separator="," close=")">
          #{id}
        </foreach>
         or ( u.corporation_id  in
        <foreach collection="dataCorporationIds" item="id" open="(" separator="," close=")">
          #{id}
        </foreach>
         and u.company_id = 0  )
           )
      </if>
    </where>
    order  by  u.id desc
  </select>
</mapper>