<?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.BackRecordMapper">
    <insert id="insertCheckDetails">
        insert into back_check_details
        (
        <if test="parentId != null">
            parent_id,
        </if>
        <if test="typeId != null">
            type_id,
        </if>
        <if test="maId != null">
            ma_id,
        </if>
        <if test="backNum != null">
            back_num,
        </if>
        <if test="backStatus != null">
            back_status,
        </if>
        <if test="createBy != null and createBy != ''">
            create_by,
        </if>
        <if test="createBy != null and createBy != ''">
            update_by,
        </if>
        update_time,
        <if test="remark != null and remark != ''">
            remark,
        </if>
        <if test="companyId != null">
            company_id,
        </if>
        create_time
        )
        values (
        <if test="parentId != null">
            #{parentId},
        </if>
        <if test="typeId != null">
            #{typeId},
        </if>
        <if test="maId != null">
            #{maId},
        </if>
        <if test="backNum != null">
            #{backNum},
        </if>
        <if test="backStatus != null">
            #{backStatus},
        </if>
        <if test="createBy != null and createBy != ''">
            #{createBy},
        </if>
        <if test="createBy != null and createBy != ''">
            #{createBy},
        </if>
        NOW(),
        <if test="remark != null and remark != ''">
            #{remark},
        </if>
        <if test="companyId != null">
            #{companyId},
        </if>
        NOW()
        )
    </insert>
    <insert id="insStlInfoTwo">
        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 (#{info.agreementId},#{info.typeId},#{info.maId},#{many},#{info.startTime},#{info.status},#{info.leaseId},#{info.leasePrice},#{info.buyPrice},'0',#{info.companyId});
    </insert>
    <update id="updateMaStatus">
        update ma_machine
        set ma_status=#{maStatus}
        where ma_id = #{maId}
    </update>
    <update id="updateStlInfo">
        update slt_agreement_info
        set end_time = now(),
            back_id = #{record.parentId},
            status = '1'
        where id = #{info.id}
    </update>
    <update id="updateStlInfoTwo">
        update slt_agreement_info
        set num = #{backNum},
            end_time = now(),
            back_id = #{record.parentId},
            status = '1'
        where id = #{info.id}
    </update>

    <select id="getBackRecordList" resultType="com.bonus.sgzb.material.domain.BackRecord">
        SELECT
            mt2.type_name AS typeName,
            mt.type_name AS typeModelName,
            baif.`code`,
            mm.ma_code AS maCode,
            bcd.back_num AS backNum,
            baif.back_person AS backPerson,
            bcd.create_time AS createTime,
            bpl.lot_name AS proName,
            bui.unit_name AS unitName
        FROM
            back_check_details bcd
                LEFT JOIN back_apply_info baif ON baif.id = bcd.parent_id
                LEFT JOIN tm_task_agreement tta ON tta.task_id = baif.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 = bcd.type_id
                LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                LEFT JOIN ma_machine mm ON mm.ma_id = bcd.ma_id
--                 LEFT JOIN sys_user su ON su.user_id = baif.back_person
        where 1 = 1
        <if test="keyWord != null and keyWord != ''">
            and (baif.`code` like concat('%',#{keyWord},'%') or
            mm.ma_code 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 bcd.create_time BETWEEN CONCAT(#{startTime}, ' 00:00:00') AND CONCAT(#{endTime}, ' 23:59:59')
        </if>
        <if test="agreementCode != null and agreementCode != ''">
            and bai.agreement_code like concat('%',#{agreementCode},'%')
        </if>
    </select>
    <select id="getAllList" resultType="com.bonus.sgzb.base.api.domain.BackApplyInfo">
        SELECT
            tta.agreement_id AS agreementId,
            bai.id,
            bai.task_id AS taskId,
            bcd.type_id AS typeId,
            SUM(bcd.back_num) AS backNum,
            bcd.parent_id AS parentId,
            bcd.create_by AS createBy,
            bcd.ma_id AS maId,
            bai.company_id AS companyId
        FROM
            back_check_details bcd
                LEFT JOIN back_apply_info bai ON bai.id = bcd.parent_id
                LEFT JOIN tm_task_agreement tta ON tta.task_id = bai.task_id
        WHERE
            parent_id = #{parentId}
        GROUP BY bcd.type_id,bcd.ma_id
    </select>
    <select id="getStlInfo" 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
        agreement_id = #{agreementId}
        AND
        type_id = #{typeId}
        <if test="maId != null">
            AND ma_id = #{maId}
        </if>
        order by start_time asc
    </select>
</mapper>