<?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.PerformanceRecordMapper">

    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.performance.PerformanceRecord">
        <id column="record_id" jdbcType="BIGINT" property="recordId"/>
        <result column="hierachy_id" jdbcType="VARCHAR" property="hierachyId"/>
        <result column="element_id" jdbcType="BIGINT" property="elementId"/>
        <result column="year" jdbcType="INTEGER" property="year"/>
        <result column="quarter" jdbcType="INTEGER" property="quarter"/>
        <result column="rate" jdbcType="VARCHAR" property="rate"/>
        <result column="weight" jdbcType="DOUBLE" property="weight"/>
        <result column="scope" jdbcType="VARCHAR" property="scope"/>
        <result column="ranges" jdbcType="INTEGER" property="ranges"/>
        <result column="is_examine" jdbcType="SMALLINT" property="isExamine"/>
        <result column="is_delete" jdbcType="SMALLINT" property="isDelete"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
    </resultMap>

    <!--我的考核Dto-->
    <resultMap id="SubResultMap" type="cn.com.countrygarden.bi.main.model.performance.dto.PerformanceRecordDto" extends="BaseResultMap"/>

    <!--绩效分析Dto-->
    <resultMap id="SubResultMap2" type="cn.com.countrygarden.bi.main.model.performance.dto.PerformanceAnalysisRecordDto" extends="BaseResultMap">
        <result column="score" jdbcType="VARCHAR" property="score"/>
        <result column="actual_score" jdbcType="VARCHAR" property="actualScore"/>
        <result column="complete" jdbcType="VARCHAR" property="complete"/>
        <result column="yearly_score" jdbcType="VARCHAR" property="yearlyScore"/>
        <result column="quarterly_score" jdbcType="VARCHAR" property="quarterlyScore"/>
        <result column="is_profit_reach" jdbcType="VARCHAR" property="isProfitReach"/>
    </resultMap>


    <!--通用查询结果-->
    <sql id="Base_Column_List">
        record_id,hierachy_id,element_id,year,quarter,rate,weight,scope,ranges,is_examine,is_delete,create_date,create_by,update_date,update_by
    </sql>

    <insert id="insertSelective" parameterType="PerformanceRecord">
        insert into performance_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="recordId != null">
                record_id,
            </if>
            <if test="hierachyId != null">
                hierachy_id,
            </if>
            <if test="elementId != null">
                element_id,
            </if>
            <if test="year != null">
                year,
            </if>
            <if test="quarter != null">
                quarter,
            </if>
            <if test="rate != null">
                rate,
            </if>
            <if test="weight != null">
                weight,
            </if>
            <if test="scope != null">
                scope,
            </if>
            <if test="ranges != null">
                ranges,
            </if>
            <if test="isExamine != null">
                is_examine,
            </if>
            <if test="isDelete != null">
                is_delete,
            </if>
            <if test="createDate != null">
                create_date,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="updateDate != null">
                update_date,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="lrdb != null">
                lrdb,
            </if>
            <if test="fwzzdf != null">
                fwzzdf,
            </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="recordId != null">
                #{recordId,jdbcType=BIGINT },
            </if>
            <if test="hierachyId != null">
                #{hierachyId,jdbcType=VARCHAR },
            </if>
            <if test="elementId != null">
                #{elementId,jdbcType=BIGINT },
            </if>
            <if test="year != null">
                #{year,jdbcType=INTEGER },
            </if>
            <if test="quarter != null">
                #{quarter,jdbcType=INTEGER},
            </if>
            <if test="rate != null">
                #{rate,jdbcType=VARCHAR},
            </if>
            <if test="weight != null">
                #{weight,jdbcType=DOUBLE},
            </if>
            <if test="scope != null">
                #{scope,jdbcType=VARCHAR},
            </if>
            <if test="ranges != null">
                #{ranges,jdbcType=INTEGER},
            </if>
            <if test="isExamine != null">
                #{isExamine,jdbcType=SMALLINT},
            </if>
            <if test="isDelete != null">
                #{isDelete,jdbcType=SMALLINT },
            </if>
            <if test="createDate != null">
                #{createDate,jdbcType=TIMESTAMP },
            </if>
            <if test="createBy != null">
                #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateDate != null">
                #{updateDate,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                #{updateBy,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

    <insert id="insertQuarterlySumRecords" parameterType="java.util.Map">
        <if test="typeNo == '1'.toString()">
            insert into performance_record(element_id,hierachy_id,year,quarter)
            select 0, precent.hierachy_id as hierachy_id,#{year},#{quarter}
            from (
            select t.hierachy_id,t.element_id
            from
            (select element_id from performance_element where type != '加/减分项' and type_no = 1) e
            left JOIN (select hierachy_id,element_id from performance_record where year = #{year} and quarter = #{quarter}) t
            on e.element_id = t.element_id
            HAVING CHAR_LENGTH(element_id) = 4
            ) precent
            group by hierachy_id
        </if>
        <if test="typeNo == '2'.toString()">
            insert into performance_record(element_id,hierachy_id,year,quarter)
            select 1, precent.hierachy_id as hierachy_id,#{year},#{quarter}
            from (
            select t.hierachy_id,t.element_id
            from
            (select element_id from performance_element where type_no = 2) e
            left join (select hierachy_id,element_id from performance_record where year = #{year} and quarter = #{quarter}) t
            on e.element_id = t.hierachy_id
            HAVING CHAR_LENGTH(element_id) = 4
            ) precent
            group by hierachy_id
        </if>
    </insert>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from performance_record
        where record_id = #{recordId,jdbcType=BIGINT}
    </delete>

    <delete id="deleteAllRecords" parameterType="java.util.Map">
        delete
        from performance_record
        where (element_id = 0 or element_id = 1) and year = #{year} and quarter = #{quarter}
    </delete>

    <update id="updateByPrimaryKeySelective" parameterType="PerformanceArea">
        update performance_record
        <trim prefix="set" suffixOverrides=",">
            <if test="hierachyId != null">
                hierachy_id = #{hierachyId,jdbcType=VARCHAR },
            </if>
            <if test="elementId != null">
                element_id = #{elementId,jdbcType=BIGINT },
            </if>
            <if test="year != null">
                year = #{year,jdbcType=INTEGER },
            </if>
            <if test="quarter != null">
                quarter = #{quarter,jdbcType=INTEGER},
            </if>
            <if test="rate != null">
                rate = #{rate,jdbcType=VARCHAR},
            </if>
            <if test="weight != null">
                weight = #{weight,jdbcType=DOUBLE},
            </if>
            <if test="scope != null">
                scope = #{scope,jdbcType=VARCHAR},
            </if>
            <if test="ranges != null">
                ranges = #{ranges,jdbcType=INTEGER},
            </if>
            <if test="isExamine != null">
                is_examine =  #{isExamine,jdbcType=SMALLINT},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete,jdbcType=SMALLINT },
            </if>
            <if test="createDate != null">
                create_date = #{createDate,jdbcType=TIMESTAMP },
            </if>
            <if test="createBy != null">
                create_by #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateDate != null">
                update_date = #{updateDate,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=VARCHAR},
            </if>
             <if test="lrdb != null">
               update_by = #{lrdb,jdbcType=VARCHAR},
            </if>
            <if test="fwzzdf != null">
               update_by = #{fwzzdf,jdbcType=VARCHAR},
            </if>
        </trim>
        where record_id = #{recordId,jdbcType=BIGINT}
    </update>

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

    <select id="selectCount" parameterType="java.util.Map">
        select count(*)
        from
        performance_record
        where
        element_id = #{elementId} and year = #{year} and quarter = #{quarter}
    </select>

    <select id="selectNotExistAreaName" parameterType="java.util.Map" resultType="java.lang.String">
        select hierachy_name
        from performance_area a
        left join
        performance_record r
        on a.hierachy_id = r.hierachy_id
        where type_no = #{typeNo}
        and a.hierachy_id not in (select hierachy_id from performance_record where element_id = #{elementId} and year = #{year} and quarter = #{quarter})
        group by hierachy_name
    </select>

    <select id="selectMonthlyNotExistAreaName" parameterType="java.util.Map" resultType="java.lang.String">
        select hierachy_name from performance_area
        where hierachy_id not in (select hierarchy_id from performance_monthly where year = #{year} and month = #{month} and element_id = #{elementId})
        and is_delete = 0 and hierachy_name and type_no = #{typeNo} NOT LIKE '%测试%' ORDER BY hierachy_id
    </select>

    <select id="selectBySelective" parameterType="PerformanceRecord" resultMap="SubResultMap">
        select
        record_id,hierachy_id,element_id,year,quarter,rate,weight,scope,ranges,is_examine,is_delete,create_date,create_by,update_date,update_by
        from performance_record
        where 1=1
        and hierachy_id = #{record.hierachyId,jdbcType=VARCHAR}
        and element_id = #{record.elementId,jdbcType=BIGINT}
        and year = #{record.year,jdbcType=INTEGER}
        and quarter = #{record.quarter,jdbcType=INTEGER}
    </select>

    <select id="selectBySelective2" parameterType="PerformanceRecord" resultMap="SubResultMap2">
        select r.record_id,r.hierachy_id,r.element_id,r.year,r.quarter,r.rate,r.weight,r.scope,r.ranges,r.is_examine,r.is_delete,
        t.at_Least_target,t.howl_target,s.score,s.actual_score,s.complete,s.yearly_score,s.quarterly_score,s.is_profit_reach
        from performance_record r
        left join performance_target t on r.record_id = t.record_id
        inner join performance_score s on r.record_id = s.record_id
        where 1=1
        and r.hierachy_id = #{record.hierachyId,jdbcType=VARCHAR}
        and r.element_id = #{record.elementId,jdbcType=BIGINT}
        and r.year =  #{record.year,jdbcType=INTEGER}
        and r.quarter =  #{record.quarter,jdbcType=INTEGER}
    </select>

    <select id="selectBySelective3" parameterType="PerformanceRecord" resultType="Integer">
        select
        record_id
        from performance_record
        where 1=1
        and hierachy_id = #{record.hierachyId,jdbcType=VARCHAR}
        and element_id = #{record.elementId,jdbcType=BIGINT}
        and year = #{record.year,jdbcType=INTEGER}
        and quarter = #{record.quarter,jdbcType=INTEGER}
    </select>

    <select id="selectScope" parameterType="java.util.Map" resultType="java.util.Map">
        select hierachy_id,scope from performance_record where 1=1
        and element_id = #{elementId,jdbcType=BIGINT}
        and year = #{year,jdbcType=INTEGER}
        and quarter = #{quarter,jdbcType=INTEGER}
    </select>

    <select id="getLastQuarterSort" resultMap="BaseResultMap">
        SELECT
        @rank :=@rank + 1 AS ranges,
        hierachy_id,
        record_id,
        actual_score
        FROM
        (
        SELECT
        r.hierachy_id,r.record_id,s.actual_score,r.element_id,r.year,r.quarter
        FROM
        performance_record r
        LEFT JOIN performance_score s
        on r.record_id = s.record_id
        WHERE
        hierachy_id IN (
        SELECT
        hierachy_id
        FROM
        performance_area
        WHERE
        is_delete = 0
        AND hierachy_name NOT LIKE '测试'
        AND type_no = 1
        )
        AND r.year = #{year}
        AND r.quarter = #{quarter}
        ) a,
        (SELECT(@rank := 0)) b
        WHERE
        element_id = 0
        ORDER BY
        actual_score DESC
    </select>

    <select id="selectCountQuarterByYear" resultType="int">
        select count(DISTINCT quarter) from performance_record where year = #{year}
    </select>

    <select id="getNewYearQuarterByType" parameterType="java.lang.String" resultMap="BaseResultMap">
        select max(quarter) as quarter,year from performance_record
        WHERE element_id in (select element_id from performance_element where type_no = #{typeNo} having CHAR_LENGTH(element_id)=4)
        GROUP BY year HAVING MAX(year)
    </select>

    <update id="updateScope" parameterType="java.util.Map">
        update performance_record set scope = #{scope} where record_id = #{recordId}
    </update>

</mapper>