<?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.AccProjectLeaseMoneyMapper">

    <insert id="insertCalcRecord" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO calc_project_month (MONTH, create_time) VALUES (#{month}, now())
    </insert>

    <select id="getLeaseMoneyDetail" resultType="com.bonus.material.push.domain.AccProjectLeaseMoneyBean">
        SELECT
            res.id,
            res.deviceName,
            res.deviceModel,
            res.typeId,
            sum(res.leaseNum) as leaseNum,
            res.startTime,
            IF(DATEDIFF(res.endTime, res.sltTime) > 0,res.sltTime,res.endTime) as endTime ,
            DATEDIFF(IF(DATEDIFF(res.endTime, res.sltTime) > 0,res.sltTime,res.endTime),res.startTime)+1 as leaseDay,
            res.leasePrice,
            sum(ROUND(res.leaseNum*res.leasePrice*(DATEDIFF(IF(DATEDIFF(res.endTime, res.sltTime) > 0,res.sltTime,res.endTime),res.startTime)+1),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,
                    IF(DATEDIFF(sai.start_time,#{startTime}) > 0,sai.start_time,#{startTime}) as startTime,
                    IF(DATEDIFF(	#{endTime} ,ifnull(tt.CREATE_TIME,#{endTime})) > 0,tt.CREATE_TIME,#{endTime})  as endTime,
                    sai.slt_time as sltTime,
                    IFNULL( ROUND( mt1.lease_price, 2 ), 0.00 ) AS leasePrice,
                    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 mt1.CONSUMABLE != 1

                ORDER BY sai.type_id

            )
                res
        group by res.typeId,
                 res.startTime,
                 res.endTime
        HAVING leaseDay > 0
    </select>

    <insert id="insertProjectMonthInfo" 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,
            1,
            1,
            #{item.buyPrice},
            #{item.money}
            )
        </foreach>
    </insert>
</mapper>
