<?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.gxwebsoft.common.system.mapper.UserMapper">

  <!-- 性别字典查询sql -->
  <sql id="selectSexDictSql">
    SELECT ta.*
    FROM sys_dictionary_data ta
           LEFT JOIN sys_dictionary tb
                     ON ta.dict_id = tb.dict_id
                       AND tb.deleted = 0
    WHERE ta.deleted = 0
      AND tb.dict_code = 'sex'
  </sql>

  <!-- 用户角色查询sql -->
  <sql id="selectUserRoleSql">
    SELECT a.user_id,
           GROUP_CONCAT(b.role_name) role_name
    FROM sys_user_role a
           LEFT JOIN sys_role b ON a.role_id = b.role_id
    GROUP BY a.user_id
  </sql>

  <!-- 关联查询sql -->
  <sql id="selectSql">
    SELECT a.*,
    b.organization_name,
    c.dict_data_name sex_name,
    e.name as groupName,
    f.name as gradeName,
    g.company_name as companyName,g.company_logo as logo,
    t.tenant_name as tenantName
    FROM sys_user a
    LEFT JOIN sys_organization b ON a.organization_id = b.organization_id
    LEFT JOIN (
    <include refid="selectSexDictSql"/>
    ) c ON a.sex = c.dict_data_code
    LEFT JOIN(
    <include refid="selectUserRoleSql"/>
    ) d ON a.user_id = d.user_id
    LEFT JOIN sys_user_group e ON a.group_id = e.group_id
    LEFT JOIN sys_user_grade f ON a.grade_id = f.grade_id
    LEFT JOIN sys_tenant t ON a.tenant_id = t.tenant_id
    LEFT JOIN sys_company g ON g.tenant_id = t.tenant_id
    <where>
      <if test="param.userId != null">
        AND a.user_id = #{param.userId}
      </if>
      <if test="param.username != null">
        AND a.username LIKE CONCAT('%', #{param.username}, '%')
      </if>
      <if test="param.uid != null">
        AND a.uid = #{param.uid}
      </if>
      <if test="param.userCode != null">
        AND a.user_code = #{param.userCode}
      </if>
      <if test="param.nickname != null">
        AND a.nickname LIKE CONCAT('%', #{param.nickname}, '%')
      </if>
      <if test="param.type != null">
        AND a.type = #{param.type}
      </if>
      <if test="param.sex != null">
        AND a.sex = #{param.sex}
      </if>
      <if test="param.phone != null">
        AND a.phone LIKE CONCAT('%', #{param.phone}, '%')
      </if>
      <if test="param.email != null">
        AND a.email LIKE CONCAT('%', #{param.email}, '%')
      </if>
      <if test="param.emailVerified != null">
        AND a.email_verified = #{param.emailVerified}
      </if>
      <if test="param.realName != null">
        AND a.real_name LIKE CONCAT('%', #{param.realName}, '%')
      </if>
      <if test="param.companyName != null">
        AND a.company_name LIKE CONCAT('%', #{param.companyName}, '%')
      </if>
      <if test="param.idCard != null">
        AND a.id_card LIKE CONCAT('%', #{param.idCard}, '%')
      </if>
      <if test="param.birthday != null">
        AND a.birthday LIKE CONCAT('%', #{param.birthday}, '%')
      </if>
      <if test="param.organizationId != null">
        AND a.organization_id = #{param.organizationId}
      </if>
      <if test="param.groupId != null">
        AND a.group_id = #{param.groupId}
      </if>
      <if test="param.isStaff != null">
        AND a.organization_id > 0
      </if>
      <if test="param.installed != null">
        AND a.installed = #{param.installed}
      </if>
      <if test="param.templateId != null">
        AND a.template_id = #{param.templateId}
      </if>
      <if test="param.merchantId != null">
        AND a.merchant_id = #{param.merchantId}
      </if>
      <if test="param.platform != null">
        AND a.platform = #{param.platform}
      </if>
      <if test="param.status != null">
        AND a.`status` = #{param.status}
      </if>
      <if test="param.createTimeStart != null">
        AND a.create_time &gt;= #{param.createTimeStart}
      </if>
      <if test="param.createTimeEnd != null">
        AND a.create_time &lt;= #{param.createTimeEnd}
      </if>
      <if test="param.recommend != null">
        AND a.recommend = #{param.recommend}
      </if>
      <if test="param.gradeId != null">
        AND a.grade_id = #{param.gradeId}
      </if>
      <if test="param.isAdmin != null">
        AND a.is_admin = #{param.isAdmin}
      </if>
      <if test="param.templateId != null">
        AND a.template_id = #{param.templateId}
      </if>
      <if test="param.isOrganizationAdmin != null">
        AND a.is_organization_admin = #{param.isOrganizationAdmin}
      </if>
      <if test="param.deleted != null">
        AND a.deleted = #{param.deleted}
      </if>
      <if test="param.deleted == null">
        AND a.deleted = 0
      </if>
      <if test="param.roleId != null">
        AND a.user_id IN (SELECT user_id FROM sys_user_role WHERE role_id=#{param.roleId})
      </if>
      <if test="param.roleCode != null">
        AND a.user_id IN (SELECT user_id FROM sys_user_role WHERE role_code=#{param.roleCode})
      </if>
      <if test="param.userIds != null">
        AND a.user_id IN
        <foreach collection="param.userIds" item="item" separator="," open="(" close=")">
          #{item}
        </foreach>
      </if>
      <if test="param.organizationIds != null">
        AND a.organization_id IN
        <foreach collection="param.organizationIds" item="item" separator="," open="(" close=")">
          #{item}
        </foreach>
      </if>
      <if test="param.phones != null">
        AND a.phone IN
        <foreach collection="param.phones" item="item" separator="," open="(" close=")">
          #{item}
        </foreach>
      </if>
      <if test="param.province != null">
        AND a.province LIKE CONCAT('%', #{param.province}, '%')
      </if>
      <if test="param.city != null">
        AND a.city LIKE CONCAT('%', #{param.city}, '%')
      </if>
      <if test="param.region != null">
        AND a.region LIKE CONCAT('%', #{param.region}, '%')
      </if>
      <if test="param.organizationName != null">
        AND b.organization_name LIKE CONCAT('%', #{param.organizationName}, '%')
      </if>
      <if test="param.sexName != null">
        AND c.dict_data_name = #{param.sexName}
      </if>
      <if test="param.expertType != null">
        AND a.expert_type = #{param.expertType}
      </if>
      <if test="param.tenantId != null">
        AND a.tenant_id = #{param.tenantId}
      </if>
      <if test="param.isSuperAdmin != null">
        AND a.username = 'superAdmin'
      </if>
      <if test="param.keywords != null">
        AND (
        a.username = #{param.keywords}
        OR a.user_id = #{param.keywords}
        OR a.id_card = #{param.keywords}
        OR a.merchant_id = #{param.keywords}
        OR a.nickname LIKE CONCAT('%', #{param.keywords}, '%')
        OR a.real_name LIKE CONCAT('%', #{param.keywords}, '%')
        OR a.phone LIKE CONCAT('%', #{param.keywords}, '%')
        OR a.email = #{param.keywords}
        )
      </if>
      <if test="param.parentId != null">
        AND a.organization_id IN (SELECT organization_id FROM sys_organization WHERE parent_id=#{param.parentId})
      </if>
    </where>
  </sql>

  <!-- 分页查询 -->
  <select id="selectPageRel" resultType="com.gxwebsoft.common.system.entity.User">
    <include refid="selectSql"></include>
  </select>

  <!-- 查询全部 -->
  <select id="selectListRel" resultType="com.gxwebsoft.common.system.entity.User">
    <include refid="selectSql"></include>
  </select>

  <!-- 根据账号查询 -->
  <select id="selectByUsername" resultType="com.gxwebsoft.common.system.entity.User">
    SELECT a.* ,
    b.organization_name,
    c.dict_data_name sex_name
    FROM sys_user a
    LEFT JOIN sys_organization b ON a.organization_id = b.organization_id
    LEFT JOIN (
    <include refid="selectSexDictSql"/>
    ) c ON a.sex = c.dict_data_code
    <where>
      AND a.deleted = 0
      AND (a.username = #{username} OR a.phone = #{username} OR a.email = #{username})
      AND a.tenant_id = #{tenantId}
      <!--            <if test="tenantId != null">-->
      <!--                AND a.tenant_id = #{tenantId}-->
      <!--            </if>-->
      <!--            <if test="tenantId == null">-->
      <!--                AND a.tenant_id = 1-->
      <!--            </if>-->
    </where>
    LIMIT 1
  </select>

  <!-- 根据手机号码查询 -->
  <select id="selectByPhone" resultType="com.gxwebsoft.common.system.entity.User">
    SELECT a.*
    FROM sys_user a
    <where>
      AND a.deleted = 0
      AND a.phone = #{phone}
      <if test="tenantId != null">
        AND a.tenant_id = #{tenantId}
      </if>
      <if test="tenantId == null">
        AND a.tenant_id = 1
      </if>
    </where>
    LIMIT 1
  </select>

  <!-- 查询单条数据 -->
  <select id="getOne" resultType="com.gxwebsoft.common.system.entity.User">
    SELECT *
    FROM sys_user
    WHERE user_id = #{param.userId}
      and deleted = 0
  </select>

  <!-- 查询统计数据 -->
  <select id="selectListStatisticsRel" resultType="com.gxwebsoft.common.system.entity.User">
    SELECT a.*,b.organization_name
    FROM sys_user a
    LEFT JOIN sys_organization b ON a.organization_id = b.organization_id
    <where>
      AND a.deleted = 0
      <if test="param.organizationId != null">
        AND b.organization_id = #{param.organizationId}
      </if>
    </where>
  </select>

  <!-- 更新用户信息 -->
  <update id="updateByUserId">
    UPDATE sys_user
    SET grade_id = #{param.gradeId}
    WHERE user_id = #{param.userId}
  </update>

  <select id="selectByUserId" resultType="com.gxwebsoft.common.system.entity.User">
    SELECT *
    FROM sys_user
    WHERE user_id = #{userId}
  </select>

  <!-- 根据手机号码查询 -->
  <select id="selectAdminByPhone" resultType="com.gxwebsoft.common.system.entity.User">
    SELECT a.*
    FROM sys_user a
    <where>
      AND a.deleted = 0
      AND a.phone = #{phone}
      AND (a.username = 'superAdmin' OR a.username = 'admin' OR a.is_admin = 1)
    </where>
  </select>

  <select id="selectListAllRel" resultType="com.gxwebsoft.common.system.entity.User">
    <include refid="selectSql"></include>
    LIMIT 50
  </select>

  <select id="pageRelAll" resultType="com.gxwebsoft.common.system.entity.User">
    <include refid="selectSql"></include>
  </select>


  <!-- 查询超级管理员 -->
  <select id="getByUserId" resultType="com.gxwebsoft.common.system.entity.User">
    SELECT a.*
    FROM sys_user a
    <where>
      AND a.user_id = #{userId}
      AND a.is_admin = 1
      AND a.deleted = 0
    </where>
  </select>

</mapper>
