<?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.app.mapper.HoldingLedgerMapper">
    <select id="getHoldingLedger" resultType="com.bonus.sgzb.app.domain.StorageStatus">
        SELECT
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        mt.unit_name AS unit,
        mt.type_id AS typeId,
        IFNULL( mt.num, 0 ) 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( mt.num, 0 ) + IFNULL( subquery1.usNum, 0 ) + IFNULL( subquery2.repairNum, 0 ) + IFNULL( subquery3.repairInputNum, 0 ) AS allNum,
        CASE
        mt.manage_type
        WHEN 0 THEN
        '否' ELSE '是'
        END manageType
        FROM
        ma_type mt
        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'
        AND mt.manage_type = 2
        <if test="typeId != null and typeId != ''">
           and mt.type_id = #{typeId}
        </if>
    </select>
    <select id="getWholeSetDetails" resultType="com.bonus.sgzb.base.api.domain.MachinePart">
        SELECT
            mt2.type_name AS typeModelName,
            mt2.type_id AS typeId,
            mt3.type_name AS typeName,
            aa.part_num AS partNum,
            mt2.unit_name AS unitName
        FROM
            ma_type mt2
                LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id
                INNER JOIN (
                SELECT
                    mws.type_id,
                    mws.part_num
                FROM
                    ma_type mt
                        LEFT JOIN ma_whole_set mws ON mt.type_id = mws.parent_id
                WHERE
                    mt.type_id = #{typeId}
            ) aa ON aa.type_id = mt2.type_id
    </select>
    <select id="getHoldingLedgerUserNum" resultType="com.bonus.sgzb.app.domain.StorageStatus">
        SELECT
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        mt.unit_name AS unit,
        mt.type_id AS typeId,
        IFNULL( mt.num, 0 ) 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( mt.num, 0 ) + IFNULL( subquery1.usNum, 0 ) + IFNULL( subquery2.repairNum, 0 ) + IFNULL( subquery3.repairInputNum, 0 ) AS allNum,
        CASE
        mt.manage_type
        WHEN 0 THEN
        '否' ELSE '是'
        END manageType
        FROM
        ma_type mt
        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'
        AND mt.type_id = #{typeId}
    </select>
    <select id="getMaSupplier" resultType="java.lang.String">
        SELECT
            msi.supplier as supplier
        FROM
            ma_type mt
                LEFT JOIN purchase_check_details pcd ON mt.type_id = pcd.type_id
                LEFT JOIN ma_supplier_info msi ON pcd.supplier_id = msi.supplier_id
        WHERE
            mt.type_id = #{typeId}
        ORDER BY pcd.input_time DESC
            LIMIT 1
    </select>

</mapper>