<?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.yeejoin.equipmanage.mapper.StockBillDetailMapper">

    <resultMap id="StockBillDetail" type="com.yeejoin.equipmanage.common.entity.vo.StockBillDetailVO">
        <result column="standard" property="standard"></result>
        <result column="single_equip_code" property="singleEquipCode"></result>
        <result column="amount" property="amount"></result>
        <result column="equipment_name" property="equipmentName"></result>
        <result column="equipment_type_name" property="equipmentTypeName"></result>
        <result column="manufacturer_name" property="manufacturerName"></result>
        <result column="unit_name" property="unitName"></result>
        <result column="org_code" property="orgCode"></result>
        <result column="type" property="type"></result>
    </resultMap>

    <select id="selectStockBillDetailPage"
            resultMap="StockBillDetail">

        <if test='type == "1"'>
            select
            wled.standard,
            wlst.single_equip_code,
            wlst.amount,
            wled.name equipment_name,
            wled.org_code,
            wlec.name equipment_type_name,
            wlmi.name manufacturer_name,
            wlu.name unit_name
            from wl_stock_bill_detail wlst
            left join wl_equipment_detail wled on wlst.equipment_detail_id = wled.id
            left join wl_equipment wleu on wled.equipment_id = wleu.id
            left join wl_unit wlu on wleu.unit_id = wlu.id
            left join wl_equipment_category wlec on wleu.category_id = wlec.id
            left join wl_manufacturer_info wlmi on wled.manufacturer_id = wlmi.id
            where wlst.stock_bill_id=#{id}
            <if test="equipmentName != null and equipmentName != ''">
                and wled.name like concat('%',#{equipmentName},'%')
            </if>
            <if test="manufacturerName != null and manufacturerName != ''">
                and wlmi.name like concat('%',#{manufacturerName},'%')
            </if>
            <if test="equipmentTypeName != null and equipmentTypeName != ''">
                and wlec.name like concat('%',#{equipmentTypeName},'%')
            </if>
        </if>
        <if test='type == "2"'>
            SELECT * FROM (
            SELECT
            wlm.repair_id,
            wlcr.standard AS standard,
            wlcr.qr_code AS single_equip_code,
            wlcr.NAME AS equipment_name,
            1 AS amount,
            wlcr.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name
            FROM
            wl_repair_detail wlm
            LEFT JOIN wl_car wlcr ON wlcr.id = wlm.car_id
            LEFT JOIN wl_equipment wleu ON wlcr.equipment_id = wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wlcr.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            WHERE
            wlm.car_id IS NOT NULL
            <if test="equipmentName != null and equipmentName != ''">
                and wlcr.name like concat('%',#{equipmentName},'%')
            </if>
            <if test="manufacturerName != null and manufacturerName != ''">
                and wlmi.name like concat('%',#{manufacturerName},'%')
            </if>
            <if test="equipmentTypeName != null and equipmentTypeName != ''">
                and wlec.name like concat('%',#{equipmentTypeName},'%')
            </if>
            UNION ALL
            SELECT
            wlm.repair_id ,
            wled.standard as standard,
            wleq.qr_code as single_equip_code,
            wled.name AS equipment_name,
            wlm.amount,
            wlsd.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name
            FROM
            wl_repair_detail wlm
            LEFT JOIN wl_stock_detail wlsd ON wlsd.id = wlm.stock_detail_id
            LEFT JOIN wl_equipment_detail wled ON wled.id = wlsd.equipment_detail_id
            left join wl_equipment_specific wleq on wlsd.qr_code = wleq.qr_code
            left join wl_equipment wleu on wled.equipment_id=wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wled.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            where wlm.stock_detail_id IS NOT NULL
            <if test="equipmentName != null and equipmentName != ''">
                and wled.name like concat('%',#{equipmentName},'%')
            </if>
            <if test="manufacturerName != null and manufacturerName != ''">
                and wlmi.name like concat('%',#{manufacturerName},'%')
            </if>
            <if test="equipmentTypeName != null and equipmentTypeName != ''">
                and wlec.name like concat('%',#{equipmentTypeName},'%')
            </if>
            ) detail WHERE detail.repair_id=#{id}

        </if>
        <if test='type == "3"'>
            SELECT * FROM (
            SELECT
            wlm.maintain_id,
            wlcr.standard AS standard,
            wlcr.qr_code AS single_equip_code,
            wlcr.NAME AS equipment_name,
            1 AS amount,
            wlcr.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name
            FROM
            wl_maintain_detail wlm
            LEFT JOIN wl_car wlcr ON wlcr.id = wlm.car_id
            LEFT JOIN wl_equipment wleu ON wlcr.equipment_id = wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wlcr.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            WHERE
            wlm.car_id IS NOT NULL
            <if test="equipmentName != null and equipmentName != ''">
                and wlcr.name like concat('%',#{equipmentName},'%')
            </if>
            <if test="manufacturerName != null and manufacturerName != ''">
                and wlmi.name like concat('%',#{manufacturerName},'%')
            </if>
            <if test="equipmentTypeName != null and equipmentTypeName != ''">
                and wlec.name like concat('%',#{equipmentTypeName},'%')
            </if>
            UNION ALL
            SELECT
            wlm.maintain_id,
            wled.standard,
            wleq.qr_code as single_equip_code,
            wled.name AS equipment_name,
            wlsd.amount,
            wlsd.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name
            FROM
            wl_maintain_detail wlm
            LEFT JOIN wl_stock_detail wlsd ON wlsd.id = wlm.stock_detail_id
            LEFT JOIN wl_equipment_detail wled ON wled.id = wlsd.equipment_detail_id
            left join wl_equipment_specific wleq on wlsd.qr_code = wleq.qr_code
            left join wl_equipment wleu on wled.equipment_id=wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wled.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            where wlm.stock_detail_id IS NOT NULL
            <if test="equipmentName != null and equipmentName != ''">
                and wled.name like concat('%',#{equipmentName},'%')
            </if>
            <if test="manufacturerName != null and manufacturerName != ''">
                and wlmi.name like concat('%',#{manufacturerName},'%')
            </if>
            <if test="equipmentTypeName != null and equipmentTypeName != ''">
                and wlec.name like concat('%',#{equipmentTypeName},'%')
            </if>
            ) detail WHERE detail.maintain_id=#{id}

        </if>
        <if test='type == "4"'>
            SELECT
            wled.standard,
            wleq.qr_code as single_equip_code,
            wled.name AS equipment_name,
            wlm.amount,
            wlsd.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name
            FROM
            wl_wastage_bill_detail wlm
            LEFT JOIN wl_stock_detail wlsd ON wlsd.id = wlm.stock_detail_id
            LEFT JOIN wl_equipment_detail wled ON wled.id = wlsd.equipment_detail_id
            left join wl_equipment_specific wleq on wlsd.qr_code = wleq.qr_code
            left join wl_equipment wleu on wled.equipment_id=wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wled.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            where wlm.wastage_bill_id=#{id}
            <if test="equipmentName != null and equipmentName != ''">
                and wled.name like concat('%',#{equipmentName},'%')
            </if>
            <if test="manufacturerName != null and manufacturerName != ''">
                and wlmi.name like concat('%',#{manufacturerName},'%')
            </if>
            <if test="equipmentTypeName != null and equipmentTypeName != ''">
                and wlec.name like concat('%',#{equipmentTypeName},'%')
            </if>
        </if>
        <if test='type == "5"'>
            SELECT * FROM (
            SELECT
            wlm.scrap_id,
            wlcr.standard AS standard,
            wlcr.qr_code AS single_equip_code,
            wlcr.NAME AS equipment_name,
            1 AS amount,
            wlcr.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name
            FROM
            wl_scrap_detail wlm
            LEFT JOIN wl_car wlcr ON wlcr.id = wlm.car_id
            LEFT JOIN wl_equipment wleu ON wlcr.equipment_id = wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wlcr.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            WHERE
            wlm.car_id IS NOT NULL
            <if test="equipmentName != null and equipmentName != ''">
                and wlcr.name like concat('%',#{equipmentName},'%')
            </if>
            <if test="manufacturerName != null and manufacturerName != ''">
                and wlmi.name like concat('%',#{manufacturerName},'%')
            </if>
            <if test="equipmentTypeName != null and equipmentTypeName != ''">
                and wlec.name like concat('%',#{equipmentTypeName},'%')
            </if>
            UNION ALL
            SELECT
            wlm.scrap_id,
            wled.standard,
            wleq.qr_code as single_equip_code,
            wled.name AS equipment_name,
            wlm.amount,
            wlsd.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name
            FROM
            wl_scrap_detail wlm
            LEFT JOIN wl_stock_detail wlsd ON wlsd.id = wlm.stock_detail_id
            LEFT JOIN wl_equipment_detail wled ON wled.id = wlsd.equipment_detail_id
            left join wl_equipment_specific wleq on wlsd.qr_code = wleq.qr_code
            left join wl_equipment wleu on wled.equipment_id=wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wled.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            where wlm.stock_detail_id IS NOT NULL
            <if test="equipmentName != null and equipmentName != ''">
                and wled.name like concat('%',#{equipmentName},'%')
            </if>
            <if test="manufacturerName != null and manufacturerName != ''">
                and wlmi.name like concat('%',#{manufacturerName},'%')
            </if>
            <if test="equipmentTypeName != null and equipmentTypeName != ''">
                and wlec.name like concat('%',#{equipmentTypeName},'%')
            </if>
            ) detail WHERE detail.scrap_id=#{id}

        </if>

    </select>

    <select id="selectStockBillDetailList" resultMap="StockBillDetail">
        <if test='type == "1"'>
            select
            wled.standard,
            wlst.single_equip_code,
            wlst.amount,
            wled.name equipment_name,
            wled.org_code,
            wlec.name equipment_type_name,
            wlmi.name manufacturer_name,
            wlu.name unit_name,
            wleu.code `type`
            from wl_stock_bill_detail wlst
            left join wl_equipment_detail wled on wlst.equipment_detail_id = wled.id
            left join wl_equipment wleu on wled.equipment_id = wleu.id
            left join wl_unit wlu on wleu.unit_id = wlu.id
            left join wl_equipment_category wlec on wleu.category_id = wlec.id
            left join wl_manufacturer_info wlmi on wled.manufacturer_id = wlmi.id
            where wlst.stock_bill_id=#{id}

        </if>
        <if test='type == "2"'>
            select * from (
            SELECT
            wlm.repair_id,
            wlcr.standard AS standard,
            wlcr.qr_code AS single_equip_code,
            wlcr.NAME AS equipment_name,
            1 AS amount,
            wlcr.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name,
            wleu.code as `type`
            FROM
            wl_repair_detail wlm
            LEFT JOIN wl_car wlcr ON wlcr.id = wlm.car_id
            LEFT JOIN wl_equipment wleu ON wlcr.equipment_id = wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wlcr.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            WHERE
            wlm.car_id IS NOT NULL

            UNION ALL
            SELECT
            wlm.repair_id,
            wled.standard,
            wleq.qr_code as single_equip_code,
            wled.name AS equipment_name,
            wlm.amount,
            wlsd.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name,
            wleu.code as `type`
            FROM
            wl_repair_detail wlm
            LEFT JOIN wl_stock_detail wlsd ON wlsd.id = wlm.stock_detail_id
            LEFT JOIN wl_equipment_detail wled ON wled.id = wlsd.equipment_detail_id
            left join wl_equipment_specific wleq on wlsd.qr_code = wleq.qr_code
            left join wl_equipment wleu on wled.equipment_id=wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wled.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            where wlm.stock_detail_id IS NOT NULL
            )  detail where detail.repair_id=#{id}

        </if>
        <if test='type == "3"'>
            SELECT * FROM (
            SELECT
            wlm.maintain_id,
            wlcr.standard AS standard,
            wlcr.qr_code AS single_equip_code,
            wlcr.NAME AS equipment_name,
            1 AS amount,
            wlcr.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name,
            wleu.code as `type`
            FROM
            wl_maintain_detail wlm
            LEFT JOIN wl_car wlcr ON wlcr.id = wlm.car_id
            LEFT JOIN wl_equipment wleu ON wlcr.equipment_id = wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wlcr.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            WHERE
            wlm.car_id IS NOT NULL

            UNION ALL
            SELECT
            wlm.maintain_id,
            wled.standard,
            wleq.qr_code as single_equip_code,
            wled.name AS equipment_name,
            wlsd.amount,
            wlsd.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name,
            wleu.code as `type`
            FROM
            wl_maintain_detail wlm
            LEFT JOIN wl_stock_detail wlsd ON wlsd.id = wlm.stock_detail_id
            LEFT JOIN wl_equipment_detail wled ON wled.id = wlsd.equipment_detail_id
            left join wl_equipment_specific wleq on wlsd.qr_code = wleq.qr_code
            left join wl_equipment wleu on wled.equipment_id=wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wled.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            where wlm.stock_detail_id IS NOT NULL
            ) detail WHERE detail.maintain_id=#{id}

        </if>
        <if test='type == "4"'>
            SELECT
            wled.standard,
            wleq.qr_code as single_equip_code,
            wled.name AS equipment_name,
            wlsd.amount,
            wlsd.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name,
            wleu.code as `type`
            FROM
            wl_wastage_bill_detail wlm
            LEFT JOIN wl_stock_detail wlsd ON wlsd.id = wlm.stock_detail_id
            LEFT JOIN wl_equipment_detail wled ON wled.id = wlsd.equipment_detail_id
            left join wl_equipment_specific wleq on wlsd.qr_code = wleq.qr_code
            left join wl_equipment wleu on wled.equipment_id=wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wled.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            where wlm.wastage_bill_id=#{id}
        </if>
        <if test='type == "5"'>
            SELECT * FROM(
            SELECT
            wlm.scrap_id,
            wlcr.standard AS standard,
            wlcr.qr_code AS single_equip_code,
            wlcr.NAME AS equipment_name,
            1 AS amount,
            wlcr.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name,
            wleu.code as `type`
            FROM
            wl_scrap_detail wlm
            LEFT JOIN wl_car wlcr ON wlcr.id = wlm.car_id
            LEFT JOIN wl_equipment wleu ON wlcr.equipment_id = wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wlcr.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            WHERE
            wlm.car_id IS NOT NULL

            UNION ALL
            SELECT
            wlm.scrap_id,
            wled.standard,
            wleq.qr_code as single_equip_code,
            wled.name AS equipment_name,
            wlsd.amount,
            wlsd.org_code,
            wlec.NAME AS equipment_type_name,
            wlmi.NAME AS manufacturer_name,
            wlu.NAME AS unit_name,
            wleu.code as `type`
            FROM
            wl_scrap_detail wlm
            LEFT JOIN wl_stock_detail wlsd ON wlsd.id = wlm.stock_detail_id
            LEFT JOIN wl_equipment_detail wled ON wled.id = wlsd.equipment_detail_id
            left join wl_equipment_specific wleq on wlsd.qr_code = wleq.qr_code
            left join wl_equipment wleu on wled.equipment_id=wleu.id
            LEFT JOIN wl_equipment_category wlec ON wleu.category_id = wlec.id
            LEFT JOIN wl_manufacturer_info wlmi ON wled.manufacturer_id = wlmi.id
            LEFT JOIN wl_unit wlu ON wleu.unit_id = wlu.id
            where wlm.stock_detail_id IS NOT NULL
            ) detail WHERE detail.scrap_id=#{id}

        </if>
    </select>
</mapper>
