<?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.PositionMapper">

  <resultMap id="positionMap" type="com.tofly.base.entity.Position">
                  <id property="id" column="ID"/>
                   <!--<result property="name" column="NAME"/>-->
                        <result property="category" column="CATEGORY"/>
                        <!--<result property="head" column="HEAD"/>-->
                        <!--<result property="deptaudit" column="DEPTAUDIT"/>-->
                        <result property="departmentId" column="DEPARTMENT_ID"/>
                        <result property="userId" column="USER_ID"/>
                      <result property="createUser" column="CREATE_USER"/>
                      <result property="createDate" column="CREATE_DATE"/>
                    <!--<association property="position"  column="category"  javaType="com.tofly.base.entity.Code" select="getName"></association>-->
                    <association property="pname"  column="category"  javaType="java.lang.String" select="getpName"></association>

  </resultMap>


    <select id="getName" resultType="com.tofly.base.entity.Code" parameterType="java.lang.String" databaseId="oracle">
        select * from tf_smpn_code_x where key='position' and value=#{category}
    </select>

    <select id="getName" resultType="com.tofly.base.entity.Code" parameterType="java.lang.String" databaseId="mysql">
        select * from TF_SMPN_CODE_X where `key`='position' and `value`=#{category}
    </select>

    <select id="getpName" resultType="string" parameterType="java.lang.String" databaseId="oracle">
        select notes from tf_smpn_code_x where key='position' and value=#{category}
    </select>

    <select id="getpName" resultType="string" parameterType="java.lang.String" databaseId="mysql">
        select notes from TF_SMPN_CODE_X where `key`='position' and `value`=#{category}
    </select>

    <select id="getPosition" resultType="map" databaseId="oracle">
        select t.*,
         (select notes  from tf_smpn_code_x c where c.key = 'position' and value=t.CATEGORY) as category_name,
         (select username from tf_smpn_user_x where id=t.user_id) username,
         (select real_name from tf_smpn_user_x where id=t.user_id) real_name
         from tf_smpn_position_x t
         where 1=1
        <if test="map.username != null and map.username.trim() != ''">
          and  exists (select 1 from tf_smpn_user_x where id=t.user_id
            and (username like '%'||#{map.username}||'%' or real_name like '%'||#{map.username}||'%'))
        </if>
        <if test="map.category != null and map.category.trim() != ''">
            and category=#{map.category}
        </if>
        <if test="map.userId != null and map.userId!= 0">
            and user_id=#{map.userId}
        </if>
    </select>

    <select id="getPosition" resultType="map" databaseId="mysql">
        select t.*,
        (select notes  from TF_SMPN_CODE_X c where c.`key` = 'position' and `value`=t.CATEGORY) as category_name,
        (select username from TF_SMPN_USER_X where id=t.user_id) username,
        (select real_name from TF_SMPN_USER_X where id=t.user_id) real_name
        from TF_SMPN_POSITION_X t
        where 1=1
        <if test="map.username != null and map.username.trim() != ''">
            and  exists (select 1 from TF_SMPN_USER_X where id=t.user_id
            and (username like CONCAT('%',#{map.username},'%') or real_name like CONCAT('%',#{map.username},'%')))
        </if>
        <if test="map.category != null and map.category.trim() != ''">
            and category=#{map.category}
        </if>
        <if test="map.userId != null and map.userId!= 0">
            and user_id=#{map.userId}
        </if>
    </select>

    <select id="getAllUserPosition" resultType="map" databaseId="oracle">
        select id,
           username,
           phone,
           avatar,
           department_id,
           create_time,
           update_time,
           lock_flag,
           status_flag,
           wx_openid,
           qq_openid,
           email,
           real_name,
           login_time,
           last_ip,
           pwderrtimes,
           worknumber,
           findpwdkey,
           aliasname,
           birthday,
           belong,
           create_user,
           (select count(1) from tf_smpn_position_x p where t.id = p.user_id) category_count,
           to_char((select wmsys.wm_concat(c.notes)
                     from tf_smpn_code_x c, tf_smpn_position_x p
                    where t.id = p.user_id
                      and p.category = c.value
                      and c.key = 'position')) as category_name

      from tf_smpn_user_x t
    </select>

    <select id="getAllUserPosition" resultType="map" databaseId="mysql">
                select id,
           username,
           phone,
           avatar,
           department_id,
           create_time,
           update_time,
           lock_flag,
           status_flag,
           wx_openid,
           qq_openid,
           email,
           real_name,
           login_time,
           last_ip,
           pwderrtimes,
           worknumber,
           findpwdkey,
           aliasname,
           birthday,
           belong,
           create_user,
           (select count(1) from TF_SMPN_POSITION_X p where t.id = p.user_id) category_count,
           (select group_concat(c.notes)
                     from TF_SMPN_CODE_X c, TF_SMPN_POSITION_X p
                    where t.id = p.user_id
                      and p.category = c.`value`
                      and c.`key` = 'position' group by c.`key`) as category_name
      from TF_SMPN_USER_X t
    </select>

</mapper>
