<?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.LeaseRecordMapper">
    <insert id="insSltInfo">
        insert into slt_agreement_info (agreement_id,type_id,ma_id,num,start_time,status,lease_id,lease_price,buy_price,is_slt,company_id)
        values (#{agreementId},#{record.typeId},#{record.maId},#{record.outNum},now(),0,#{record.parentId},#{ma.finalPrice},#{ma.buyPrice},'0',#{record.companyId});
    </insert>
    <update id="updSltInfo">
        update slt_agreement_info
        set num = #{num}
        where id = #{id}
    </update>

    <select id="getLeaseRecordList" resultType="com.bonus.sgzb.material.domain.LeaseRecord">
        SELECT mt2.type_name      as typeName,
               mt.type_name       as typeModelName,
               bai.agreement_code as agreementCode,
               mm.ma_code         as maCode,
               mt.unit_name       as unit,
               lod.out_num        as preNum,
               su.nick_name       as leasePerson,
               lod.create_time    as createTime,
               bpl.lot_name       as proName,
               bui.unit_name      as unitName
        FROM lease_out_details lod
                 LEFT JOIN lease_apply_info lai on lai.id = lod.parent_id
                 LEFT JOIN tm_task_agreement tta on tta.task_id = lai.task_id
                 LEFT JOIN bm_agreement_info bai on bai.agreement_id = tta.agreement_id
                 LEFT JOIN bm_project_lot bpl on bpl.lot_id = bai.project_id
                 LEFT JOIN bm_unit_info bui on bui.unit_id = bai.unit_id
                 LEFT JOIN ma_type mt on mt.type_id = lod.type_id
                 LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_id
                 LEFT JOIN ma_machine mm on mm.ma_id = lod.ma_id
                 LEFT JOIN sys_user su on su.user_id = lai.lease_person
        where 1 = 1
        <if test="keyWord != null and keyWord != ''">
            and (bai.agreement_code like concat('%',#{keyWord},'%') or
            mm.ma_code like concat('%',#{keyWord},'%') or
            mt.unit_name like concat('%',#{keyWord},'%') or
            su.nick_name like concat('%',#{keyWord},'%'))
        </if>
        <if test="typeName != null and typeName != ''">
            and mt2.type_name like concat('%',#{typeName},'%')
        </if>
        <if test="typeModelName != null and typeModelName != ''">
            and mt.type_name like concat('%',#{typeModelName},'%')
        </if>
        <if test="unitId != null">
            and bui.unit_id = #{unitId}
        </if>
        <if test="proId != null">
            and bpl.lot_id = #{proId}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND lod.create_time BETWEEN CONCAT(#{startTime}, ' 00:00:00') AND CONCAT(#{endTime}, ' 23:59:59')
        </if>
    </select>
    <select id="getSltAgreementInfo" resultType="com.bonus.sgzb.base.api.domain.SltAgreementInfo">
        SELECT
            id,
            agreement_id AS agreementId,
            type_id AS typeId,
            ma_id AS maId,
            num AS num,
            start_time AS startTime,
            end_time AS endTime,
            status AS status,
            lease_id AS leaseId,
            back_id AS backId,
            lease_price AS leasePrice,
            buy_price AS buyPrice,
            company_id AS companyId
        FROM
            slt_agreement_info
        WHERE
            lease_id = #{parentId}
          AND
            type_id = #{typeId}
          AND
            ma_id IS NULL
          AND
            DATE(start_time) = CURDATE();
    </select>
    <select id="getAgreementId" resultType="java.lang.String">
        select agreement_id
        from tm_task_agreement
        where task_id = #{taskId}
    </select>
    <select id="getProtocol" resultType="java.lang.String">
        select protocol
        from bm_agreement_info
        where agreement_id = #{agreementId}
    </select>
    <select id="getMaType" resultType="com.bonus.sgzb.base.api.domain.MaType">
        select lease_price as leasePrice,
               rent_price as rentPrice,
               buy_price as buyPrice,
               pay_price as payPrice,
               company_id as companyId
        from ma_type
        where type_id = #{typeId} and del_flag = 0
    </select>

</mapper>