<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bonus.sgzb.material.mapper.StorageStatusMapper">

    <select id="getStorageStatusList" resultType="com.bonus.sgzb.material.domain.StorageStatus">
        SELECT
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        mt.unit_name AS unit,
        CASE
        mt.manage_type
        WHEN 0 THEN
        IFNULL( subquery0.num, 0 ) ELSE IFNULL( mt.num, 0 )
        END AS num,
        IFNULL( subquery1.usNum, 0 ) AS usNum,
        IFNULL( subquery2.repairNum, 0 ) AS repairNum,
        IFNULL( subquery3.repairInputNum, 0 ) AS repairInputNum,
        IFNULL( subquery4.inputNum, 0 ) AS inputNum,
        IFNULL( subquery5.scrapNum, 0 ) AS scrapNum,
        IFNULL( subquery6.scrapNum, 0 ) AS preScrapNum,
        IFNULL( subquery7.preOutNum, 0 ) AS preOutNum,
        CASE
        mt.manage_type
        WHEN 0 THEN
        IFNULL( subquery0.num, 0 )+ IFNULL( subquery1.usNum, 0 ) + IFNULL( subquery2.repairNum, 0 ) + IFNULL( subquery3.repairInputNum, 0 ) ELSE IFNULL( mt.num, 0 )+ IFNULL( subquery1.usNum, 0 ) + IFNULL( subquery2.repairNum, 0 ) + IFNULL( subquery3.repairInputNum, 0 )
        END AS allNum,
        CASE
        mt.manage_type
        WHEN 0 THEN
        '否' ELSE '是'
        END manageType
        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
        LEFT JOIN (
        SELECT
        subquery1.type_id,
        subquery1.typeName,
        subquery1.typeModelName,
        IFNULL( subquery1.outNum, 0 ) AS outNum,
        IFNULL( subquery2.backNum, 0 ) AS backNum,
        CASE

        WHEN IFNULL( subquery1.outNum, 0 ) - IFNULL( subquery2.backNum, 0 ) > 0 THEN
        IFNULL( subquery1.outNum, 0 ) - IFNULL( subquery2.backNum, 0 ) ELSE 0
        END AS usNum
        FROM
        (
        SELECT
        mt.type_id,
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        SUM(
        IFNULL( lod.out_num, 0 )) AS outNum
        FROM
        lease_out_details lod
        LEFT JOIN ma_type mt ON mt.type_id = lod.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        GROUP BY
        mt.type_id
        ) AS subquery1
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        SUM(
        IFNULL( bcd.back_num, 0 )) backNum
        FROM
        back_check_details bcd
        LEFT JOIN ma_type mt ON mt.type_id = bcd.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        LEFT JOIN ma_machine mm ON mm.ma_id = bcd.ma_id
        GROUP BY
        mt.type_id
        ) AS subquery2 ON subquery1.type_id = subquery2.type_id
        ) AS subquery1 ON mt.type_id = subquery1.type_id
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        SUM(
        IFNULL( rad.repair_num, 0 ) - IFNULL( rad.repaired_num, 0 ) - IFNULL( rad.scrap_num, 0 )) AS repairNum
        FROM
        repair_apply_details rad
        LEFT JOIN ma_type mt ON mt.type_id = rad.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        WHERE
        IFNULL( rad.repair_num, 0 ) - IFNULL( rad.repaired_num, 0 ) - IFNULL( rad.scrap_num, 0 ) > 0
        GROUP BY
        mt.type_id
        ) AS subquery2 ON subquery2.type_id = mt.type_id
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        SUM(
        IFNULL( rid.repair_num, 0 ) - IFNULL( rid.input_num, 0 )) AS repairInputNum
        FROM
        repair_input_details rid
        LEFT JOIN ma_type mt ON mt.type_id = rid.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        WHERE
        IFNULL( rid.repair_num, 0 ) - IFNULL( rid.input_num, 0 ) > 0
        GROUP BY
        mt.type_id
        ) AS subquery3 ON subquery3.type_id = mt.type_id
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        SUM(
        IFNULL( pcd.check_num, 0 ) - IFNULL( pcd.input_num, 0 )) AS inputNum
        FROM
        purchase_check_details pcd
        LEFT JOIN ma_type mt ON mt.type_id = pcd.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        WHERE
        IFNULL( pcd.check_num, 0 ) - IFNULL( pcd.input_num, 0 ) > 0
        GROUP BY
        mt.type_id
        ) AS subquery4 ON subquery4.type_id = mt.type_id
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        sum(
        IFNULL( sad.scrap_num, 0 )) AS scrapNum
        FROM
        scrap_apply_details sad
        LEFT JOIN ma_type mt ON mt.type_id = sad.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        WHERE
        sad.STATUS = 1
        GROUP BY
        mt.type_id
        ) AS subquery5 ON subquery5.type_id = mt.type_id
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        sum(
        IFNULL( sad.scrap_num, 0 )) AS scrapNum
        FROM
        scrap_apply_details sad
        LEFT JOIN ma_type mt ON mt.type_id = sad.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        WHERE
        sad.STATUS = 0
        GROUP BY
        mt.type_id
        ) AS subquery6 ON subquery6.type_id = mt.type_id
        LEFT JOIN (
        SELECT
        sum(
        IFNULL( lad.pre_num, 0 ) - IFNULL( lad.al_num, 0 )) AS preOutNum,
        lad.type_id
        FROM
        lease_apply_details lad
        LEFT JOIN lease_apply_info lai ON lad.parennt_id = lai.id
        LEFT JOIN tm_task tt ON tt.task_id = lai.task_id
        WHERE
        tt.task_status IN ( 30, 31, 32, 33, 34, 117 )
        GROUP BY
        lad.type_id
        ) AS subquery7 ON subquery7.type_id = mt.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        WHERE
        mt.`level` = 4
        AND mt.del_flag = '0'
        <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>