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

  <resultMap id="sourceMap" type="com.tofly.base.entity.Source">
                  <id property="id" column="ID"/>
                      <result property="pid" column="PID"/>
                        <result property="name" column="NAME"/>
                        <result property="cval" column="CVAL"/>
                        <result property="createTime" column="CREATE_TIME"/>
                        <result property="createUser" column="CREATE_USER"/>
                        <result property="ckey" column="CKEY"/>
  </resultMap>

    <select id="getSourceByUser" parameterType="java.lang.Long" resultType="map">
        select s.*,(select code from TF_SMPN_SOURCEDIC_X where id=s.pid) as code
  from TF_SMPN_SOURCE_X s
 where exists (select 1
          from TF_SMPN_DEPARTMENTSOURCE_X ds,
               TF_SMPN_DEPARTMENT_X       d,
               TF_SMPN_USER_X             u
         where ds.department_id = d.id
           and ds.source_id = s.id
           and d.id = u.department_id
           and d.status_flag = '1'
           and u.status_flag = '1'
           and u.id=#{id})
    </select>

    <select id="listSources" parameterType="java.lang.Long"  resultType="map" databaseId="oracle">
        select sd.*,
           (select notes
              from tf_smpn_code_x c
             where key = '0007'
               and ulevel = '2'
               and value = sd.code) name,
               sd.name groupname
      from tf_smpn_sourcedic_x sd
      where exists
       (select 1
                from tf_smpn_source_x s, tf_smpn_departmentsource_x ds
               where s.id = ds.source_id
                 and s.pid = sd.id
                 and ds.department_id = #{departmentId})

    </select>

    <select id="listSources" parameterType="java.lang.Long"  resultType="map" databaseId="mysql">
                select sd.*,
           (select notes
              from TF_SMPN_CODE_X c
             where `key` = '0007'
               and ulevel = '2'
               and `value` = sd.code) name,
               sd.`name` groupname
      from TF_SMPN_SOURCEDIC_X sd
      where exists
       (select 1
                from TF_SMPN_SOURCE_X s, TF_SMPN_DEPARTMENTSOURCE_X ds
               where s.id = ds.source_id
                 and s.pid = sd.id
                 and ds.department_id = #{departmentId})

    </select>

</mapper>
