<?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,slt_month_id,unit_id,project_id,month,cost_bearing_party)
        values
        (#{agreementId},#{sltMonthId},#{unitId},#{projectId},#{month},#{costBearingParty})
    </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)
        values
        (#{typeId},#{maId},#{unit},#{startTime},#{endTime},#{sltDays},#{sltCosts},#{monthTemporarilyCosts},#{proMonthCostId},#{num},#{leasePrice})
    </insert>
    <update id="updateProMonCosts">
        update project_month_costs set costs = #{costs} 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 calc_project_month
        where month = #{month}
    </select>

    <select id="getMonthCosts" resultType="com.bonus.sgzb.base.api.domain.SltAgreementInfo">
        SELECT
            pmc.id as id,
            bui.unit_name AS unitName,
            bp.lot_name AS projectName,
            spm.month as month,
            pmc.cost_bearing_party as costBearingParty,
            pmc.costs as costs
        FROM
            project_month_costs pmc
            LEFT JOIN slt_project_month spm ON pmc.slt_month_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
            spm.month = DATE_FORMAT(#{endTime}, '%Y-%m')
    </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>

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

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

</mapper>