<?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.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",
		ledgerInfo.name AS "ledgerInfo.name",
		ledgerInfo.code AS "ledgerInfo.code",
		ledgerInfo.type_id AS "ledgerInfo.typeId",
		ledgerInfo.model AS "ledgerInfo.model",
		ledgerInfo.amount AS "ledgerInfo.amount",
		a.remarks AS "remarks",
		a.receiver AS "receiver.id",
		receiver.name AS "receiver.name"
	</sql>
	
	<sql id="materialRequisitionInfoJoins">
		LEFT JOIN sys_user receiver ON receiver.id = a.receiver
		LEFT JOIN t_wh_ledger_info ledgerInfo ON ledgerInfo.id = a.ledger_info_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>
			<if test="receiver != null and receiver.id != null and receiver.id != ''">
				AND a.receiver = #{receiver.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.id},
			#{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.id},
			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>