<?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.materialProvided.mapper.MaterialProvidedMapper">
    
	<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.serialNumber As "serialNumber",
		a.planToBatch As "planToBatch",
		a.caseInformation As "caseInformation",
		a.materialExtension As "materialExtension",
		a.materialCode As "materialCode",
		a.materialDescription As "materialDescription",
		a.extendedCode As "extendedCode",
		a.extendedDescription As "extendedDescription",
		a.measuringUnit As "measuringUnit",
		a.referencePrice As "referencePrice",
		a.version AS "version"

	</sql>
	<select id="get" resultType="MaterialProvided" >
		SELECT 
			<include refid="materialColumns"/>
		FROM xm_material_provided a
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="MaterialProvided" >
		SELECT
			<include refid="materialColumns"/>
		FROM xm_material_provided 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>
	
	<select id="findAllList" resultType="MaterialProvided" >
		SELECT
			<include refid="materialColumns"/>
		FROM xm_material_provided 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">
		INSERT INTO xm_material_provided(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			serialNumber,
			planToBatch,
			caseInformation,
			materialExtension,
			materialCode,
			materialDescription,
			extendedCode,
			extendedDescription,
			measuringUnit,
			referencePrice,
			version
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{serialNumber},
			#{planToBatch},
			#{caseInformation},
			#{materialExtension},
			#{materialCode},
			#{materialDescription},
			#{extendedCode},
			#{extendedDescription},
			#{measuringUnit},
			#{referencePrice},
			#{version}
		)
	</insert>

	<insert id="insertList">
		replace INTO xm_material_provided(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			serialNumber,
			planToBatch,
			caseInformation,
			materialExtension,
			materialCode,
			materialDescription,
			extendedCode,
			extendedDescription,
			measuringUnit,
			referencePrice,
			version
		) 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.serialNumber},
			#{item.planToBatch},
			#{item.caseInformation},
			#{item.materialExtension},
			#{item.materialCode},
			#{item.materialDescription},
			#{item.extendedCode},
			#{item.extendedDescription},
			#{item.measuringUnit},
			#{item.referencePrice},
			#{item.version}
			)
		</foreach>
	</insert>

	<update id="update">
		UPDATE xm_material_provided SET

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

	<!-- 查找最大的版本号 -->
	<select id="findMaxVersion" resultType="java.lang.Integer">
		select version from xm_material_provided group by version order by version desc LIMIT 1
	</select>

	<select id="findAllVersion" resultType="com.jeeplus.modules.sg.managementcenter.materialProvided.entity.MaterialProvided">
		select version from xm_material_provided
		<where>
			del_flag = '0'
		</where>
		group by version order by version DESC
	</select>
</mapper>