<?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.clz.mapper.MaterialBackInfoMapper">
    <insert id="insertBackApplyInfo" useGeneratedKeys="true" keyProperty="id">
        insert into clz_back_apply_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="taskId != null">task_id,</if>
            <if test="backPerson != null">back_person,</if>
            <if test="phone != null">phone,</if>
            <if test="directAuditBy != null">direct_audit_by,</if>
            <if test="directAuditTime != null">direct_audit_time,</if>
            <if test="directAuditRemark != null">direct_audit_remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
            <if test="backTime != null">back_time,</if>
            <if test="status != null">status,</if>
            <if test="directId != null">direct_id,</if>
            <if test="backStyle != null and backStyle != ''">back_style,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="backPerson != null">#{backPerson},</if>
            <if test="phone != null">#{phone},</if>
            <if test="directAuditBy != null">#{directAuditBy},</if>
            <if test="directAuditTime != null">#{directAuditTime},</if>
            <if test="directAuditRemark != null">#{directAuditRemark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="backTime != null">#{backTime},</if>
            <if test="status != null">#{status},</if>
            <if test="directId != null">#{directId},</if>
            <if test="backStyle != null and backStyle != ''">#{backStyle},</if>
        </trim>
    </insert>

    <insert id="insertBackApplyDetails" useGeneratedKeys="true" keyProperty="id">
        insert into clz_back_apply_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="preNum != null">pre_num,</if>
            <if test="auditNum != null">audit_num,</if>
            <if test="badNum != null">bad_num,</if>
            <if test="goodNum != null">good_num,</if>
            <if test="num != null">use_num,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="apDetection != null and apDetection != ''">ap_detection,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="preNum != null">#{preNum},</if>
            <if test="auditNum != null">#{auditNum},</if>
            <if test="badNum != null">#{badNum},</if>
            <if test="goodNum != null">#{goodNum},</if>
            <if test="num != null">#{num},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="apDetection != null and apDetection != ''">#{apDetection},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>

    <insert id="insertCheckDetails">
        insert into clz_back_check_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="preNum != null">back_num,</if>
            <if test="maId != null">ma_id,</if>
            <if test="status != null">status,</if>
            <if test="backStatus != null">back_status,</if>
            <if test="isFinished != null">is_finished,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
            <if test="apDetection != null and apDetection != ''">ap_detection,</if>
            <if test="badNum != null">bad_num,</if>
            <if test="goodNum != null">good_num,</if>
            <if test="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="preNum != null">#{preNum},</if>
            <if test="maId != null">#{maId},</if>
            <if test="status != null">#{status},</if>
            <if test="backStatus != null">#{backStatus},</if>
            <if test="isFinished != null">#{isFinished},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="apDetection != null and apDetection != ''">#{apDetection},</if>
            <if test="badNum != null">#{badNum},</if>
            <if test="goodNum != null">#{goodNum},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>

    <update id="updateBack">
        update clz_back_apply_info set status = #{taskStatus} where id = #{id}
    </update>

    <update id="updateBackDetails">
        update clz_back_apply_details set status = #{taskStatus} where parent_id = #{id}
    </update>

    <update id="updateStlInfo">
        update clz_slt_agreement_info
        set end_time = now(),
            update_time = now(),
            back_id = #{record.id},
            status = '1'
        where id = #{info.id}
    </update>

    <update id="updateStlInfoTwo">
        update clz_slt_agreement_info
        set num = #{backNum},
            end_time = now(),
            update_time = now(),
            back_id = #{record.id},
            status = '1'
        where id = #{info.id}
    </update>

    <update id="updateBackApplyInfo">
        update clz_back_apply_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="backPerson != null">back_person = #{backPerson},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="directAuditBy != null">direct_audit_by = #{directAuditBy},</if>
            <if test="directAuditTime != null">direct_audit_time = #{directAuditTime},</if>
            <if test="directAuditRemark != null">direct_audit_remark = #{directAuditRemark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="backTime != null">back_time = #{backTime},</if>
            <if test="status != null">`status` = #{status},</if>
            <if test="directId != null">direct_id = #{directId},</if>
            <if test="printStatus != null">print_status = #{printStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateSignById">
        update clz_back_apply_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="backSignUrl != null">back_sign_url = #{backSignUrl},</if>
            <if test="backSignType != null">back_sign_type = #{backSignType},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateCheckDetails">
        update back_check_details set is_finished = '1', update_time = now()
        where parent_id = #{id} and type_id = #{typeId} and (is_finished is null or is_finished != 1)
        <if test="maId != null">
            and ma_id = #{maId}
        </if>
    </update>

    <delete id="deleteBackApplyDetails">
        delete from clz_back_apply_details where parent_id = #{id}
    </delete>

    <delete id="deleteCheckDetails">
        delete from clz_back_check_details where parent_id = #{id}
    </delete>

    <delete id="deleteBackApply">
        delete from clz_back_apply_info where id = #{id}
    </delete>

    <insert id="insStlInfoTwo">
        insert into clz_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)
        values (#{info.agreementId},#{info.typeId},#{info.maId},#{many},#{info.startTime},#{info.status},#{info.leaseId},#{info.leasePrice},#{info.buyPrice},'0',#{info.companyId},#{info.leaseType},now());
    </insert>

    <select id="selectBackApplyInfoList" resultType="com.bonus.material.clz.domain.back.MaterialBackApplyInfo">
        SELECT
        bai.id as id,
        bai.task_id as taskId,
        bai.`code` as code,
        bai.back_person as backPerson,
        bai.phone as phone,
        bp.pro_id as proId,
        bai.remark as remark,
        bp.pro_name as proName,
        bt.unit_id as teamId,
        bt.unit_name as teamName,
        bai.back_time as backTime,
        tt.task_status as taskStatus,
        bai.create_by as createBy,
        bai.create_time as createTime,
        bai.back_sign_url as backSignUrl,
        bai.back_sign_type as backSignType,
        GROUP_CONCAT(DISTINCT mt2.type_id) as typeId,
        GROUP_CONCAT(DISTINCT mt2.type_name) AS typeName,
        bai.`status` AS status,
        bai.print_status as printStatus,
        SUM(IFNULL(bad.pre_num, 0)) AS backNum,
        bp.external_id AS externalId,
        bp.imp_unit AS impUnit,
        bt.bzz_idcard AS idCard
        FROM
        clz_back_apply_info bai
        LEFT JOIN clz_back_apply_details bad on bad.parent_id = bai.id
        LEFT JOIN tm_task tt on tt.task_id = bai.task_id
        LEFT JOIN tm_task_agreement tta on tta.task_id = tt.task_id
        LEFT JOIN clz_bm_agreement_info bagi on bagi.agreement_id = tta.agreement_id
        AND bagi.`status` = '1'
        LEFT JOIN bm_project bp on bp.pro_id = bagi.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bt on bt.unit_id = bagi.unit_id
        AND bt.del_flag = '0'
        LEFT JOIN ma_type mt1 ON mt1.type_id = bad.type_id AND mt1.del_flag = '0'
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt1.parent_id AND mt2.del_flag = '0'
        WHERE
        1=1
        <if test="teamId != null and teamId != ''">
            and bt.id = #{teamId}
        </if>
        <if test="proId != null and proId != ''">
            and bp.pro_id = #{proId}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( bai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        <if test="appTaskStatus != null and appTaskStatus == 0">
            and tt.task_status = #{appTaskStatus}
        </if>
        <if test="appTaskStatus != null and appTaskStatus == 1">
            and (tt.task_status = 1 or tt.task_status = 2)
        </if>
        <if test="status != null">
            and tt.task_status = #{status}
        </if>
        <if test="idCard != null and idCard != ''">
            and bt.bzz_idcard = #{idCard}
        </if>
        <if test="projectIdList != null  and projectIdList.size() > 0">
            and bp.external_id in
            <foreach item="item" collection="projectIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY bai.`code`
        ORDER BY bai.create_time desc
    </select>

    <select id="getTotalList" resultType="com.bonus.material.clz.domain.back.MaterialBackApplyInfo">
        SELECT
        bai.id as id,
        bai.task_id as taskId,
        bai.`code` as code,
        bai.back_person as backPerson,
        bai.phone as phone,
        bp.pro_id as proId,
        bai.remark as remark,
        bp.pro_name as proName,
        bu.unit_id as unitId,
        bu.unit_name as unitName,
        tt.task_status as taskStatus,
        bai.create_by as createBy,
        bai.create_time as backTime,
        bai.back_sign_url as backSignUrl,
        bai.back_sign_type as backSignType,
        GROUP_CONCAT(DISTINCT mt2.type_id) as typeId,
        GROUP_CONCAT(DISTINCT mt2.type_name) AS typeName,
        bai.`status` AS status,
        bai.print_status as printStatus,
        bp.external_id AS externalId,
        bp.imp_unit AS impUnit,
        bu.bzz_idcard AS idCard
        FROM
        back_apply_info bai
        LEFT JOIN back_apply_details bad on bad.parent_id = bai.id
        LEFT JOIN tm_task tt on tt.task_id = bai.task_id
        LEFT JOIN tm_task_agreement tta on tta.task_id = tt.task_id
        LEFT JOIN bm_agreement_info bagi on bagi.agreement_id = tta.agreement_id
        AND bagi.`status` = '1'
        LEFT JOIN bm_project bp on bp.pro_id = bagi.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bu on bu.unit_id = bagi.unit_id
        AND bu.del_flag = '0'
        LEFT JOIN ma_type mt1 ON mt1.type_id = bad.type_id AND mt1.del_flag = '0'
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt1.parent_id AND mt2.del_flag = '0'
        WHERE
        1=1
        <if test="unitId != null">
            and bu.unit_id = #{unitId}
        </if>
        <if test="proId != null">
            and bp.pro_id = #{proId}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( bai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        <if test="appTaskStatus != null and appTaskStatus == 0">
            and tt.task_status = #{appTaskStatus}
        </if>
        <if test="appTaskStatus != null and appTaskStatus == 1">
            and (tt.task_status = 1 or tt.task_status = 2)
        </if>
        <if test="status != null">
            and tt.task_status = #{status}
        </if>
        <if test="projectIdList != null  and projectIdList.size() > 0">
            and bp.external_id in
            <foreach item="item" collection="projectIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY bai.`code`
        ORDER BY bai.create_time desc
    </select>

    <select id="getDetailsList" resultType="com.bonus.material.clz.domain.back.MaterialBackApplyInfo">
        SELECT
            mt.type_name AS modelCode,
            mt2.type_name AS typeCode,
            mt.manage_type AS manageType,
            IFNULL( bad.pre_num, 0 ) AS backNum,
            mt.unit_name AS unitNames
        FROM
            back_apply_details bad
                LEFT JOIN ma_type mt ON bad.type_id = mt.type_id
                AND mt.`level` = '4' AND mt.del_flag = '0'
                LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                AND mt2.`level` = '3' AND mt2.del_flag = '0'
                LEFT JOIN back_apply_info bai ON bad.parent_id = bai.id
        WHERE
            bai.`code` = #{code}
    </select>

    <select id="selectBackApplyInfoById" resultType="com.bonus.material.clz.domain.back.MaterialBackApplyInfo">
        SELECT
            bai.id AS id,
            bai.CODE AS CODE,
            bai.task_id AS taskId,
            bai.back_person AS backPerson,
            bai.phone AS phone,
            bai.create_by AS createBy,
            bai.create_time AS createTime,
            bai.update_by AS updateBy,
            bai.update_time AS updateTime,
            bai.remark AS remark,
            bai.STATUS AS STATUS,
            bai.print_status AS printStatus,
            ba.agreement_id AS agreementId,
            bt.unit_id AS teamId,
            bt.unit_name AS teamName,
            bp.pro_id AS proId,
            bp.pro_name AS proName,
            bai.direct_audit_by AS directAuditBy,
            bai.back_sign_url AS backSignUrl,
            bai.back_sign_type AS backSignType
        FROM
            clz_back_apply_info bai
                LEFT JOIN tm_task_agreement tta ON bai.task_id = tta.task_id
                LEFT JOIN clz_bm_agreement_info ba ON ba.agreement_id = tta.agreement_id
                AND ba.`status` = 1
                LEFT JOIN bm_unit bt ON bt.unit_id = ba.unit_id
                AND bt.del_flag = '0'
                LEFT JOIN bm_project bp ON ba.project_id = bp.pro_id
                AND bp.del_flag = '0'
        WHERE
            bai.id = #{id}
    </select>

    <select id="selectBackApplyDetailsListByTaskId" resultType="com.bonus.material.clz.domain.back.MaterialBackApplyDetails">
        SELECT
        ba.id AS id,
        ba.CODE AS CODE,
        ba.parent_id AS parentId,
        ba.type_id AS typeId,
        mt2.type_name AS materialType,
        mt.type_name AS typeName,
        mt1.type_name AS materialName,
        mt.unit_name AS unitName,
        mt.unit_value AS unitValue,
        mt.manage_type AS manageType,
        ba.pre_num AS preNum,
        ba.use_num AS num,
        ba.STATUS AS STATUS,
        ba.create_by AS createBy,
        ba.create_time AS createTime,
        ba.update_by AS updateBy,
        ba.update_time AS updateTime,
        ba.remark AS remark,
        ba.ap_detection AS apDetection,
        ba.bad_num AS badNum,
        ba.good_num AS goodNum,
        ba.status AS status
        FROM
        clz_back_apply_details ba
        LEFT JOIN ma_type mt ON mt.type_id = ba.type_id and mt.del_flag = 0
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id and mt1.del_flag = 0
        LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id and mt2.del_flag = 0
        WHERE ba.parent_id = #{id}
        <if test="keyWord != null  and keyWord != ''">
            and (
            mt1.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%') or
            mt2.type_name like concat('%', #{keyWord}, '%')
            )
        </if>
    </select>

    <select id="selectByCode" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
            bcd.ma_id AS maId,
            mm.ma_code as maCode,
            bcd.type_id AS typeId,
            mt2.type_name AS materialType,
            mt.type_name AS typeName,
            mt1.type_name AS materialName,
            bcd.ap_detection AS apDetection,
            sd.dict_label AS maStatusName,
            bcd.bad_num AS badNum,
            bcd.good_num AS goodNum,
            mm.ma_status AS maStatus
        FROM
            clz_back_check_details bcd
                left join ma_machine mm on bcd.ma_id = mm.ma_id
                left join ma_type mt ON mt.type_id = mm.type_id and mt.del_flag = 0
                left join ma_type mt1 ON mt.parent_id = mt1.type_id and mt1.del_flag = 0
                left join ma_type mt2 ON mt1.parent_id = mt2.type_id and mt2.del_flag = 0
                LEFT JOIN sys_dict_data sd ON mm.ma_status = sd.dict_value
                and sd.dict_type = 'ma_machine_status'
        where bcd.parent_id = #{id}
    </select>

    <select id="selectDto" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
        bcd.ma_id AS maId,
        mm.ma_code as maCode,
        bcd.type_id AS typeId,
        mt2.type_name AS materialType,
        mt.type_name AS materialName,
        mt1.type_name AS typeName,
        bcd.ap_detection AS apDetection,
        mm.ma_status AS maStatus,
        bcd.bad_num AS badNum,
        bcd.good_num AS goodNum
        FROM
        clz_back_check_details bcd
        left join ma_machine mm on bcd.ma_id = mm.ma_id
        left join ma_type mt ON mt.type_id = mm.type_id and mt.del_flag = 0
        left join ma_type mt1 ON mt.parent_id = mt1.type_id and mt1.del_flag = 0
        left join ma_type mt2 ON mt1.parent_id = mt2.type_id and mt2.del_flag = 0
        where bcd.parent_id = #{id}
        <if test="maId != null">
            and bcd.ma_id = #{maId}
        </if>
    </select>

    <select id="getDetailsById" resultType="com.bonus.material.clz.domain.back.MaterialBackApplyDetails">
        SELECT
            mt1.type_name as typeName,
            mt.type_name as typeModel,
            bcd.type_id as typeId,
            bcd.back_num as preNum,
            bcd.ma_id as maId,
            mm.ma_code as maCode,
            bcd.parent_id as parentId,
            bcd.create_by as createBy,
            bcd.create_time as createTime,
            bai.task_id as taskId,
            bcd.ap_detection as apDetection
        FROM
            clz_back_check_details bcd
                LEFT JOIN ma_type mt ON bcd.type_id = mt.type_id
                AND mt.del_flag = '0'
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                AND mt1.del_flag = '0'
                LEFT JOIN ma_machine mm ON bcd.ma_id = mm.ma_id
                LEFT JOIN clz_back_apply_info bai ON bcd.parent_id = bai.id
        WHERE
            bcd.parent_id = #{parentId} and bcd.type_id = #{typeId}
    </select>

    <select id="selectBackDetails" resultType="com.bonus.material.clz.domain.back.MaterialBackApplyInfo">
        SELECT
        bai.id AS id,
        tta.agreement_id AS agreementId,
        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,
        mm.ma_code AS maCode
        FROM
        clz_back_check_details bcd
        LEFT JOIN clz_back_apply_info bai ON bai.id = bcd.parent_id
        LEFT JOIN tm_task_agreement tta ON tta.task_id = bai.task_id
        LEFT JOIN ma_machine mm ON bcd.ma_id = mm.ma_id
        WHERE
        bcd.parent_id = #{id}
        <if test="typeId != '' and typeId != null">
            and bcd.type_id = #{typeId}
        </if>
        GROUP By bcd.type_id,bcd.ma_id
    </select>

    <select id="getStlInfo" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT
        id AS id,
        agreement_id AS agreementId,
        type_id AS typeId,
        ma_id AS maId,
        num AS backNum,
        start_time AS startTime,
        end_time AS endTime,
        status AS status,
        lease_id AS leaseId,
        lease_type AS leaseType,
        back_id AS backId,
        lease_price AS leasePrice,
        buy_price AS buyPrice,
        company_id AS companyId,
        lease_style AS leaseStyle
        FROM
        clz_slt_agreement_info
        WHERE
        agreement_id = #{agreementId}
        AND type_id = #{typeId}
        AND status = 0
        <if test="maId != null">
            AND ma_id = #{maId}
        </if>
        order by start_time asc
    </select>

    <select id="getBackInfoDetails" resultType="com.bonus.material.clz.domain.back.MaterialBackApplyTotalInfo">
        SELECT
        bcd.id AS id,
        bcd.parent_id AS parentId,
        mt.type_id AS typeId,
        mt2.type_name AS materialName,
        mt.type_name AS typeName,
        mt.manage_type AS manageType,
        mt.unit_name AS unitName,
        SUM( bcd.back_num ) AS backNum,
        bcd.create_time AS backTime,
        bt.unit_name AS teamName,
        bp.pro_name AS proName,
        bai.back_person AS backPerson,
        bai.create_by AS createBy,
        bai.`code` AS code,
        bp.external_id AS externalId,
        bp.imp_unit AS impUnit,
        bt.bzz_idcard AS idCard
        FROM
        clz_back_check_details bcd
        LEFT JOIN ma_type mt ON bcd.type_id = mt.type_id
        AND mt.`level` = '4' AND mt.del_flag = '0'
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        AND mt2.`level` = '3' AND mt2.del_flag = '0'
        LEFT JOIN clz_back_apply_info bai ON bcd.parent_id = bai.id
        LEFT JOIN tm_task tt ON tt.task_id = bai.task_id
        LEFT JOIN tm_task_agreement tta ON tta.task_id = tt.task_id
        LEFT JOIN clz_bm_agreement_info bagi ON bagi.agreement_id = tta.agreement_id
        AND bagi.`status` = '1'
        LEFT JOIN bm_project bp ON bp.pro_id = bagi.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bt ON bt.unit_id = bagi.unit_id
        AND bt.del_flag = '0'
        WHERE
        bcd.is_finished = 1
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( bcd.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        <if test="projectIdList != null  and projectIdList.size() > 0">
            and bp.external_id in
            <foreach item="item" collection="projectIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="keyWord != null and keyWord != ''">
            and (
            mt2.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%') or
            bt.unit_name like concat('%', #{keyWord}, '%') or
            bp.pro_name like concat('%', #{keyWord}, '%') or
            bai.back_person like concat('%', #{keyWord}, '%') or
            bai.create_by like concat('%', #{keyWord}, '%') or
            bai.`code` like concat('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY
        mt.type_id,
        bt.unit_id,
        bp.pro_id,
        bai.`code`
        ORDER BY
        bcd.create_time DESC
    </select>

    <select id="getSecondBackInfo" resultType="com.bonus.material.clz.domain.vo.back.MaterialBackMaCodeVo">
        SELECT
        mt.type_id as typeId,
        mt1.type_name as materialName,
        mt.type_name as typeName,
        mm.ma_id as maId,
        mm.ma_code as maCode,
        bai.back_person as backPerson,
        bcd.create_time as backTime
        FROM
        clz_back_check_details bcd
        LEFT JOIN ma_machine mm ON bcd.ma_id = mm.ma_id
        LEFT JOIN ma_type mt ON mm.type_id = mt.type_id
        AND mt.del_flag = '0'
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        AND mt1.del_flag = '0'
        LEFT JOIN clz_back_apply_info bai ON bcd.parent_id = bai.id
        WHERE
        bcd.parent_id = #{parentId}
        and mt.type_id = #{typeId}
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt1.type_name LIKE CONCAT(CONCAT('%', #{keyWord}), '%')
            OR mt.type_name LIKE CONCAT(CONCAT('%', #{keyWord}), '%')
            OR mm.ma_code LIKE CONCAT(CONCAT('%', #{keyWord}), '%')
            OR bai.back_person LIKE CONCAT(CONCAT('%', #{keyWord}), '%')
            )
        </if>
        ORDER BY
        bcd.create_time DESC
    </select>

    <select id="getUseTypeTreeL4" resultType="com.bonus.common.biz.domain.TypeTreeNode">
        SELECT
            mt.type_id as typeId,
            mt1.type_name as materialName,
            mt.type_name as typeName,
            mt.parent_id as parentId,
            mt.unit_name as unitName,
            mt.unit_value as unitValue,
            mt.manage_type as manageType,
            SUM(sai.num) AS num,
            mt.LEVEL as level,
            clai.lease_style as leaseStyle
        FROM
            ma_type mt
                LEFT JOIN clz_slt_agreement_info sai ON mt.type_id = sai.type_id
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id and mt1.del_flag = '0'
                LEFT JOIN clz_lease_apply_info clai ON sai.lease_id = clai.id
        WHERE
            sai.STATUS = '0' and sai.agreement_id = #{agreementId}
        GROUP BY
            mt.type_id
    </select>

    <select id="getMachineById" resultType="com.bonus.material.clz.domain.vo.MaterialMaCodeVo">
        SELECT
            mm.ma_id AS maId,
            mm.ma_code AS maCode,
            '在用' AS maStatusName,
            mt1.type_name AS materialName,
            mt.type_id AS typeId,
            mt.type_name AS typeName,
            mt2.type_name AS materialType
        FROM
            clz_lease_out_details lod
                LEFT JOIN ma_machine mm ON lod.ma_id = mm.ma_id
                LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
                AND mt.del_flag = '0'
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                AND mt1.del_flag = '0'
                LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id
                AND mt2.del_flag = '0'
                LEFT JOIN clz_slt_agreement_info sai on lod.type_id = sai.type_id
                and sai.ma_id = lod.ma_id
                LEFT JOIN clz_bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
        WHERE
            sai.status = '0'
          AND mt.type_id = #{typeId}
          AND ba.unit_id = #{teamId}
          AND ba.project_id = #{proId}
        <if test="maCode != null and maCode != ''">
            AND mm.ma_code LIKE CONCAT(CONCAT('%', #{maCode}), '%')
        </if>
        GROUP BY mm.ma_id
    </select>

    <select id="selectAgreementId" resultType="java.lang.Long">
        SELECT
            tt.agreement_id AS agreementId
        FROM
            clz_back_apply_info ba
                LEFT JOIN tm_task_agreement tt ON ba.task_id = tt.task_id
        WHERE
            ba.id = #{id}
    </select>

    <select id="selectByAgreementIdAndTypeIdAndMaId" resultType="com.bonus.material.clz.domain.back.MaterialBackApplyInfo">
        SELECT
            agreement_id AS agreementId,
            type_id AS typeId,
            ma_id AS maId
        FROM
            clz_slt_agreement_info
        WHERE
            status = '0'
            AND agreement_id = #{agreementId}
            AND type_id = #{typeId}
          <if test="maId != null">
            AND ma_id = #{maId}
          </if>
    </select>

    <select id="selectByAgreementIdAndTypeId" resultType="com.bonus.material.clz.domain.back.MaterialBackApplyInfo">
        SELECT
        agreement_id AS agreementId,
        type_id AS typeId,
        ma_id AS maId
        FROM
        clz_slt_agreement_info
        WHERE
        status = '0'
        AND agreement_id = #{agreementId}
        AND type_id = #{typeId}
        <if test="maId != null">
            AND ma_id = #{maId}
        </if>
    </select>

    <select id="getMachine" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
        mm.ma_id AS maId,
        mm.ma_code AS maCode,
        '在用' AS maStatusName,
        mt1.type_name AS typeName,
        mm.type_id AS typeId,
        mt.type_name AS materialName,
        mt2.type_name AS materialType,
        ba.unit_id AS teamId,
        bu.unit_name AS teamName,
        ba.project_id AS proId,
        bp.pro_name AS proName,
        ba.agreement_id AS agreementId
        FROM
        clz_lease_out_details lod
        LEFT JOIN clz_slt_agreement_info cs ON lod.ma_id = cs.ma_id
        AND lod.type_id = cs.type_id
        LEFT JOIN ma_machine mm ON lod.ma_id = mm.ma_id
        LEFT JOIN ma_type mt ON mm.type_id = mt.type_id
        AND mt.del_flag = '0'
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        AND mt1.del_flag = '0'
        LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id
        AND mt2.del_flag = '0'
        LEFT JOIN clz_lease_apply_info lai ON lod.parent_id = lai.id
        LEFT JOIN clz_bm_agreement_info ba ON cs.agreement_id = ba.agreement_id
        LEFT JOIN bm_project bp ON bp.pro_id = ba.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bu ON bu.unit_id = ba.unit_id
        AND bu.del_flag = '0'
        WHERE
        cs.`status` = '0'
        AND ba.unit_id = #{teamId}
        AND ba.project_id = #{proId}
        <if test="maCode != null and maCode != ''">
            AND mm.ma_code LIKE CONCAT(CONCAT('%', #{maCode}), '%')
        </if>
        <if test="qrCode != null and qrCode != ''">
            AND mm.qr_code = #{qrCode}
        </if>
    </select>

</mapper>