<?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.profitwarehousing.mapper.ProfitWarehousingInfoMapper">
    
	<sql id="profitWarehousingInfoColumns">
		a.id AS "id",
		a.profit_warehousing_id AS "profitWarehousing.id",
		profitWarehousing.number AS "profitWarehousing.number",
		a.name AS "name",
		a.type AS "type.id",
		type.name AS "type.name",
		type.code AS "type.code",
		type.amount AS "type.amount",
		type.unit AS "type.unit",
		a.marking AS "marking",
		a.shelves_ids AS "shelvesIds",
		a.warehouse_ids AS "warehouseIds",
		a.num AS "num",
		a.qr_id AS "qrCode.id",
		qrCode.url AS "qrCode.url",
		qrCode.state AS "qrCode.state",
		qrCode.code AS "qrCode.code",
		a.amount AS "amount",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag"
	</sql>
	
	<sql id="profitWarehousingInfoJoins">
		LEFT JOIN t_wh_material_type type ON type.id = a.type
		LEFT JOIN t_wh_qr qrCode ON qrCode.id = a.qr_id
		LEFT JOIN t_wh_profit_warehousing profitWarehousing ON profitWarehousing.id = a.profit_warehousing_id
	</sql>
	
    
	<select id="get" resultType="ProfitWarehousingInfo" >
		SELECT 
			<include refid="profitWarehousingInfoColumns"/>
		FROM t_wh_profit_warehousing_info a
		<include refid="profitWarehousingInfoJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="ProfitWarehousingInfo" >
		SELECT 
			<include refid="profitWarehousingInfoColumns"/>
		FROM t_wh_profit_warehousing_info a
		<include refid="profitWarehousingInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="profitWarehousing != null and profitWarehousing.id != null and profitWarehousing.id != ''">
				AND a.profit_warehousing_id = #{profitWarehousing.id}
			</if>
			<if test="type != null and type.id != null and type.id != ''">
				AND a.type = #{type.id}
			</if>
			<if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
				AND profitWarehousing.time &gt;= #{beginTime} AND profitWarehousing.time &lt;= #{endTime} AND (qrCode.state = '2')
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="ProfitWarehousingInfo" >
		SELECT 
			<include refid="profitWarehousingInfoColumns"/>
		FROM t_wh_profit_warehousing_info a
		<include refid="profitWarehousingInfoJoins"/>
		<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.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO t_wh_profit_warehousing_info(
			id,
			profit_warehousing_id,
			name,
			type,
			marking,
			shelves_ids,
			warehouse_ids,
			num,
			qr_id,
			amount,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{profitWarehousing.id},
			#{name},
			#{type.id},
			#{marking},
			#{shelvesIds},
			#{warehouseIds},
			#{num},
			#{qrCode.id},
			#{amount},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE t_wh_profit_warehousing_info SET 	
			profit_warehousing_id = #{profitWarehousing.id},
			name = #{name},
			type = #{type.id},
			marking = #{marking},
			shelves_ids = #{shelvesIds},
			qr_id = #{qrCode.id},
			warehouse_ids = #{warehouseIds},
			num = #{num},
			amount = #{amount},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM t_wh_profit_warehousing_info
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE t_wh_profit_warehousing_info SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="ProfitWarehousingInfo" statementType="STATEMENT">
		select * FROM t_wh_profit_warehousing_info  where ${propertyName} = '${value}'
	</select>
	
</mapper>