<?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.EquipmentMapper">
    <resultMap id="EquipmentAppMessage" type="com.yeejoin.equipmanage.common.entity.vo.EquipmentBaseAppVO">
        <result column="equip_id" property="equipId"></result>
        <result column="equipment_detail_id" property="equipDetailId"></result>

        <result column="standard" property="standard"></result>
        <result column="country" property="country"></result>
        <result column="manufacturer_name" property="manufacturerName"></result>
        <result column="brand" property="brand"></result>
        <result column="category_name" property="categoryName"></result>
        <result column="description" property="description"></result>
        <result column="create_date" property="createTime"></result>
        <result column="manufacturer_id" property="manufacturerId"></result>
        <result column="equip_name" property="equipName"></result>
        <result column="equip_code" property="equipCode"></result>
        <result column="expiry_date" property="expiryDate"></result>
        <result column="maintenance_cycle" property="maintenanceCycle"></result>
        <result column="full_name" property="description"></result>
        <result column="name" property="sysName"></result>
    </resultMap>

    <resultMap id="ImportantEquipmentVO" type="com.yeejoin.equipmanage.common.entity.vo.ImportantEquipmentVO">
        <result column="id" property="id"></result>
        <result column="name" property="name"></result>
        <result column="code" property="code"></result>
        <result column="address" property="address"></result>
        <result column="charge_dept_id" property="chargeDeptId"></result>
        <result column="charge_user_id" property="chargeUserId"></result>
        <result column="fire_station" property="fireStation"></result>
        <result column="room" property="room"></result>
        <result column="risk_source" property="riskSource"></result>
        <result column="building" property="building"></result>
        <result column="is_indoor" property="isIndoor"></result>
        <collection property="images" select="com.yeejoin.equipmanage.mapper.EquipmentMapper.getPreplanPicture" column="id">
            <result column="type" property="type"></result>
            <result column="picture" property="picture"></result>
        </collection>
    </resultMap>

    <resultMap id="PreplanPictureVO" type="com.yeejoin.equipmanage.common.entity.vo.PreplanPictureVO">
        <result column="type" property="type"></result>
        <result column="picture" property="picture"></result>
    </resultMap>

    <resultMap id="DownloadEquipmentData" type="com.yeejoin.equipmanage.common.entity.vo.DownloadEquipmentDataVO">
        <result column="equipment_specific_id" property="equipmentSpecificId"></result>
        <result column="equipment_specific_qr_code" property="equipmentSpecificQrCode"></result>
        <result column="equipment_specific_single" property="equipmentSpecificSingle"></result>
        <result column="equipment_specific_code" property="equipmentSpecificCode"></result>
        <result column="equipment_detail_name" property="equipmentDetailName"></result>
        <result column="equipment_detail_perf_quota_str" property="equipmentDetailPerfQuotaStr"></result>
        <result column="warehouse_structure_name" property="warehouseStructureName"></result>
        <result column="description" property="description"></result>
        <result column="equipment_pref_quota" property="equipmentPrefQuota"></result>
        <result column="stock_detail_amount" property="stockDetailAmount"></result>
        <result column="stock_detail_warehouse_structure_id" property="stockDetailWarehouseStructureId"></result>
        <result column="stock_detail_warehouse_id" property="stockDetailWarehouseId"></result>
        <result column="stock_detail_status" property="stockDetailStatus"></result>
        <result column="unit_id" property="unitId"></result>
        <result column="unit_name" property="unitName"></result>
        <result column="category_code" property="categoryCode"></result>
        <result column="stock_detail_id" property="stockDetailId"></result>
    </resultMap>

    <select id="getEquipmentList" resultType="Map">
        SELECT
        cate.CODE AS categoryCode,
        cate.NAME AS categoryName,
        ( CASE WHEN LEFT ( cate.CODE, 1 ) = '2' THEN 'car' ELSE 'equip' END ) AS type,
        cate.is_consumptive isConsumptive,
        eq.category_id categoryId,
        eq.code,
        eq.name,
        eq.remark,
        eq.id,
        eq.is_iot isIot,
        unit.NAME AS unitName
        FROM
        wl_equipment eq
        LEFT JOIN wl_unit unit ON unit.id = eq.unit_id
        LEFT JOIN wl_equipment_category cate ON cate.id = eq.category_id
        <where>
            <if test="whereConditionStr != null and whereConditionStr != '' ">
                eq.name like CONCAT("%",#{whereConditionStr},"%" )
            </if>
            <if test="isIot != null and isIot != '' ">
                eq.is_iot = #{isIot}
            </if>
        </where>
        ORDER BY eq.id DESC
        LIMIT #{start},#{page}
    </select>

    <select id="getEquipmentCount" resultType="Integer">
        SELECT
        COUNT(1)
        FROM
        wl_equipment eq
        LEFT JOIN wl_unit unit ON unit.id = eq.unit_id
        LEFT JOIN wl_equipment_category cate ON cate.id = eq.category_id
        <where>
            <if test="whereConditionStr != null and whereConditionStr != '' ">
                eq.name like CONCAT("%",#{whereConditionStr},"%" )
            </if>
        </where>
    </select>

    <select id="getEquipmentAppMessage" resultMap="EquipmentAppMessage">
		SELECT DISTINCT
		    wle.id as equip_id,
		    wleq.id as equipmentSpecificId,
		    wleq.equipment_detail_id,
		    wled.name as equip_name,
		    wled.code as equip_code,
		    wled.expiry_date,
			wled.standard,
			wlsd.name AS country,
			wled.brand,
			wleq.qr_code AS qrCode,
			wlec.NAME AS category_name,
			wlsbd.description,
			wlsbd.create_date,
			wled.manufacturer_id,
			wled.maintenance_cycle,
			stru.full_name,
			(select group_concat(sys.name) from `f_fire_fighting_system`  sys where find_in_set(sys.id, wleq.system_id)) as name
		FROM
		    wl_equipment_specific wleq
		    left join wl_equipment_detail wled on wleq.equipment_detail_id = wled.id
			LEFT JOIN wl_equipment wle ON wle.id = wled.equipment_id
			LEFT JOIN wl_manufacturer_info wlai ON wled.manufacturer_id = wlai.id
			LEFT JOIN wl_equipment_category wlec ON wle.category_id = wlec.id
			left join wl_stock_detail wlsbd on wleq.qr_code = wlsbd.qr_code
			LEFT JOIN wl_system_dic wlsd ON wlsd.id = wled.country
			left join wl_warehouse_structure as stru on wlsbd.warehouse_structure_id = stru.id
			where wleq.qr_code=#{qrCode}
    </select>

    <select id="getImportantEquipmentList" resultMap="ImportantEquipmentVO">
        select
        fe.id,
        fe.name,
        fe.code,
        fe.address,
        wlfi.field_value fire_station,
        wlfi1.name risk_source,
        fe.charge_dept_id,
        fe.room,
        fe.building,
        fe.is_indoor,
        fe.charge_user_id
    from f_equipment fe
     left join wl_form_instance wlfi on fe.fire_station_id=wlfi.instance_id
        left join wl_warehouse_structure wlfi1 on fe.risk_source_id = wlfi1.id
     where fe.id in
        <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="getPreplanPicture" resultMap="PreplanPictureVO">
        select
        type,picture
        from f_preplan_picture where equipment_id = #{id}
    </select>

    <select id="getDownloadEquipmentData" resultMap="DownloadEquipmentData">
          SELECT
	wles.id AS equipment_specific_id,
	wles.equipment_detail_id AS equipment_specific_equipment_detail_id,
	wles.qr_code AS equipment_specific_qr_code,
	wles.single AS equipment_specific_single,
	wles.CODE AS equipment_specific_code,
	wled.NAME AS equipment_detail_name,
	wled.perf_quota_str AS equipment_detail_perf_quota_str,
	wle.id AS equipmentId,
	wle.NAME AS equipment_name,
	wle.CODE AS equipment_code,
	wle.pref_quota AS equipment_pref_quota,
	wlsd.amount AS stock_detail_amount,
	wlsd.warehouse_structure_id AS stock_detail_warehouse_structure_id,
	wlsd.warehouse_id AS stock_detail_warehouse_id,
	wlsd.STATUS AS stock_detail_status,
	wlsd.id as stock_detail_id,
	wlu.id AS unit_id,
	wlsd.description,
	wlws.name as warehouse_structure_name,
	wlu.NAME AS unit_name,
	wlec.`code` category_code
FROM
	wl_equipment_specific wles
	LEFT JOIN wl_equipment_detail wled ON wles.equipment_detail_id = wled.id
	LEFT JOIN wl_equipment wle ON wled.equipment_id = wle.id
	LEFT JOIN wl_unit wlu ON wle.unit_id = wlu.id
	LEFT JOIN wl_stock_detail wlsd ON wles.id = wlsd.equipment_specific_id
	left join wl_warehouse_structure wlws on wlsd.warehouse_structure_id = wlws.id
	left join wl_equipment_category wlec on wlec.id  = wle.category_id
    </select>
    <select id="getEquipListBySpecific" resultType="com.yeejoin.equipmanage.common.vo.EquipmentVo">
        <![CDATA[
        SELECT
            e.id,
            e.`name`,
            e.category_id,
            e.`code`,
            'fireEquipment' AS type
        FROM
            wl_equipment_specific es
            LEFT JOIN wl_equipment_detail ed ON ed.id = es.equipment_detail_id
            LEFT JOIN wl_equipment e ON e.id = ed.equipment_id
        WHERE
            e.id IS NOT NULL
        GROUP BY
            e.id
        ]]>
    </select>
    <select id="querySpecificIndexList" resultType="com.yeejoin.equipmanage.common.entity.vo.EquipmentIndexVO">
       SELECT
            WI.id,
            WI.unit as unitName,
            WI.name as perfQuotaName,
            si.`value`,
            WI.group_name
        FROM
            wl_equipment_specific_index  si,
            wl_equipment_index WI
        where si.equipment_index_id = WI.id and si.equipment_specific_id = #{equipmentSpecificId}
    </select>


     <select id="getEquipBySpecific" resultType="com.yeejoin.equipmanage.common.vo.EquipmentVo">
        <![CDATA[
        SELECT
            e.id,
            e.`name`,
            e.category_id,
            e.`code`,
            e.is_iot
        FROM
			wl_equipment e
			LEFT JOIN wl_equipment_detail ed ON ed.equipment_id = e.id
			LEFT JOIN wl_equipment_specific es ON ed.id = es.equipment_detail_id 
		WHERE
			es.id = #{equipmentSpecificId}
        ]]>
    </select>



    <select id="listLikePage" resultType="Map">
        SELECT
        eq.id,
        eq.category_id categoryId,
        eq.name,
        cate.NAME AS unitName
        FROM
        wl_equipment eq
        LEFT JOIN wl_equipment_category cate ON cate.id = eq.category_id
        <where>
            <if test="code != null and code != '' ">
                eq.code like CONCAT(#{code},"%" )
            </if>
        </where>
    </select>


</mapper>
