<?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.PerformanceCompanyMapper">
    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.performance.PerformanceCompany">
        <constructor>
            <idArg column="com_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
            <arg column="hierarchy_id" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="hierarchy_name" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="type" javaType="java.lang.String" jdbcType="VARCHAR" />
            <arg column="is_delete" javaType="java.lang.Short" jdbcType="SMALLINT"/>
            <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>

    <resultMap id="compDto" type="cn.com.countrygarden.bi.main.model.performance.dto.PerformanceCompanyDto">
        <id column="com_id" property="comId" jdbcType="BIGINT"/>
        <result column="hierarchy_id" property="hierarchyId" jdbcType="VARCHAR"/>
        <result column="hierarchy_name" property="hierarchyName" 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"/>
    </resultMap>

    <sql id="Base_Column_List">
    com_id, hierarchy_id, hierarchy_name, type, is_delete, 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_company
        where com_id = #{comId,jdbcType=BIGINT}
    </select>

    <!--获得有效的城市公司-->
    <select id="selectPerformanceCompany" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from performance_company
        where is_delete = 0
    </select>

    <!--查询缺少的城市-->
    <select id="selectNotInNameCompany" resultType="java.lang.String"
            parameterType="cn.com.countrygarden.bi.main.model.performance.PerformanceScore">
        select hierarchy_name from performance_company
        where hierarchy_id not in (select hierarchy_id from performance_score where year=#{year} and quarterly=#{quarterly} and element_id=#{elementId})
        and is_delete=0 ORDER BY hierarchy_id
    </select>

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

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from performance_company
    where com_id = #{comId,jdbcType=BIGINT}
  </delete>

    <insert id="insertSelective" parameterType="cn.com.countrygarden.bi.main.model.performance.PerformanceCompany">
        insert into performance_company
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="comId != null">
                com_id,
            </if>
            <if test="hierarchyId != null">
                hierarchy_id,
            </if>
            <if test="hierarchyName != null">
                hierarchy_name,
            </if>
            <if test="type != null">
                type,
            </if>
            <if test="isDelete != null">
                is_delete,
            </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="comId != null">
                #{comId,jdbcType=BIGINT},
            </if>
            <if test="hierarchyId != null">
                #{hierarchyId,jdbcType=VARCHAR},
            </if>
            <if test="hierarchyName != null">
                #{hierarchyName,jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                #{type,jdbcType=VARCHAR},
            </if>
            <if test="isDelete != null">
                #{isDelete,jdbcType=SMALLINT},
            </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.PerformanceCompany">
        update performance_company
        <set>
            <if test="hierarchyId != null">
                hierarchy_id = #{hierarchyId,jdbcType=VARCHAR},
            </if>
            <if test="hierarchyName != null">
                hierarchy_name = #{hierarchyName,jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                type = #{type,jdbcType=VARCHAR},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete,jdbcType=SMALLINT},
            </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 com_id = #{comId,jdbcType=BIGINT}
    </update>

    <!--通过多个id查询城市公司Dto列表-->
    <select id="selectByIds" resultMap="compDto">
        SELECT com_id,hierarchy_id,hierarchy_name
        FROM performance_company c
        <where>
            <if test="hierarchyIdList != null">
                c.hierarchy_id IN
                <foreach collection="hierarchyIdList" item="hierarchyId" open="(" separator="," close=")">
                    #{hierarchyId}
                </foreach>
            </if>
        </where>
    </select>

    <!--过滤区域信息-->
    <select id="selectCompFilter" resultType="java.lang.String"  parameterType="java.util.List">
        select hierarchy_id from performance_company where hierarchy_id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        and is_delete = 0 and hierarchy_name NOT LIKE '%测试%'
    </select>

    <!--通过hierachyId得到数据-->
    <select id="selectByHierarchyId" parameterType="java.lang.String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from performance_company
        where hierarchy_id = #{hierarchy_id}
    </select>

    <!--得到所有的-->
    <select id="selectValidCompany" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from performance_company
        where is_delete = 0 and hierarchy_name NOT LIKE '%测试%'
    </select>

    <!--通过城市公司ID获得类型值-->
    <select id="getTypeByHierarchyId" parameterType="String" resultType="String">
        select type from performance_company where hierarchy_id = #{hierarchyId} and is_delete = 0
    </select>


</mapper>