<?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.push.mapper.ProDataUseInfoMapper">

    <select id="getProDataUseInfo" resultType="com.bonus.material.push.domain.ProIdsBean">
        SELECT
        ANY_VALUE(bp.pro_name) as proName,
        sum(sai.num) typeNum,
        ANY_VALUE(mt2.type_id) as typeId,
        mt2.is_statics as isStatics,
        bp.external_id as externalId
        FROM slt_agreement_info sai
        LEFT JOIN bm_agreement_info bai ON sai.agreement_id = bai.agreement_id
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        LEFT JOIN ma_type mt ON sai.type_id = mt.type_id
        LEFT JOIN ma_type mt2 ON mt.parent_id = mt2.type_id
        WHERE
        sai.is_slt = 0
        AND sai.end_time IS NULL
        AND bp.external_id = #{proId}
        AND mt2.type_id IN
        <foreach collection="typeIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY bp.external_id, mt2.is_statics
    </select>
    <select id="getTypeId" resultType="java.lang.Integer">
        select type_id from ma_type where is_statics in (1,2,3,4,5,6,7)
    </select>
    <select id="getMachineDetails" resultType="com.bonus.material.push.domain.MachineInfoBean">
        select
            mt.type_id as typeId,
            mt2.type_name as typeName,
            mt.type_name as typeModelName,
            mt.unit_name as unit,
            sai.num as num,
            mm.assets_code as assetsCode,
            mt.rent_price as rentPrice,
            '在用' as status
        from slt_agreement_info sai
        left join bm_agreement_info bai on sai.agreement_id = bai.agreement_id
        left join bm_project bp on bai.project_id = bp.pro_id
        left join ma_type mt on sai.type_id = mt.type_id
        left join ma_type mt2 on mt.parent_id = mt2.type_id
        left join ma_machine mm on sai.ma_id = mm.ma_id
        where sai.is_slt = 0 and sai.end_time is null
        AND bp.external_id  = #{externalId}
        AND mt2.is_statics = #{isStatics}
        <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>
    </select>
    <select id="getTypeKeeperName" resultType="java.lang.String">
        select group_concat(DISTINCT su.nick_name) from ma_type_keeper mtk
        left join sys_user su on mtk.user_id = su.user_id
        left join sys_user_post sp on su.user_id = sp.user_id
        where mtk.type_id = #{typeId} and sp.post_id = 5
    </select>
    <select id="getNumsByStatics" resultType="com.bonus.material.push.domain.StaticsNumsBean">
        SELECT
        CASE mt.manage_type
        WHEN 0 THEN
        IFNULL(subquery0.num, 0)
        ELSE
        IFNULL(mt.storage_num, 0)
        END                            AS num,
        IFNULL(subquery1.usNum, 0)          AS useNum,
        IFNULL(subquery2.repairNum, 0)      AS repairNum,
        CASE mt.manage_type
        WHEN 0 THEN
        IFNULL(subquery0.num, 0) + IFNULL(subquery1.usNum, 0) + IFNULL(subquery2.repairNum, 0)
        ELSE
        IFNULL(mt.storage_num, 0) + IFNULL(subquery1.usNum, 0) + IFNULL(subquery2.repairNum, 0)
        END                             AS inventoryNum,
        mt2.is_statics AS isStatics
        FROM ma_type mt
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt4.type_name AS constructionType,
        mt4.type_id   AS firstTypeId,
        mt3.type_name AS materialType,
        mt3.type_id   AS secondTypeId,
        mt2.type_name AS typeName,
        mt2.type_id   AS thirdTypeId,
        mt.type_name  AS typeModelName,
        count(mm.ma_id) AS 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
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE mm.ma_code is not null and mm.ma_status in (1)
        GROUP BY mt.type_id) AS subquery0 ON subquery0.type_id = mt.type_id
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt4.type_name AS constructionType,
        mt4.type_id AS firstTypeId,
        mt3.type_name AS materialType,
        mt3.type_id AS secondTypeId,
        mt2.type_name AS typeName,
        mt2.type_id AS thirdTypeId,
        mt.type_name AS typeModelName,
        SUM(IFNULL( sai.num, 0 )) AS usNum,
        sd.dept_name AS unitName
        FROM
        slt_agreement_info sai
        left join bm_agreement_info bai on sai.agreement_id = bai.agreement_id
        left join bm_project bp on bai.project_id = bp.pro_id
        LEFT JOIN ma_type mt ON mt.type_id = sai.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_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 sys_dept sd ON sd.dept_id = bp.imp_unit
        WHERE
        sai.`status` = '0' and sai.`is_slt` = '0'
        <if test="unitName != null and unitName !=''">
            AND sd.dept_name = #{unitName}
        </if>
        <if test="deptId != null">
            AND sd.dept_id  = #{deptId}
        </if>
        <if test="isStatics != null">
            and mt2.is_statics = #{isStatics}
        </if>
        AND sai.end_time IS NULL
        AND sai.back_id IS NULL
        GROUP BY mt.type_id
        ) AS subquery1
        ON mt.type_id = subquery1.type_id
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt4.type_name                         AS constructionType,
        mt4.type_id                           AS firstTypeId,
        mt3.type_name                         AS materialType,
        mt3.type_id                           AS secondTypeId,
        mt2.type_name                         AS typeName,
        mt2.type_id                           AS thirdTypeId,
        mt.type_name                          AS typeModelName,
        SUM(
        IFNULL(rad.repair_num, 0) - IFNULL(rad.repaired_num, 0) -
        IFNULL(rad.scrap_num, 0)) AS repairNum,
        sd.dept_name AS unitName
        FROM repair_apply_details rad
        LEFT JOIN tm_task tt ON rad.task_id = tt.task_id
        LEFT JOIN tm_task_agreement tta ON tt.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        LEFT JOIN ma_type mt ON mt.type_id = rad.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_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 sys_dept sd ON sd.dept_id = bp.imp_unit
        WHERE
        rad.status = '0'
        <if test="unitName != null and unitName !=''">
            AND sd.dept_name = #{unitName}
        </if>
        <if test="deptId != null">
            AND sd.dept_id  = #{deptId}
        </if>
        <if test="isStatics != null">
            and mt2.is_statics = #{isStatics}
        </if>
        GROUP BY
        mt.type_id) AS subquery2 ON subquery2.type_id = mt.type_id
        LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_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
        WHERE mt.del_flag = '0' and mt.`level` = '4' and mt2.is_statics is not null
        <if test="isStatics != null">
            and mt2.is_statics = #{isStatics}
        </if>
    </select>
    <select id="getApproachingAndTimeout" resultType="com.bonus.material.push.domain.StaticsNumsBean">

        SELECT
        COUNT(CASE
        WHEN mm.next_check_time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 30 DAY)
        THEN 1
        ELSE NULL
        END) AS ApproachingNum,
        COUNT(CASE
        WHEN mm.next_check_time &lt; NOW()
        THEN 1
        ELSE NULL
        END) AS TimeoutNum
        FROM ma_machine mm
        LEFT JOIN slt_agreement_info sai on sai.ma_id = mm.ma_id and mm.type_id = sai.type_id
        LEFT JOIN bm_agreement_info bai ON sai.agreement_id = bai.agreement_id
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        LEFT JOIN ma_type mt ON sai.type_id = mt.type_id
        LEFT JOIN ma_type mt2 ON mt.parent_id = mt2.type_id
        left join sys_dept sd ON sd.dept_id = bp.imp_unit
        where mm.next_check_time is not null and mt.`level` = '4'
        and sai.`status`='0' and sai.end_time is null
        and bp.external_id is not null
        and mm.ma_status = 2
        and mt2.type_id not in (6032,6008,6270,6012,6761)
        <if test="deptId != null">
            AND sd.dept_id = #{deptId}
        </if>
        AND mm.next_check_time IS NOT NULL

    </select>
    <select id="getApproachingDetails" resultType="com.bonus.material.push.domain.MachineInfoBean">
        SELECT
            sd.dept_name as deptName,
            bp.pro_center as proCenter,
            bp.pro_name as proName,
            mt2.type_name as typeName,
            mt.type_name as typeModelName,
            mt.unit_name as unit,
            mm.ma_code as maCode,
            sai.start_time as leaseTime,
            mm.next_check_time as expireTime,
            CASE
            WHEN #{type} = 0 THEN DATEDIFF(mm.next_check_time, NOW())
            WHEN #{type} = 1 THEN DATEDIFF(NOW(), mm.next_check_time)
            ELSE NULL
            END AS daysDiff
        FROM ma_machine mm
                 LEFT JOIN slt_agreement_info sai on sai.ma_id = mm.ma_id and mm.type_id = sai.type_id
                 LEFT JOIN bm_agreement_info bai ON sai.agreement_id = bai.agreement_id
                 LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
                 LEFT JOIN sys_dept sd on bp.imp_unit = sd.dept_id
                 LEFT JOIN ma_type mt ON sai.type_id = mt.type_id
                 LEFT JOIN ma_type mt2 ON mt.parent_id = mt2.type_id
        WHERE mm.next_check_time is not null AND mt.`level`=4
        and bp.external_id is not null
        and sai.`status`='0' and sai.end_time is null
        and mm.ma_status = 2
        and mt2.type_id not in (6032,6008,6270,6012,6761)
        <if test="proId != null">
            AND bp.pro_id = #{proId}
        </if>
        <if test="type != null and type == 0">
            AND mm.next_check_time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 30 DAY)
        </if>
        <if test="type != null and type == 1">
            AND mm.next_check_time &lt; NOW()
        </if>
        <if test="deptName != null and deptName !=''">
            AND sd.dept_name like concat ('%', #{deptName}, '%')
        </if>
        <if test="proCenter != null and proCenter !=''">
            AND bp.pro_center like concat ('%', #{proCenter}, '%')
        </if>
        <if test="proName != null and proName !=''">
            AND bp.pro_name like concat ('%', #{proName}, '%')
        </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="proId != null">
            GROUP BY bp.pro_id
        </if>
        order by mt.jiju_type desc
    </select>
    <select id="getCompletionNotRefunded" resultType="com.bonus.material.push.domain.MachineInfoBean">
        select
            count(distinct bp.external_id) num,
            sd.dept_name as deptName,
            sd.dept_id as deptId
        from slt_agreement_info sai
                 left join bm_agreement_info bai on sai.agreement_id = bai.agreement_id
                 left join bm_project bp on bai.project_id = bp.pro_id
                 left join sys_dept sd on bp.imp_unit = sd.dept_id
        where sai.is_slt = 0 and sai.end_time is null and bp.actual_end_date is not null  and bp.pro_center is not null and bp.external_id is not null
        and dept_id not in (339,340,342,346)
          <if test="deptName != null and deptName != ''">
            AND sd.dept_name = #{deptName}
        </if>
        GROUP BY bp.imp_unit
    </select>
    <select id="getCompletionNotRefundedDetails" resultType="com.bonus.material.push.domain.MachineInfoBean">
        select
        bp.pro_name as proName,
        bp.actual_end_date as actualEndDate,
        DATEDIFF(NOW(), bp.actual_end_date) as daysDiff,
        sai.num num,
        mt.type_name as typeModelName,
        mt.unit_name as unit,
        mt2.type_name as typeName,
        sd.dept_name as deptName,
        bp.pro_center as proCenter
        from slt_agreement_info sai
        left join bm_agreement_info bai on sai.agreement_id = bai.agreement_id
        left join bm_project bp on bai.project_id = bp.pro_id
        left join sys_dept sd on bp.imp_unit = sd.dept_id
        left join ma_type mt on sai.type_id = mt.type_id
        left join ma_type mt2 on mt.parent_id = mt2.type_id
        where sai.status = 0 and bp.actual_end_date is not null and bp.actual_end_date is not null  and bp.pro_center is not null
        and mt.jiju_type = 2
        <if test="deptId != null">
            and sd.dept_id = #{deptId}
        </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>
    </select>
    <select id="getInventoryAlert" resultType="com.bonus.material.push.domain.InventoryAlertBean">
        select
        mt.type_name as typeModelName,
        mt2.type_name as typeName,
        mt.storage_num as num
        from ma_type mt
        left join ma_type mt2 on mt.parent_id = mt2.type_id
        where mt.del_flag = 0 and mt.`level`=4
        <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>
    </select>
    <select id="getInventoryAlertName" resultType="com.bonus.material.push.domain.InventoryAlertBean">
        select
            mt.type_name as typeModelName,
            mt2.type_name as typeName
        from ma_type mt
                 left join ma_type mt2 on mt.parent_id = mt2.type_id
        where mt.del_flag = 0 and mt.`level`=4
        <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>
    </select>
    <select id="getApproachingDetailsByUnit" resultType="java.util.Map">
        SELECT COUNT(CASE
                         WHEN mm.next_check_time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 30 DAY) THEN 1
                         ELSE NULL END)
                                                                                   AS approachingNum,
               COUNT(CASE WHEN mm.next_check_time &lt; NOW() THEN 1 ELSE NULL END) AS timeoutNum,
               sd.dept_name                                                        as deptName
        FROM ma_machine mm
                 LEFT JOIN slt_agreement_info sai ON sai.ma_id = mm.ma_id
            AND mm.type_id = sai.type_id
            AND sai.`status` = '0'
            AND sai.end_time IS NULL
                 LEFT JOIN bm_agreement_info bai ON sai.agreement_id = bai.agreement_id
                 LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
                 LEFT JOIN sys_dept sd on bp.imp_unit = sd.dept_id
                 LEFT JOIN ma_type mt ON sai.type_id = mt.type_id
                 LEFT JOIN ma_type mt2 ON mt.parent_id = mt2.type_id
        WHERE mm.next_check_time IS NOT NULL
          AND mt.`level` = '4'
          AND sd.dept_name in
              ('送电一分公司', '送电二分公司', '检修试验分公司', '建筑分公司（消防分公司）', '安徽宏源电力建设有限公司',
               '变电分公司')
        GROUP BY bp.imp_unit
    </select>
    <select id="getMaTypeList" resultType="com.bonus.material.push.domain.MachineInfoBean">
        SELECT
        mt.type_id AS typeId,
        mt.type_name AS specification,
        mt2.type_name AS materialName,
        mt.unit_name AS materialDw,
        mt.storage_num AS subKcNum
        FROM
        ma_type mt
        LEFT JOIN ma_type mt2 ON mt.parent_id = mt2.type_id
        WHERE
        mt.del_flag = 0
        AND mt.`level` =4
        <if test="materialName != null and materialName !=''">
            AND mt2.type_name like concat ('%', #{materialName}, '%')
        </if>
        <if test="specification != null and specification !=''">
            AND mt.type_name like concat ('%', #{specification}, '%')
        </if>
    </select>
    <select id="getUseNumsDetails" resultType="com.bonus.material.push.domain.MachineInfoBean">
        select
        mt.type_id as typeId,
        mt2.type_name as typeName,
        mt.type_name as typeModelName,
        mt.unit_name as unit,
        mm.assets_code as assetsCode,
        ifnull(mt.lease_price,0) as rentPrice,
        SUM(IFNULL( sai.num, 0 )) AS num,
        '在用' AS status
        FROM
        slt_agreement_info sai
        left join bm_agreement_info bai on sai.agreement_id = bai.agreement_id
        left join bm_project bp on bai.project_id = bp.pro_id
        LEFT JOIN ma_type mt ON mt.type_id = sai.type_id
        left join ma_machine mm ON mm.ma_id = sai.ma_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_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 sys_dept sd ON sd.dept_id = bp.imp_unit
        WHERE
        sai.`status` = '0' and sai.`is_slt` = '0'
        <if test="unitName != null and unitName !=''">
            AND sd.dept_name = #{unitName}
        </if>
        <if test="deptId != null">
            AND sd.dept_id  = #{deptId}
        </if>
        <if test="isStatics != null">
            and mt2.is_statics = #{isStatics}
        </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>
        AND sai.end_time IS NULL
        AND sai.back_id IS NULL
        GROUP BY mt.type_id
    </select>
    <select id="getRepairNumsDetails" resultType="com.bonus.material.push.domain.MachineInfoBean">
        SELECT
        mt.type_id as typeId,
        mt2.type_name as typeName,
        mt.type_name as typeModelName,
        mt.unit_name as unit,
        mm.assets_code as assetsCode,
        ifnull(mt.lease_price,0) as rentPrice,
        SUM(
        IFNULL(rad.repair_num, 0) - IFNULL(rad.repaired_num, 0) -
        IFNULL(rad.scrap_num, 0)) AS num,
        '在修' AS status
        FROM repair_apply_details rad
        LEFT JOIN tm_task tt ON rad.task_id = tt.task_id
        LEFT JOIN tm_task_agreement tta ON tt.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        LEFT JOIN ma_type mt ON mt.type_id = rad.type_id
        left join ma_machine mm ON mm.ma_id = rad.ma_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_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 sys_dept sd ON sd.dept_id = bp.imp_unit
        WHERE
        rad.status = '0'
        <if test="unitName != null and unitName !=''">
            AND sd.dept_name = #{unitName}
        </if>
        <if test="deptId != null">
            AND sd.dept_id  = #{deptId}
        </if>
        <if test="isStatics != null">
            and mt2.is_statics = #{isStatics}
        </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>
        GROUP BY
        mt.type_id
    </select>
    <select id="getNumsDetails" resultType="com.bonus.material.push.domain.MachineInfoBean">
        SELECT
        mt.type_id as typeId,
        mt2.type_name as typeName,
        mt.type_name as typeModelName,
        mt.unit_name as unit,
        mm.assets_code as assetsCode,
        ifnull(mt.lease_price,0) as rentPrice,
        CASE
        mt.manage_type
        WHEN 0 THEN
        IFNULL( COUNT( mm.ma_id ), 0 )
        ELSE
        IFNULL( mt.storage_num, 0 )
        END AS num,
        '在库' AS STATUS
        FROM
        ma_type mt
        LEFT JOIN ma_machine mm ON mt.type_id = mm.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_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
        WHERE
        mt.LEVEL = 4
        <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="isStatics != null">
            and mt2.is_statics = #{isStatics}
        </if>
        AND (
        mt.manage_type != 0
        OR (
        mt.manage_type = 0
        AND mm.ma_code IS NOT NULL
        AND mm.ma_status IN (1)
        ))
        GROUP BY
        mt.type_id
    </select>
    <select id="getAllMacheDetails" resultType="com.bonus.material.push.domain.MachineInfoBean">
        ( SELECT
            mt.type_id as typeId,
            mt2.type_name as typeName,
            mt.type_name as typeModelName,
            mt.unit_name as unit,
            mm.assets_code as assetsCode,
            ifnull(mt.lease_price,0) as rentPrice,
              SUM(
                      IFNULL( sai.num, 0 )) AS num,
              '在用' AS STATUS
          FROM
              slt_agreement_info sai
                  LEFT JOIN bm_agreement_info bai ON sai.agreement_id = bai.agreement_id
                  LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
                  LEFT JOIN ma_type mt ON mt.type_id = sai.type_id
                  left join ma_machine mm ON mm.ma_id = sai.ma_id
                  LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_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 sys_dept sd ON sd.dept_id = bp.imp_unit
          WHERE
              sai.STATUS = '0' AND mt.LEVEL = 4
            <if test="unitName != null and unitName !=''">
                AND sd.dept_name = #{unitName}
            </if>
            <if test="deptId != null">
                AND sd.dept_id  = #{deptId}
            </if>
            <if test="isStatics != null">
                and mt2.is_statics = #{isStatics}
            </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>
            AND sai.is_slt = '0'
            AND sai.end_time IS NULL
            AND sai.back_id IS NULL
          GROUP BY
              mt.type_id
        ) UNION ALL
        (
            SELECT
                mt.type_id as typeId,
                mt2.type_name as typeName,
                mt.type_name as typeModelName,
                mt.unit_name as unit,
                mm.assets_code as assetsCode,
                ifnull(mt.lease_price,0) as rentPrice,
                SUM(
                        IFNULL( rad.repair_num, 0 ) - IFNULL( rad.repaired_num, 0 ) - IFNULL( rad.scrap_num, 0 )) AS num,
                '在修' AS STATUS
            FROM
                repair_apply_details rad
                    LEFT JOIN tm_task tt ON rad.task_id = tt.task_id
                    LEFT JOIN tm_task_agreement tta ON tt.task_id = tta.task_id
                    LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
                    LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
                    LEFT JOIN ma_type mt ON mt.type_id = rad.type_id
                    left join ma_machine mm ON mm.ma_id = rad.ma_id
                    LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_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 sys_dept sd ON sd.dept_id = bp.imp_unit
            WHERE
                rad.STATUS = '0' AND mt.LEVEL = 4
            <if test="unitName != null and unitName !=''">
                AND sd.dept_name = #{unitName}
            </if>
            <if test="deptId != null">
                AND sd.dept_id  = #{deptId}
            </if>
            <if test="isStatics != null">
                and mt2.is_statics = #{isStatics}
            </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>
            GROUP BY
                mt.type_id
        ) UNION ALL
        (
        SELECT
        mt.type_id as typeId,
        mt2.type_name as typeName,
        mt.type_name as typeModelName,
        mt.unit_name as unit,
        mm.assets_code as assetsCode,
        ifnull(mt.lease_price,0) as rentPrice,
        CASE
        mt.manage_type
        WHEN 0 THEN
        IFNULL( COUNT( mm.ma_id ), 0 )
        ELSE
        IFNULL( mt.storage_num, 0 )
        END AS num,
        '在库' AS STATUS
        FROM
        ma_type mt
        LEFT JOIN ma_machine mm ON mt.type_id = mm.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_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
        WHERE
        mt.LEVEL = 4
        <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="isStatics != null">
            and mt2.is_statics = #{isStatics}
        </if>
        AND (
        mt.manage_type != 0
        OR (
        mt.manage_type = 0
        AND mm.ma_code IS NOT NULL
        AND mm.ma_status IN (1)
        ))
        GROUP BY
        mt.type_id)
    </select>
    <select id="getPersonNameAndPhone" resultType="com.bonus.system.api.domain.SysUser">
        SELECT
            su.nick_name AS nickName,
            su.phonenumber
        FROM ma_type_keeper mtk
                 LEFT JOIN sys_user su ON mtk.user_id = su.user_id
        WHERE mtk.type_id = #{typeId}
          AND su.phonenumber IS NOT NULL
        ORDER BY mtk.user_id DESC
            LIMIT 1
    </select>
    <select id="getCompletionNotRefundedSub" resultType="com.bonus.material.push.domain.MachineInfoBean">
        select
            bp.pro_center as proCenter,
            count(distinct bp.external_id) num,
            sd.dept_name as deptName,
            sd.dept_id as deptId
        from slt_agreement_info sai
                 left join bm_agreement_info bai on sai.agreement_id = bai.agreement_id
                 left join bm_project bp on bai.project_id = bp.pro_id
                 left join sys_dept sd on bp.imp_unit = sd.dept_id
        where sai.is_slt = 0 and sai.end_time is null and bp.actual_end_date is not null and sd.dept_name = #{deptName}  and bp.pro_center is not null and bp.external_id is not null
        GROUP BY bp.pro_center
    </select>
    <select id="getCompletionNotRefundedDetailsSub" resultType="com.bonus.material.push.domain.MachineInfoBean">
        select
            bp.pro_name as proName,
            bp.pro_center as proCenter,
            bp.actual_end_date as actualEndDate,
            DATEDIFF(NOW(), bp.actual_end_date) as daysDiff,
            sai.num num,
            mt.type_name as typeModelName,
            mt.unit_name as unit,
            sd.dept_name as deptName,
            mt2.type_name as typeName
        from slt_agreement_info sai
                 left join bm_agreement_info bai on sai.agreement_id = bai.agreement_id
                 left join bm_project bp on bai.project_id = bp.pro_id
                 left join sys_dept sd on bp.imp_unit = sd.dept_id
                 left join ma_type mt on sai.type_id = mt.type_id
                 left join ma_type mt2 on mt.parent_id = mt2.type_id
        where sai.status = 0 and bp.actual_end_date is not null
        and bp.pro_center is not null
        <if test="proCenter != null and proCenter !=''">
              and bp.pro_center= #{proCenter}
        </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>
    </select>

</mapper>
