<?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.PerformanceElementMapper">
    <resultMap id="ScoreInfoDtoMap" type="cn.com.countrygarden.bi.main.model.performance.dto.PerformanceDetailInfoDto">
        <result column="elementId" property="elementInfo.elementId" jdbcType="BIGINT"/>
        <result column="preElementId" property="elementInfo.preElementId" jdbcType="BIGINT"/>
        <result column="elementName" property="elementInfo.name" jdbcType="VARCHAR"/>
        <result column="elementType" property="elementInfo.type" jdbcType="VARCHAR"/>
        <result column="elementLevel" property="elementInfo.level" jdbcType="INTEGER"/>
        <result column="elementUnit" property="elementInfo.unit" jdbcType="INTEGER"/>
        <result column="targetHierarchyId" property="elementInfo.createdBy" jdbcType="VARCHAR"/>
        <collection property="scoreDetailInfo" ofType="cn.com.countrygarden.bi.main.model.performance.dto.PerformanceScoreDetailInfoDto">
            <result column="targetId" property="targetInfo.targetId" jdbcType="BIGINT"/>
            <result column="targetElementId" property="targetInfo.elementId" jdbcType="BIGINT"/>
            <result column="targeThresholdTarget" property="targetInfo.thresholdTarget" jdbcType="VARCHAR"/>
            <result column="targetAtLeastTarget" property="targetInfo.atLeastTarget" jdbcType="VARCHAR"/>
            <result column="targetScreamTarget" property="targetInfo.screamTarget" jdbcType="VARCHAR"/>
            <result column="targetHowlTarget" property="targetInfo.howlTarget" jdbcType="VARCHAR"/>
            <result column="targetFrequency" property="targetInfo.frequency" jdbcType="VARCHAR"/>
            <result column="targetYear" property="targetInfo.year" jdbcType="INTEGER"/>
            <result column="targetMonth" property="targetInfo.month" jdbcType="INTEGER"/>
            <result column="targetQuarterly" property="targetInfo.quarterly" jdbcType="VARCHAR"/>
            <result column="targetWeight" property="targetInfo.weight" jdbcType="FLOAT"/>
            <result column="targetIsCheck" property="targetInfo.isCheck" jdbcType="BOOLEAN"/>
            <result column="targetHierarchyId" property="targetInfo.hierarchyId" jdbcType="VARCHAR"/>
            <result column="scoreId" property="scoreInfo.scoreId" jdbcType="BIGINT"/>
            <result column="scoreElementId" property="scoreInfo.elementId" jdbcType="BIGINT"/>
            <result column="score" property="scoreInfo.score" jdbcType="REAL"/>
            <result column="scoreActualScore" property="scoreInfo.actualScore" jdbcType="REAL"/>
            <result column="scoreType" property="scoreInfo.type" jdbcType="VARCHAR"/>
            <result column="scoreComplete" property="scoreInfo.complete" jdbcType="REAL"/>
            <result column="scoreSquareNo" property="scoreInfo.squareNo" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>
    <resultMap id="ElementInfo" type="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="type_no" property="typeNo" jdbcType="VARCHAR"/>
        <result column="level" property="level" jdbcType="INTEGER"/>
        <result column="unit" property="unit" jdbcType="INTEGER"/>
        <result column="created_date" property="createdDate" jdbcType="VARCHAR"/>
        <result column="created_by" property="createdBy" jdbcType="VARCHAR"/>
        <result column="update_date" property="updateDate" jdbcType="VARCHAR"/>
        <result column="update_by" property="updateBy" jdbcType="VARCHAR"/>

    </resultMap>
    <sql id="ScoreInfoDtoColumn">
        e.element_id elementId,e.pre_element_id preElementId,e.name elementName,e.type elementType,e.level elementLevel,e.unit elementUnit,
        t.target_id targetId,t.element_id targetElementId,t.threshold_target targeThresholdTarget,t.at_least_target targetAtLeastTarget,
        t.scream_target targetScreamTarget,
        t.howl_target targetHowlTarget,t.frequency targetFrequency,s.year targetYear,s.month targetMonth,s.quarterly targetQuarterly,t.weight targetWeight,
        t.is_check targetIsCheck,s.hierarchy_id targetHierarchyId,s.score_id scoreId,s.element_id scoreElementId,s.score score,
        s.actual_score scoreActualScore,s.type scoreType,
        s.complete scoreComplete,s.square_no scoreSquareNo
    </sql>

    <select id="selectScoreInfo" resultMap="ScoreInfoDtoMap">
        SELECT <include refid="ScoreInfoDtoColumn"/>
        FROM performance_element e
        INNER JOIN performance_score s ON s.element_id = e.element_id
        INNER JOIN performance_env env ON env.hierarchy_id = s.hierarchy_id
        LEFT JOIN performance_target t ON t.element_id = e.element_id AND s.year = t.year AND s.quarterly = t.quarterly AND t.hierarchy_id = s.hierarchy_id
        WHERE s.hierarchy_id IN
        <foreach collection="hierarchyIdList" item="hierarchyId" open="(" separator="," close=")">
            #{hierarchyId}
        </foreach>
        AND e.level != 4
        ORDER BY s.quarterly,s.element_id DESC
    </select>

    <select id = "selectBigElement" resultMap="ElementInfo">
        select * from performance_element where pre_element_id is null
    </select>

    <select id="selectFirstElement" resultMap="ElementInfo">
       SELECT *  from  performance_element e where  pre_element_id =#{element.elementId}
    </select>

    <select id="selectElementByPreElementId" resultMap="ElementInfo">
        SELECT * from performance_element where pre_element_id like concat(#{firstElementId},'%')
    </select>

    <!--查询当前用户的所有指标(1表拥有，2表非拥有)  -->
    <select id="selectUserCodePower" resultMap="ElementInfo" parameterType="java.util.Map">
        <if test="sign == 1">
            select * from performance_app.performance_element,biapp.user_target b where element_id = b.elementId and pre_element_id is NULL and b.userCode = #{userCode} HAVING CHAR_LENGTH(element_id) != 1;
        </if>
        <if test="sign == 2">
            select * from performance_app.performance_element where element_id not in (select p.element_id from performance_app.performance_element p,biapp.user_target b where p.element_id = b.elementId and p.pre_element_id is NULL and b.userCode = #{userCode} and p.element_id != 0) and pre_element_id is NULL HAVING CHAR_LENGTH(element_id) != 1
        </if>
    </select>

    <!--通过id查询指标名称-->
    <select id="selectElementNameById" parameterType="long" resultType="cn.com.countrygarden.bi.main.model.performance.PerformanceElement">
        select * from performance_element where element_id = #{elementId}
    </select>

    <!--根据类型获取指标-->
    <select id="selectElementByType" parameterType="java.lang.String" resultMap="ElementInfo">
        select element_id,name,type_no,type
        from performance_element
        where
        1=1
        and type_no = #{type}
        <!--<if test=" type == '1'.toString() ">-->
            <!--and type_no = '1'-->
        <!--</if>-->
        <!--<if test=" type == '2'.toString() ">-->
            <!--and type_no = '2'-->
        <!--</if>-->
    </select>
</mapper>