<?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.PerformanceEnvMapper">
    <resultMap id="BaseColumn" type="cn.com.countrygarden.bi.main.model.performance.PerformanceEnv">
        <id column="env_id" property="envId" jdbcType="BIGINT"/>
        <result column="hierarchy_id" property="hierarchyId" jdbcType="VARCHAR"/>
        <result column="hierarchy_name" property="hierarchyName" jdbcType="VARCHAR"/>
        <result column="square" property="square" jdbcType="VARCHAR"/>
        <result column="scope" property="scope" jdbcType="VARCHAR"/>
        <result column="created_date" property="createdDate" jdbcType="TIMESTAMP"/>
        <result column="created_by" property="createdBy" jdbcType="VARCHAR"/>
        <result column="update_date" property="updateDate" jdbcType="TIMESTAMP"/>
        <result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
        <result column="IsDelete" jdbcType="SMALLINT" property="isdelete"/>
        <result column="IsComp" jdbcType="SMALLINT" property="iscomp"/>
        <!-- <collection property="performanceTarget" ofType="cn.com.countrygarden.bi.main.model.performance.PerformanceTarget">
           <id column="target_id" property="targetId" jdbcType="BIGINT"/>
           <result column="element_id" property="elementId" jdbcType="BIGINT"/>
           <result column="threshold_target" property="thresholdTarget" jdbcType="VARCHAR"/>
           <result column="at_least_target" property="atLeastTarget" jdbcType="VARCHAR"/>
           <result column="target_average" property="targetAverage" jdbcType="VARCHAR"/>
           <result column="scream_target" property="screamTarget" jdbcType="VARCHAR"/>
           <result column="howl_target" property="howlTarget" jdbcType="VARCHAR"/>
           <result column="hierarchy" property="hierarchy" jdbcType="VARCHAR"/>
           <result column="hierarchy_id" property="hierarchyId" jdbcType="VARCHAR"/>
           <result column="frequency" property="frequency" jdbcType="VARCHAR"/>
           <result column="year" property="year" jdbcType="INTEGER"/>
           <result column="month" property="month" jdbcType="INTEGER"/>
           <result column="quarterly" property="quarterly" jdbcType="VARCHAR"/>
           <result column="weight" property="weight" jdbcType="DOUBLE"/>
           <result column="is_check" property="isCheck" jdbcType="VARCHAR"/>
           <result column="struct_param" property="structParam" jdbcType="VARCHAR"/>
            <result column="created_date" property="createdDate" jdbcType="TIMESTAMP"/>
           <result column="created_by" property="createdBy" jdbcType="VARCHAR"/>
           <result column="update_date" property="updateDate" jdbcType="TIMESTAMP"/>
           <result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
       </collection>  -->
        <!--  <association property="performanceElement" javaType="cn.com.countrygarden.bi.main.model.performance.PerformanceElement">
          <id column="element_id" property="elementId" jdbcType="BIGINT"/>
             <result column="pre_element_id" property="preElementId" jdbcType="BIGINT"/>
             <result column="name" property="name" jdbcType="VARCHAR"/>
             <result column="type" property="type" jdbcType="VARCHAR"/>
             <result column="level" property="level" jdbcType="INTEGER"/>
             <result column="unit" property="unit" jdbcType="INTEGER"/>
             <result column="created_date" property="createdDate" jdbcType="TIMESTAMP"/>
             <result column="created_by" property="createdBy" jdbcType="VARCHAR"/>
             <result column="update_date" property="updateDate" jdbcType="TIMESTAMP"/>
             <result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
         </association>
         <association property="performanceTarget" javaType="cn.com.countrygarden.bi.main.model.performance.PerformanceTarget">
             <id column="target_id" property="targetId" jdbcType="BIGINT"/>
             <result column="element_id" property="elementId" jdbcType="BIGINT"/>
             <result column="threshold_target" property="thresholdTarget" jdbcType="VARCHAR"/>
             <result column="at_least_target" property="atLeastTarget" jdbcType="VARCHAR"/>
             <result column="target_average" property="targetAverage" jdbcType="VARCHAR"/>
             <result column="scream_target" property="screamTarget" jdbcType="VARCHAR"/>
             <result column="howl_target" property="howlTarget" jdbcType="VARCHAR"/>
             <result column="hierarchy" property="hierarchy" jdbcType="VARCHAR"/>
             <result column="hierarchy_id" property="hierarchyId" jdbcType="VARCHAR"/>
             <result column="frequency" property="frequency" jdbcType="VARCHAR"/>
             <result column="year" property="year" jdbcType="INTEGER"/>
             <result column="month" property="month" jdbcType="INTEGER"/>
             <result column="quarterly" property="quarterly" jdbcType="VARCHAR"/>
             <result column="weight" property="weight" jdbcType="DOUBLE"/>
             <result column="is_check" property="isCheck" jdbcType="VARCHAR"/>
             <result column="struct_param" property="structParam" jdbcType="VARCHAR"/>
              <result column="created_date" property="createdDate" jdbcType="TIMESTAMP"/>
             <result column="created_by" property="createdBy" jdbcType="VARCHAR"/>
             <result column="update_date" property="updateDate" jdbcType="TIMESTAMP"/>
             <result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
          </association>  -->
    </resultMap>

    <resultMap id="OrganDto" type="cn.com.countrygarden.bi.main.model.performance.dto.PerformanceEnvDto"
               extends="BaseColumn">
    </resultMap>

    <resultMap id="ScoreListDto" type="cn.com.countrygarden.bi.main.model.performance.dto.PerformanceScoreListDto">
        <result column="envId" property="organInfo.envId" jdbcType="BIGINT"/>
        <result column="envHierarchyId" property="organInfo.hierarchyId" jdbcType="VARCHAR"/>
        <result column="envHierarchyName" property="organInfo.hierarchyName" jdbcType="VARCHAR"/>
        <result column="envSquare" property="organInfo.square" jdbcType="VARCHAR"/>
        <result column="envScope" property="organInfo.scope" jdbcType="VARCHAR"/>
        <collection property="scoreInfo"
                    ofType="cn.com.countrygarden.bi.main.model.performance.dto.PerformanceScoreDto">
            <result column="scoreScoreId" property="scoreId" jdbcType="BIGINT"/>
            <result column="scoreScore" property="score" jdbcType="REAL"/>
            <result column="scoreActualScore" property="actualScore" jdbcType="REAL"/>
            <result column="scoreComplete" property="complete" jdbcType="REAL"/>
            <!--<result column="scoreMonth" property="month" jdbcType="INTEGER"/>-->
            <result column="scoreQuarterly" property="quarterly" jdbcType="VARCHAR"/>
            <result column="scoreYear" property="year" jdbcType="INTEGER"/>
            <result column="scoreNum" property="num" jdbcType="INTEGER"/>
            <result column="scoreAtLeastTarget" property="atLeastTarget" jdbcType="VARCHAR"/>
            <result column="scoreHowlTarget" property="howlTarget" jdbcType="VARCHAR"/>
            <result column="scoreAverageScore" property="averageScore" jdbcType="REAL"/>
            <result column="scoreTotalAvgScore" property="actualAverageScore" jdbcType="REAL"/>
        </collection>
    </resultMap>

    <resultMap id="scoreInfoDto" type="cn.com.countrygarden.bi.main.model.performance.dto.PerformanceScoreDto">
        <result column="scoreScoreId" property="scoreId" jdbcType="BIGINT"/>
        <result column="scoreScore" property="score" jdbcType="REAL"/>
        <result column="scoreActualScore" property="actualScore" jdbcType="REAL"/>
        <result column="scoreComplete" property="complete" jdbcType="REAL"/>
        <!--<result column="scoreMonth" property="month" jdbcType="INTEGER"/>-->
        <result column="scoreQuarterly" property="quarterly" jdbcType="VARCHAR"/>
        <result column="scoreYear" property="year" jdbcType="INTEGER"/>
        <result column="scoreNum" property="num" jdbcType="INTEGER"/>
        <result column="scoreAtLeastTarget" property="atLeastTarget" jdbcType="VARCHAR"/>
        <result column="scoreHowlTarget" property="howlTarget" jdbcType="VARCHAR"/>
        <result column="scoreAverageScore" property="averageScore" jdbcType="REAL"/>
        <result column="scoreTotalAvgScore" property="actualAverageScore" jdbcType="REAL"/>
    </resultMap>

    <sql id="ScoreListColumn">
        sta.average_score scoreAverageScore,
        sta.actual_average_score scoreTotalAvgScore,
        s.score scoreScore,
        s.actual_score scoreActualScore,
        s.score_id scoreScoreId,
        s.complete scoreComplete,
        s.quarterly scoreQuarterly,
        s.year scoreYear,
        s.num scoreNum,
        t.at_least_target scoreAtLeastTarget,
        t.howl_target scoreHowlTarget
    </sql>


    <select id="selectScoreList" resultMap="scoreInfoDto">
        SELECT
        <include refid="ScoreListColumn"/>
        FROM performance_score s
        LEFT JOIN performance_statistics sta ON sta.element_id = s.element_id AND sta.year = s.year AND sta.quarterly =
        s.quarterly
        LEFT JOIN performance_target t ON t.hierarchy_id = s.hierarchy_id AND t.quarterly = s.quarterly AND t.year =
        s.year AND t.element_id = s.element_id
        WHERE s.hierarchy_id = #{hierarchyId}
        AND s.element_id = #{element} AND s.year =#{year} and s.quarterly = #{quarter}
    </select>

    <insert id="insertEnvList" parameterType="java.util.List">
        INSERT INTO performance_env VALUES
        <foreach collection="envList" item="env" separator=",">
            (#{env.envId},#{env.hierarchyId},#{env.hierarchyName},#{env.square}
            ,#{env.scope},#{env.createdDate},#{env.createdBy},#{env.updateDate},#{env.updateBy})
        </foreach>
    </insert>

    <select id="selectById" resultMap="OrganDto">
        SELECT env_id,hierarchy_id,hierarchy_name,square,scope
        FROM performance_env e
        <where>
            <if test="hierarchyIdList != null">
                e.hierarchy_id IN
                <foreach collection="hierarchyIdList" item="hierarchyId" open="(" separator="," close=")">
                    #{hierarchyId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getByHierarchyId" resultMap="BaseColumn">
    SELECT e.hierarchy_name
    FROM
    performance_env e
    where
     e.hierarchy_id = #{env.hierarchyId} 
</select>

    <!--查询缺少的区域-->
    <select id="selectNotInName" resultType="java.lang.String"
            parameterType="cn.com.countrygarden.bi.main.model.performance.PerformanceScore">
        select hierarchy_name from performance_env
        where hierarchy_id not in (select hierarchy_id from performance_score where year=#{year} and quarterly=#{quarterly} and element_id=#{elementId})
        and IsDelete=0 and IsComp=1 and hierarchy_name LIKE '%区域%' and hierarchy_name NOT LIKE '%测试%' ORDER BY hierarchy_id
    </select>

    <!--查询缺少的城市-->
    <select id="selectNotInNameCompany" resultType="java.lang.String"
            parameterType="cn.com.countrygarden.bi.main.model.performance.PerformanceScore">
        select hierarchy_name from performance_env
        where hierarchy_id not in (select hierarchy_id from performance_score where year=#{year} and quarterly=#{quarterly} and element_id=#{elementId})
        and IsDelete=0 and IsComp=1 and hierarchy_name LIKE '%公司%' and hierarchy_name NOT LIKE '%测试%' ORDER BY hierarchy_id
    </select>

    <!--查询缺少的月度区域-->
    <select id="selectMonthlyNotInNameRegion" resultType="java.lang.String" parameterType="java.util.Map">
        select hierarchy_name from performance_env
        where hierarchy_id not in (select hierarchy_id from performance_monthly where year=#{year} and month=#{month} and element_id=#{elementId})
        and IsDelete=0 and IsComp=1 and hierarchy_name LIKE '%区域%' and hierarchy_name NOT LIKE '%测试%' ORDER BY hierarchy_id
    </select>

    <!--绩效所有有效区域-->
    <select id="selectPerformanceOrgan" resultMap="BaseColumn">
        select * from performance_env where IsDelete=0 and IsComp=1 and hierarchy_name LIKE '%区域%' and hierarchy_name NOT LIKE '%测试%' ORDER BY hierarchy_id
    </select>

    <!--绩效所有有效公司-->
    <select id="selectPerformanceCompany" resultMap="BaseColumn">
        select * from performance_env where IsDelete=0 and IsComp=1 and hierarchy_name LIKE '%公司%' and hierarchy_name NOT LIKE '%测试%' ORDER BY hierarchy_id
    </select>

    <!--过滤城市公司和有效的区域-->
    <select id="selectOrganFilter" resultType="java.lang.String"  parameterType="java.util.List">
        select hierarchy_id from performance_env where hierarchy_id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        and IsDelete=0 and IsComp=1 and hierarchy_name like '%区域%' and hierarchy_name NOT LIKE '%测试%' ORDER BY hierarchy_id
    </select>

</mapper>