<?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.tofly.base.mapper.SdeversionMapper">

  <resultMap id="sdeversionMap" type="com.tofly.base.entity.Sdeversion">
                <id property="id" column="ID"/>
                      <result property="userId" column="USER_ID"/>
                      <result property="departmentId" column="DEPARTMENT_ID"/>
                      <result property="sdeVersion" column="SDE_VERSION"/>
                      <result property="auditor" column="AUDITOR"/>
                                                                                          <association property="auditorName"  column="AUDITOR" select="getauditorName" ></association>
  </resultMap>


    <select id="getauditorName" resultType="java.lang.Object" databaseId="oracle">
      select decode(#{auditor},0,'不是审核员',1,'是审核员') as auditorName from dual
    </select>

    <select id="getauditorName" resultType="java.lang.Object" databaseId="mysql">
      select (case #{auditor} when 0 then '不是审核员'
           when 1 then '是审核员' else '' end ) as auditorName
    </select>


    <resultMap id="userSdeversionMap" type="com.tofly.base.entity.UserSdeversion">
        <id property="id" column="ID"/>
        <result property="username" column="USERNAME"/>
        <result property="salt" column="SALT"/>
        <result property="phone" column="PHONE"/>
        <result property="avatar" column="AVATAR"/>
        <result property="departmentId" column="DEPARTMENT_ID"/>
        <result property="departmentName" column="departmentName"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="lockFlag" column="LOCK_FLAG"/>
        <result property="statusFlag" column="STATUS_FLAG"/>
        <result property="wxOpenid" column="WX_OPENID"/>
        <result property="qqOpenid" column="QQ_OPENID"/>
        <result property="email" column="EMAIL"/>
        <result property="realName" column="REAL_NAME"/>
        <result property="loginTime" column="LOGIN_TIME"/>
        <result property="lastIp" column="LAST_IP"/>
        <result property="pwderrtimes" column="PWDERRTIMES"/>
        <result property="worknumber" column="WORKNUMBER"/>
        <result property="findpwdkey" column="FINDPWDKEY"/>
        <result property="aliasname" column="ALIASNAME"/>
        <result property="birthday" column="BIRTHDAY"/>
        <result property="belong" column="BELONG"/>
        <result property="createUser" column="CREATE_USER"/>
        <result column="USER_LEVEL" property="userLevel"/>
        <result column="sfbd" property="sfbd"/>
        <association property="roleName" column="ID" javaType="java.lang.String" select="getRoleName"></association>
        <association property="sdeversion"  javaType="java.util.Map">
            <result property="versionId" column="VERSIONID"/>
            <result property="sdeVersion" column="SDE_VERSION"/>
            <result property="auditor" column="AUDITOR"/>
            <association property="auditorName" javaType="java.lang.String" column="AUDITOR" select="getauditorName" ></association>
        </association>
    </resultMap>

    <select id="getRoleName" resultType="string" databaseId="oracle">
       select to_char((select wmsys.wm_concat(o.role_name)
                      from TF_SMPN_ROLE_X o, TF_SMPN_USERROLE_X n
                     where n.role_id = o.id
                       and l.id = n.user_id)) as role_name
                       from TF_SMPN_USER_X l
                       where l.id=#{id}
    </select>

    <select id="getRoleName" resultType="string" databaseId="mysql">
       select (select group_concat(o.role_name)
                from TF_SMPN_ROLE_X o, TF_SMPN_USERROLE_X n
                where n.role_id = o.id
                and l.id = n.user_id GROUP BY l.id) as role_name
               from TF_SMPN_USER_X l
               where l.id=#{id}
    </select>

    <select id="getUserSdeversionMap" resultMap="userSdeversionMap">
        select l.id,
        username,
        phone,
        avatar,
        l.department_id,
        (select name from TF_SMPN_DEPARTMENT_X where id=l.department_id) as departmentName,
        create_time,
        lock_flag,
        status_flag,
        wx_openid,
        qq_openid,
        email,
        real_name,
        login_time,
        last_ip,
        pwderrtimes,
        worknumber,
        findpwdkey,
        aliasname,
        birthday,
        belong,
        create_user,
        user_level,
        (select decode(count(1),0,'不存在','存在')  from TF_SMPN_SDEVERSION_X where user_ID=l.id) as sfcz,
        tss.ID as VERSIONID,
        tss.SDE_VERSION,
        tss.AUDITOR
        from TF_SMPN_USER_X l left  join  TF_SMPN_SDEVERSION_X tss on l.id=tss.user_id
        where 1=1
        <if test="user.realName != null and user.realName.trim() != ''">
            and (real_Name like '%'||#{user.realName}||'%' or username like '%'||#{user.realName}||'%')
        </if>
        <if test="user.departmentId != null">
            and l.department_id = #{user.departmentId}
        </if>
        <if test='user.sfcz != null and user.sfcz == "0" '>
            and not exists (select 1 from TF_SMPN_SDEVERSION_X where user_id=l.id)
        </if>
        <if test='user.sfcz != null and user.sfcz == "1" '>
            and  exists (select 1 from TF_SMPN_SDEVERSION_X where user_id=l.id)
        </if>
        <if test="user.menuId != null">
            and exists (select 1
            from TF_SMPN_USERROLE_X ur, TF_SMPN_ROLEMENU_X rm
            where ur.role_id = rm.role_id
            and ur.user_id = l.id
            and rm.menu_id = #{user.menuId})
        </if>
    </select>
</mapper>
