<?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.material.push.mapper.ConsProjectLeaseMoneyMapper">
    <select id="getRealtionList"  resultType="com.bonus.material.push.domain.ConsProjectLeaseMoneyBean">
        select
            bai.agreement_id as agreementId
        from bm_agreement_info bai
                 left join bm_unit bu on bai.unit_id = bu.unit_id
                 left join bm_project bp on bai.project_id = bp.pro_id
    </select>

    <select id="getConsMonthMoney" resultType="com.bonus.material.push.domain.ConsProjectLeaseMoneyBean">
        SELECT
            res.id,
            res.deviceName,
            res.deviceModel,
            res.typeId,
            sum(res.leaseNum) as leaseNum,
            res.startTime,
            res.buyPrice as leasePrice,
            res.code,
            sum(ROUND(res.leaseNum*res.buyPrice*res.type,2))  as leaseMoney,
            res.time
        FROM
            (
                SELECT
                    sai.id,
                    sai.type_id as typeId,
                    mt2.type_name as deviceName,
                    mt1.type_name as deviceModel,
                    sai.num AS leaseNum,
                    sai.start_time as startTime,
                    tt.CODE as code,
                    ROUND(IFNULL(mt1.NOTAX_PRICE,0) ,2) as buyPrice,
                    1 as type,
                    tt.CREATE_TIME as time
                FROM
                    slt_agreement_info sai
                    LEFT JOIN ma_type mt1 ON sai.type_id = mt1.type_id
                    LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id
                    LEFT JOIN tm_task tt on sai.back_id = tt.task_id
                WHERE sai.agreement_id = #{agreementId} AND sai.start_time BETWEEN #{startTime} AND #{endTime} AND mt1.CONSUMABLE = 1 AND (ISNULL(sai.end_time) OR LEFT(sai.start_time,7) != LEFT(sai.end_time,7))

                UNION

                SELECT
                    sai.ID,
                    sai.type_id as typeId,
                    mt2.type_name as deviceName,
                    mt1.type_name as deviceModel,
                    sai.NUM AS leaseNum,
                    sai.start_time as startTime,
                    tt.CODE as code,
                    ROUND(IFNULL(mt1.NOTAX_PRICE,0) ,2) as buyPrice,
                    -1 as type,tt.CREATE_TIME as time
                FROM
                    slt_agreement_info sai
                    LEFT JOIN ma_type mt1 ON sai.type_id = mt1.type_id
                    LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id
                    LEFT JOIN tm_task tt on sai.back_id = tt.task_id
                WHERE
                    sai.agreement_id = #{agreementId} AND sai.status = 1
                  AND sai.end_time BETWEEN #{startTime} AND #{endTime} AND mt1.CONSUMABLE = 1 AND LEFT(sai.start_time,7) != LEFT(sai.end_time,7) AND sai.start_time > '2024-01-01'


            ) res
        group by res.typeId,
                 res.startTime,
                 res.code
    </select>

    <insert id="insertProjectConsMonthInfo" parameterType="java.util.List">
        insert into project_month_info (agreementId,typeId,leaseNum,leaseDate,returnDate,leasePrice,leaseMoney,
        taskId,push_status,push_time,push_remark,is_flag,type,jiju_type,buy_price,money)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.leaseName},
            #{item.typeId},
            #{item.leaseNum},
            #{item.startTime},
            #{item.returnDate},
            #{item.leasePrice},
            #{item.leaseMoney},
            #{item.taskId},
            #{item.pushStatus},
            #{item.pushTime},
            #{item.pushRemark},
            0,
            2,
            1,
            #{item.buyPrice},
            #{item.money}
            )
        </foreach>
    </insert>

    <select id="getScrapMoney" resultType="com.bonus.material.push.domain.ConsProjectLeaseMoneyBean">
        select
            sad.id as id,
            sad.apply_id as applyId,
            sad.type_id as typeId,
            sad.ma_id as maId,
            sad.num as leaseNum,
            sad.start_time as startTime,
            sad.end_time as endTime,
            sad.price as leasePrice,
            sad.money as leaseMoney
        from slt_agreement_details sad
                 left join slt_agreement_apply saa on sad.apply_id = saa.id
        where
            saa.agreement_id = #{agreementId}
          and sad.slt_type=4 and sad.is_charge = 1
    </select>

    <insert id="insertProjectOthersMonthInfo" parameterType="java.util.List">
        insert into project_month_info (agreementId,typeId,leaseNum,leaseDate,returnDate,leasePrice,leaseMoney,
        taskId,push_status,push_time,push_remark,is_flag,type,jiju_type,buy_price,money)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.leaseName},
            #{item.typeId},
            #{item.leaseNum},
            #{item.startTime},
            #{item.endTime},
            #{item.leasePrice},
            #{item.leaseMoney},
            #{item.taskId},
            #{item.pushStatus},
            #{item.pushTime},
            #{item.pushRemark},
            0,
            #{item.type},
            1,
            #{item.buyPrice},
            #{item.money}
            )
        </foreach>
    </insert>

    <select id="getLostMoney" resultType="com.bonus.material.push.domain.ConsProjectLeaseMoneyBean">
        select
            sad.id as id,
            sad.apply_id as applyId,
            sad.type_id as typeId,
            sad.ma_id as maId,
            sad.num as leaseNum,
            sad.start_time as startTime,
            sad.end_time as endTime,
            sad.price as leasePrice,
            sad.money as leaseMoney
        from slt_agreement_details sad
                 left join slt_agreement_apply saa on sad.apply_id = saa.id
        where
            saa.agreement_id = #{agreementId}
          and sad.slt_type=2 and sad.is_charge = 1
    </select>

    <select id="getRepairMoney" resultType="com.bonus.material.push.domain.ConsProjectLeaseMoneyBean">
        select
            sad.id as id,
            sad.apply_id as applyId,
            sad.type_id as typeId,
            sad.ma_id as maId,
            sad.num as leaseNum,
            sad.start_time as startTime,
            sad.end_time as endTime,
            sad.price as leasePrice,
            sad.money as leaseMoney
        from slt_agreement_details sad
                 left join slt_agreement_apply saa on sad.apply_id = saa.id
        where
            saa.agreement_id = #{agreementId}
          and sad.slt_type=3 and sad.is_charge = 1
    </select>

    <insert id="insertProjectMonthCosts" parameterType="java.util.List">
        insert into project_month_costs (agreement_id,lease_money,cons_money,scrap_money,
        lost_money,repair_money,task_id,is_flag,month,jiju_type)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.agreementId},
            #{item.leaseTotal},
            #{item.consTotal},
            #{item.scrapTotal},
            #{item.lostTotal},
            #{item.repairTotal},
            #{item.taskId},
            0,
            #{item.month},
            1
            )
        </foreach>
    </insert>

    <select id="getIwsProjectList" resultType="com.bonus.material.basic.domain.BmProject">
        select
        dfs.id as externalId,
        case dfs.project_status
            when '开工准备' then 0
            when '在建' then 1
            when '停工' then 2
            when '完工未竣工' then 3
            when '竣工' then 4
            else dfs.project_status
        end as proStatus,
        dfs.real_end_date as actualEndDate
        from data_center.dx_fb_son dfs
    </select>

    <update id="updateIwsProject">
        <foreach collection="list" item="item" separator=";">
            UPDATE bm_project
            SET
            pro_status = #{item.proStatus},
            actual_end_date = #{item.actualEndDate}
            WHERE external_id = #{item.externalId}
        </foreach>
    </update>
</mapper>
