<?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.materialInventory.mapper.MaterialInventoryMapper">
    
	<sql id="materialColumns">
		a.id AS "id",
		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.materialCode	AS  "materialCode",
		a.materialDetail	AS  "materialDetail",
		a.stockQuantity	AS  "stockQuantity",
		a.frozenQuantity	AS  "frozenQuantity",
		a.requireCount AS "requireCount",
		a.outCount AS "outCount",
	    a.availableStockQuantity AS "availableStockQuantity"
	</sql>
	<select id="get" resultType="MaterialInventory" >
		SELECT 
			<include refid="materialColumns"/>
		FROM xm_material_inventory a
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="MaterialInventory" >
		SELECT
			<include refid="materialColumns"/>
		FROM xm_material_inventory a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="materialCode != null and materialCode != '' ">
				AND materialCode LIKE CONCAT(CONCAT('%',#{materialCode},'%'))
			</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="MaterialInventory" >
		SELECT
			<include refid="materialColumns"/>
		FROM xm_material_inventory a
			<include refid="materialColumns"/>
		<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">
		replace INTO xm_material_inventory(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			materialCode,
			materialDetail,
			stockQuantity,
			frozenQuantity,
			requireCount,
			outCount,
			availableStockQuantity
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{materialCode},
			#{materialDetail},
			#{stockQuantity},
			#{frozenQuantity},
			#{requireCount},
			#{outCount},
		  	#{availableStockQuantity}
		)
	</insert>

	<insert id="insertList">
		replace INTO xm_material_inventory(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			materialCode,
			materialDetail,
			stockQuantity,
			frozenQuantity,
			requireCount,
			outCount,
			availableStockQuantity
		) VALUES
		<foreach collection="list" item="item" index="index" separator="," >
			(
			#{item.id},
			#{item.createBy.id},
			#{item.createDate},
			#{item.updateBy.id},
			#{item.updateDate},
			#{item.remarks},
			#{item.delFlag},
			#{item.materialCode},
			#{item.materialDetail},
			#{item.stockQuantity},
			#{item.frozenQuantity},
			#{item.requireCount},
			#{item.outCount},
			#{item.availableStockQuantity}
			)
		</foreach>
	</insert>

	<update id="update">
		UPDATE xm_material_inventory SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			materialCode = #{materialCode},
			materialDetail = #{materialDetail},
			stockQuantity =#{stockQuantity},
			frozenQuantity =#{frozenQuantity},
			requireCount = #{requireCount},
			outCount = #{outCount},
			availableStockQuantity =#{availableStockQuantity}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM xm_material_inventory
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE xm_material_inventory SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="MaterialInventory" statementType="STATEMENT">
		select * FROM xm_material_inventory  where ${propertyName} = '${value}'
	</select>

	<select id="findByList" resultType="com.jeeplus.modules.sg.managementcenter.materialInventory.entity.MaterialInventory">
		SELECT
		<include refid="materialColumns"/>
		FROM xm_material_inventory a
		<where>
			a.materialCode in
			<foreach collection="list" item="item" open="(" close=")" separator=",">
				#{item.materialCode}
			</foreach>
		</where>
	</select>

	<!--<select id="findListInventory" parameterType="java.util.List" resultType="com.jeeplus.modules.sg.managementcenter.materialInventory.entity.MaterialInventory">-->
		<!--select-->
		<!--<include refid="materialColumns"/>-->
		<!--FROM xm_material_inventory a-->
		<!--WHERE a.del_flag = '0'-->
		<!--and a.materialCode in-->
		<!--<if test="list != null">-->
			<!--<foreach collection="list" index="index" item="item" open="("-->
					 <!--separator="," close=")">-->
				<!--#{item.materialCode}-->
			<!--</foreach>-->
		<!--</if>-->
	<!--</select>-->
</mapper>