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

    <resultMap id="departmentMap" type="com.tofly.base.entity.Department">
        <id property="id" column="ID"/>
        <result property="name" column="NAME"/>
        <result property="sort" column="SORT"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="statusFlag" column="STATUS_FLAG"/>
        <result property="parentId" column="PARENT_ID"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="code" column="CODE"/>
        <result property="shortName" column="SHORT_NAME"/>
        <result property="companyId" column="COMPANY_ID"/>
        <result property="phone" column="PHONE"/>
        <association property="companyName" column="COMPANY_ID" select="getCompanyName"/>
        <association property="parentName" column="PARENT_ID" select="getBmName"/>
        <association property="statusName" column="ID" select="getStatus"/>
        <association property="userName" column="CREATE_USER" select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>
    <select id="getCompanyName" resultType="string">
        select COMPANY_NAME as companyName
        from TF_SMPN_COMPANY_X
        where id = #{departmentId}
    </select>
    <select id="getStatus" resultType="string" parameterType="long">
        select decode(STATUS_FLAG, '1', '启用', '2', '禁用')
        from TF_SMPN_DEPARTMENT_X
        where ID = #{ID}
    </select>

    <select id="getBmName" resultType="string" parameterType="long">
        select NAME as parentName
        from TF_SMPN_DEPARTMENT_X
        where ID = #{PARENT_ID}
          and STATUS_FLAG = 1
    </select>


    <select id="listSourceByDept" parameterType="map" resultType="map" databaseId="oracle">
        select m.*,
               decode((select 1
                       from tf_smpn_departmentsource_x ds
                       where m.id = ds.source_id
                         and ds.department_id = #{departmentId,jdbcType=NUMERIC}),
                      1,
                      'true',
                      'false') is_exists
        from tf_smpn_source_x m
        where pid = #{pid}
    </select>


    <select id="listSourceByDept" parameterType="map" resultType="map" databaseId="mysql">
        select m.*,
               case (select 1
                     from TF_SMPN_DEPARTMENTSOURCE_X ds
                     where m.id = ds.source_id
                       and ds.department_id = #{departmentId,jdbcType=NUMERIC})
                   when 1 then 'true'
                   else 'false' end is_exists
        from TF_SMPN_SOURCE_X m
        where pid = #{pid}
    </select>


    <select id="subList" resultMap="departmentMap">
        select *
        from tf_smpn_department_x
        connect by prior id = parent_id
        <choose>
            <when test='returnSelf !=null and returnSelf =="0" '>
                start with parent_id = #{id}
            </when>
            <otherwise>
                start with id = #{id}
            </otherwise>
        </choose>
    </select>
    <select id="getRelatedDeptIds" resultType="long">
        select id
        from tf_smpn_department_x
        connect by prior id = parent_id
        start with id = #{id}
    </select>
</mapper>
