<?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.dup.ManagerDataMapper">
  <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.dup.ManagerData">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="organ_code" jdbcType="VARCHAR" property="organCode" />
    <result column="organ_name" jdbcType="VARCHAR" property="organName" />
    <result column="comm_id" jdbcType="VARCHAR" property="commId" />
    <result column="comm_name" jdbcType="VARCHAR" property="commName" />
    <result column="manager_name" jdbcType="VARCHAR" property="managerName" />
    <result column="phone_percentage" jdbcType="DOUBLE" property="phonePercentage" />
    <result column="wechat_percentage" jdbcType="DOUBLE" property="wechatPercentage" />
    <result column="wechat_cover_percentage" jdbcType="DOUBLE" property="wechatCoverPercentage" />
    <result column="recognition_percentage" jdbcType="DOUBLE" property="recognitionPercentage" />
    <result column="satisfaction" jdbcType="DOUBLE" property="satisfaction" />
    <result column="received_quick_events" jdbcType="BIGINT" property="receivedQuickEvents" />
    <result column="received_repair_events" jdbcType="BIGINT" property="receivedRepairEvents" />
    <result column="received_complaint_events" jdbcType="BIGINT" property="receivedComplaintEvents" />
    <result column="solved_quick_events" jdbcType="BIGINT" property="solvedQuickEvents" />
    <result column="solved_repair_events" jdbcType="BIGINT" property="solvedRepairEvents" />
    <result column="solved_complaint_events" jdbcType="BIGINT" property="solvedComplaintEvents" />
    <result column="request_received" jdbcType="BIGINT" property="requestReceived" />
    <result column="request_solved" jdbcType="BIGINT" property="requestSolved" />
    <result column="wechat_praise" jdbcType="BIGINT" property="wechatPraise" />
    <result column="letter_praise" jdbcType="BIGINT" property="letterPraise" />
    <result column="banner_num" jdbcType="BIGINT" property="bannerNum" />
    <result column="other_praise" jdbcType="BIGINT" property="otherPraise" />
    <result column="wechat_subscription" jdbcType="BIGINT" property="wechatSubscription" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="begin_date" jdbcType="DATE" property="beginDate" />
    <result column="end_date" jdbcType="DATE" property="endDate" />
    <result column="created_date" jdbcType="TIMESTAMP" property="createdDate" javaType="java.util.Date"/>
    <result column="created_by" jdbcType="VARCHAR" property="createdBy" />
    <result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate" javaType="java.util.Date"/>
    <result column="updated_by" jdbcType="VARCHAR" property="updatedBy" />
    <result column="customres" jdbcType="BIGINT" property="customres" />
    <result column="wechat_customers" jdbcType="BIGINT" property="wechatCustomers" />
    <result column="added_wechat" jdbcType="BIGINT" property="addedWechat" />    
    <result column="collectionRateTarget" jdbcType="DOUBLE" property="collectionRateTarget"/>
    <result column="collectionRate" jdbcType="DOUBLE" property="collectionRate"/>
    <result column="completionRate" jdbcType="DOUBLE" property="completionRate"/>    
    <result column="groupBusiness" jdbcType="DOUBLE" property="groupBusiness"/>
    <result column="parkingSale" jdbcType="DOUBLE" property="parkingSale"/>
    <result column="houseAgency" jdbcType="DOUBLE" property="houseAgency"/>
  </resultMap>

  <sql id="sumTotal">
    <![CDATA[
      SELECT COUNT(DISTINCT m.organ_code) AS organ_name,
        COUNT(DISTINCT m.comm_id) AS comm_name,
        COUNT(m.manager_name) AS manager_name,
        SUM(m.customres) as customres,
        SUM(m.wechat_customers) as wechat_customers,
        SUM(m.added_wechat) as added_wechat,
        SUM(m.phone_percentage) as phone_percentage,
        SUM(m.wechat_percentage) as wechat_percentage,
        SUM(m.wechat_cover_percentage) as wechat_cover_percentage,
        SUM(m.recognition_percentage) as recognition_percentage,
        SUM(m.satisfaction) as satisfaction,
        SUM(m.request_received) as request_received,
        SUM(m.request_solved) as request_solved,
        SUM(m.wechat_praise) as wechat_praise,
        SUM(m.letter_praise) as letter_praise,
        SUM(m.banner_num) as banner_num,
        SUM(m.other_praise) as other_praise,
        SUM(m.wechat_subscription) as wechat_subscription,
        SUM(m.collectionRateTarget) AS collectionRateTarget,
        SUM(m.collectionRate) AS collectionRate,
        SUM(m.completionRate) AS completionRate,
        SUM(m.groupBusiness) AS groupBusiness,
        SUM(m.parkingSale) AS parkingSale,
        SUM(m.houseAgency) AS houseAgency
      FROM fenghuang_manager_data m
      INNER JOIN fenghuang_comm c ON m.comm_id = c.erp_comm_id
      WHERE  #{beginDate} <= m.begin_date AND m.begin_date <= #{endDate} AND status<>'unfill'
    ]]>
  </sql>

  <select id="fincByDate" resultType="cn.com.countrygarden.bi.main.model.dup.ManagerData">
    <![CDATA[
      SELECT DISTINCT m.id AS id,m.organ_code AS organCode,m.organ_name AS organName,m.comm_id AS commId,m.comm_name AS commName,m.manager_name AS managerName,m.phone_percentage AS phonePercentage,
     m.wechat_percentage AS wechatPercentage,m.wechat_cover_percentage AS wechatCoverPercentage,
     m.recognition_percentage AS recognitionPercentage,m.satisfaction AS satisfaction,m.received_quick_events AS receivedQuickEvents,
     m.received_repair_events AS receivedRepairEvents,m.received_complaint_events AS receivedComplaintEvents,m.solved_quick_events AS solvedQuickEvents,
     m.solved_repair_events AS solvedRepairEvents,m.solved_complaint_events AS solvedComplaintEvents,m.request_received AS requestReceived,
     m.request_solved AS requestSolved,m.wechat_praise AS wechatPraise,m.letter_praise AS letterPraise,m.banner_num AS bannerNum,m.other_praise AS otherPraise,
     m.wechat_subscription AS wechatSubscription,m.status AS `status`,m.begin_date AS beginDate,m.end_date AS endDate,m.created_date AS createdDate,
     m.created_by AS createdBy,m.updated_date AS updatedDate,m.updated_by AS updatedBy,m.customres AS customres,
     m.wechat_customers AS wechatCustomers,m.added_wechat AS addedWechat,m.files AS files,
     CASE WHEN m.collectionRateTarget IS NULL THEN 0 ELSE m.collectionRateTarget END collectionRateTarget,CASE WHEN m.collectionRate IS NULL THEN 0 ELSE m.collectionRate END collectionRate,
     CASE WHEN m.collectionRate IS NOT NULL AND m.collectionRate<>0 AND m.collectionRateTarget IS NOT NULL AND m.collectionRateTarget<> 0
     THEN CAST(m.collectionRate/m.collectionRateTarget*100 AS DECIMAL(18,2)) ELSE 0 END completionRate,
     CASE WHEN m.groupBusiness IS NULL THEN 0 ELSE m.groupBusiness END groupBusiness,
     CASE WHEN m.parkingSale IS NULL THEN 0 ELSE m.parkingSale END parkingSale,
     CASE WHEN m.houseAgency IS NULL THEN 0 ELSE m.houseAgency END houseAgency
	  FROM  fenghuang_manager_data m
      INNER JOIN fenghuang_comm c ON m.comm_id = c.erp_comm_id
      WHERE m.`status`!='unfill' AND #{beginDate} <= m.begin_date AND m.begin_date <= #{endDate}
    ]]>
    <if test="organCode != null and organCode != ''">
      AND m.organ_code = #{organCode}
    </if>
    <if test="commId != null and commId != ''">
      AND m.comm_id = #{commId}
    </if>
    <if test="status != null and status != ''">
      AND m.status = #{status}
    </if>
    <if test="CommIdfenghuang!=null">
               and m.comm_id not in
                <foreach item="CommIdfenghuang" index="index" collection="CommIdfenghuang" open="(" separator="," close=")">
          #{CommIdfenghuang}
        </foreach>
      </if>
   
    <choose>
      <when test="organCodes != null and commIds != null">
        AND ( m.organ_code IN
        <foreach item="item" index="index" collection="organCodes" open="(" separator="," close=")">
          #{item}
        </foreach>
        OR m.comm_id IN
        <foreach item="item" index="index" collection="commIds" open="(" separator="," close=")">
          #{item}
        </foreach>
        )
      </when>
      <when test="organCodes != null and commIds == null">
        AND m.organ_code IN
        <foreach item="item" index="index" collection="organCodes" open="("     separator="," close=")">
          #{item}
        </foreach>
      </when>
      <when test="organCodes == null and commIds != null">
        AND m.comm_id IN
        <foreach item="item" index="index" collection="commIds" open="("     separator="," close=")">
          #{item}
        </foreach>
      </when>
     <!--  <when test="CommIdfenghuang!=null">
               and m.comm_id not in
                <foreach item="CommIdfenghuang" index="index" collection="CommIdfenghuang" open="(" separator="," close=")">
          #{CommIdfenghuang}
        </foreach>
      </when> -->
    </choose>
     <!--  <if test="CommIdfenghuang!=null">
         and m.comm_id not in 
         <foreach item="CommIdfenghuang" index="index" collection="CommIdfenghuang" open="(" separator="," close=")">
          #{CommIdfenghuang}
        </foreach>
    </if>  -->
    <![CDATA[
      ORDER BY m.status DESC,m.organ_code ASC , m.comm_id ASC
    ]]>
  </select>
  <select id="sumByDate" resultMap="BaseResultMap">
    <include refid="sumTotal"></include>
    <if test="organCode != null and organCode != ''">
      AND m.organ_code = #{organCode}
    </if>
    <if test="commId != null and commId != ''">
      AND m.comm_id = #{commId}
    </if>
    <if test="CommIdfenghuang!=null">
      and m.comm_id not in
      <foreach item="CommIdfenghuang" index="index" collection="CommIdfenghuang" open="(" separator="," close=")">
        #{CommIdfenghuang}
      </foreach>
    </if>
    <choose>
      <when test="organCodes != null and commIds != null">
        AND ( m.organ_code IN
          <foreach item="item" index="index" collection="organCodes" open="(" separator="," close=")">
            #{item}
          </foreach>
        OR m.comm_id IN
          <foreach item="item" index="index" collection="commIds" open="(" separator="," close=")">
            #{item}
          </foreach>
        )
      </when>
      <when test="organCodes != null and commIds == null">
        AND m.organ_code IN
        <foreach item="item" index="index" collection="organCodes" open="("     separator="," close=")">
          #{item}
        </foreach>
      </when>
      <when test="organCodes == null and commIds != null">
        AND m.comm_id IN
        <foreach item="item" index="index" collection="commIds" open="("     separator="," close=")">
          #{item}
        </foreach>
      </when>
    </choose>
  </select>
  
  <select id="lastDataListDTO" resultType="cn.com.countrygarden.bi.main.model.dup.ManagerData">
    <![CDATA[
    SELECT DISTINCT fd.id AS id,fd.organ_code AS organCode,fd.organ_name AS organName,fd.comm_id AS commId,fd.comm_name AS commName,fd.manager_name AS managerName,fd.phone_percentage AS phonePercentage,fd.wechat_percentage AS wechatPercentage,fd.wechat_cover_percentage AS wechatCoverPercentage,
    fd.recognition_percentage AS recognitionPercentage,fd.satisfaction AS satisfaction,fd.received_quick_events AS receivedQuickEvents,fd.received_repair_events AS receivedRepairEvents,fd.received_complaint_events AS receivedComplaintEvents,fd.solved_quick_events AS solvedQuickEvents,
    fd.solved_repair_events AS solvedRepairEvents,fd.solved_complaint_events AS solvedComplaintEvents,fd.request_received AS requestReceived,fd.request_solved AS requestSolved,fd.wechat_praise AS wechatPraise,fd.letter_praise AS letterPraise,fd.banner_num AS bannerNum,fd.other_praise AS otherPraise,
    fd.wechat_subscription AS wechatSubscription,fd.status AS STATUS,fd.begin_date AS beginDate,fd.end_date AS endDate,fd.created_date AS createdDate,fd.created_by AS createdBy,fd.updated_date AS updatedDate,fd.updated_by AS updatedDate,fd.customres AS customres,fd.wechat_customers AS wechatCustomers,
    fd.added_wechat AS addedWechat,fd.files AS files,
    CASE WHEN fd.collectionRate IS NULL THEN 0 ELSE fd.collectionRate END collectionRate,
    CASE WHEN fd.collectionRateTarget IS NULL THEN 0 ELSE fd.collectionRateTarget END collectionRateTarget,
    CASE WHEN fd.collectionRate IS NOT NULL AND fd.collectionRate<>0 AND fd.collectionRateTarget IS NOT NULL AND fd.collectionRateTarget<> 0
    THEN CAST(fd.collectionRate/fd.collectionRateTarget*100 AS DECIMAL(18,2)) ELSE 0 END completionRate,
    CASE WHEN fd.groupBusiness IS NULL THEN 0 ELSE fd.groupBusiness END groupBusiness,
    CASE WHEN fd.parkingSale IS NULL THEN 0 ELSE fd.parkingSale END parkingSale,
    CASE WHEN fd.houseAgency IS NULL THEN 0 ELSE fd.houseAgency END houseAgency
    FROM fenghuang_manager_data fd
    ]]>
       <where>
            <if test="setName!=null">
                 fd.manager_name IN
                 <foreach item="setName" index="index" collection="setName" open="(" separator="," close=")">
                        #{setName}
                 </foreach>
            </if>

            <if test="organCode!=null">
              and fd.organ_code=#{organCode}
            </if>

         <if test="commId!=null">
                and fd.comm_id=#{commId}

            </if>
         and fd.status=#{status} and fd.begin_date like concat('%',#{beginDate},'%') and fd.end_date like concat('%',#{endDate},'%')
       </where>
  
         ORDER BY fd.begin_date DESC 
  </select>
  
  <select id="psndoc" resultType="cn.com.countrygarden.bi.main.dto.ManagerMapOrganTreeDTO">

SELECT DISTINCT
	doc.`CODE` as code,
	doc.name as name
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
WHERE
		job.PK_POST IN ('未定岗','见习管家','凤凰管家','凤凰管家（副主管）','凤凰管家（主管）','凤凰管家（主管级）','凤凰管家（副经理）',
	'凤凰管家（经理）','凤凰管家（经理级）','凤凰管家（领班级）')
  
  </select>
  
  <select id="fincByDatelist" resultType="cn.com.countrygarden.bi.main.model.dup.ManagerData">
      SELECT m.id as id,m.manager_name as managerName
      FROM  fenghuang_manager_data m
	  WHERE m.user_id=#{userId} and m.begin_date like concat('%',#{collectionrateTime},'%')
	  ORDER BY m.begin_date DESC
	  LIMIT 0,1
  </select>
  <select id="fincByLastMonth" resultType="cn.com.countrygarden.bi.main.model.dup.ManagerData">
    -- 管家数据
    (<![CDATA[
      SELECT DISTINCT m.id AS id,m.organ_code AS organCode,m.organ_name AS organName,m.comm_id AS commId,m.comm_name AS commName,m.manager_name AS managerName,m.phone_percentage AS phonePercentage,
     m.wechat_percentage AS wechatPercentage,m.wechat_cover_percentage AS wechatCoverPercentage,
     m.recognition_percentage AS recognitionPercentage,m.satisfaction AS satisfaction,m.received_quick_events AS receivedQuickEvents,
     m.received_repair_events AS receivedRepairEvents,m.received_complaint_events AS receivedComplaintEvents,m.solved_quick_events AS solvedQuickEvents,
     m.solved_repair_events AS solvedRepairEvents,m.solved_complaint_events AS solvedComplaintEvents,m.request_received AS requestReceived,
     m.request_solved AS requestSolved,m.wechat_praise AS wechatPraise,m.letter_praise AS letterPraise,m.banner_num AS bannerNum,m.other_praise AS otherPraise,
     m.wechat_subscription AS wechatSubscription,m.status AS `status`,m.begin_date AS beginDate,m.end_date AS endDate,m.created_date AS createdDate,
     m.created_by AS createdBy,m.updated_date AS updatedDate,m.updated_by AS updatedBy,m.customres AS customres,
     m.wechat_customers AS wechatCustomers,m.added_wechat AS addedWechat,m.files AS files,
     CASE WHEN m.collectionRateTarget IS NULL THEN 0 ELSE m.collectionRateTarget END collectionRateTarget,CASE WHEN m.collectionRate IS NULL THEN 0 ELSE m.collectionRate END collectionRate,
     CASE WHEN m.collectionRate IS NOT NULL AND m.collectionRate<>0 AND m.collectionRateTarget IS NOT NULL AND m.collectionRateTarget<> 0
     THEN CAST(m.collectionRate/m.collectionRateTarget*100 AS DECIMAL(18,2)) ELSE 0 END completionRate,
     CASE WHEN m.groupBusiness IS NULL THEN 0 ELSE m.groupBusiness END groupBusiness,
     CASE WHEN m.parkingSale IS NULL THEN 0 ELSE m.parkingSale END parkingSale,
     CASE WHEN m.houseAgency IS NULL THEN 0 ELSE m.houseAgency END houseAgency
	  FROM  fenghuang_manager_data m
      INNER JOIN fenghuang_comm c ON m.comm_id = c.erp_comm_id
      WHERE m.`status`!='unfill' AND #{beginDate} <= m.begin_date AND m.begin_date <= #{endDate}
    ]]>
    <include refid="queryLastMonthCondition"></include>
    <![CDATA[
      ORDER BY m.status DESC,m.organ_code ASC , m.comm_id ASC
    ]]> )
    UNION
--  管家地图
    (SELECT DISTINCT null id,cac.region_code AS organCode,cac.region_name AS organName,cac.comm_code AS commId,cac.comm_name AS commName,cac.`name` AS managerName,null phonePercentage,
    null wechatPercentage,null wechatCoverPercentage,
    null recognitionPercentage,null satisfaction,null receivedQuickEvents,
    null receivedRepairEvents,null receivedComplaintEvents,null solvedQuickEvents,
    null solvedRepairEvents,null solvedComplaintEvents,null requestReceived,
    null requestSolved,null wechatPraise,null letterPraise,null bannerNum,null otherPraise,
    null wechatSubscription,null `status`,null beginDate,null endDate,null createdDate,
    null createdBy,null updatedDate,null updatedBy,null customres,
    null wechatCustomers,null addedWechat,null files,
    null collectionRateTarget,
    null collectionRate,
    null completionRate,
    null groupBusiness,
    null parkingSale,
    null houseAgency
    from `manager_housekeeper_cache` cac
      WHERE cac.region_code=#{organCode} and  cac.comm_code=#{commId} and
      cac.name NOT in(
          SELECT DISTINCT m.manager_name AS managerName
          FROM  fenghuang_manager_data m
          WHERE m.`status`!='unfill' AND #{beginDate}  <![CDATA[<=]]> m.begin_date
          AND m.begin_date  <![CDATA[<=]]> #{endDate}
          <include refid="queryLastMonthCondition"></include>
      )
    )
  </select>


  <sql id="queryLastMonthCondition">
    <if test="organCode != null and organCode != ''">
      AND m.organ_code = #{organCode}
    </if>
    <if test="commId != null and commId != ''">
      AND m.comm_id = #{commId}
    </if>
    <if test="status != null and status != ''">
      AND m.status = #{status}
    </if>
    <if test="CommIdfenghuang!=null">
      and m.comm_id not in
      <foreach item="item" index="index" collection="CommIdfenghuang" open="(" separator="," close=")">
        #{item}
      </foreach>
    </if>

    <choose>
      <when test="organCodes != null and commIds != null">
        AND ( m.organ_code IN
        <foreach item="item" index="index" collection="organCodes" open="(" separator="," close=")">
          #{item}
        </foreach>
        OR m.comm_id IN
        <foreach item="item" index="index" collection="commIds" open="(" separator="," close=")">
          #{item}
        </foreach>
        )
      </when>
      <when test="organCodes != null and commIds == null">
        AND m.organ_code IN
        <foreach item="item" index="index" collection="organCodes" open="("     separator="," close=")">
          #{item}
        </foreach>
      </when>
      <when test="organCodes == null and commIds != null">
        AND m.comm_id IN
        <foreach item="item" index="index" collection="commIds" open="("     separator="," close=")">
          #{item}
        </foreach>
      </when>
    </choose>
  </sql>

</mapper>