<?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.basic.mapper.BmReportMapper">

    <select id="getPurChaseReportList" resultType="com.bonus.material.basic.domain.report.PurChaseReportInfo">
        SELECT
            pcd.task_id as taskId,
            pcd.type_id as typeId,
            mt2.type_name as typeName,
            mt1.type_name as typeModelName,
            mt1.unit_name as unitName,
            IFNULL(pcd.purchase_num, 0) as purchaseNum,
            IFNULL(pcd.check_num, 0) as passNum,
            IFNULL(pcd.input_num, 0) as inputNum,
            IFNULL(pcd.purchase_price, 0) as purchasePrice,
            IFNULL(pcd.purchase_tax_price, 0) as purchasePriceNoTax,
            msi.supplier as supplierName,
            pci.create_time as purchaseTime,
            pcd.check_time as passTime,
            pcd.input_time as inputTime,
            a.creator as inputUser,
            IFNULL(pcd.check_num, 0) - IFNULL(pcd.input_num, 0) as pendingInputNum,
            tt.`code` as inputCode,
            mt1.manage_type as manageType
        FROM
            purchase_check_details pcd
                LEFT JOIN ma_type mt1 ON pcd.type_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 tm_task tt ON pcd.task_id = tt.task_id
                LEFT JOIN purchase_check_info pci ON pcd.task_id = pci.task_id
                LEFT JOIN ma_supplier_info msi ON pci.supplier_id = msi.supplier_id
                LEFT JOIN (
                SELECT GROUP_CONCAT(DISTINCT creator) AS creator, task_id, type_id
                FROM bm_storage_log
                GROUP BY task_id, type_id
            ) a ON pcd.task_id = a.task_id AND pcd.type_id = a.type_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        LEFT JOIN ma_type_manage mtm ON mt4.type_id = mtm.type_id
        WHERE
        pcd.check_num > 0
        <if test="userId != null ">
            and mtm.user_id = #{userId}
        </if>
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR msi.supplier LIKE CONCAT('%', #{keyWord}, '%')
            OR a.creator LIKE CONCAT('%', #{keyWord}, '%')
            OR tt.`code` LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( pci.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
      ORDER BY pcd.input_time DESC
    </select>

    <select id="getPurChaseReportDetailsList"
            resultType="com.bonus.material.basic.domain.report.PurChaseReportDetails">
        SELECT
        mt2.type_name AS typeName,
        mt1.type_name AS typeModelName,
        pcd.purchase_price AS purchasePrice,
        pcd.purchase_tax_price AS purchasePriceNoTax,
        bqi.ma_code AS maCode
        FROM
        bm_qrcode_info bqi
        LEFT JOIN ma_type mt1 ON bqi.type_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 purchase_check_details pcd ON bqi.task_id = pcd.task_id
        AND pcd.type_id = bqi.type_id
        WHERE
        bqi.task_id = #{taskId} and bqi.type_id = #{typeId}
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR bqi.ma_code LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
    </select>

    <select id="getPurChaseReportSuccessList"
            resultType="com.bonus.material.basic.domain.report.PurChaseReportDetails">
        SELECT
        mt2.type_name AS typeName,
        mt1.type_name AS typeModelName,
        pcd.purchase_price AS purchasePrice,
        pcd.purchase_tax_price AS purchasePriceNoTax,
        bqi.ma_code AS maCode
        FROM
        bm_qrcode_info bqi
        LEFT JOIN ma_type mt1 ON bqi.type_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 purchase_check_details pcd ON bqi.task_id = pcd.task_id
        AND pcd.type_id = bqi.type_id
        WHERE
        bqi.task_id = #{taskId} and bqi.type_id = #{typeId} and pcd.status != 12
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR bqi.ma_code LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
    </select>

    <select id="getPurChaseReportInBoundList"
            resultType="com.bonus.material.basic.domain.report.PurChaseReportDetails">
        SELECT
        mt2.type_name AS typeName,
        mt1.type_name AS typeModelName,
        pcd.purchase_price AS purchasePrice,
        pcd.purchase_tax_price AS purchasePriceNoTax,
        bqi.ma_code AS maCode
        FROM
        bm_qrcode_info bqi
        left join ma_machine mm on bqi.ma_code = mm.ma_code
        LEFT JOIN ma_type mt1 ON bqi.type_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 purchase_check_details pcd ON bqi.task_id = pcd.task_id
        AND pcd.type_id = bqi.type_id
        WHERE
        bqi.task_id = #{taskId} and bqi.type_id = #{typeId} and bqi.ma_code is not null and mm.ma_status != 0
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR bqi.ma_code LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
    </select>

    <select id="getPurChaseReportNotInBoundList"
            resultType="com.bonus.material.basic.domain.report.PurChaseReportDetails">
        SELECT
        mt2.type_name AS typeName,
        mt1.type_name AS typeModelName,
        pcd.purchase_price AS purchasePrice,
        pcd.purchase_tax_price AS purchasePriceNoTax,
        bqi.ma_code AS maCode
        FROM
        bm_qrcode_info bqi
        left join ma_machine mm on bqi.ma_code = mm.ma_code
        LEFT JOIN ma_type mt1 ON bqi.type_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 purchase_check_details pcd ON bqi.task_id = pcd.task_id
        AND pcd.type_id = bqi.type_id
        WHERE
        bqi.task_id = 1149 and bqi.type_id = 517  and mm.ma_status = 0
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR bqi.ma_code LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
        UNION ALL

        SELECT
        mt2.type_name AS typeName,
        mt1.type_name AS typeModelName,
        pcd.purchase_price AS purchasePrice,
        pcd.purchase_tax_price AS purchasePriceNoTax,
        bqi.ma_code AS maCode
        FROM
        bm_qrcode_info bqi
        LEFT JOIN ma_type mt1 ON bqi.type_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 purchase_check_details pcd ON bqi.task_id = pcd.task_id
        AND pcd.type_id = bqi.type_id
        WHERE
        bqi.task_id = 1149 and bqi.type_id = 517  and bqi.ma_code is null
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR bqi.ma_code LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
    </select>

    <select id="getLeaseOutList" resultType="com.bonus.material.basic.domain.report.LeaseOutInfo">
        SELECT
            mt2.type_name AS typeName,
            mt1.type_name AS typeModelName,
            mt1.unit_name AS unitName,
            lad.pre_num as leaseNum,
            IFNULL( lad.al_num, 0 ) as outNum,
            lad.pre_num - IFNULL( lad.al_num, 0 ) as pendingOutNum,
            lai.create_time as leaseTime,
            a.createTime as outTime,
            bu.unit_name as leaseUnitName,
            bp.pro_name as leaseProjectName,
            lai.create_by as leasePersonName,
            a.createBy as outPersonName,
            lai.`code` as code,
            mt1.manage_type as manageType,
            lad.parent_id as parentId,
            lad.type_id as typeId,
            tt.task_type as taskType,
            mtm.user_id as userId
        FROM
            lease_apply_details lad
                LEFT JOIN ma_type mt1 ON lad.type_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 lease_apply_info lai ON lad.parent_id = lai.id
                LEFT JOIN tm_task tt ON tt.task_id = lai.task_id
                LEFT JOIN tm_task_agreement tta ON lai.task_id = tta.task_id
                LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
                LEFT JOIN bm_unit bu ON bai.unit_id = bu.unit_id
                LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
                LEFT JOIN ( SELECT lod.parent_id, lod.type_id, GROUP_CONCAT( DISTINCT su.nick_name ) AS createBy, MAX( lod.create_time ) AS createTime FROM lease_out_details lod
                LEFT JOIN sys_user su ON lod.create_by = su.user_id GROUP BY parent_id, type_id ) a ON a.parent_id = lad.parent_id
                AND a.type_id = lad.type_id
                LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
                LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
                LEFT JOIN ma_type_manage mtm ON mt4.type_id = mtm.type_id
        WHERE
        1 = 1
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
    ORDER BY a.createTime DESC
    </select>

    <select id="getLeaseOutDetailsList" resultType="com.bonus.material.basic.domain.report.LeaseOutDetails">
        SELECT
        mt2.type_name AS typeName,
        mt1.type_name AS typeModelName,
        mm.ma_code as maCode,
        lod.create_by as outPersonName,
        lod.create_time as outTime
        FROM
        lease_out_details lod
        LEFT JOIN ma_type mt1 ON lod.type_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 ma_machine mm ON lod.ma_id = mm.ma_id
        WHERE
        lod.parent_id = #{parentId} and lod.type_id = #{typeId}
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mm.ma_code LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY mm.ma_id
    </select>

    <select id="getBackInputList" resultType="com.bonus.material.basic.domain.report.BackInputInfo">
        SELECT
            mt2.type_name as typeName,
            mt1.type_name as typeModelName,
            mt1.unit_name as unitName,
            bad.pre_num as backNum,
            bad.create_time as backTime,
            bu.unit_name as backUnitName,
            bp.pro_name as backProjectName,
            bai.back_person as backPersonName,
            bad.create_by as createBy,
            bai.`code` as code,
            bad.parent_id as parentId,
            bad.type_id as typeId,
            mt1.manage_type as manageType,
            mtm.user_id as userId
        FROM
            back_apply_details bad
                LEFT JOIN ma_type mt1 ON bad.type_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 back_apply_info bai ON bad.parent_id = bai.id
                LEFT JOIN tm_task_agreement tta ON bai.task_id = tta.task_id
                LEFT JOIN bm_agreement_info ba ON tta.agreement_id = ba.agreement_id
                LEFT JOIN bm_unit bu ON ba.unit_id = bu.unit_id
                LEFT JOIN bm_project bp ON ba.project_id = bp.pro_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        LEFT JOIN ma_type_manage mtm ON mt4.type_id = mtm.type_id
        WHERE
        mt2.type_name is not null and mt1.type_name is not null
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR bu.unit_name LIKE CONCAT('%', #{keyWord}, '%')
            OR bp.pro_name LIKE CONCAT('%', #{keyWord}, '%')
            OR bai.back_person LIKE CONCAT('%', #{keyWord}, '%')
            OR bai.`code` LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( bad.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
     ORDER BY bad.create_time DESC
    </select>

    <select id="getBackDetailsInputList" resultType="com.bonus.material.basic.domain.report.BackInputDetails">
        SELECT
        mt2.type_name AS typeName,
        mt1.type_name AS typeModelName,
        mm.ma_code as maCode,
        bad.create_by as inputPersonName,
        bad.create_time as inputTime
        FROM
        back_check_details bad
        LEFT JOIN ma_type mt1 ON bad.type_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 ma_machine mm ON bad.ma_id = mm.ma_id
        WHERE
        bad.parent_id = #{parentId} and bad.type_id = #{typeId}
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mm.ma_code LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY mm.ma_id
    </select>

    <select id="getRepairList" resultType="com.bonus.material.basic.domain.report.RepairInfo">
        SELECT
        mt2.type_name AS typeName,
        mt1.type_name AS typeModelName,
        mt1.unit_name AS unitName,
        SUM(IFNULL( rad.repair_num, 0 )) as backNum,
        SUM(IFNULL( rad.repaired_num, 0 )) as repairedNum,
        SUM(IFNULL( rad.scrap_num, 0 )) as pendingScrapNum,
        SUM(IFNULL( rad.repair_num, 0 ) - IFNULL( rad.repaired_num, 0 )- IFNULL( rad.scrap_num, 0 )) as pendingRepairNum,
        rad.create_time as backTime,
        IFNULL(a.create_time, rad.update_time) as repairTime,
        bu.unit_name as leaseUnitName,
        bp.pro_name as leaseProjectName,
        rad.repairer as repairPersonName,
        u.nick_name as nickName,
        tt.`code` as code,
        rad.task_id as taskId,
        rad.type_id as typeId,
        mt1.manage_type as manageType
        FROM
        repair_apply_details rad
        LEFT JOIN ma_type mt1 ON rad.type_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 (SELECT task_id, type_id, MAX(create_time) as create_time  FROM repair_apply_record
        GROUP BY task_id, type_id) a ON rad.task_id = a.task_id
        AND rad.type_id = a.type_id
        LEFT JOIN tm_task tt on rad.task_id = tt.task_id
        LEFT JOIN tm_task_agreement tta ON rad.task_id = tta.task_id
        LEFT JOIN bm_agreement_info ba ON tta.agreement_id = ba.agreement_id
        LEFT JOIN bm_unit bu ON ba.unit_id = bu.unit_id
        LEFT JOIN bm_project bp ON ba.project_id = bp.pro_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        LEFT JOIN ma_type_manage mtm ON mt4.type_id = mtm.type_id
        left join sys_user u on u.user_id = rad.repairer and u.del_flag = '0'
        WHERE
        1 = 1
        <if test="userId != null ">
            and mtm.user_id = #{userId}
        </if>
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR bu.unit_name LIKE CONCAT('%', #{keyWord}, '%')
            OR bp.pro_name LIKE CONCAT('%', #{keyWord}, '%')
            OR rad.repairer LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( rad.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        GROUP BY rad.task_id, rad.type_id
        ORDER BY rad.create_time DESC
    </select>

    <select id="getRepairDetailsList" resultType="com.bonus.material.basic.domain.report.RepairDetails">
        SELECT
        mt2.type_name AS typeName,
        mt1.type_name AS typeModelName,
        mm.ma_code AS maCode,
        rad.repairer AS repairPersonName,
        su.nick_name as nickName,
        rar.create_time AS repairTime
        FROM
        repair_apply_details rad
        LEFT JOIN ma_type mt1 ON rad.type_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 ma_machine mm ON rad.ma_id = mm.ma_id
        LEFT JOIN repair_apply_record rar ON rad.task_id = rar.task_id
        and rad.type_id = rar.type_id and rar.ma_id = rad.ma_id
        left join sys_user su on rad.repairer = su.user_id and su.del_flag = '0'
        WHERE
        rad.task_id = #{taskId} and rad.type_id = #{typeId}
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mm.ma_code LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY mm.ma_id
    </select>

    <select id="getRepairInputList" resultType="com.bonus.material.basic.domain.report.RepairInputDto">
        SELECT
            mt2.type_name AS typeName,
            mt1.type_name AS typeModelName,
            mt1.unit_name AS unitName,
            SUM(IFNULL( rid.repair_num, 0 )) as repairedNum,
            SUM(IFNULL( rid.input_num, 0 )) as inputNum,
            rid.create_time as repairTime,
            a.create_time as inputTime,
            a.creator as inputPersonName,
            SUM(IFNULL( rid.repair_num, 0 ) - IFNULL( rid.input_num, 0 )) as pendingInputNum,
            tt.`code` as code,
            rid.task_id as taskId,
            rid.type_id as typeId,
            mt1.manage_type as manageType
        FROM
            repair_input_details rid
                LEFT JOIN ma_type mt1 ON rid.type_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 tm_task tt on rid.task_id = tt.task_id
                LEFT JOIN ( SELECT task_id, type_id, GROUP_CONCAT( DISTINCT creator ) AS creator, MAX( create_time ) AS create_time FROM bm_storage_log GROUP BY task_id, type_id) a ON rid.task_id = a.task_id
                AND rid.type_id = a.type_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        LEFT JOIN ma_type_manage mtm ON mt4.type_id = mtm.type_id
        WHERE
        1 = 1
        <if test="userId != null ">
            and mtm.user_id = #{userId}
        </if>
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR tt.`code` LIKE CONCAT('%', #{keyWord}, '%')
            OR a.creator LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( rid.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        GROUP BY rid.task_id,rid.type_id
        ORDER BY a.create_time DESC
    </select>

    <select id="getRepairInputDetailsList" resultType="com.bonus.material.basic.domain.report.RepairInputDetails">
        SELECT
            mt2.type_name as typeName,
            mt1.type_name as typeModelName,
            mm.ma_code as maCode,
            bsl.creator as inputPersonName,
            bsl.create_time as inputTime
        FROM
            repair_input_details rid
                LEFT JOIN ma_type mt1 ON rid.type_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 ma_machine mm ON rid.ma_id = mm.ma_id
                LEFT JOIN bm_storage_log bsl ON rid.task_id = bsl.task_id
                AND rid.type_id = bsl.type_id
        WHERE
        rid.task_id = #{taskId} and rid.type_id = #{typeId}
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mm.ma_code LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY mm.ma_id
    </select>
    <select id="getLeaseInfoById" resultType="com.bonus.material.basic.domain.report.LeaseOutInfo">
      select  bu.unit_name as leaseUnitName,
            bp.pro_name as leaseProjectName
        from  lease_apply_info lai
            LEFT JOIN bm_unit bu ON lai.unit_id = bu.unit_id
            LEFT JOIN bm_project bp ON lai.project_id = bp.pro_id
        where  lai.id=#{parentId}
    </select>

    <select id="getScrapList" resultType="com.bonus.material.basic.domain.report.ScrapInfo">
        SELECT
        mt1.type_name AS typeName,
        mt.type_name AS typeModelName,
        mt.unit_name AS unitName,
        IFNULL( sad.scrap_num, 0 ) AS scrapNum,
        sad.create_time AS scrapTime,
        sad.create_by AS scrapPersonName,
        bui.unit_name AS backUnit,
        bpi.pro_name AS backPro,
        CASE
        sad.scrap_source
        WHEN 2 THEN
        '维修报废'
        WHEN 3 THEN
        '盘点报废' ELSE NULL
        END AS scrapType,
        tt.`code` AS scrapCode,
        sad.task_id AS taskId,
        sad.type_id AS typeId,
        mt.manage_type AS manageType,
        mm.ma_code AS maCode,
        IFNULL( mt.buy_price, 0 ) AS buyPrice,
        sad.scrap_reason AS scrapReason,
        rad.repair_id AS repairId
        FROM
        scrap_apply_details sad
        LEFT JOIN ma_type mt ON sad.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 tm_task tt ON sad.task_id = tt.task_id
        LEFT JOIN tm_task_agreement tta ON sad.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai2 ON tta.agreement_id = bai2.agreement_id
        LEFT JOIN bm_unit bui ON bai2.unit_id = bui.unit_id
        LEFT JOIN bm_project bpi ON bai2.project_id = bpi.pro_id
        AND bpi.del_flag = '0'
        LEFT JOIN ma_machine mm ON sad.ma_id = mm.ma_id
        LEFT JOIN repair_audit_details rad ON rad.id = sad.parent_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt1.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        LEFT JOIN ma_type_manage mtm ON mt4.type_id = mtm.type_id
        WHERE
        sad.ledger_status = '1'
        <if test="userId != null ">
            and mtm.user_id = #{userId}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[AND DATE_FORMAT(sad.create_time, '%Y-%m-%d') BETWEEN #{startTime} AND #{endTime}]]>
        </if>
    </select>

    <select id="getScrapDetailsList" resultType="com.bonus.material.basic.domain.report.ScrapDetailsInfo">
        SELECT
            mt1.type_name AS typeName,
            mt.type_name AS typeModelName,
            mt.buy_price AS buyPrice,
            mm.ma_code AS maCode
        FROM
            scrap_apply_details sad
                LEFT JOIN ma_type mt ON sad.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 sad.ma_id = mm.ma_id
        WHERE
            sad.task_id = #{taskId} AND sad.type_id = #{typeId}
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mt.type_name LIKE CONCAT('%', #{keyWord}, '%')
            OR mm.ma_code LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
    </select>
    <select id="selectUserNameById" resultType="java.lang.String">
        SELECT
            u.nick_name as repairPersonName
        FROM
            sys_user u
        WHERE
            u.user_id = #{repairPersonName}
    </select>

</mapper>
