<?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.modules.warehouse.storage.mapper.StorageInfoMapper">
    
	<sql id="storageInfoColumns">
		a.id AS "id",
		a.storage_id AS "storage.id",
		a.name AS "name",
		a.type_id AS "type.id",
		a.model AS "model",
		a.goods_info_id AS "goodsInfo.id",
		a.shelves_ids AS "shelvesIds",
		a.warehouse_ids AS "warehouseIds",
		a.qr_id AS "qrCode.id",
		a.remarks AS "remarks",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.del_flag AS "delFlag",
		a.amount AS "amount",
		a.num AS "num",

		type.name AS "type.name",
		type.code AS "type.code",
		type.amount AS "type.amount",
		type.unit AS "type.unit",

		s.batch_num AS "storage.batchNum",

		qr.code AS "qrCode.code",
		qr.state AS "qrCode.state"
	</sql>
	
	<sql id="storageInfoJoins">
		LEFT JOIN t_wh_material_type type ON type.id = a.type_id
		LEFT JOIN t_wh_qr qr on qr.id = a.qr_id
		LEFT JOIN t_wh_storage s on s.id = a.storage_id
	</sql>
	
    
	<select id="get" resultType="StorageInfo" >
		SELECT 
			<include refid="storageInfoColumns"/>
		FROM t_wh_storage_info a
		<include refid="storageInfoJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="StorageInfo" >
		SELECT 
			<include refid="storageInfoColumns"/>
		FROM t_wh_storage_info a
		<include refid="storageInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="storage != null and storage.id != null and storage.id != ''">
				AND a.storage_id = #{storage.id}
			</if>
			<if test="goodsInfo != null and goodsInfo.id != null and goodsInfo.id != ''">
				AND a.goods_info_id = #{goodsInfo.id}
			</if>
			<if test="name != null and name != ''">
				AND a.name LIKE
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="type != null and type.id != null and type.id != ''">
				AND a.type_id = #{type.id}
			</if>

			<if test="shelvesIds != null and shelvesIds != null and shelvesIds != ''">
				AND a.shelves_id LIKE
				<if test="dbName == 'oracle'">'%'||#{shelvesIds}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{shelvesIds}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{shelvesIds},'%')</if>
			</if>


			<if test="model != null and model != ''">
				AND a.model LIKE 
					<if test="dbName == 'oracle'">'%'||#{model}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{model}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{model},'%')</if>
			</if>
			<if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
				AND a.create_date &gt;= #{beginTime} AND a.create_date &lt;= #{endTime} AND qr.state &lt;&gt; '1'
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="StorageInfo" >
		SELECT 
			<include refid="storageInfoColumns"/>
		FROM t_wh_storage_info a
		<include refid="storageInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO t_wh_storage_info(
			id,
			storage_id,
			name,
			type_id,
			model,
			goods_info_id,
			shelves_ids,
			warehouse_ids,
			qr_id,
			amount,
			num,
			remarks,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag
		) VALUES (
			#{id},
			#{storage.id},
			#{name},
			#{type.id},
			#{model},
			#{goodsInfo.id},
			#{shelvesIds},
			#{warehouseIds},
			#{qrCode.id},
			#{amount},
			#{num},
			#{remarks},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE t_wh_storage_info SET 	
			storage_id = #{storage.id},
			name = #{name},
			type_id = #{type.id},
			model = #{model},
			goods_info_id = #{goodsInfo.id},
			shelves_ids = #{shelvesIds},
			warehouse_ids = #{warehouseIds},
			amount = #{amount},
			num = #{num},
			remarks = #{remarks},
			update_by = #{updateBy.id},
			update_date = #{updateDate}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM t_wh_storage_info
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE storage_id = #{storage.id}
			</otherwise>
		</choose>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE t_wh_storage_info SET 
			del_flag = #{DEL_FLAG_DELETE}
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE storage_id = #{storage.id}
			</otherwise>
		</choose>
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="StorageInfo" statementType="STATEMENT">
		select * FROM t_wh_storage_info  where ${propertyName} = '${value}'
	</select>


</mapper>