<?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.app.mapper.LeaseOutDetailsMapper">

    <select id="selectListByParentId" resultType="com.bonus.sgzb.base.api.domain.LeaseOutDetails">
        SELECT
            lod.id,lod.parent_id,lod.type_id,lod.ma_id,lod.out_num,lod.out_type,lod.create_by,
            lod.create_time,lod.update_by,lod.update_time,lod.remark,lod.company_id
        FROM
            lease_out_details lod
        <where>
            <if test="_parameter != null and _parameter != '' ">
                lod.parent_id = #{parentId}
            </if>
        </where>
    </select>
    <select id="getTaskId" resultType="java.lang.String">
        select task_id
        from lease_apply_info
        where id = #{parentId}
    </select>
    <select id="getMachineStatus" resultType="java.lang.String">
        select ma_status
        from ma_machine
        where ma_id = #{maId}
    </select>

    <select id="leaseOutRecord" resultType="com.bonus.sgzb.app.domain.LeaseApplyDetails">
        SELECT
            lod.id,
            bui.unit_name as unitName,
            bpl.lot_name as proName,
            mt2.type_name as typeName,
            mt.type_name as typeModelName,
            lod.create_time as createTime,
            lod.out_num as num
        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
            where 1=1
        <if test="keyWord != null  and keyWord != ''">
            and (bui.unit_name like concat('%', #{keyWord}, '%') or
            bpl.lot_name like concat('%', #{keyWord}, '%') or
            mt2.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%')
                )
        </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>
        ORDER BY lod.create_time DESC
    </select>

    <select id="proUseRecord" resultType="com.bonus.sgzb.app.domain.LeaseApplyDetails">
        SELECT
        t1.unitName,
        t1.lotName as proName,
        t1.typeName,
        t1.typeCode as typeModelName,
        t1.outNum as alNum,
        IFNULL(t1.outNum,0) - IFNULL(t2.backNum,0) as num
        FROM
        (SELECT
        bui.unit_name as unitName,
        bpl.lot_name as lotName,
        mt2.type_name as typeName,
        mt.type_name as typeCode,
        SUM(lod.out_num) as outNum,
        bai.unit_id,
        bai.project_id,
        lod.type_id
        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
        where 1=1
        <if test="keyWord != null  and keyWord != ''">
            and (bui.unit_name like concat('%', #{keyWord}, '%') or
            bpl.lot_name like concat('%', #{keyWord}, '%') or
            mt2.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY bai.unit_id,bai.project_id,lod.type_id) t1
        LEFT JOIN
        (SELECT
        bui.unit_name as unitName,
        bpl.lot_name as lotName,
        mt2.type_name as typeName,
        mt.type_name as typeCode,
        SUM(bcd.back_num) as backNum,
        bai.unit_id,
        bai.project_id,
        bcd.type_id
        FROM
        back_check_details bcd
        LEFT JOIN back_apply_info bapi on bapi.id=bcd.parent_id
        LEFT JOIN tm_task_agreement tta on tta.task_id=bapi.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
        where 1=1
        <if test="keyWord != null  and keyWord != ''">
            and (bui.unit_name like concat('%', #{keyWord}, '%') or
            bpl.lot_name like concat('%', #{keyWord}, '%') or
            mt2.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY bai.unit_id,bai.project_id,bcd.type_id) t2
        ON
        t1.unit_id = t2.unit_id
        AND t1.project_id = t2.project_id
        AND t1.type_id = t2.type_id

    </select>
    <select id="getAgreementId" resultType="java.lang.String">
        select agreement_id
        from tm_task_agreement
        where task_id = #{taskId}
    </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>
    <select id="getProtocol" resultType="java.lang.String">
        select protocol
        from bm_agreement_info
        where agreement_id = #{agreementId}
    </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
            status = '0'
          AND
            DATE(start_time) = CURDATE();
    </select>
    <select id="selectByTypeId" resultType="com.bonus.sgzb.base.api.domain.MaType">
        select * from ma_type WHERE
        type_id = #{record.typeId}
    </select>
    <select id="getCountOfCodeMachine" resultType="java.lang.Integer">
        select count(mm.ma_id)
        FROM ma_type mt
            left join ma_machine mm on mm.type_id=mt.type_id
        WHERE
            mm.type_id = #{record.typeId}
              and mm.ma_code is not null
              and mm.ma_status in (15)
              and mt.`level` = 4
              and mt.del_flag = '0'
    </select>

    <update id="updateLeaseApplyDetailsOutNum">
        UPDATE
            lease_apply_details
        SET
            al_num = IF(al_num IS NULL, #{record.outNum}, al_num + #{record.outNum}),
            <if test="record.updateBy != null and record.updateBy!= '' ">
                update_by = #{record.updateBy},
            </if>
            update_time = now(),
            status = '1'
        WHERE
            parennt_id = #{record.parentId} and type_id = #{record.typeId}
    </update>

    <insert id="insert">
       insert into lease_out_details(parent_id,type_id,ma_id,out_num,out_type,create_by,create_time,update_by,update_time,remark,company_id)
       values(#{parentId},#{typeId},#{maId},#{outNum},#{outType},#{createBy},now(),#{updateBy},now(),#{remark},#{companyId})
    </insert>

    <insert id="insertSelective">
       insert into lease_out_details
       <trim prefix="(" suffix=")" suffixOverrides=",">
           <if test="parentId!= null">
               parent_id,
           </if>
           <if test="typeId!= null">
               type_id,
           </if>
           <if test="maId!= null">
               ma_id,
           </if>
           <if test="outNum!= null">
               out_num,
           </if>
           <if test="outType!= null">
               out_type,
           </if>
           <if test="createBy!= null">
               create_by,
           </if>
           <if test="updateBy!= null">
               update_by,
           </if>
           <if test="remark!= null">
               remark,
           </if>
           <if test="companyId!= null">
               company_id,
           </if>
           <if test="carCode!= null">
               car_code,
           </if>
           create_time,
           update_time
       </trim>
       <trim prefix="values (" suffix=")" suffixOverrides=",">
           <if test="parentId!= null">
               #{parentId},
           </if>
           <if test="typeId!= null">
               #{typeId},
           </if>
           <if test="maId!= null">
               #{maId},
           </if>
           <if test="outNum!= null">
               #{outNum},
           </if>
           <if test="outType!= null">
               #{outType},
           </if>
           <if test="createBy!= null">
               #{createBy},
           </if>
           <if test="updateBy!= null">
               #{updateBy},
           </if>
           <if test="remark!= null">
               #{remark},
           </if>
           <if test="companyId!= null">
               #{companyId},
           </if>
           <if test="carCode!= null">
               #{carCode},
           </if>
           NOW(),
           NOW()
       </trim>
    </insert>

    <update id="updateMaTypeStockNum">
        UPDATE
            ma_type
        SET
            num = num - #{record.outNum} ,update_time = NOW()
        WHERE
            type_id = #{record.typeId}
    </update>

    <update id="updateMaMachineStatus">
        UPDATE
            ma_machine
        SET
            ma_status = '16',create_time = NOW()
        <where>
            type_id = #{record.typeId}
            <if test="record.maId != null and record.maId != ''">
                and ma_id = #{record.maId}
            </if>
        </where>
    </update>
    <update id="updateTaskStatus">
        update tm_task
        set task_status = #{status},
            update_time = NOW()
        where task_id = #{taskId}
    </update>
    <update id="updSltInfo">
        update slt_agreement_info
        set num = #{num},
            update_time = now(),
            ck_remark = #{ckRemark}
        where id = #{id}
    </update>
    <update id="updateMaTypeStockNumCt">
        UPDATE
            ma_type
        SET
            num = #{num} ,update_time = NOW()
        WHERE
            type_id = #{typeId}
    </update>
    <update id="updateMaTypeNum">
        UPDATE
            ma_type
        SET
            num = #{num} ,update_time = NOW()
        WHERE
            type_id = #{typeId}
    </update>
    <update id="updateLeaseApplyDetails">
        UPDATE
            lease_apply_details
        SET
            status = '2'
        WHERE
            parennt_id = #{record.parentId} and type_id = #{record.typeId}
    </update>

    <insert id="insertAgreementInfo">
        insert into tm_task_agreement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="record.parentId != null">
                task_id,
            </if>
            <if test="record.agreementId!= null">
                agreement_id,
            </if>
            <if test="record.createBy!= null">
                create_by,
            </if>
            <if test="record.updateBy!= null">
                update_by,
            </if>
            <if test="record.remark!= null">
                remark,
            </if>
            <if test="record.companyId!= null">
                company_id,
            </if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="record.parentId != null">
                    #{record.parentId},
            </if>
            <if test="record.agreementId != null">
                #{record.typeId},
            </if>
            <if test="record.createBy != null">
                #{record.createBy},
            </if>
            <if test="record.updateBy != null">
                #{record.updateBy},
            </if>
            <if test="record.remark != null">
                #{record.remark},
            </if>
            <if test="record.companyId != null">
                #{record.companyId},
            </if>
            now()
        </trim>
    </insert>
    <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,lease_type,create_time,ck_remark)
        values (#{agreementId},#{record.typeId},#{record.maId},#{record.outNum},now(),0,#{record.parentId},#{ma.finalPrice},#{ma.buyPrice},'0',#{record.companyId},#{record.leaseType},now(),#{record.remark});
    </insert>

    <select id="leaseOutRecordList" resultType="com.bonus.sgzb.base.api.domain.LeaseOutDetails">
        SELECT
            tt.task_id,
            tt.task_status,
            tt.`code`,
            tt.create_time,
            bui.unit_name AS unitName,
            bpl.lot_name AS proName,
            COALESCE(SUM(lad.pre_num), 0) AS preNum,
            COALESCE(SUM(lad.al_num), 0) AS outNum
        FROM
            tm_task tt
                LEFT JOIN tm_task_agreement tta ON tta.task_id = tt.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 lease_apply_info lai ON lai.task_id = tt.task_id
                LEFT JOIN lease_apply_details lad ON lad.parennt_id = lai.id AND lad.pre_num IS NOT NULL
        WHERE
            tt.task_type = '29'
            AND tt.`status` = '1'
            AND tt.task_status IN ('33', '34', '35')
            <if test="code != null and code != ''">
                AND tt.`code` like concat('%', #{code}, '%')
            </if>
            <if test="proName != null and proName != ''">
                AND bpl.lot_name like concat('%', #{proName}, '%')
            </if>
            <if test="unitName != null and unitName != ''">
                AND bui.unit_name like concat('%', #{unitName}, '%')
            </if>
        GROUP BY
            tt.task_id
    </select>
    <select id="getOutboundNum" resultType="com.bonus.sgzb.app.domain.LeaseApplyDetails">
        SELECT pre_num AS preNum,
               pre_num - IFNULL(al_num, 0) AS nums
        FROM lease_apply_details
        WHERE id = #{id} AND (pre_num - IFNULL(al_num, 0)) > 0
    </select>
    <select id="getMaTypeDetails" resultType="com.bonus.sgzb.app.domain.TmTask">
        SELECT
            mws2.type_id AS typeId,
            mws2.part_num AS partNum
        FROM
            ma_type mt1
                LEFT JOIN ma_whole_set mws2 ON mt1.type_id = mws2.parent_id
        WHERE
            mt1.type_id = #{typeId}
    </select>
    <select id="getMachineParts" resultType="com.bonus.sgzb.base.api.domain.MachinePart">
        SELECT
            mt.type_name AS typeModelName,
            mt.unit_name as unitName,
            ifnull(mt.num , 0) as num,
            m2.type_name As typeName
        FROM
            ma_type mt
                LEFT JOIN ma_type m2 ON mt.parent_id = m2.type_id
        WHERE
            mt.type_id = #{typeId}
    </select>
    <select id="getmaChineByCt" resultType="java.lang.Integer">
        SELECT
            CASE mt.manage_type
                WHEN 0 THEN
                    IFNULL(subquery0.num, 0)
                ELSE
                    IFNULL(mt.num, 0)
                END as num
        FROM ma_type mt
                 left join (SELECT mt.type_id,
                                   mt2.type_name AS             typeName,
                                   mt.type_name  AS             typeModelName,
                                   count(mm.ma_id) num
                            FROM ma_machine mm
                                     LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
                                     LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                            WHERE mm.ma_code is not null and mm.ma_status in (15)
                            GROUP BY mt.type_id) AS subquery0 ON subquery0.type_id = mt.type_id
        WHERE mt.del_flag = '0' and mt.type_id = #{typeId}
    </select>
    <select id="getLeaseApplyDetails" resultType="com.bonus.sgzb.app.domain.LeaseApplyDetails">
        SELECT
            ifnull( pre_num, 0 ) AS preNum,
            ifnull( audit_num, 0 ) AS auditNum,
            ifnull( al_num, 0 ) AS alNum
        FROM
            lease_apply_details
        WHERE
            parennt_id = #{record.parentId}
          AND type_id = #{record.typeId}
    </select>
</mapper>