<?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.PerformanceMonthlyStatisticsMapper">
    <resultMap id="BaseResultMap"
               type="cn.com.countrygarden.bi.main.model.performance.monthly.PerformanceMonthlyStatistics">
        <constructor>
            <idArg column="pms_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
            <arg column="element_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
            <arg column="year" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="month" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="average_rate" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <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">
    pms_id, element_id, year, month, average_rate, 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_statistics
        where pms_id = #{pmsId,jdbcType=BIGINT}
    </select>

    <!--得到当前指标的最后月份-->
    <select id="selectMaxMonth" parameterType="java.util.Map" resultType="java.lang.Integer"> 
        select MAX(month) from performance_monthly_statistics where element_id = #{elementId,jdbcType=BIGINT} and year = #{year,jdbcType=INTEGER} GROUP BY element_id
    </select>

    <!--根据年/月/指标得到唯一一条数据-->
    <select id="getAverage" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from performance_monthly_statistics where element_id = #{elementId,jdbcType=BIGINT} and year = #{year,jdbcType=INTEGER} and month = #{month,jdbcType=INTEGER}
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from performance_monthly_statistics
    where pms_id = #{pmsId,jdbcType=BIGINT}
    </delete>

    <!--删除表中 年/月/指标 记录-->
    <delete id="deleteAverage" parameterType="java.util.Map">
        delete from performance_monthly_statistics
        where year = #{year,jdbcType=INTEGER} and  month = #{month,jdbcType=INTEGER}
    </delete>

    <!--重新计算 年/月/指标 指标平均完成率-->
    <insert id="insertMonthlyAverageScore" parameterType="java.util.Map">
        INSERT INTO performance_monthly_statistics (element_id,year,month,average_rate,created_date,created_by,update_date,update_by)
        SELECT
        e.element_id,#{year,jdbcType=INTEGER},#{month,jdbcType=INTEGER},IF(count(s.hierarchy_id) = 0,0,sum(IF(s.complete_rate=-1,0,s.complete_rate)) / count(s.hierarchy_id)) as average_rate,
        now() as created_date,'0000',now() as update_date,'0000'
        FROM
        performance_element e
        LEFT JOIN (select * from performance_monthly where year = #{year,jdbcType=INTEGER} and month =#{month,jdbcType=INTEGER}) s
        on e.element_id = s.element_id where e.type != '加/减分项'
        GROUP BY  e.element_id HAVING CHAR_LENGTH(e.element_id) = 4
    </insert>

    <insert id="insertSelective"
            parameterType="cn.com.countrygarden.bi.main.model.performance.monthly.PerformanceMonthlyStatistics">
        insert into performance_monthly_statistics
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pmsId != null">
                pms_id,
            </if>
            <if test="elementId != null">
                element_id,
            </if>
            <if test="year != null">
                year,
            </if>
            <if test="month != null">
                month,
            </if>
            <if test="averageRate != null">
                average_rate,
            </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="pmsId != null">
                #{pmsId,jdbcType=BIGINT},
            </if>
            <if test="elementId != null">
                #{elementId,jdbcType=BIGINT},
            </if>
            <if test="year != null">
                #{year,jdbcType=INTEGER},
            </if>
            <if test="month != null">
                #{month,jdbcType=INTEGER},
            </if>
            <if test="averageRate != null">
                #{averageRate,jdbcType=VARCHAR},
            </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.monthly.PerformanceMonthlyStatistics">
        update performance_monthly_statistics
        <set>
            <if test="elementId != null">
                element_id = #{elementId,jdbcType=BIGINT},
            </if>
            <if test="year != null">
                year = #{year,jdbcType=INTEGER},
            </if>
            <if test="month != null">
                month = #{month,jdbcType=INTEGER},
            </if>
            <if test="averageRate != null">
                average_rate = #{averageRate,jdbcType=VARCHAR},
            </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 pms_id = #{pmsId,jdbcType=BIGINT}
    </update>
</mapper>