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

	<resultMap id="StockDetail" type="com.yeejoin.equipmanage.common.entity.StockDetail">
		    <id column="id" property="id"/>
		    <result column="batch_num" property="batchNum"/>
		    <result column="amount" property="amount"/>
		    <result column="stock_id" property="stockId"/>
		    <result column="warehouse_structure_id" property="warehouseStructureId"/>
		    <result column="rfid" property="rfid"/>
		    <result column="equipment_detail_id" property="equipmentDetailId"/>
		    <result column="org_code" property="orgCode"/>
		    <result column="qr_code" property="qrCode"/>
		    <result column="warehouse_id" property="warehouseId"/>
		    <result column="org_code" property="companyName"/>
		    <result column="status" property="status"/>
		    <result column="company_name" property="companyName"/>
		    <result column="department_name" property="departmentName"/>
		    <result column="department_orgcode" property="departmentOrgcode"/>
		   <result column="create_date" property="createDate"/>

		   <!--  <association column="equipment_detail_id" property="equipmentDetail" select="com.yeejoin.fireIotPlatform.common.dao.mapper.EquipmentDetailMapper.queryEquipmentDetailById"/>
		    <association column="shelf_cell_id" property="shelfCell" select="com.yeejoin.fireIotPlatform.common.dao.mapper.ShelfCellMapper.queryShelfCellById"/>
		    <association column="warehouse_id" property="warehouse" select="com.yeejoin.fireIotPlatform.common.dao.mapper.WarehouseMapper.queryWarehouseById"/>
		    <association column="stock_id" property="stock" select="com.yeejoin.fireIotPlatform.common.dao.mapper.StockMapper.queryStockById"/>
 -->		</resultMap>

     <select id="findByIdInAndStatus" resultMap="StockDetail" >
		SELECT  * FROM  wl_stock_detail WHERE wl_stock_detail.status=#{status}

		<if test="ids!=null and ids.size()&gt; 0">
        <foreach collection="ids" item="id" separator="," open="and id in(" close=")">
            #{id}
        </foreach>
    </if>

	</select>

	 <select id="findCategoryNameByID" resultType="Map" >

   SELECT category.name from wl_stock_detail stock_detail INNER JOIN wl_equipment_detail equipment_detail
	ON stock_detail.equipment_detail_id = equipment_detail.id
	INNER JOIN wl_equipment equipment ON equipment.id = equipment_detail.equipment_id
	INNER JOIN wl_equipment_category category ON category.id = equipment.category_id
	WHERE stock_detail.id = #{id}

	</select>
	<select id="getStatisticsEquipCount"
			resultType="com.yeejoin.equipmanage.common.entity.vo.StatisticsEquipCountVO">
		select 'S' as code, IFNULL(sum(wlsd.amount),0) as sum from wl_stock_detail wlsd
		left join wl_equipment_detail wled on wlsd.equipment_detail_id = wled.id
		left join wl_equipment wle on wle.id = wled.equipment_id
		left join wl_equipment_category wlec on wle.category_id=wlec.id
		where left(wlec.code, 3)=410 and (wlsd.`status`=1 or wlsd.`status`=10)

		union all
		select 'PM' as code, IFNULL(sum(wlsd.amount),0) as sum from wl_stock_detail wlsd
		left join wl_equipment_detail wled on wlsd.equipment_detail_id = wled.id
		left join wl_equipment wle on wle.id = wled.equipment_id
		left join wl_equipment_category wlec on wle.category_id=wlec.id
		where left(wlec.code, 3)=420 and (wlsd.`status`=1 or wlsd.`status`=10)

		union all
		select 'GF' as code, IFNULL(sum(wlsd.amount),0) as sum from wl_stock_detail wlsd
		left join wl_equipment_detail wled on wlsd.equipment_detail_id = wled.id
		left join wl_equipment wle on wle.id = wled.equipment_id
		left join wl_equipment_category wlec on wle.category_id=wlec.id
		where left(wlec.code, 3)=430 and (wlsd.`status`=1 or wlsd.`status`=10)

		union all
		select 'QT' as code, IFNULL(sum(wlsd.amount),0) as sum from wl_stock_detail wlsd
		left join wl_equipment_detail wled on wlsd.equipment_detail_id = wled.id
		left join wl_equipment wle on wle.id = wled.equipment_id
		left join wl_equipment_category wlec on wle.category_id=wlec.id
		where left(wlec.code, 3)=440 and (wlsd.`status`=1 or wlsd.`status`=10)
	</select>
	<delete id="clearWareHouse">
		delete from wl_stock_detail where warehouse_structure_id =#{id}
	</delete>
	<update id="updateStatus">
			update wl_stock_detail
			<trim prefix="set" prefixOverrides="," suffix="where id=#{bean.stockDetailId}">
				amount = amount - 1,
				<if test="status != null">status=#{status}</if>
			</trim>
	</update>
	<update id="updateBatch"  parameterType="java.util.List">
		<foreach collection="list" item="item" index="index" open="" close="" separator=";">
			update wl_stock_detail
			<trim prefix="set" prefixOverrides="," suffix="where id=#{item.stockDetailId}">
				amount = amount - 1,
				<if test="status != null">status=#{status}</if>
			</trim>
		</foreach>
	</update>
<!--	<update id="updateBatch"  parameterType="java.util.List">-->
<!--		<foreach collection="list" item="item" index="index" open="" close="" separator=";">-->
<!--			update wl_stock_detail-->
<!--			<set>-->
<!--				status=${status},-->
<!--				amount = amount - 1-->
<!--			</set>-->
<!--			where id = ${item.stockDetailId}-->
<!--		</foreach>-->
<!--	</update>-->
</mapper>
