<?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.performance.PerformanceMonthlyMapper">
    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.performance.PerformanceMonthly">
        <constructor>
            <idArg column="month_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
            <arg column="element_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
            <arg column="complete" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="annual_target" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="complete_rate" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="year" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="month" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="hierarchy_id" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="ranges" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="created_date" javaType="java.util.Date" jdbcType="TIMESTAMP"/>
            <arg column="created_by" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="update_date" javaType="java.util.Date" jdbcType="TIMESTAMP"/>
            <arg column="update_by" javaType="java.lang.String" jdbcType="VARCHAR"/>
        </constructor>
    </resultMap>
    <sql id="Base_Column_List">
    month_id, element_id, complete, annual_target, complete_rate, year, month, hierarchy_id, 
    ranges, created_date, created_by, update_date, update_by
  </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from performance_monthly
        where month_id = #{monthId,jdbcType=BIGINT}
    </select>

    <!--通过年，月度，区域，指标查询唯一一条记录-->
    <select id="getSelect" parameterType="cn.com.countrygarden.bi.main.model.performance.PerformanceMonthly"
            resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from performance_monthly
        where 1=1
        <if test="year != null">
            and year = #{year,jdbcType=INTEGER}
        </if>
        <if test="month != null">
            and month = #{month,jdbcType=INTEGER}
        </if>
        <if test="hierarchyId != null">
            and hierarchy_id = #{hierarchyId,jdbcType=VARCHAR}
        </if>
        <if test="elementId != null">
            and element_id = #{elementId,jdbcType=BIGINT}
        </if>
    </select>

    <!--得到年/月排名-->
    <select id="getMonthSort" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        month_id,element_id,complete,annual_target,complete_rate,year,month,hierarchy_id,@monthRank:=@monthRank+1 as ranges, created_date, created_by, update_date, update_by
        from performance_monthly a ,(select (@monthRank :=0) ) b
        where element_id = #{elementId,jdbcType=BIGINT} AND year = #{year,jdbcType=INTEGER} and month = #{month,jdbcType=INTEGER}
        ORDER BY complete_rate DESC
    </select>

    <!--删除0和1用于计算存储的指标-->
    <delete id="deleteMonthlyAvgScore" parameterType="java.util.Map">
        delete from performance_monthly where year = #{year,jdbcType=INTEGER} and month = #{month,jdbcType=INTEGER} and element_id=1 or element_id=0
    </delete>


    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from performance_monthly
    where month_id = #{monthId,jdbcType=BIGINT}
    </delete>


    <!--计算每个区域的平均分-->
    <insert id="insertMonthAvgScoreRegion" parameterType="java.util.Map">
    insert into performance_monthly(element_id,complete_rate,year,month,hierarchy_id,created_date,created_by,update_date,update_by)
        select 0,precent.complete_rate as complete_rate,#{year,jdbcType=INTEGER},#{month,jdbcType=INTEGER},precent.hierarchy_id,now(),'0000',now(),'0000'
        from(
            select ROUND(SUM(complete_rate)/COUNT(element_id),2) as complete_rate,hierarchy_id
            from(
                select m.element_id as element_id,complete_rate,hierarchy_id
                from
                (select * from performance_element where type != '城市公司' and type !='加/减分项') e
                LEFT JOIN (select * from performance_monthly where year=#{year,jdbcType=INTEGER} and month=#{month,jdbcType=INTEGER}) m
                on e.element_id = m.element_id
                HAVING CHAR_LENGTH(element_id) = 4 ) a
                GROUP BY hierarchy_id
            ) precent
    </insert>

    <!--计算每个城市公司的平均分-->
    <insert id="insertMonthAvgScoreCompany" parameterType="java.util.Map">
    insert into performance_monthly(element_id,complete_rate,year,month,hierarchy_id,created_date,created_by,update_date,update_by)
        select 1,precent.complete_rate as complete_rate,#{year,jdbcType=INTEGER},#{month,jdbcType=INTEGER},precent.hierarchy_id,now(),'0000',now(),'0000'
        from(
            select ROUND(SUM(complete_rate)/COUNT(element_id),2) as complete_rate,hierarchy_id
            from(
                select m.element_id as element_id,complete_rate,hierarchy_id
                from
                (select * from performance_element where type = '城市公司') e
                LEFT JOIN (select * from performance_monthly where year=#{year,jdbcType=INTEGER} and month=#{month,jdbcType=INTEGER}) m
                on e.element_id = m.element_id
                HAVING CHAR_LENGTH(element_id) = 4 ) a
                GROUP BY hierarchy_id
            ) precent
    </insert>

    <insert id="insertSelective" parameterType="cn.com.countrygarden.bi.main.model.performance.PerformanceMonthly">
        insert into performance_monthly
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="monthId != null">
                month_id,
            </if>
            <if test="elementId != null">
                element_id,
            </if>
            <if test="complete != null">
                complete,
            </if>
            <if test="annualTarget != null">
                annual_target,
            </if>
            <if test="completeRate != null">
                complete_rate,
            </if>
            <if test="year != null">
                year,
            </if>
            <if test="month != null">
                month,
            </if>
            <if test="hierarchyId != null">
                hierarchy_id,
            </if>
            <if test="ranges != null">
                ranges,
            </if>
            <if test="createdDate != null">
                created_date,
            </if>
            <if test="createdBy != null">
                created_by,
            </if>
            <if test="updateDate != null">
                update_date,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="monthId != null">
                #{monthId,jdbcType=BIGINT},
            </if>
            <if test="elementId != null">
                #{elementId,jdbcType=BIGINT},
            </if>
            <if test="complete != null">
                #{complete,jdbcType=VARCHAR},
            </if>
            <if test="annualTarget != null">
                #{annualTarget,jdbcType=VARCHAR},
            </if>
            <if test="completeRate != null">
                #{completeRate,jdbcType=VARCHAR},
            </if>
            <if test="year != null">
                #{year,jdbcType=INTEGER},
            </if>
            <if test="month != null">
                #{month,jdbcType=INTEGER},
            </if>
            <if test="hierarchyId != null">
                #{hierarchyId,jdbcType=VARCHAR},
            </if>
            <if test="ranges != null">
                #{ranges,jdbcType=INTEGER},
            </if>
            <if test="createdDate != null">
                #{createdDate,jdbcType=TIMESTAMP},
            </if>
            <if test="createdBy != null">
                #{createdBy,jdbcType=VARCHAR},
            </if>
            <if test="updateDate != null">
                #{updateDate,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                #{updateBy,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective"
            parameterType="cn.com.countrygarden.bi.main.model.performance.PerformanceMonthly">
        update performance_monthly
        <set>
            <if test="elementId != null">
                element_id = #{elementId,jdbcType=BIGINT},
            </if>
            <if test="complete != null">
                complete = #{complete,jdbcType=VARCHAR},
            </if>
            <if test="annualTarget != null">
                annual_target = #{annualTarget,jdbcType=VARCHAR},
            </if>
            <if test="completeRate != null">
                complete_rate = #{completeRate,jdbcType=VARCHAR},
            </if>
            <if test="year != null">
                year = #{year,jdbcType=INTEGER},
            </if>
            <if test="month != null">
                month = #{month,jdbcType=INTEGER},
            </if>
            <if test="hierarchyId != null">
                hierarchy_id = #{hierarchyId,jdbcType=VARCHAR},
            </if>
            <if test="ranges != null">
                ranges = #{ranges,jdbcType=INTEGER},
            </if>
            <if test="createdDate != null">
                created_date = #{createdDate,jdbcType=TIMESTAMP},
            </if>
            <if test="createdBy != null">
                created_by = #{createdBy,jdbcType=VARCHAR},
            </if>
            <if test="updateDate != null">
                update_date = #{updateDate,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=VARCHAR},
            </if>
        </set>
        where month_id = #{monthId,jdbcType=BIGINT}
    </update>

    <select id="selectOrganYearAndMonth" resultType="java.util.Map">
        select max(month) as month,year from performance_monthly WHERE
        element_id in (select element_id from performance_element where type!='城市公司' having CHAR_LENGTH(element_id)=4)
        GROUP BY year HAVING MAX(year)
    </select>

    <select id="selectCompanyYearAndMonth" resultType="java.util.Map">
        select max(month) as month,year from performance_monthly WHERE
        element_id in (select element_id from performance_element where type='城市公司' having CHAR_LENGTH(element_id)=4)
        GROUP BY year HAVING MAX(year)
    </select>

    <select id="selectCountMonthByYear" parameterType="int" resultType="int">
        select count(DISTINCT month) from performance_monthly where year = #{year}
    </select>

</mapper>