<?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.ScrapMapper">
    <select id="getImgById" resultType="String">
        <if test="type != 2">
            select
            eq.img
            from
            wl_scrap_detail as det
            left join wl_stock_detail as stdet on det.stock_detail_id =stdet .id
            left join wl_equipment_detail as eqdet on stdet.equipment_detail_id = eqdet .id
            left join wl_equipment as eq on eqdet.equipment_id =eq.id
            where det.scrap_id =#{id}
        </if>
        <if test="type == 2">
             select
            eq.img
            from
            wl_scrap_detail as det
            left join wl_car as wc on det.car_id =wc.id
            left join wl_equipment as eq on wc.equipment_id =eq.id
            where det.scrap_id =#{id}
        </if>
        limit 1
    </select>

    <select id="getOneByQrcode" resultType="java.lang.Integer">
        select count(1)
        from
            wl_scrap_detail as wsd
        left join wl_stock_detail as stdet on wsd.stock_detail_id = stdet.id
        where
        stdet.qr_code = #{qrCode}
        and
        stdet.status = 7
    </select>

    <select id="getCarByQrcode" resultType="java.lang.Integer">
        select count(1)
        from
            wl_scrap_detail as wsd
        left join wl_car as wc on wsd.car_id = wc.id
        where
        wc.qr_code = #{qrCode}
        and
        wc.car_state = 7
    </select>
    <select id="geteqTypeAndAmount" resultType="java.util.Map">
        select
                wsd.amount as amount ,
                substring( wed.code,1,1) as eqType,
                IFNULL(wu.name, '') as unitName
                from
                wl_scrap_detail as wsd
                left join wl_stock_detail as wskd on wskd.id =  wsd.stock_detail_id
                left join wl_equipment_detail as wed on wed.id = wskd.equipment_detail_id
                left join wl_equipment as we on we.id = wed.equipment_id
                left join wl_unit as wu on wu.id = we.unit_id
                where
                wsd.scrap_id = #{id}
                and wsd.car_id is null

                union all

                select
                wsd.amount,
                '2' as code,
                IFNULL(wu.name, '') as unitName
                from
                wl_scrap_detail as wsd
                left join wl_car as wc on wc.id = wsd.car_id
                left join wl_equipment as we on we.id = wc.equipment_id
                left join wl_unit as wu on wu.id = we.unit_id
                where
                wsd.scrap_id = #{id}
                and wsd.car_id is not null
    </select>
</mapper>
