<?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.goods.mapper.GoodsInfoMapper">
    
	<sql id="goodsInfoColumns">
		a.id AS "id",
		a.name AS "name",
		a.type_id AS "type.id",
		a.model AS "model",
		a.num AS "num",
		a.remarks AS "remarks",
		a.amount as "amount",
		a.info_sum as "infoSum",
		a.qr_interval AS "qrInterval",
		a.goods_id AS "goods.id",
		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",
				
				
		type.code AS "type.code",
		type.name AS "type.name",
		type.amount AS "type.amount",
		type.unit AS "type.unit",
		qr.id AS "qrCode.id",
		qr.code "qrCode.code",
		qr.state "qrCode.state",
		qr.url "qrCode.url"
	</sql>
	
	<sql id="goodsInfoJoins">
		
		LEFT JOIN t_wh_material_type type ON type.id = a.type_id
		LEFT JOIN t_wh_goods b ON b.id = a.goods_id
		LEFT JOIN t_wh_qr qr on qr.goods_info_id = a.id
	</sql>
	
    
	<select id="get" resultType="GoodsInfo" >
		SELECT 
			<include refid="goodsInfoColumns"/>
		FROM t_wh_goods_info a
		<include refid="goodsInfoJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="GoodsInfo" >
		SELECT 
			<include refid="goodsInfoColumns"/>
		FROM t_wh_goods_info a
		<include refid="goodsInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="name != null and name != ''">
				AND a.name = #{name}
			</if>
			<if test="type != null and type.id != null and type.id != ''">
				AND a.type_id = #{type.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>
			<if test="goods != null and goods.id != null and goods.id != ''">
				AND a.goods_id = #{goods.id}
			</if>
			AND a.num > 0
		</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="GoodsInfo" >
		SELECT 
			<include refid="goodsInfoColumns"/>
		FROM t_wh_goods_info a
		<include refid="goodsInfoJoins"/>
		<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_goods_info(
			id,
			name,
			type_id,
			model,
			num,
			remarks,
			amount,
			info_sum,
			qr_interval,
			goods_id,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag
		) VALUES (
			#{id},
			#{name},
			#{type.id},
			#{model},
			#{num},
			#{remarks},
			#{amount},
			#{infoSum},
			#{qrInterval},
			#{goods.id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE t_wh_goods_info SET 	
			name = #{name},
			type_id = #{type.id},
			model = #{model},
			num = #{num},
			remarks = #{remarks},
			amount = #{amount},
			info_sum = #{infoSum},
			qr_interval = #{qrInterval},
			goods_id = #{goods.id},
			update_by = #{updateBy.id},
			update_date = #{updateDate}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM t_wh_goods_info
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE goods_id = #{goods.id}
			</otherwise>
		</choose>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE t_wh_goods_info SET 
			del_flag = #{DEL_FLAG_DELETE}
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE goods_id = #{goods.id}
			</otherwise>
		</choose>
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="GoodsInfo" statementType="STATEMENT">
		select * FROM t_wh_goods_info  where ${propertyName} = '${value}'
	</select>
	
</mapper>