<?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.materialrequisition.mapper.MaterialRequisitionInfoMapper">
    
	<sql id="materialRequisitionInfoColumns">
		a.id AS "id",
		a.material_requisition_id AS "materialRequisition.id",
		a.ledger_info_id AS "ledgerInfo.id",
		a.remarks AS "remarks",
		a.receiver AS "receiver",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",

		ledgerInfo.code AS "ledgerInfo.code",
		ledgerInfo.batch_num AS "ledgerInfo.batchNum",
		ledgerInfo.model AS "ledgerInfo.model",
		ledgerInfo.amount AS "ledgerInfo.amount",
		ledgerInfo.shelves_ids AS "ledgerInfo.shelvesIds",
		ledgerInfo.warehouse_ids AS "ledgerInfo.warehouseIds",


		ledgerInfo.type_id AS "ledgerInfo.type.id",
		tpye.name AS "ledgerInfo.type.name",
		tpye.code AS "ledgerInfo.type.code",
		tpye.unit AS "ledgerInfo.type.unit",


		a.del_flag AS "delFlag"
	</sql>
	
	<sql id="materialRequisitionInfoJoins">
		LEFT JOIN t_wh_ledger_info ledgerInfo ON ledgerInfo.id = a.ledger_info_id
		LEFT JOIN t_wh_material_type tpye ON ledgerInfo.type_id = tpye.id
	</sql>
	
    
	<select id="get" resultType="MaterialRequisitionInfo" >
		SELECT 
			<include refid="materialRequisitionInfoColumns"/>
		FROM t_wh_material_requisition_info a
		<include refid="materialRequisitionInfoJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="MaterialRequisitionInfo" >
		SELECT 
			<include refid="materialRequisitionInfoColumns"/>
		FROM t_wh_material_requisition_info a
		<include refid="materialRequisitionInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="materialRequisition != null and materialRequisition.id != null and materialRequisition.id != ''">
				AND a.material_requisition_id = #{materialRequisition.id}
			</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="MaterialRequisitionInfo" >
		SELECT 
			<include refid="materialRequisitionInfoColumns"/>
		FROM t_wh_material_requisition_info a
		<include refid="materialRequisitionInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="materialRequisition != null and materialRequisition.id != null and materialRequisition.id != ''">
				AND a.material_requisition_id = #{materialRequisition.id}
			</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>
	
	<insert id="insert">
		INSERT INTO t_wh_material_requisition_info(
			id,
			material_requisition_id,
			receiver,
			ledger_info_id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{materialRequisition.id},
			#{receiver},
			#{ledgerInfo.id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE t_wh_material_requisition_info SET 	
			material_requisition_id = #{materialRequisition.id},
			ledger_info_id = #{ledgerInfo.id},
			receiver = #{receiver},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM t_wh_material_requisition_info
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE t_wh_material_requisition_info SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="MaterialRequisitionInfo" statementType="STATEMENT">
		select * FROM t_wh_material_requisition_info  where ${propertyName} = '${value}'
	</select>
	
</mapper>