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

	<select id="universal" resultType="Map">
		SELECT
		wec.NAME AS categoryName,
		wed.NAME AS name,
		wed.CODE AS code,
		wec.CODE AS
		categoryCode,
		COUNT( wed.NAME ) AS count,
		SUM( wsd.amount ) AS sum,
		warehouse_structure_id AS warehouseStructureId,
		wws.full_name AS
		warehouseStructureName,
		wsd.equipment_detail_id AS equipmentDetailId,
		wed.standard,
		wmi.NAME AS manufacturerName,
		wed.brand,
		wed.equipment_id
		AS equipmentId,
		"" AS carNum
		FROM
		wl_stock_detail wsd
		LEFT JOIN
		wl_equipment_detail wed ON wed.id = wsd.equipment_detail_id
		LEFT JOIN
		wl_equipment we ON we.id = wed.equipment_id
		LEFT JOIN
		wl_equipment_category wec ON wec.id = we.category_id
		LEFT JOIN
		wl_manufacturer_info wmi ON wmi.id = wed.manufacturer_id
		LEFT JOIN
		wl_warehouse_structure wws ON wws.id =
		wsd.warehouse_structure_id
		<where>
			AND wsd.warehouse_id = #{warehouseId}
			AND wsd.STATUS != 7
			AND wsd.STATUS != 11
			<if test="keyword != null and keyword != ''">AND wed.NAME LIKE CONCAT('%',#{keyword},'%' ) </if>
		</where>
		GROUP BY
		warehouse_structure_id,
		equipment_detail_id
		UNION
		SELECT
		wec.NAME
		AS categoryName,
		we.NAME AS
		name,
		c.car_code AS code,
		wec.CODE AS
		categoryCode,
		1 AS count,
		1 AS sum,
		"" AS
		warehouseStructureId,
		"" AS
		warehouseStructureName,
		"" AS
		equipmentDetailId,
		"" AS standard,
		wmi.NAME
		AS manufacturerName,
		c.brand,
		c.equipment_id AS equipmentId,
		c.car_num AS
		carNum
		FROM
		wl_car c
		LEFT JOIN
		wl_equipment we ON we.id = c.equipment_id
		LEFT JOIN
		wl_equipment_category wec ON wec.id = we.category_id
		LEFT JOIN
		wl_manufacturer_info wmi ON wmi.id = c.manufacturer_id
		<where>
			AND c.org_code = #{orgcode}
			<if test="keyword != null and keyword != ''">AND we.NAME LIKE CONCAT('%',#{keyword},'%' ) OR c.car_num
				LIKE CONCAT('%',#{keyword},'%' ) </if>
		</where>
		ORDER BY NAME
		LIMIT #{offset},#{size}
	</select>
</mapper>
