<?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.bii.BiIncomeMonthMapper">
    <resultMap id="BaseColumn" type="cn.com.countrygarden.bi.main.model.bii.BiIncomeMonth">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="delete_flg" property="deleteFlg" jdbcType="VARCHAR"/>
        <result column="created_date" property="createdDate" jdbcType="TIMESTAMP"/>
        <result column="created_user_id" property="createdUserId" jdbcType="VARCHAR"/>
        <result column="update_date" property="updateDate" jdbcType="TIMESTAMP"/>
        <result column="update_user_id" property="updateUserId" jdbcType="VARCHAR"/>
        <result column="year" property="year" jdbcType="VARCHAR"/>
        <result column="month" property="month" jdbcType="VARCHAR"/>
        <result column="corp_cost_id" property="corpCostId" jdbcType="VARCHAR"/>
        <result column="com_corp_cost_id" property="comCorpCostId" jdbcType="VARCHAR"/>
        <result column="cost_name" property="costName" jdbcType="VARCHAR"/>
        <result column="paid_amount" property="paidAmount" jdbcType="DECIMAL"/>
        <result column="qoq" property="qoq" jdbcType="DECIMAL"/>
    </resultMap>

    <insert id="insertToMonthIncomeJuZheLe" parameterType="cn.com.countrygarden.bi.main.model.bii.BiTargetYear">
        INSERT INTO bi_income_month VALUES
            (#{bim.id},#{bim.deleteFlg},#{bim.createdDate},#{bim.createdUserId},#{bim.updateDate},#{bim.updateUserId},
            #{bim.year},#{bim.month},#{bim.corpCostId},#{bim.comCorpCostId},#{bim.costName},#{bim.paidAmount},#{bim.qoq})
 </insert>
    <insert id="insertToMonthIncomeHZJuZheLe" parameterType="java.lang.Integer">
        INSERT INTO bi_income_month(
			id,
			year,
			month,
			corp_cost_id,
			com_corp_cost_id,
			cost_name,
			paid_amount -- ,
			-- qoq
		)

SELECT	CASE WHEN  b.year IS NOT NULL THEN b.id ELSE NULL END as id,
			#{y},
			#{m},
			a.corp_cost_id,
			a.com_corp_cost_id,
			'汇总',
			a.paid_amount -- ,
			-- a.qoq
		FROM
(SELECT	corp_cost_id as com_corp_cost_id,corp_cost_id,sum(paid_amount) as paid_amount
			FROM 	bi_income_month
			WHERE year = #{y}
			AND month = #{m}
			AND corp_cost_id = 100000999
			AND com_corp_cost_id != corp_cost_id
			GROUP BY corp_cost_id) a
		LEFT JOIN bi_income_month b
		ON a.corp_cost_id = b.corp_cost_id
		AND a.com_corp_cost_id = b.com_corp_cost_id
		AND b.year = #{y}
		AND b.month = #{m}
ON DUPLICATE KEY UPDATE
		paid_amount = a.paid_amount,
		-- qoq = a.qoq,
		update_date = now();

    </insert>

    <update id="updateToMonthIncomeJuZheLe">
        UPDATE
        bi_income_month 
        SET
        paid_amount=#{bim.paidAmount},
        update_date=#{bim.updateDate},
        update_user_id = #{bim.updateUserId}
        where corp_cost_id=#{bim.corpCostId}  and year=#{bim.year} and month=#{bim.month}
 </update>
    <select id="getcall" useCache="false" statementType="CALLABLE">
    <![CDATA[

     {call sp_bi_data_clean()}

        ]]>
 </select>

    <select id="getToMonthIncomeJuZheLeone" resultMap="BaseColumn">
    SELECT
    t.*
    FROM
    bi_income_month t
    where
    t.corp_cost_id = #{bim.corpCostId} and t.year = #{bim.year} and t.month = #{bim.month} and t.corp_cost_id != t.com_corp_cost_id
</select>
</mapper>