<?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="cn.com.countrygarden.bi.main.dao.manager.map.ManagerMapMapper">

    <cache eviction="LRU"
           type="org.apache.ibatis.cache.impl.PerpetualCache"
           flushInterval="120000"
           size="1024"
           readOnly="true"/>
    <!-- 通用查询映射结果 -->
    <resultMap id="HouseMapperResultMap" type="cn.com.countrygarden.bi.main.dto.HouseKeeperDTO">
        <id column="BID" property="BID" />
        <result column="ADDR" property="ADDR" />
        <result column="BIRTHDATE" property="BIRTHDATE" />
        <result column="CODE" property="CODE" />
        <result column="CREATIONTIME" property="CREATIONTIME" />
        <result column="CREATOR" property="CREATOR" />
        <result column="DATAORIGINFLAG" property="DATAORIGINFLAG" />
        <result column="EMAIL" property="EMAIL" />
        <result column="ENABLESTATE" property="ENABLESTATE" />
        <result column="HOMEPHONE" property="HOMEPHONE" />
        <result column="IDTYPE" property="IDTYPE" />
        <result column="ISSHOPASSIST" property="ISSHOPASSIST" />
        <result column="JOINWORKDATE" property="JOINWORKDATE" />
        <result column="MNECODE" property="MNECODE" />
        <result column="MOBILE" property="MOBILE" />
        <result column="MODIFIEDTIME" property="MODIFIEDTIME" />
        <result column="MODIFIER" property="MODIFIER" />
        <result column="NAME" property="NAME" />
        <result column="NAME2" property="NAME2" />
        <result column="NAME3" property="NAME3" />
        <result column="NAME4" property="NAME4" />
        <result column="NAME5" property="NAME5" />
        <result column="NAME6" property="NAME6" />
        <result column="OFFICEPHONE" property="OFFICEPHONE" />
        <result column="PK_GROUP" property="PK_GROUP" />
        <result column="PK_ORG" property="PK_ORG" />
        <result column="PK_PSNDOC" property="PK_PSNDOC" />
        <result column="SEX" property="SEX" />
        <result column="TS" property="TS" />
        <result column="USEDNAME" property="USEDNAME" />
        <result column="BLOODTYPE" property="BLOODTYPE" />
        <result column="CENSUSADDR" property="CENSUSADDR" />
        <result column="CHARACTERRPR" property="CHARACTERRPR" />
        <result column="COUNTRY" property="COUNTRY" />
        <result column="DIE_DATE" property="DIE_DATE" />
        <result column="DIE_REMARK" property="DIE_REMARK" />
        <result column="EDU" property="EDU" />
        <result column="FAX" property="FAX" />
        <result column="FILEADDRESS" property="FILEADDRESS" />
        <result column="HEALTH" property="HEALTH" />
        <result column="ISHISKEYPSN" property="ISHISKEYPSN" />
        <result column="JOINPOLITYDATE" property="JOINPOLITYDATE" />
        <result column="MARITAL" property="MARITAL" />
        <result column="MARRIAGEDATE" property="MARRIAGEDATE" />
        <result column="NATIONALITY" property="NATIONALITY" />
        <result column="NATIVEPLACE" property="NATIVEPLACE" />
        <result column="PENELAUTH" property="PENELAUTH" />
        <result column="PERMANRESIDE" property="PERMANRESIDE" />
        <result column="PK_DEGREE" property="PK_DEGREE" />
        <result column="PK_HRORG" property="PK_HRORG" />
        <result column="POLITY" property="POLITY" />
        <result column="POSTALCODE" property="POSTALCODE" />
        <result column="PROF" property="PROF" />
        <result column="RETIREDATE" property="RETIREDATE" />
        <result column="SECRET_EMAIL" property="SECRET_EMAIL" />
        <result column="SHORTNAME" property="SHORTNAME" />
        <result column="TITLETECHPOST" property="TITLETECHPOST" />
        <result column="FIRSTNAME" property="FIRSTNAME" />
        <result column="LASTNAME" property="LASTNAME" />
        <result column="ISCADRE" property="ISCADRE" />
        <result column="ISHISLEADER" property="ISHISLEADER" />
        <result column="AGE" property="AGE" />
        <result column="WORKAGE" property="WORKAGE" />
        <result column="GLBDEF21" property="GLBDEF21" />
        <result column="GLBDEF1" property="GLBDEF1" />
        <result column="GLBDEF2" property="GLBDEF2" />
        <result column="GLBDEF3" property="GLBDEF3" />
        <result column="GLBDEF4" property="GLBDEF4" />
        <result column="GLBDEF5" property="GLBDEF5" />
        <result column="GLBDEF6" property="GLBDEF6" />
        <result column="GLBDEF7" property="GLBDEF7" />
        <result column="GLBDEF8" property="GLBDEF8" />
        <result column="GLBDEF9" property="GLBDEF9" />
        <result column="GLBDEF10" property="GLBDEF10" />
        <result column="GLBDEF11" property="GLBDEF11" />
        <result column="GLBDEF12" property="GLBDEF12" />
        <result column="GLBDEF13" property="GLBDEF13" />
        <result column="GLBDEF14" property="GLBDEF14" />
        <result column="GLBDEF15" property="GLBDEF15" />
        <result column="GLBDEF17" property="GLBDEF17" />
        <result column="GLBDEF18" property="GLBDEF18" />
        <result column="GLBDEF19" property="GLBDEF19" />
        <result column="GLBDEF20" property="GLBDEF20" />
        <result column="GLBDEF22" property="GLBDEF22" />
        <result column="GLBDEF23" property="GLBDEF23" />
        <result column="GLBDEF16" property="GLBDEF16" />
        <result column="GLBDEF24" property="GLBDEF24" />
        <result column="GLBDEF25" property="GLBDEF25" />
        <result column="GLBDEF26" property="GLBDEF26" />
        <result column="GLBDEF27" property="GLBDEF27" />
        <result column="GLBDEF28" property="GLBDEF28" />
        <result column="GLBDEF29" property="GLBDEF29" />
        <result column="GLBDEF30" property="GLBDEF30" />
        <result column="RN" property="RN" />
        <result column="DR" property="DR" />
        <result column="followFlag" property="followFlag" />
        <result column="projectName" property="projectName" />
        <result column="gardenDistrict" property="gardenDistrict" />
        <result column="hotlineConnectionTime" property="hotlineConnectionTime" />
        <result column="housekeeperPhone" property="housekeeperPhone" />
        <result column="jurisdictionMonth" property="jurisdictionMonth" />
        <result column="phoneIsfixed" property="phoneIsfixed" />
        <result column="serviceCenterHotline" property="serviceCenterHotline" />
        <result column="remark" property="remark" />
        <result column="housekeeperId" property="housekeeperId" />
        <result column="joinworkDate" property="joinworkDate" />
        <result column="houses" property="houses" />
        <result column="villas" property="villas" />
        <result column="shops" property="shops" />
        <result column="jobLv" property="jobLv" />
        <result column="CommName" property="commName" />
        <result column="organName" property="organName" />
        <result column="developmentsubject" property="developmentSubject" />
        <result column="userCode" property="userCode" />
        <result column="hjid" property="hjid" />
    </resultMap>

    <select id="countHousekeeperByProjects" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        (
        SELECT  1 FROM (
        select PK_PSNDOC,PK_POST,PK_ORG FROM performance_app.hi_psnjob
        WHERE
        ENDFLAG = 'N'
        AND LASTFLAG = 'Y'
        AND ISMAINJOB = 'Y'
        AND INDOCFLAG = 'Y'
        ) job
        LEFT JOIN biapp.wcx_region_project_rel r ON r.property_project_name = job.PK_ORG
        LEFT JOIN performance_app.bd_psndoc doc ON doc.PK_PSNDOC = job.PK_PSNDOC
        <where>
            job.PK_POST IN ('未定岗','见习管家','凤凰管家（副主管）','凤凰管家（主管）','凤凰管家（副经理）','凤凰管家（经理）')
            <if test="projects!=null and projects.size()>0">
                AND doc.PK_ORG IN
                <foreach item="item" index="index" collection="projects" open="("     separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY job.`PK_PSNDOC`
        ) table_count
    </select>

    <select id="countHouseKeeperPageDataByOrganCode" resultType="java.lang.Integer" useCache="false">
        SELECT
        COUNT(1)
        FROM
        performance_app.bd_psndoc doc
        LEFT JOIN (
        SELECT
        PK_POST,
        CLERKCODE
        FROM
        performance_app.hi_psnjob WHERE
        ENDFLAG = 'N'
        AND LASTFLAG = 'Y'
        AND ISMAINJOB = 'Y'
        AND INDOCFLAG = 'Y'
        ) job ON job.CLERKCODE = doc.`CODE`
        LEFT JOIN fenghuang_house_keeper_communication fhkc ON fhkc.bid = doc.BID
        LEFT JOIN performance_app.bd_type bt ON bt.pk_org = doc.PK_ORG
        <where>
            job.PK_POST IN ('未定岗','见习管家','凤凰管家','凤凰管家（副主管）','凤凰管家（主管）','凤凰管家（主管级）','凤凰管家（副经理）','凤凰管家（经理）','凤凰管家（经理级）','凤凰管家（领班级）')
            <if test="commNames!=null and commNames.size()>0">
                AND bt.pk_org in
                <foreach item="item" index="index" collection="commNames" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="userCode!=null and userCode!=''">
                AND doc.CODE = #{userCode}
            </if>
            <if test="phoneNums!=null and phoneNums.size()>0">
                AND doc.MOBILE IN
                <foreach item="item" index="index" collection="phoneNums" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="gardenDistricts!=null and gardenDistricts!=''">
                AND fhkc.garden_district = #{gardenDistricts}
            </if>
        </where>
    </select>

    <select id="selectHouseKeeperPageDataByOrganCode" resultMap="HouseMapperResultMap" useCache="false">
        <![CDATA[
      SELECT DISTINCT
            (SELECT DISTINCT REPLACE( REPLACE(Four_tissue,'（房产）',	''),'（物业）',	'') FROM performance_app.bd_type where pk_org = doc.PK_ORG) organName,
            r.property_project_name projectName,
            r.project_name commName,
            doc.ADDR,
            doc.BIRTHDATE,
            doc.`CODE`,
            doc.CREATIONTIME,
            doc.CREATOR,
            doc.DATAORIGINFLAG,
            doc.EMAIL,
            doc.ENABLESTATE,
            doc.HOMEPHONE,
            doc.ID,
            doc.IDTYPE,
            doc.ISSHOPASSIST,
            doc.JOINWORKDATE ,
            doc.MNECODE,
            doc.MOBILE,
            doc.MODIFIEDTIME,
            doc.MODIFIER,
            doc.`NAME`,
            doc.NAME2,
            doc.NAME3,
            doc.NAME4,
            doc.NAME6,
            doc.OFFICEPHONE,
            doc.PK_GROUP,
            doc.pk_org PK_ORG,
            doc.PK_PSNDOC,
            doc.SEX,
            doc.TS,
            doc.USEDNAME,
            doc.BLOODTYPE,
            doc.CENSUSADDR,
            doc.CHARACTERRPR,
            doc.COUNTRY,
            doc.DIE_DATE,
            doc.DIE_REMARK,
            doc.EDU,
            doc.FAX,
            doc.FILEADDRESS,
            doc.HEALTH,
            doc.ISHISKEYPSN,
            doc.JOINPOLITYDATE,
            doc.MARITAL,
            doc.MARRIAGEDATE,
            doc.NATIONALITY,
            doc.NATIVEPLACE,
            doc.PENELAUTH,
            doc.PERMANRESIDE,
            doc.PK_DEGREE,
            doc.POLITY,
            doc.POSTALCODE,
            doc.PROF,
            doc.RETIREDATE,
            doc.SECRET_EMAIL,
            doc.SHORTNAME,
            doc.TITLETECHPOST,
            doc.FIRSTNAME,
            doc.LASTNAME,
            doc.ISCADRE,
            doc.ISHISLEADER,
            doc.AGE,
            doc.WORKAGE,
            doc.GLBDEF21,
            doc.GLBDEF1,
            doc.GLBDEF2,
            doc.GLBDEF3,
            doc.GLBDEF4,
            doc.GLBDEF5,
            doc.GLBDEF6,
            doc.GLBDEF7,
            doc.GLBDEF8,
            doc.GLBDEF9,
            doc.GLBDEF10,
            doc.GLBDEF11,
            doc.GLBDEF12,
            doc.GLBDEF13,
            doc.GLBDEF14,
            doc.GLBDEF15,
            doc.GLBDEF17 joinworkDate,
            doc.GLBDEF18,
            doc.GLBDEF19,
            doc.GLBDEF20,
            doc.GLBDEF22,
            doc.GLBDEF23,
            doc.GLBDEF16,
            doc.GLBDEF24,
            doc.GLBDEF25,
            doc.GLBDEF26,
            doc.GLBDEF27,
            doc.GLBDEF28,
            doc.GLBDEF29,
            doc.GLBDEF30,
            doc.RN,
            doc.DR,
            doc.BID,
            fhkc.id housekeeperId,
            fhkc.garden_district gardenDistrict,
            fhkc.hotline_connection_time hotlineConnectionTime,
            fhkc.housekeeper_phone housekeeperPhone,
            fhkc.jurisdiction_month jurisdictionMonth,
            fhkc.phone_isfixed phoneIsfixed,
            fhkc.service_center_hotline serviceCenterHotline,
            fhkc.remark remark,
            job.PK_POST jobLv,
            job.HJID hjid
        FROM
            performance_app.bd_psndoc doc
        LEFT JOIN biapp.wcx_region_project_rel r ON r.property_project_name = doc.PK_ORG
        LEFT JOIN (
            SELECT
                HJID,
                PK_POST,
                CLERKCODE,
                PK_PSNDOC
            FROM
                performance_app.hi_psnjob
            WHERE
                ENDFLAG = 'N'
            AND LASTFLAG = 'Y'
            AND ISMAINJOB = 'Y'
            AND INDOCFLAG = 'Y'
        ) job ON doc.PK_PSNDOC = job.PK_PSNDOC
        LEFT JOIN (
            SELECT
                id,
                garden_district,
                hotline_connection_time,
                housekeeper_phone,
                jurisdiction_month,
                phone_isfixed,
                service_center_hotline,
                remark,
                user_code,
                bid
            FROM
                biapp.fenghuang_house_keeper_communication
        ) fhkc ON fhkc.bid = doc.BID
         ]]>
        <where>
            job.PK_POST IN ('未定岗','客服经理','客服副经理','一级客户助理','二级客户助理','三级客户助理','见习管家','见习管家','凤凰管家','凤凰管家（副主管）','凤凰管家（主管）','凤凰管家（副经理）','凤凰管家（经理）','凤凰管家（经理级）','凤凰管家（高级经理）')
            <if test="commNames!=null and commNames.size()>0">
                AND doc.pk_org in
                <foreach item="item" index="index" collection="commNames" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="userCode!=null and userCode!=''">
                AND doc.CODE = #{userCode}
            </if>
            <if test="phoneNums!=null and phoneNums.size()>0">
                AND doc.MOBILE IN
                <foreach item="item" index="index" collection="phoneNums" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="gardenDistricts!=null and gardenDistricts!=''">
                AND fhkc.garden_district = #{gardenDistricts}
            </if>
        </where>
        <![CDATA[ ORDER BY r.project_name ASC ]]>
        <if test="startRow!=null and pageSize!=null">
            limit #{startRow}, #{pageSize}
        </if>
    </select>
    <!--
    <if test="page!=null">
        limit #{page.startRow}, #{page.pageSize}
        </if>
    -->
    <select id="selectCommNameByProjectName" resultType="java.lang.String" >
    	SELECT IFNULL(property_project_name,'') projectName
    	FROM biapp.wcx_region_project_rel
    	WHERE project_name=#{projectName}
    </select>
    <select id="selectCommNameByProjectNameAndOrganName" resultType="java.lang.String">
    	SELECT IFNULL(property_project_name,'') projectName
    	FROM biapp.wcx_region_project_rel
    	WHERE project_name=#{projectName} AND region_name=#{organName}
    </select>
    <select id="selectAllOrganTree" resultType="cn.com.countrygarden.bi.main.dto.ManagerMapOrganTreeDTO">
--     	SELECT  DISTINCT SUBSTRING_INDEX(REPLACE(a.Four_tissue,'/',''),'（',1) name,mmc.organ_code code
--     	FROM ( SELECT DISTINCT Four_tissue
--             FROM performance_app.bd_type
--             GROUP BY Four_tissue
-- 		) a
-- 		LEFT JOIN biapp.manager_map_cache mmc ON SUBSTRING_INDEX(a.Four_tissue,'（',1) = mmc.organ_name
-- 		WHERE a.Four_tissue IS NOT NULL
-- 		ORDER BY name  ASC
        SELECT  DISTINCT SUBSTRING_INDEX(REPLACE(a.Four_tissue,'/',''),'（',1) name
    	FROM ( SELECT DISTINCT Four_tissue
            FROM performance_app.bd_type
            GROUP BY Four_tissue
		) a
		WHERE REPLACE(a.Four_tissue,'/','') IS NOT NULL
		ORDER BY name  ASC
    </select>
    <select id="selectCommNameTreeByOrganName" resultType="cn.com.countrygarden.bi.main.dto.ManagerMapOrganTreeDTO">
    	SELECT DISTINCT pk_org NAME
		FROM performance_app.bd_type
		WHERE Four_tissue LIKE '${organName}%' AND pk_org IS NOT NULL
    </select>
    <select id="selectCommNameTreeByOrganNameStr" resultType="java.lang.String">
    	SELECT DISTINCT pk_org pkOrg
		FROM performance_app.bd_type
		WHERE Four_tissue LIKE '${organName}%' AND pk_org IS NOT NULL
    </select>
    <select id="selectAllCommNameTree" resultType="java.lang.String">
        SELECT DISTINCT pk_org pkOrg
        FROM performance_app.bd_type
        WHERE SUBSTRING_INDEX(Four_tissue,'（',1) IN
        <foreach item="item" index="index" collection="organNames" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="selectCommNameByPropertyProjectName" resultType="java.lang.String">
    	SELECT IFNULL(project_name,'') projectName
    	FROM biapp.wcx_region_project_rel
    	WHERE property_project_name=IF (INSTR(#{propertyProjectName},'(') > 1,REPLACE (#{propertyProjectName},RIGHT (#{propertyProjectName},INSTR(REVERSE(#{propertyProjectName}),'(')),''),#{propertyProjectName})
    </select>
    <select id="selectPropertyProjectNameByCommName" resultType="java.lang.String">
    	SELECT IFNULL(property_project_name,'') projectName
    	FROM biapp.wcx_region_project_rel
    	WHERE project_name=
    	IF (INSTR(#{projectName},'(') > 1,REPLACE (#{projectName},RIGHT (#{projectName},INSTR(REVERSE(#{projectName}),'(')),''),#{projectName})
    </select>
    <select id="selectOrganTreeByErpOrgan" resultType="cn.com.countrygarden.bi.main.dto.ManagerMapOrganTreeDTO">
        SELECT DISTINCT SUBSTRING_INDEX(REPLACE(Four_tissue,'/',''),'（',1) name
        FROM ( SELECT Four_tissue
        FROM performance_app.bd_type
        WHERE REPLACE(Four_tissue,'/','') like '${organName}%'
        GROUP BY Four_tissue
        ) a
        ORDER BY name  ASC
        LIMIT 1
    </select>
    <select id="selectHouserKeeperIsEffective" resultType="java.lang.Integer">
         SELECT
                IFNULL(count(1),0) count
            FROM
                performance_app.hi_psnjob
            WHERE
                ENDFLAG = 'N'
            AND LASTFLAG = 'Y'
            AND ISMAINJOB = 'Y'
            AND INDOCFLAG = 'Y'
            AND PK_PSNDOC = #{pkPsndoc}
            AND PK_POST IN ('未定岗','见习管家','凤凰管家','凤凰管家（副主管）','凤凰管家（主管）','凤凰管家（主管级）'
              ,'凤凰管家（副经理）','凤凰管家（经理）','凤凰管家（经理级）','凤凰管家（领班级）')
    </select>
</mapper>
