<?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,
               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 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>