<?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.settlement.mapper.SltAgreementReduceMapper">
    <resultMap type="com.bonus.material.settlement.domain.SltAgreementReduce" id="SltAgreementReudceResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="creator"    column="creator"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="status"    column="status"    />
        <result property="companyId"    column="company_id"    />
        <result property="cost"    column="cost"    />
        <result property="remark"    column="remark"    />
        <result property="typeId"    column="typeId"    />
        <result property="maName"    column="maName"    />
        <result property="maModel"    column="maModel"    />
        <result property="num"    column="num"    />
        <result property="leasePrice"    column="leasePrice"    />
        <result property="startTime"    column="startTime"    />
        <result property="endTime"    column="endTime"    />
        <result property="days"    column="days"    />
        <result property="leaseMoney"    column="leaseMoney"    />
        <result property="reduceStartTime"    column="reduceStartTime"    />
        <result property="reduceEndTime"    column="reduceEndTime"    />
    </resultMap>

    <sql id="selectSltAgreementReduceVo">
        select id, code, creator, create_time, update_time, status, company_id, cost, remark from slt_reduce_apply
    </sql>

    <delete id="deleteReduceDetail">
        delete
        from slt_reduce_details
        where apply_id = #{id}

    </delete>

    <select id="selectSltAgreementReduceList" parameterType="com.bonus.material.settlement.domain.SltAgreementReduce" resultMap="SltAgreementReudceResult">
        <include refid="selectSltAgreementReduceVo"/>
        <where>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="creator != null  and creator != ''"> and creator = #{creator}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="cost != null "> and cost = #{cost}</if>
        </where>
    </select>



    <select id="getApplyReduceList" resultMap="SltAgreementReudceResult">


        SELECT
        rs.id,
        rs.typeId,
        rs.agreementId,
        rs.maName,
        rs.maModel,
        rs.maUnit,
        SUM(rs.num) as num,
        rs.leasePrice,
        LEFT(rs.startTime,10) startTime,
        LEFT(rs.endTime,10) endTime,
        DATEDIFF(rs.endTime,rs.startTime)+1 as days ,
        ROUND(SUM(rs.num)*rs.leasePrice*(DATEDIFF(rs.endTime,rs.startTime)+1) ,2)AS leaseMoney
        FROM
        (

        SELECT
        sai.id,
        sai.agreement_id as agreementId,
        sai.type_id as typeId,
        sai.ma_id,
        mt2.type_name as maName,
        mt.type_name as maModel,
        mt.unit_name as maUnit,
        sai.num,
        sai.start_time as startTime,
        IFNULL(sai.end_time,NOW()) as endTime,
        DATEDIFF(IFNULL(sai.end_time,NOW()),sai.start_time)+1 as days,
        sai.`status`,
        sai.lease_price as leasePrice
        FROM
        slt_agreement_info sai
        left join ma_type mt on sai.type_id = mt.type_id
        LEFT JOIN ma_type mt2 on mt.parent_id = mt2.type_id
        where
        sai.agreement_id = #{agreementId}
        <if test="startTime != null">
            AND(
                DATE_FORMAT(start_time,'%Y-%m-%d') BETWEEN DATE_FORMAT(#{startTime},'%Y-%m-%d') AND DATE_FORMAT(#{endTime},'%Y-%m-%d') OR
                DATE_FORMAT(start_time,'%Y-%m-%d') BETWEEN DATE_FORMAT(#{startTime},'%Y-%m-%d') AND DATE_FORMAT(#{endTime},'%Y-%m-%d')
            )
        </if>

        <if test="typeId != null">
            and sai.type_id = #{typeId}

        </if>
        <if test="maName != null and maName!=''">
            AND INSTR(mt2.type_name,#{maName}) >0
        </if>
        <if test="typeName != null and typeName!=''">
            AND INSTR(mt.type_name,#{typeName}) >0
        </if>
        ) rs
        GROUP BY rs.typeId,rs.startTime
        ORDER BY rs.maName


    </select>

    <select id="getMaxCode" resultType="java.lang.String">
        SELECT
            MAX( CODE ) AS maxCode
        FROM
        slt_reduce_apply
        WHERE
        code LIKE CONCAT('%', #{date},'%')
    </select>

    <insert id="insertReduce"  useGeneratedKeys="true" keyProperty="id">
        insert into slt_reduce_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="creator != null">creator,</if>
            <if test="createTime != null">create_time,</if>
            <if test="agreementId != null">agreement_id,</if>
            <if test="status != null">status,</if>
            <if test="companyId != null">company_id,</if>
            <if test="cost != null">cost,</if>
            <if test="remark != null">remark,</if>
            <if test="reduceStartTime != null">r_start_time,</if>
            <if test="reduceEndTime != null">r_end_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="creator != null">#{creator},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="agreementId != null">#{agreementId},</if>
            <if test="status != null">#{status},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="cost != null">#{cost},</if>
            <if test="remark != null">#{remark},</if>
            <if test="reduceStartTime != null">#{reduceStartTime},</if>
            <if test="reduceEndTime != null">#{reduceEndTime},</if>
        </trim>

    </insert>

    <update id="updateReduce">
        update slt_reduce_apply
        set
        <if test="code != null">code = #{code},</if>
        <if test="agreementId != null">agreement_id=#{agreementId},</if>
        <if test="status != null">status=#{status},</if>
        <if test="companyId != null">company_id=#{companyId},</if>
        <if test="remark != null">remark=#{remark},</if>
        <if test="reduceStartTime != null">r_start_time=#{reduceStartTime},</if>
        <if test="reduceEndTime != null">r_end_time=#{reduceEndTime},</if>
        update_time = now()
        where id = #{id}
    </update>

    <insert id="insertReduceDetail">
        insert into slt_reduce_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">apply_id,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="typeId != null">type_id,</if>
            <if test="num != null">num,</if>
            <if test="reduceNum != null">reduce_num,</if>
            <if test="reduceStartTime != null">r_start_time,</if>
            <if test="reduceEndTime != null">r_end_time,</if>
            <if test="reduceLeasePrice != null">lease_price,</if>
            <if test="reduceLeaseMoney != null">lease_money,</if>
            <if test="reduceDays != null">days,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{id},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="num != null">#{num},</if>
            <if test="reduceNum != null">#{reduceNum},</if>
            <if test="reduceStartTime != null">#{reduceStartTime},</if>
            <if test="reduceEndTime != null">#{reduceEndTime},</if>
            <if test="reduceLeasePrice != null">#{reduceLeasePrice},</if>
            <if test="reduceLeaseMoney != null">#{reduceLeaseMoney},</if>
            <if test="reduceDays != null">#{reduceDays},</if>
        </trim>
    </insert>

        <insert id="insertBatchReduceDetail" parameterType="java.util.List">
            INSERT INTO slt_reduce_details
            (apply_id, start_time, end_time, type_id, num, reduce_num, r_start_time, r_end_time, lease_price, lease_money, days)
            VALUES
            <foreach collection="detailList" item="item" index="index" separator=",">
                (
                #{item.id},
                #{item.startTime},
                #{item.endTime},
                #{item.typeId},
                #{item.num},
                #{item.reduceNum},
                #{item.reduceStartTime},
                #{item.reduceEndTime},
                #{item.leasePrice},
                #{item.reduceLeaseMoney},
                #{item.reduceDays}
                )
            </foreach>
        </insert>

    <select id="findOverlappingReductions" resultMap="SltAgreementReudceResult">

            SELECT
                sd.id,
                sd.r_start_time as reduceStartTime,
                sd.r_end_time as reduceEndTime
            FROM slt_reduce_details sd
                left join slt_reduce_apply sa on sd.apply_id = sa.id
            WHERE
                sa.agreement_id = #{agreementId}
                AND sd.type_id = #{typeId}
                AND  sd.r_start_time &lt;= #{reduceEndTime}
              AND sd.r_end_time &gt;= #{reduceStartTime}

    </select>

    <select id="selectByMatype" resultType="com.bonus.material.settlement.domain.SltAgreementReduce">
        SELECT
            sai.id,
            sai.agreement_id as agreementId,
            mt2.type_id as typeId,
            mt2.type_name as maName,
            mt.type_name as maModel,
            mt.unit_name as maUnit

        FROM
            slt_agreement_info sai
                left join ma_type mt on sai.type_id = mt.type_id
                LEFT JOIN ma_type mt2 on mt.parent_id = mt2.type_id
        where
            sai.agreement_id = #{agreementId}
          group by mt2.type_id
    </select>
    <select id="selectByMaModel" resultType="com.bonus.material.settlement.domain.SltAgreementReduce">
        SELECT
            sai.id,
            sai.agreement_id as agreementId,
            sai.type_id as typeId,
            mt2.type_name as maName,
            mt.type_name as maModel,
            mt.unit_name as maUnit

        FROM
            slt_agreement_info sai
                left join ma_type mt on sai.type_id = mt.type_id
                LEFT JOIN ma_type mt2 on mt.parent_id = mt2.type_id
        where
            sai.agreement_id = #{agreementId} and mt2.type_id = #{typeId}
        group by mt.type_id
    </select>

    <select id="getReductionList" resultType="com.bonus.material.settlement.domain.SltAgreementReduce">
        SELECT mt2.type_name   as typeName,
               mt.type_name   as modeName,
               mt.type_name    as modelName,
               mt.unit_name    as unitName,
               srd.lease_price as leasePrice,
               srd.reduce_num  as reduceNum,
               srd.start_time  as startTime,
               srd.end_time    as endTime,
               srd.days        as days,
               srd.lease_money as leaseMoney,
               sra.remark
        FROM slt_reduce_apply sra
                 INNER JOIN slt_reduce_details srd on sra.id = srd.apply_id
                 LEFT JOIN ma_type mt on mt.type_id = srd.type_id
                 LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_id
        WHERE sra.agreement_id = #{agreementId}
    </select>

    <select id="selectAuditReduceList" resultType="com.bonus.material.settlement.domain.SltAgreementReduce">

        SELECT mt2.type_name   as maName,
               mt.type_name    as maModel,
               mt.unit_name    as maUnit,
               srd.lease_price as leasePrice,
               srd.reduce_num  as reduceNum,
               srd.start_time  as startTime,
               srd.end_time    as endTime,
               srd.days        as days,
               srd.lease_money as leaseMoney,
               sra.remark
        FROM slt_reduce_apply sra
                 LEFT JOIN slt_reduce_details srd on sra.id = srd.apply_id
                 LEFT JOIN ma_type mt on mt.type_id = srd.type_id
                 LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_id
        WHERE sra.agreement_id = #{agreementId}
    </select>

    <select id="getReductionListBatch" resultType="com.bonus.material.settlement.domain.SltAgreementReduce">
        SELECT mt2.type_name   as maName,
               mt.type_name    as maModel,
               mt.unit_name    as maUnit,
               srd.lease_price as leasePrice,
               srd.reduce_num  as reduceNum,
               srd.start_time  as startTime,
               srd.end_time    as endTime,
               srd.days        as days,
               srd.lease_money as leaseMoney,
               sra.remark
        FROM slt_reduce_apply sra
                 LEFT JOIN slt_reduce_details srd on sra.id = srd.apply_id
                 LEFT JOIN ma_type mt on mt.type_id = srd.type_id
                 LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_id
        <where>
            <if test="agreementIds != null and agreementIds.size > 0">
                sra.agreement_id in
                <foreach item="item" index="index" collection="agreementIds" open="(" separator="," close=")">
                        #{item}
                </foreach>
            </if>
        </where>
    </select>

</mapper>
