<?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="com.bonus.sgzb.material.mapper.CalMonthlyMapper">

    <insert id="insertCalcRecord" parameterType="com.bonus.sgzb.material.domain.CalMonthlyBean" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO slt_project_month (month,create_time) VALUES (#{month},now())
    </insert>
    <insert id="insertProMonCosts" useGeneratedKeys="true" keyProperty="id">
        insert into project_month_costs(agreement_id,task_id,unit_id,project_id,month,cost_bearing_party,create_time)
        values
        (#{agreementId},#{taskId},#{unitId},#{projectId},#{month},#{costBearingParty},now())
    </insert>
    <insert id="insertProjectMonthDetail">
        insert into project_month_detail (type_id,ma_id,unit,start_time,end_time,slt_days,slt_costs,month_temporarily_costs,pro_month_cost_id,num,lease_price,create_time)
        values
        (#{typeId},#{maId},#{unit},#{startTime},#{endTime},#{sltDays},#{sltCosts},#{monthTemporarilyCosts},#{proMonthCostId},#{num},#{leasePrice},now())
    </insert>
    <update id="updateProMonCosts">
        update project_month_costs set costs = #{costs},update_time = now() where id = #{id}
    </update>

    <select id="getCalcRecords" parameterType="com.bonus.sgzb.material.domain.CalMonthlyBean" resultType="com.bonus.sgzb.material.domain.CalMonthlyBean">
        select id,month from slt_project_month
        where month = #{month}
    </select>

    <select id="getMonthCosts" resultType="com.bonus.sgzb.base.api.domain.SltAgreementInfo">
        SELECT
            GROUP_CONCAT(pmc.id) as ids,
            bui.unit_name AS unitName,
            bp.lot_name AS projectName,
            spm.month as month,
            pmc.agreement_id as agreementId,
            pmc.cost_bearing_party as costBearingParty,
            SUM(pmc.costs) as costs
        FROM
            project_month_costs pmc
            LEFT JOIN slt_project_month spm ON pmc.task_id = spm.id
            LEFT JOIN bm_agreement_info bai ON pmc.agreement_id = bai.agreement_id
            LEFT JOIN bm_project_lot bp ON bp.lot_id = bai.project_id
            LEFT JOIN bm_unit_info bui ON bui.unit_id = bai.unit_id
        WHERE 1=1
        <if test="endTime != null and endTime != ''">
           and spm.month = #{endTime}
        </if>
        AND pmc.agreement_id = #{agreementId}
        <if test="costBearingParty != null and costBearingParty != ''">
            and pmc.cost_bearing_party = #{costBearingParty}
        </if>
        GROUP BY spm.month,pmc.cost_bearing_party
    </select>
    <select id="getMonthDetails" resultType="com.bonus.sgzb.base.api.domain.SltAgreementInfo">
        SELECT
            mt1.type_name AS typeName,
            mt.type_name AS modelName,
            bui.unit_name AS unitName,
            bp.lot_name AS projectName,
            mt.unit_name AS nuitName,
            pmd.num as num,
            pmc.cost_bearing_party as costBearingParty,
            pmc.`month` as month,
            pmd.lease_price as leasePrice,
            DATE_FORMAT( pmd.start_time, '%Y-%m-%d' ) as startTime,
            DATE_FORMAT( pmd.end_time, '%Y-%m-%d' ) as endTime,
            pmd.slt_days as leaseDays,
            pmd.slt_costs as costs,
            pmd.month_temporarily_costs as realCosts
        FROM
            project_month_detail pmd
            LEFT JOIN project_month_costs pmc ON pmd.pro_month_cost_id = pmc.id
            LEFT JOIN bm_unit_info bui ON bui.unit_id = pmc.unit_id
            LEFT JOIN bm_project_lot bp ON bp.lot_id = pmc.project_id
            LEFT JOIN ma_type mt ON pmd.type_id = mt.type_id
            LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
            LEFT JOIN ma_machine mm ON mm.ma_id = pmd.ma_id
        WHERE
            pmc.id = #{id}
    </select>
    <select id="selectIdByProjectIdAndMonth" resultType="java.lang.String">
        select id from project_month_costs
        where project_id = #{projectId} and month = #{month} and cost_bearing_party = #{costBearingParty}
    </select>

    <delete id="deleteCalcRecord" parameterType="com.bonus.sgzb.material.domain.CalMonthlyBean">
        delete from slt_project_month
        where id = #{id}
    </delete>

    <delete id="deleteMonthlyDetail" parameterType="com.bonus.sgzb.material.domain.CalMonthlyBean">
        delete
        from project_month_detail
        where pro_month_cost_id in
        (select id from project_month_costs where task_id = #{id})
    </delete>

    <delete id="deleteMonthlyCosts" parameterType="com.bonus.sgzb.material.domain.CalMonthlyBean">
        delete from project_month_costs
        where task_id = #{id}
    </delete>
    <delete id="deleteCostByProjectIdAndMonth">
        delete from project_month_costs
        where project_id = #{projectId} and month = #{month} and cost_bearing_party = #{costBearingParty}
    </delete>
    <delete id="deleteDetailsByProMonthCostId">
        delete from project_month_detail
        where pro_month_cost_id = #{proMonthCostId}
    </delete>

</mapper>