<?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.PerformanceStatisticsMapper">
    <resultMap id="BaseColumn" type="cn.com.countrygarden.bi.main.model.performance.PerformanceStatistics">
        <id column="statistics_id" property="statisticsId" jdbcType="BIGINT"/>
        <result column="element_id" property="elementId" jdbcType="BIGINT"/>
        <result column="year" property="year" jdbcType="INTEGER"/>
        <result column="month" property="month" jdbcType="INTEGER"/>
        <result column="quarterly" property="quarter" jdbcType="VARCHAR"/>
        <result column="average_score" property="averageScore" jdbcType="REAL"/>
        <result column="actual_average_score" property="actualAverageScore" jdbcType="REAL"/>
        <result column="created_date" property="createdDate" jdbcType="DATE"/>
        <result column="created_by" property="createdBy" jdbcType="VARCHAR"/>
        <result column="update_date" property="updateDate" jdbcType="DATE"/>
        <result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="SqlColumn">
        statistics_id,element_id,year,month,quarterly,average_score,actual_average_score,created_date,created_by,update_date,update_by
    </sql>

    <!--季度平均分插入和删除操作-->
    <!--@Author:Geoffrey-->
    <!--start-->
    <select id="insertQuarterlyAverageScore">
        INSERT INTO performance_statistics(element_id,year,quarterly,average_score,actual_average_score,created_date,created_by,update_date,update_by)
        SELECT
        e.element_id,#{year},#{quarter},IF(count(s.hierarchy_id) = 0,0,sum(s.score) / count(s.hierarchy_id)) as averageScore,
        IF(s.element_id = 0,IF(count(s.hierarchy_id) = 0,0,sum(s.actual_score) / count(s.hierarchy_id)),0) as actualAverageScore,
        now() as createDate,'0000' as createBy,now() as updateDate,'0000' as updateBy

        FROM
        performance_element e
        LEFT JOIN (select * from performance_score where year = #{year} and quarterly = #{quarter}) s
        on e.element_id = s.element_id
        LEFT JOIN
        (select * from performance_target where is_check = 1 and year = #{year} and quarterly = #{quarter} ) t
        on s.element_id = t.element_id and  t.hierarchy_id = s.hierarchy_id
        GROUP BY  e.element_id;
    </select>

    <delete id="deleteQuarterlyAverageScore">
        delete from performance_statistics where year = #{year} and quarterly = #{quarter};
    </delete>

    <!--end-->

    <insert id="insertStatisticsList" parameterType="java.util.List">
        INSERT INTO performance_statistics VALUES
        <foreach collection="statisticsList" item="statistics" separator=",">
            (#{statistics.statisticsId},#{statistics.elementId},#{statistics.year},#{statistics.month},#{statistics.quarterly},
            #{statistics.averageScore},#{statistics.actualAverageScore},#{statistics.createdDate},#{statistics.createdBy},#{statistics.updateDate},
            #{statistics.updateBy})
        </foreach>
    </insert>

    <select id="selectStatisticsList" resultMap="BaseColumn">
        SELECT
        <include refid="SqlColumn"/>
        FROM performance_statistics
        WHERE 1 = 1
        <if test="sta.year != null">
            AND year = #{sta.year}
        </if>
        <if test="sta.quarterly != null">
            AND quarterly = #{sta.quarterly}
        </if>
    </select>


    <delete id="deleteAllByRecordIds" parameterType="java.util.Map">
        delete from performance_statistics
        where year=#{year} and quarterly = #{quarter}

    </delete>

    <insert id="insertQuarterlyAverageScore2" parameterType="java.util.Map">
        insert into performance_statistics(element_id,year,quarterly,average_score,actual_average_score)
        select
         t.element_id as element_id,
         #{year},
         #{quarter},
         IF(count(t.hierachy_id) = 0,0,sum(s.score) / count(t.hierachy_id)) as averageScore,
         IF(t.element_id = 0,IF(count(t.hierachy_id) = 0,0,sum(s.actual_score) / count(t.hierachy_id)),0) as actualAverageScore
        from
					(select record_id,score,actual_score from performance_score) s
					INNER JOIN (select record_id,hierachy_id,element_id from performance_record where is_examine = 1 and year = #{year} and quarter = #{quarter} ) t
					on s.record_id = t.record_id
		group by element_id
    </insert>

    <select id="selectBySelective" parameterType="java.util.Map" resultMap="BaseColumn">
        select
        <include refid="SqlColumn"/>
        from performance_statistics
        where element_id = #{elementId} and year = #{year} and quarterly = #{quarter}
    </select>

    <select id="selectAllQuarterAve" parameterType="java.util.Map" resultMap="BaseColumn">
        select statistics_id,element_id,year,quarterly,average_score,actual_average_score
        from performance_statistics
        where element_id = #{elementId} and year = #{year}
        order by quarterly asc
    </select>

</mapper>