<?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_id AS "shelves.id",
		a.warehouse_id AS "warehouse.id",
		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",
				
		type.name AS "type.name",
		type.code AS "type.code",
				
		s.name AS "shelves.name",
		warehouse.name AS "warehouse.name",

		qr.code AS "qrCode.code"
	</sql>
	
	<sql id="storageInfoJoins">
		LEFT JOIN t_wh_material_type type ON type.id = a.type_id
		LEFT JOIN t_wh_warehouse warehouse ON warehouse.id = a.warehouse_id
		LEFT JOIN t_wh_shelves s on s.id = a.shelves_id
		LEFT JOIN t_wh_qr qr on qr.id = a.qr_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="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="shelves != null and shelves.id != null and shelves.id != ''">
				AND a.shelves_id = #{shelves.id}
			</if>
            <if test="goodsInfo != null and goodsInfo.id != null and goodsInfo.id != ''">
                AND a.goods_info_id = #{goodsInfo.id}
            </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>
		</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_id,
			warehouse_id,
			qr_id,
			amount,
			remarks,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag
		) VALUES (
			#{id},
			#{storage.id},
			#{name},
			#{type.id},
			#{model},
			#{goodsInfo.id},
			#{shelves.id},
			#{warehouse.id},
			#{qrCode.id},
			#{amount},
			#{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_id = #{shelves.id},
			warehouse_id = #{warehouse.id},
			amount = #{amount},
			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>

	<select id="findCsiList" resultType="com.jeeplus.modules.warehouse.storage.dto.CountStorageInfo" >
		SELECT
		a.name AS "name",
		a.type_id AS "type.id",
		type.name AS "type.name",
		a.model AS "model",
		sum(a.amount) AS "sum",
		a.amount AS "amount",
		a.goods_info_id AS "goodsInfo.id",
		a.shelves_id AS "shelves.id",
		s.name AS "shelves.name",
		w.id AS "warehouse.id",
		w.name AS "warehouse.name",
		qr.state AS "state",
		COUNT( a.id ) AS "num"
		FROM
		t_wh_storage_info a
		LEFT JOIN t_wh_material_type type on a.type_id = type.id
		LEFT JOIN t_wh_shelves s on s.id = a.shelves_id
		LEFT JOIN t_wh_warehouse w on w.id = s.warehouse_id
		LEFT JOIN t_wh_qr qr ON qr.id = a.qr_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="storage != null and storage.id != null and storage.id != ''">
				AND a.storage_id = #{storage.id}
			</if>
		</where>
		GROUP BY a.goods_info_id,a.shelves_id,qr.state,a.amount
	</select>

</mapper>