<?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.EquipmentSpecificRelationMapper">

    <resultMap id="associatedEquipmentList" type="com.yeejoin.equipmanage.common.entity.vo.AssociatedEquipmentVO">
        <result column="id" property="id"></result>
        <result column="fullqrCode" property="fullqrCode"></result>
        <result column="amount" property="amount"></result>
        <result column="equipmentName" property="equipmentName"></result>
        <result column="unitName" property="unitName"></result>
        <result property="qrCode" column="qr_code"></result>
        <result property="standard" column="standard"></result>
        <result property="img" column="img"></result>
        <result property="warehouseStructureName" column="full_name"></result>
        <result property="systemName" column="systemName"></result>
        <result property="amount" column="amount"></result>
        <result property="manufacturerName" column="manufacturerName"></result>
        <result property="status" column="status"></result>
        <result property="type" column="type"></result>
        <result property="code" column="code"></result>
        <result property="iotCode" column="iotCode"></result>
        <result property="associateType" column="associateType"></result>
        <result property="eqpSpecificId" column="eqpSpecificId"></result>
    </resultMap>
    <select id="getAssociatedEquipmentPageList" resultMap="associatedEquipmentList">
        SELECT
        esr.type associateType,
        ed.id,
        es.qr_code,
        es.id eqpSpecificId,
        CONCAT( '01#', es.qr_code ) fullqrCode,
        ed.standard,
        eqp.img,
        ed.name equipmentName,
        ws.full_name,
        u.`name` unitName,
        (select group_concat(ffs.name) from  `f_fire_fighting_system` ffs where ffs.id = es.system_id) as systemName,
        sd.amount,
        'equipment' AS type,
        mi.`name` manufacturerName,
        es.code,
        es.iot_code iotCode,
        CASE
        sd.`status`
        WHEN '1' THEN
        '在位'
        WHEN '2' THEN
        '执勤'
        WHEN '3' THEN
        '维修'
        WHEN '6' THEN
        '退役'
        WHEN '7' THEN
        '报废'
        WHEN '10' THEN
        '车载'
        WHEN '11' THEN
        '损耗' ELSE '未入库'
        END AS STATUS
        FROM
        wl_equipment_specific_relation esr
        LEFT JOIN wl_equipment_specific es ON es.id = esr.relation_equipment_specific_id
        LEFT JOIN wl_equipment_detail ed ON ed.id = es.equipment_detail_id
        LEFT JOIN wl_stock_detail sd ON sd.equipment_detail_id = es.equipment_detail_id
        LEFT JOIN wl_warehouse_structure ws ON ws.id = sd.warehouse_structure_id
        LEFT JOIN wl_equipment eqp ON eqp.id = ed.equipment_id
        LEFT JOIN wl_unit u ON u.id = eqp.unit_id
        LEFT JOIN wl_manufacturer_info mi ON mi.id = ed.manufacturer_id
        WHERE 1=1
        <if test="equipId != null and equipId != ''">
            AND esr.equipment_specific_id = #{equipId}
        </if>
        <if test="type != null and type != -1">
            AND esr.type = #{type}
        </if>
        ORDER BY
        esr.id
    </select>
    <select id="getAssociatedEqpList"
            resultType="com.yeejoin.equipmanage.common.entity.vo.EquipmentDetailVO">
        SELECT
        ed.id eqpId,
        ed.name eqpName,
        ed.code,
        es.id eqpSpecificId
        FROM
        wl_equipment_specific_relation esr
        LEFT JOIN wl_equipment_specific es ON es.id = esr.relation_equipment_specific_id
        LEFT JOIN wl_equipment_detail ed ON ed.id = es.equipment_detail_id
        WHERE 1=1
        <if test="eqpSpecificId != null and eqpSpecificId != ''">
            AND esr.equipment_specific_id = #{eqpSpecificId}
        </if>
        <if test="type != null and type != -1">
            AND esr.type = #{type}
        </if>
        ORDER BY
        esr.id
    </select>
    <select id="queryByRelatedEqpSpecIds" resultType="java.lang.Long">
        SELECT id
        FROM wl_equipment_specific_relation
        WHERE relation_equipment_specific_id = #{eqpSpecificId}
          and equipment_specific_id IN (SELECT relation_equipment_specific_id
                                        FROM wl_equipment_specific_relation
                                        WHERE equipment_specific_id = #{eqpSpecificId})
    </select>
    <select id="getEqpDetailCategoryIdAndName"
            resultType="com.yeejoin.equipmanage.common.entity.vo.EquipmentDetailVO">
        SELECT
        ed.id eqpId,
        ed.NAME eqpName,
        ed.CODE,
        es.id eqpSpecificId
        FROM
        wl_equipment_specific es
        LEFT JOIN wl_equipment_detail ed ON es.equipment_detail_id = ed.id
        LEFT JOIN wl_equipment e ON e.id = ed.equipment_id
        WHERE
        1 = 1
        <if test="eqpName != null and eqpName != ''">
            AND ed.name LIKE CONCAT('%', #{eqpName}, '%')
        </if>
        <if test="categoryIdList != null and categoryIdList.size() > 0">
            AND e.category_id in
            <foreach collection="categoryIdList" item="categoryId" open="(" separator="," close=")">
                #{categoryId}
            </foreach>
        </if>
    </select>
</mapper>
