<?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.sg.managementcenter.materialplan.mapper.MaterialPlanMapper">
    
	<sql id="planColumns">
		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",
		a.unique_code As "uniqueCode",
		a.require_coding As "requireCoding",
		a.material_description As "description",
		a.expansion_code As "expansionCode",
		a.expansion_description As "expansionDescription",
		a.big_describe As "bigDescribe",
		a.moderate_describe As "moderateDescribe",
		a.small_describe As "smallDescribe",
		a.require_number As "requireNumber",
		a.material_unit As "util",
		a.plan_to_batch As "planToBatch",
		a.delivery_schedule As "deliverySchedule",
		a.flag_storage As "flagStorage",
		a.inventory_quantity As "inventoryQuantity",
		a.number_frozen As "numberFrozen",
		a.supplier_name As "supplierName"
	</sql>
    
	<select id="get" resultType="MaterialPlan" >
		SELECT 
			<include refid="planColumns"/>
		FROM xm_material_plan a
		WHERE a.unique_code = #{id}
	</select>
	
	<select id="findList" resultType="MaterialPlan" >
		SELECT
			<include refid="planColumns"/>
		FROM xm_material_plan a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="uniqueCode != null and  uniqueCode != ''">
				AND a.unique_code LIKE CONCAT(CONCAT('%',#{uniqueCode},'%'))
			</if>
			<if test="requireCoding != null and  requireCoding != ''">
				AND a.require_coding LIKE CONCAT(CONCAT('%',#{requireCoding},'%'))
			</if>
			<if test="description != null and  description != ''">
				AND a.material_description LIKE CONCAT(CONCAT('%',#{description},'%'))
			</if>
			<if test="expansionCode != null and  expansionCode != ''">
				AND a.expansion_code LIKE CONCAT(CONCAT('%',#{expansionCode},'%'))
			</if>
			<if test="expansionDescription != null and  expansionDescription != ''">
				AND a.expansion_description LIKE CONCAT(CONCAT('%',#{expansionDescription},'%'))
			</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="MaterialPlan" >
		SELECT
		<include refid="planColumns"/>
		FROM xm_material_plan a
		<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 xm_material_plan(
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			unique_code,
			require_coding,
			material_description,
			expansion_code,
			expansion_description,
			big_describe,
			moderate_describe,
			small_describe,
			require_number,
			material_unit,
			plan_to_batch,
			delivery_schedule,
			flag_storage,
			inventory_quantity,
			number_frozen,
			supplier_name
		) VALUES
		(
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{uniqueCode},
			#{requireCoding},
			#{description},
			#{expansionCode},
    		#{expansionDescription},
    		#{bigDescribe},
    		#{moderateDescribe},
    		#{smallDescribe},
    		#{requireNumber},
    		#{util},
    		#{planToBatch},
    		#{deliverySchedule},
    		#{flagStorage},
    		#{inventoryQuantity},
    		#{numberFrozen},
    		#{supplierName}
		)
	</insert>

	<insert id="insertList">
		replace INTO xm_material_plan(
		create_by,
		create_date,
		update_by,
		update_date,
		remarks,
		del_flag,
		unique_code,
		require_coding,
		material_description,
		expansion_code,
		expansion_description,
		big_describe,
		moderate_describe,
		small_describe,
		require_number,
		material_unit,
		plan_to_batch,
		delivery_schedule,
		flag_storage,
		inventory_quantity,
		number_frozen,
		supplier_name
		) VALUES
		<foreach collection="list" item="item" index="index" separator="," >
			(
			#{item.createBy.id},
			#{item.createDate},
			#{item.updateBy.id},
			#{item.updateDate},
			#{item.remarks},
			#{item.delFlag},
			#{item.uniqueCode},
			#{item.requireCoding},
			#{item.description},
			#{item.expansionCode},
			#{item.expansionDescription},
			#{item.bigDescribe},
			#{item.moderateDescribe},
			#{item.smallDescribe},
			#{item.requireNumber},
			#{item.util},
			#{item.planToBatch},
			#{item.deliverySchedule},
			#{item.flagStorage},
			#{item.inventoryQuantity},
			#{item.numberFrozen},
			#{item.supplierName}
			)
		</foreach>
	</insert>

	<update id="update">
		UPDATE xm_material_plan SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			require_coding = #{requireCoding},
			material_description = #{description},
			expansion_code = #{expansionCode},
			expansion_description = #{expansionDescription},
			big_describe = #{bigDescribe},
			moderate_describe = #{moderateDescribe},
			small_describe = #{smallDescribe},
			require_number = #{requireNumber},
			material_unit = #{util},
			plan_to_batch = #{planToBatch},
			delivery_schedule =#{deliverySchedule},
			flag_storage = #{flagStorage},
			inventory_quantity = #{inventoryQuantity},
			number_frozen = #{numberFrozen},
			supplier_name = #{supplierName}
		WHERE unique_code = #{uniqueCode}
	</update>

	<update id="updateInventory">
		UPDATE xm_material_plan SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			flag_storage = #{flagStorage},
			inventory_quantity = #{inventoryQuantity},
			number_frozen = #{numberFrozen},
			supplier_name = #{supplierName}
		WHERE unique_code = #{uniqueCode}
	</update>

	<update id="updateTime">
		UPDATE xm_material_plan SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			delivery_schedule =#{deliverySchedule}
		WHERE unique_code = #{uniqueCode}
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM xm_material_plan
		WHERE unique_code = #{id}
	</update>

	<update id="deleteData">
		DELETE FROM xm_material_plan
		WHERE unique_code= #{unique_code}
	</update>

	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE xm_material_plan SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE unique_code = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="Project" statementType="STATEMENT">
		select * FROM xm_material_plan  where ${unique_code} = '${value}'
	</select>

</mapper>