<?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.AssessmentMonthlyTargetMapper">
    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.performance.monthly.AssessmentMonthlyTarget">
        <constructor>
            <idArg column="amt_id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="element_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
            <arg column="lock_status" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="public_status" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="year" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="month" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <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>

    <sql id="Base_Column_List">
    amt_id, element_id, lock_status, public_status, year, month, created_date, created_by, 
    update_date, update_by
    </sql>

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

    <!--查询状态-->
    <select id="selectJudgeLock" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from assessment_monthly_target
        where element_id = #{elementId,jdbcType=BIGINT} and year = #{year,jdbcType=INTEGER} and month = #{month,jdbcType=INTEGER}
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from assessment_monthly_target
    where amt_id = #{amtId,jdbcType=INTEGER}
    </delete>

    <insert id="insertSelective"
            parameterType="cn.com.countrygarden.bi.main.model.performance.monthly.AssessmentMonthlyTarget">
        insert into assessment_monthly_target
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="amtId != null">
                amt_id,
            </if>
            <if test="elementId != null">
                element_id,
            </if>
            <if test="lockStatus != null">
                lock_status,
            </if>
            <if test="publicStatus != null">
                public_status,
            </if>
            <if test="year != null">
                year,
            </if>
            <if test="month != null">
                month,
            </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="amtId != null">
                #{amtId,jdbcType=INTEGER},
            </if>
            <if test="elementId != null">
                #{elementId,jdbcType=BIGINT},
            </if>
            <if test="lockStatus != null">
                #{lockStatus,jdbcType=INTEGER},
            </if>
            <if test="publicStatus != null">
                #{publicStatus,jdbcType=INTEGER},
            </if>
            <if test="year != null">
                #{year,jdbcType=INTEGER},
            </if>
            <if test="month != null">
                #{month,jdbcType=INTEGER},
            </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.monthly.AssessmentMonthlyTarget">
        update assessment_monthly_target
        <set>
            <if test="elementId != null">
                element_id = #{elementId,jdbcType=BIGINT},
            </if>
            <if test="lockStatus != null">
                lock_status = #{lockStatus,jdbcType=INTEGER},
            </if>
            <if test="publicStatus != null">
                public_status = #{publicStatus,jdbcType=INTEGER},
            </if>
            <if test="year != null">
                year = #{year,jdbcType=INTEGER},
            </if>
            <if test="month != null">
                month = #{month,jdbcType=INTEGER},
            </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 amt_id = #{amtId,jdbcType=INTEGER}
    </update>

    <!--查询月度未锁定区域-->
    <select id="selectUnLockOrganList" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from assessment_monthly_target
        where element_id in (select element_id from performance_app.performance_element where type != '城市公司' having CHAR_LENGTH(element_id)=4)
        and year = #{year,jdbcType=INTEGER} and month = #{month,jdbcType=INTEGER} and lock_status = 0
    </select>

    <!--查询月度未锁定城市公司-->
    <select id="selectUnLockCompanyList" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from assessment_monthly_target
        where element_id in (select element_id from performance_app.performance_element where type = '城市公司' having CHAR_LENGTH(element_id)=4)
        and year = #{year,jdbcType=INTEGER} and month = #{month,jdbcType=INTEGER} and lock_status = 0
    </select>

    <!--更新区域月度发布状态-->
    <update id="updateOrganMonthlyPublicStatus" parameterType="java.util.Map" >
        update assessment_monthly_target set public_status = 1
        where element_id in
        (select element_id  from performance_app.performance_element  where type !='城市公司' having CHAR_LENGTH(element_id)=4)
        and year=#{year,jdbcType=INTEGER} and month =#{month,jdbcType=INTEGER}
    </update>

    <!--更新城市公司月度发布状态-->
    <update id="updateCompanyMonthlyPublicStatus" parameterType="java.util.Map" >
        update assessment_monthly_target set public_status = 1
        where element_id in
        (select element_id  from performance_app.performance_element  where type ='城市公司' having CHAR_LENGTH(element_id)=4)
        and year=#{year,jdbcType=INTEGER} and month =#{month,jdbcType=INTEGER}
    </update>

    <!--查询区域最后一个月的发布状态-->
    <select id="selectMonthlyLastPublicOrgan" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from assessment_monthly_target
        where element_id in
        (select element_id from performance_app.performance_element where type !='城市公司' having CHAR_LENGTH(element_id)=4)
        and year=#{year,jdbcType=INTEGER} and month =#{month,jdbcType=INTEGER} and public_status=0
    </select>

    <!--查询城市公司最后一个月的发布状态-->
    <select id="selectMonthlyLastPublicCompany" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from assessment_monthly_target
        where element_id in
        (select element_id from performance_app.performance_element where type ='城市公司' having CHAR_LENGTH(element_id)=4)
        and year=#{year,jdbcType=INTEGER} and month =#{month,jdbcType=INTEGER} and public_status=0
    </select>
</mapper>