<?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.marketing.ContractMapper">
    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.marketing.Contract">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="comm_id" jdbcType="INTEGER" property="commId"/>
        <result column="is_delete" jdbcType="TINYINT" property="isDelete"/>
        <result column="contract_area" jdbcType="DECIMAL" property="contractArea"/>
        <result column="contract_date" jdbcType="DATE" property="contractDate"/>
        <result column="state" jdbcType="VARCHAR" property="state"/>
        <result column="gmt_create" jdbcType="TIMESTAMP" property="gmtCreate"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="gmt_modified" jdbcType="TIMESTAMP" property="gmtModified"/>
        <result column="modified_by" jdbcType="VARCHAR" property="modifiedBy"/>
        <result column="cancel_date" jdbcType="DATE" property="cancelDate"/>
    </resultMap>

    <resultMap id="SumByResultMap" type="cn.com.countrygarden.bi.main.model.marketing.ContractSummary">
        <result column="project_type" jdbcType="VARCHAR" property="projectType"/>
        <result column="corp_type" jdbcType="VARCHAR" property="corpType"/>
        <result column="contract_area" jdbcType="DECIMAL" property="contractArea"/>
        <result column="organs" jdbcType="INTEGER" property="organs"/>
        <result column="comms" jdbcType="INTEGER" property="comms"/>
        <result column="organ_id" jdbcType="VARCHAR" property="organId"/>
        <result column="quota" jdbcType="DECIMAL" property="quotaContractArea"/>
        <result column="rate" jdbcType="DECIMAL" property="rate"/>
    </resultMap>

    <select id="sumBy"
            parameterType="map"
            resultMap="SumByResultMap">
        <![CDATA[
SELECT
        ]]>
        <choose>
            <when test="by != null and by == 'corpType'">
                <![CDATA[
                  p.`name` AS corp_type,
                ]]>
            </when>
            <when test="by != null and by == 'projectType'">
                <![CDATA[
                  p.`name` AS project_type,
                ]]>
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <![CDATA[
    SUM(c.contract_area) AS contract_area,
    COUNT(DISTINCT c.organ_id) AS organs,
    COUNT(1) AS comms
    FROM comm AS c
        ]]>
        <choose>
            <when test="by != null and by == 'corpType'">
                <![CDATA[
        LEFT JOIN cooperation_type AS p ON (p.`code` = c.corp_type AND p.is_delete = 0)

                ]]>
            </when>
            <when test="by != null and by == 'projectType'">
                <![CDATA[
        LEFT JOIN project_type AS p ON (p.`code` = c.project_type AND p.is_delete = 0)
                ]]>
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <where>
            <if test="1 == 1">
                <![CDATA[
                  AND c.is_delete = 0
                  AND c.contract_area IS NOT NULL
                ]]>
            </if>
            <if test="afterDate != null">
                <![CDATA[
                  AND c.sign_date >= #{afterDate}
                ]]>
            </if>
            <if test="beforeDate != null">
                <![CDATA[
                  AND c.sign_date <= #{beforeDate}
                ]]>
            </if>

        </where>
        <choose>
            <when test="by != null and by == 'corpType'">
                <![CDATA[
                  GROUP BY c.corp_type
                ]]>
            </when>
            <when test="by != null and by == 'projectType'">
                <![CDATA[
                  GROUP BY c.project_type
                ]]>
            </when>
            <otherwise>

            </otherwise>
        </choose>
    </select>


    <select id="tendency"
            parameterType="map"
            resultMap="SumByResultMap">
        <![CDATA[
            SELECT DATE_FORMAT( sign_date, '%c' ) AS month,
                SUM( contract_area ) AS contract_area
            FROM comm
            WHERE is_delete = 0
        ]]>

        <if test="afterDate != null">
            <![CDATA[
              AND sign_date >= #{afterDate}
            ]]>
        </if>
        <![CDATA[
            GROUP BY
                month
            ORDER BY
                month
        ]]>
    </select>

    <select id="ranking"
            parameterType="map"
            resultMap="SumByResultMap">
        <![CDATA[
SELECT
    q.organ_id,
    q.contract_area AS quota,
    IF(ISNULL(r.contract_area),
        0,
        r.contract_area) AS contract_area,
    ROUND(IF(ISNULL(r.contract_area),
                0,
                r.contract_area) / q.contract_area,
            2) AS rate
FROM
    marketing_quota AS q
        LEFT JOIN
    (SELECT
        c.organ_id, SUM(c.contract_area) AS contract_area
    FROM
        comm AS c
    WHERE
        c.sign_date >= #{afterDate}
            AND c.is_delete = 0
    GROUP BY c.organ_id) AS r ON (q.organ_id = r.organ_id)
WHERE
    q.year = #{year} AND q.is_delete = 0
ORDER BY rate DESC
]]>
        <if test="limit != null">
            <![CDATA[
                  LIMIT #{limit}
                ]]>
        </if>
    </select>
</mapper>