<?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.jeeplus.psimanage.psiWareHouse.mapper.PsiWareHouseBasicMapper">
    <select id="findList" resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		SELECT
			b.id as purchaseId,
			a.id,
			a.create_by_id as createBy,
			a.create_time as createDate,
			a.ware_house_number,
			a.ware_house_name,
			a.ware_house_date,
			a.handled_by,
			a.handled_by_office,
			so.name as handledByOfficeName,
			sy.name as handleByName,
			a.proc_ins_id,
			a.process_definition_id,
			ifnull(a.status,'0') as status,
			art.ID_ as task_id
		FROM
			psi_management_warehouse_basics a
			left join psi_management_pruchase_request_basics b on a.purchase_no = b.purchase_no and b.del_flag = 0
			left join sys_office so on a.handled_by_office = so.id
			left join sys_user sy on sy.id = a.handled_by
			left join act_ru_task art ON a.proc_ins_id = art.PROC_INST_ID_
			${ew.customSqlSegment}
		ORDER BY a.update_time DESC
    </select>
	<select id="getUserNameByUserId" resultType="java.lang.String">
		select name from sys_user where id = #{id}
	</select>
	<select id="summaryList2"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
			SELECT
				a.id,
				a.create_by_id as createBy,
				a.create_time as createDate,
				a.ware_house_type,
				a.trade_name,
				a.company,
				b.NAME AS wareHouseTypeName,
				sum( trade_number ) AS allNumber,
				b.collect_number AS borrowNumber,
				a.current_inventory AS tradeNumber,
				b.mmcId as mmcId
			FROM
				psi_management_warehouse_detailed a
				LEFT JOIN (
				SELECT
					sum( g.collect_number ) AS collect_number,
					e.goods_name,
					c.NAME as name,
					e.basic_id as mmcId,
				STATUS
				FROM
					psi_management_collect_detailed e
					LEFT JOIN psi_management_collect_basics f ON e.basic_id = f.id
					AND f.del_flag = 0
					LEFT JOIN psi_management_type c ON e.collect_type = c.id
					left join psi_management_collect_detailed g on e.goods_name = g.goods_name and g.del_flag = 0
				WHERE
					f.`status` = 5 AND e.del_flag = 0
				GROUP BY
					e.id
				) b ON a.trade_name = b.goods_name
				LEFT JOIN psi_management_type c ON a.ware_house_type = c.id
				${ew.customSqlSegment}
				GROUP BY a.trade_name,a.ware_house_type
				HAVING sum( trade_number ) - IFNULL(collect_number,0) &lt;= 5
				ORDER BY a.update_time DESC
	</select>
    <select id="summaryList"
            resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		SELECT
			a.id,
			a.create_by_id as createBy,
			a.create_time as createDate,
			a.ware_house_type,
			a.trade_name,
			a.company,
			c.name as wareHouseTypeName,
			sum( trade_number ) AS allNumber,
			b.collect_number AS borrowNumber,
			loss.loss_number AS lossNumber,
			sum(a.current_inventory) AS tradeNumber,
			a.warn_num,
			a.warn_flag,
			a.produce_date,
			a.shelf_life,
			a.shelf_life_unit,
			a.spec,
			pmcdra.returned_num as returnedNum,
			pmcdra.not_surplus_stock as notSurplusStock
		FROM
			psi_management_warehouse_detailed a
			left join
			(
			select
			    sum(collect_number) as collect_number,
			    goods_name,
			    status
			 from psi_management_collect_detailed e
			left join psi_management_collect_basics f on e.basic_id = f.id and f.del_flag = 0
			WHERE f.`status` = 5 and e.del_flag = 0 and e.is_return = '0'
			GROUP BY e.goods_name
			) b
			on a.trade_name = b.goods_name
			left join
			(
				select
					sum(loss_number) as loss_number,
					goods_name,
					status
				from psi_management_loss_detailed e
						 left join psi_management_loss_basics f on e.basic_id = f.id and f.del_flag = 0
				WHERE f.`status` = 5 and e.del_flag = 0
				GROUP BY e.goods_name
			) loss
			on a.trade_name = loss.goods_name
			left join
			(
				select
					sum(returned_num) AS returned_num,
					sum(not_surplus_stock) AS not_surplus_stock,
					inventory_id,
					goods_name,
					status
				from psi_management_collect_detailed_record e
						 left join psi_management_collect_basics pmcba on e.basic_id = pmcba.id and pmcba.del_flag = 0
				WHERE pmcba.`status` = 5 and e.del_flag = 0
				group BY e.goods_name
			) pmcdra
			on a.trade_name = pmcdra.goods_name
			LEFT JOIN psi_management_warehouse_basics bas on a.basic_id = bas.id
			left join psi_management_type c on a.ware_house_type = c.id
			${ew.customSqlSegment}
			GROUP BY a.trade_name,a.ware_house_type
			ORDER BY a.ware_house_type DESC,a.trade_name
	</select>

	<select id="exportSummaryList"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareSummaryExportDto">
		SELECT
			a.id,
			a.create_by_id as createBy,
			a.create_time as createDate,
			a.ware_house_type,
			a.trade_name,
			a.company,
			c.name as wareHouseTypeName,
			IFNULL(his_detail.trade_number,0)  AS allNumber,
			b.collect_number AS borrowNumber,
			loss.loss_number AS lossNumber,
			IFNULL(loss_after.loss_number, 0) AS afterLossNumber,
			IFNULL(after_pmwd.trade_number, 0) AS afterTradeNumber,
			IFNULL(after_pmcdra.not_surplus_stock, 0) AS afterNotSurplusStock,
			sum(a.current_inventory) AS tradeNumber,
			all_stock.allCurrentInventory,
			a.warn_num,
			a.warn_flag,
			a.produce_date,
			a.shelf_life,
			a.shelf_life_unit,
			a.spec,
			pmcdra.returned_num as returnedNum,
			IFNULL(pmcdra.not_surplus_stock, 0) AS notSurplusStock
		FROM
			psi_management_warehouse_detailed a
				left join
			(
				select
					sum(collect_number) as collect_number,
					goods_name,
					status
				from psi_management_collect_detailed e
						 left join psi_management_collect_basics f on e.basic_id = f.id and f.del_flag = 0
				WHERE f.`status` = 5
				  and e.del_flag = 0
				  and e.is_return = '0'
				<if test="dto != null and dto.dateRange != null and dto.dateRange.length == 2">
					and f.collect_date &gt;= #{dto.dateRange[0]} AND f.collect_date &lt;= #{dto.dateRange[1]}
				</if>
				GROUP BY e.goods_name
			) b
			on a.trade_name = b.goods_name
				left join
			(
				select
					sum(loss_number) as loss_number,
					goods_name,
					status
				from psi_management_loss_detailed e
						 left join psi_management_loss_basics f on e.basic_id = f.id and f.del_flag = 0
				WHERE f.`status` = 5
				  and e.del_flag = 0
				<if test="dto != null and dto.dateRange != null and dto.dateRange.length == 2">
					and f.collect_date &gt;= #{dto.dateRange[0]} AND f.collect_date &lt;= #{dto.dateRange[1]}
				</if>
				GROUP BY e.goods_name
			) loss
			on a.trade_name = loss.goods_name
			left join(
				select
				sum(loss_number) as loss_number,
				goods_name,
				status
				from psi_management_loss_detailed e
				left join psi_management_loss_basics f on e.basic_id = f.id and f.del_flag = 0
				WHERE f.`status` = 5
				and e.del_flag = 0
				<if test="dto != null and dto.dateRange != null and dto.dateRange.length == 2">
				and f.collect_date  &gt;= #{dto.dateRange[1]}
				</if>
				GROUP BY e.goods_name
			) loss_after
			on a.trade_name = loss_after.goods_name
				left join
			(
				select
					sum(returned_num) AS returned_num,
					sum(not_surplus_stock) AS not_surplus_stock,
					goods_name
				from psi_management_collect_detailed_record e
						 left join psi_management_collect_basics pmcba on e.basic_id = pmcba.id and pmcba.del_flag = 0
				WHERE pmcba.`status` = 5
				  and e.del_flag = 0
				<if test=" dto != null and dto.dateRange != null and dto.dateRange.length == 2">
					and pmcba.collect_date &gt;= #{dto.dateRange[0]} AND pmcba.collect_date &lt;= #{dto.dateRange[1]}
				</if>
				group BY e.goods_name
			) pmcdra
			on a.trade_name = pmcdra.goods_name
		LEFT JOIN (
				select
					sum(returned_num) AS returned_num,
					sum(not_surplus_stock) AS not_surplus_stock,
					goods_name
				from psi_management_collect_detailed_record e
					left join psi_management_collect_basics pmcba on e.basic_id = pmcba.id and pmcba.del_flag = 0
					WHERE pmcba.`status` = 5
					and e.del_flag = 0
				<if test=" dto != null and dto.dateRange != null and dto.dateRange.length == 2">
					and pmcba.collect_date &gt;= #{dto.dateRange[1]}
				</if>
				group BY e.goods_name
			) after_pmcdra
		on a.trade_name = after_pmcdra.goods_name
			LEFT JOIN (
				SELECT
				trade_name,
				SUM(current_inventory) AS allCurrentInventory
				FROM psi_management_warehouse_detailed
				WHERE del_flag = 0
				GROUP BY trade_name
			) all_stock ON a.trade_name = all_stock.trade_name
			LEFT JOIN (
				SELECT
					pwd.trade_name,
					SUM(pwd.trade_number) AS trade_number
				FROM psi_management_warehouse_detailed pwd
				left join psi_management_warehouse_basics pmcba on pwd.basic_id = pmcba.id and pmcba.del_flag = 0
				WHERE pwd.del_flag = 0
					<if test=" dto != null and dto.dateRange != null and dto.dateRange.length == 2">
						and pmcba.ware_house_date &gt;= #{dto.dateRange[0]} AND pmcba.ware_house_date &lt;= #{dto.dateRange[1]}
					</if>
					GROUP BY pwd.trade_name
			) his_detail ON a.trade_name = his_detail.trade_name
			LEFT JOIN (
				SELECT
					ab.trade_name,
					SUM(ab.trade_number) AS trade_number
				FROM psi_management_warehouse_detailed ab
				left join psi_management_warehouse_basics pmcba on ab.basic_id = pmcba.id and pmcba.del_flag = 0
				WHERE ab.del_flag = 0
				<if test="dto != null and dto.dateRange != null and dto.dateRange.length == 2">
					and pmcba.ware_house_date  &gt;= #{dto.dateRange[1]}
				</if>
				GROUP BY ab.trade_name
			) after_pmwd ON a.trade_name = after_pmwd.trade_name
				LEFT JOIN psi_management_warehouse_basics bas on a.basic_id = bas.id
				left join psi_management_type c on a.ware_house_type = c.id
			${ew.customSqlSegment}
		GROUP BY a.trade_name,a.ware_house_type
		ORDER BY a.ware_house_type DESC,a.trade_name
	</select>


	<select id="getByProduceDateAndName"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		SELECT
			a.id,
			a.create_by_id as createBy,
			a.create_time as createDate,
			a.ware_house_type,
			a.trade_name,
			a.company,
			c.name as wareHouseTypeName,
			sum( trade_number ) AS allNumber,
			b.collect_number AS borrowNumber,
			sum(a.current_inventory) AS tradeNumber,
			a.warn_num,
			a.warn_flag,
			min(a.produce_date) as produce_date,
			a.shelf_life,
			a.shelf_life_unit,
			a.spec
		FROM
			psi_management_warehouse_detailed a
				left join
			(
				select
					sum(collect_number) as collect_number,
					goods_name,
					status
				from psi_management_collect_detailed e
						 left join psi_management_collect_basics f on e.basic_id = f.id and f.del_flag = 0
				WHERE f.`status` = 5 and e.del_flag = 0
				GROUP BY e.goods_name
			) b
			on a.trade_name = b.goods_name
				LEFT JOIN psi_management_warehouse_basics bas on a.basic_id = bas.id
				left join psi_management_type c on a.ware_house_type = c.id
			${ew.customSqlSegment}
		GROUP BY a.trade_name,a.ware_house_type
		ORDER BY a.ware_house_type DESC,a.trade_name
	</select>

	<select id="getByProduceDateNotMerge"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		SELECT
			a.id,
			a.produce_date as produce_date,
			a.trade_name,
			a.spec,
			a.company,
			a.current_inventory,
			pmwd.warn_num,
			pmwd.warn_flag
		FROM
			psi_management_warehouse_detailed a
				LEFT JOIN psi_management_warehouse_basics bas on a.basic_id = bas.id
				LEFT JOIN psi_management_warehouse_detailed pmwd on bas.id = pmwd.basic_id
				left join psi_management_type c on a.ware_house_type = c.id
			${ew.customSqlSegment}

		GROUP BY a.trade_name,a.ware_house_type,a.produce_date
		ORDER BY a.produce_date asc
	</select>
	<select id="getAllData"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		SELECT
			a.id,
			a.produce_date as produce_date,
			a.trade_name,
			a.spec,
			a.company,
			a.current_inventory,
			a.shelf_life,
			a.shelf_life_unit,
			pmwd.warn_num,
			pmwd.warn_flag
		FROM
			psi_management_warehouse_detailed a
				LEFT JOIN psi_management_warehouse_basics bas on a.basic_id = bas.id
				LEFT JOIN psi_management_warehouse_detailed pmwd on bas.id = pmwd.basic_id
				left join psi_management_type c on a.ware_house_type = c.id
			${ew.customSqlSegment}

		GROUP BY a.trade_name,a.ware_house_type,a.produce_date
		ORDER BY a.produce_date asc
	</select>

	<select id="nearExpiryList"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		select
		    a.trade_name,
		    a.produce_date,
		    a.shelf_life,
		    a.spec,
		    a.shelf_life_unit,
		    a.current_inventory
		from psi_management_warehouse_detailed a
		left join psi_management_warehouse_basics pmwb on pmwb.id = a.basic_id and pmwb.del_flag= '0'
		where a.del_flag = '0'
	</select>

    <select id="findRequestId" resultType="java.lang.String">
		select id from psi_management_pruchase_request_basics where purchase_no = #{purchaseNo} and del_flag = 0
	</select>
	<select id="wareHouseHistoryList"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		SELECT
			b.create_time as dateTest,
			a.id as wareHouseId,
			a.ware_house_number,
			a.ware_house_name,
			a.ware_house_date,
			sy.name as wareHouseHandledBy,
			so.name as wareHouseHandledByOff,
			b.trade_name,
			b.trade_number,
			b.current_inventory,
			b.ware_house_type,
			b.produce_date,
			b.shelf_life,
			b.spec,
			b.shelf_life_unit
		FROM
			psi_management_warehouse_basics a
			LEFT JOIN
			psi_management_warehouse_detailed b on a.id = b.basic_id and b.del_flag = 0

			left join sys_user sy on sy.id = a.handled_by
			left join sys_office so on sy.office_id = so.id

			${ew.customSqlSegment}
		ORDER BY a.create_time DESC
	</select>
	<select id="collectHistoryList"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		SELECT
			b.create_time as dateTest2,
			a.id as collectId,
			a.collect_no,
			sy.name as collectHandleBy,
			so.name as collectHandleByOff,
			a.collect_date,
			b.goods_name,
			b.collect_number,
			pmwd.current_inventory,
			pmwd.spec,
			pmcdr.not_surplus_stock as notSurplusStock,
			b.collect_type as collectType
		FROM
			psi_management_collect_basics a
			LEFT JOIN
			psi_management_collect_detailed b on a.id = b.basic_id and b.del_flag = 0
			LEFT JOIN psi_management_collect_detailed_record pmcdr ON b.id = pmcdr.detail_id
			LEFT JOIN psi_management_warehouse_detailed pmwd ON pmcdr.inventory_id = pmwd.id
			left join sys_user sy on sy.id = a.handled_by
			left join sys_office so on sy.office_id = so.id

			${ew.customSqlSegment}
		ORDER BY a.update_time DESC
	</select>
	<select id="getByTypeAndGoodsName"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		SELECT
			a.id,
			a.create_by_id as createBy,
			a.create_time as createDate,
			a.ware_house_type,
			a.trade_name,
			a.company,
			c.name as wareHouseTypeName,
			sum(trade_number) as allNumber,
			b.collect_number as borrowNumber,
			a.current_inventory as tradeNumber,
			a.warn_num,
			a.warn_flag
		FROM
			psi_management_warehouse_detailed a
			left join
			(select sum(collect_number) as collect_number,goods_name,status from psi_management_collect_detailed e
			left join psi_management_collect_basics f on e.basic_id = f.id and f.del_flag = 0
			WHERE f.`status` = 5 and e.collect_type = #{collectTypeId}
			GROUP BY e.goods_name) b
			on a.trade_name = b.goods_name
			LEFT JOIN psi_management_warehouse_basics bas on a.basic_id = bas.id
			left join psi_management_type c on a.ware_house_type = c.id
			where a.trade_name = #{goodsName}
			  and a.ware_house_type = #{collectTypeId}
			  and a.del_flag = 0
			GROUP BY a.trade_name
			ORDER BY a.update_time DESC
	</select>
	<select id="getByTypeAndGoodsNameByProduceDate"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		SELECT
			a.id,
			a.create_by_id as createBy,
			a.create_time as createDate,
			a.ware_house_type,
			a.trade_name,
			a.company,
			c.name as wareHouseTypeName,
			sum(trade_number) as allNumber,
			b.collect_number as borrowNumber,
			IFNULL((sum(trade_number) - collect_number),sum(trade_number)) as tradeNumber
		FROM
			psi_management_warehouse_detailed a
				left join
			(select sum(collect_number) as collect_number,goods_name,status from psi_management_collect_detailed e
																					 left join psi_management_collect_basics f on e.basic_id = f.id and f.del_flag = 0
			 WHERE f.`status` = 5 and e.collect_type = #{collectTypeId}
			 GROUP BY e.goods_name) b
			on a.trade_name = b.goods_name

				LEFT JOIN psi_management_warehouse_basics bas on a.basic_id = bas.id
				left join psi_management_type c on a.ware_house_type = c.id
		where a.trade_name = #{goodsName}
		  and a.ware_house_type = #{collectTypeId}
		  and a.produce_date = #{produceDate}
		  and a.del_flag = 0
		GROUP BY a.trade_name
		ORDER BY a.produce_date asc
	</select>

	<select id="getByTypeAndGoodsNameOrderByProduceDate"
			resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		SELECT
			a.id,
			a.create_by_id as createBy,
			a.create_time as createDate,
			a.ware_house_type,
			a.trade_name,
			a.company,
			c.name as wareHouseTypeName,
			a.trade_number,
			a.current_inventory
		FROM
			psi_management_warehouse_detailed a
				left join
			(select sum(collect_number) as collect_number,goods_name,status from psi_management_collect_detailed e
																					 left join psi_management_collect_basics f on e.basic_id = f.id and f.del_flag = 0
			 WHERE f.`status` = 5 and e.collect_type = #{collectTypeId}
			 GROUP BY e.goods_name) b
			on a.trade_name = b.goods_name
				LEFT JOIN psi_management_warehouse_basics bas on a.basic_id = bas.id
				left join psi_management_type c on a.ware_house_type = c.id
		where a.trade_name = #{goodsName}
		  and a.ware_house_type = #{collectTypeId}
		  and a.del_flag = 0
		ORDER BY a.produce_date asc
	</select>

	<update id="saveUpdateUser">
		update psi_management_warehouse_basics set update_user = #{userId} where id = #{basicId}
	</update>
	<select id="getUpdateUser" resultType="string">
		select update_user from psi_management_warehouse_basics where id = #{basicId}
	</select>
	<update id="deleteUpdateUser">
		update psi_management_warehouse_basics set update_user = null where id = #{basicId}
	</update>
	<select id="getByTypeAndTradeName" resultType="com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto">
		select
			warn_num,
			warn_flag
		from psi_management_warehouse_detailed
		where trade_name = #{tradeName} and ware_house_type = #{typeId} and del_flag = '0'
	</select>
	<select id="getWareHouseInfoByTime" resultType="java.lang.String">
		SELECT
		IFNULL((sum(trade_number) - collect_number),sum(trade_number)) as tradeNumber
		FROM
		psi_management_warehouse_detailed a
		left join psi_management_type c on a.ware_house_type = c.id

		left join
		(select sum(collect_number) as collect_number,goods_name,status from psi_management_collect_detailed e
		left join psi_management_collect_basics f on e.basic_id = f.id and f.del_flag = 0
		WHERE f.`status` = 5 and e.del_flag = 0 and e.produce_date &lt;= #{produceDate} and e.collect_type = #{wareHouseType} and e.goods_name = #{tradeName}) b
		on a.trade_name = b.goods_name

		WHERE a.ware_house_type = #{wareHouseType}
		and a.del_flag = 0
		and a.trade_name = #{tradeName}
		and a.produce_date &lt;= #{produceDate}
		GROUP BY a.trade_name,a.ware_house_type
		ORDER BY a.ware_house_type DESC
	</select>
	<select id="getCollectInfoByTime" resultType="java.lang.String">
		SELECT
		IFNULL((sum(trade_number) - collect_number),sum(trade_number)) as tradeNumber
		FROM
		psi_management_warehouse_detailed a
		left join psi_management_type c on a.ware_house_type = c.id

		left join
		(select sum(collect_number) as collect_number,goods_name,status from psi_management_collect_detailed e
		left join psi_management_collect_basics f on e.basic_id = f.id and f.del_flag = 0
		WHERE f.`status` = 5 and e.del_flag = 0 and e.create_time &lt;= #{createDate} and e.collect_type = #{wareHouseType} and e.goods_name = #{tradeName}) b
		on a.trade_name = b.goods_name

		WHERE a.ware_house_type = #{wareHouseType}
		and a.del_flag = 0
		and a.trade_name = #{tradeName}
		and a.create_time &lt;= #{createDate}
		GROUP BY a.trade_name,a.ware_house_type
		ORDER BY a.ware_house_type DESC
	</select>
	<select id="getCollectNumByType" resultType="java.lang.String">
		SELECT
		sum( collect_number ) AS collect_number
	FROM
		psi_management_collect_detailed e
		LEFT JOIN psi_management_collect_basics f ON e.basic_id = f.id
		AND f.del_flag = 0
	WHERE
		f.`status` = 5
		AND e.del_flag = 0
		and e.collect_type = #{wareHouseType} and e.goods_name = #{tradeName}
	</select>
	<update id="updateWarnFlagByTradeNameAndType">
		update psi_management_warehouse_detailed
		set warn_flag = #{warnFlag}
		where trade_name = #{tradeName} and ware_house_type = #{typeId}
	</update>
	<update id="updateWarnNumByTradeNameAndType">
		update psi_management_warehouse_detailed
		set warn_num = #{warnNum}
		where trade_name = #{tradeName} and ware_house_type = #{typeId}
	</update>
	<update id="clearUpdateCause">
		update psi_management_warehouse_basics set update_cause = null where id = #{id}
	</update>
</mapper>
