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

    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.performance.PerformanceArea">
        <id column="area_id" jdbcType="INTEGER" property="areaId"/>
        <result column="type_no" jdbcType="VARCHAR" property="typeNo"/>
        <result column="hierachy_id" jdbcType="VARCHAR" property="hierachyId"/>
        <result column="hierachy_name" jdbcType="VARCHAR" property="hierachyName"/>
        <result column="sub_type" jdbcType="VARCHAR" property="subType"/>
        <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>

    <resultMap id="areaDto" type="cn.com.countrygarden.bi.main.model.performance.dto.PerformanceAreaDto"
               extends="BaseResultMap">
    </resultMap>

    <!--通用查询结果-->
    <sql id="Base_Column_List">
       area_id,type_no,hierachy_id,hierachy_name,sub_type,is_delete,create_date,create_by,update_date,update_by
    </sql>

    <insert id="insertSelective" parameterType="PerformanceArea">
        insert into performance_area
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId != null">
                area_id,
            </if>
            <if test="typeNo != null">
                type_no,
            </if>
            <if test="hierachyId != null">
                hierachy_id,
            </if>
            <if test="hierachyName != null">
                hierachy_name,
            </if>
            <if test="subType != null">
                sub_type,
            </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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="areaId != null">
                    #{areaId,jdbcType=INTEGER },
                </if>
                <if test="typeNo != null">
                    #{typeNo,jdbcType=VARCHAR },
                </if>
                <if test="hierachyId != null">
                    #{hierachyId,jdbcType=VARCHAR },
                </if>
                <if test="hierachyName != null">
                    #{hierachyName,jdbcType=VARCHAR },
                </if>
                <if test="subType != null">
                    #{subType,jdbcType=VARCHAR},
                </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>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from performance_area
        where area_id = #{areaId,jdbcType=INTEGER}
    </delete>

    <update id="updateByPrimaryKeySelective" parameterType="PerformanceArea">
        update performance_area
        <trim prefix="set" suffixOverrides=",">
            <if test="typeNo != null">
               type_no = #{typeNo,jdbcType=VARCHAR },
            </if>
            <if test="hierachyId != null">
               hierachy_id = #{hierachyId,jdbcType=VARCHAR },
            </if>
            <if test="hierachyName != null">
               hierachy_name = #{hierachyName,jdbcType=VARCHAR },
            </if>
            <if test="subType != null">
               sub_type = #{subType,jdbcType=VARCHAR},
            </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>
        </trim>
        where area_id = #{areaId,jdbcType=INTEGER}
    </update>

    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from performance_area
        where area_id = #{areaId,jdbcType=INTEGER}
    </select>

    <select id="selectAllByTypeNo" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from performance_area
        where
           is_delete = 0 and hierachy_name NOT LIKE '%测试%'
            <if test="typeNo == 1">
               and type_no = 1 and hierachy_name LIKE '%区域%'
            </if>
            <if test="typeNo == 2">
               and type_no = 2 and hierachy_name LIKE '%公司%'
            </if>
        order by hierachy_id
    </select>

    <select id="selectCountByTypeNo" parameterType="java.lang.String" resultType="int">
        select count(*)
        from performance_area
        where
        is_delete = 0 and hierachy_name NOT LIKE '%测试%'
        <if test="typeNo == 1">
            and type_no = 1 and hierachy_name LIKE '%区域%'
        </if>
        <if test="typeNo == 2">
            and type_no = 2 and hierachy_name LIKE '%公司%'
        </if>
        order by hierachy_id
    </select>

    <select id="areaFilter" parameterType="java.util.List" resultType="java.lang.String">
        select hierachy_id
        from performance_area
        where hierachy_id in
        <foreach collection="areaList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        and is_delete = 0 and type_no = #{typeNo}
       <if test="typeNo == 1">
           and hierachy_name like '%区域%'
       </if>
        <if test="typeNo == 2">
            and hierachy_name like '%公司%'
        </if>
        and hierachy_name NOT LIKE '%测试%' ORDER BY hierachy_id
    </select>

    <select id="selectByIds" parameterType="java.util.List" resultMap="areaDto">
        select
        <include refid="Base_Column_List"/>
        from performance_area
        where hierachy_id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="getSubType" parameterType="java.lang.String" resultType="java.lang.String">
        select sub_type from performance_area where hierachy_id = #{hierachyId}
    </select>

    <select id="selectHierachyName" parameterType="java.lang.String" resultType="java.lang.String">
        select hierachy_name from performance_area where hierachy_id = #{hierachyId}
    </select>


</mapper>