<?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.materialproject.mapper.MaterialProjectMapper">
    
	<sql id="infoColumns">
		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.reserve_project_name AS "reserveProjectName",
		a.project_name AS "projectName",
		a.reserve_code AS "reserveCode",
		a.project_code AS "projectCode",
		a.project_define AS "projectDefine",
		a.project_attribute AS "projectAttribute",
		a.project_batch AS "projectBatch"
	</sql>

    
	<select id="get" resultType="MaterialProject" >
		SELECT 
			<include refid="infoColumns"/>
		FROM xm_material_project a
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="MaterialProject" >
		SELECT 
			<include refid="infoColumns"/>
		FROM xm_material_project a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="projectStatus == '' ">
				<if test="reserveCodes != null ">
					AND a.reserve_code in
					<foreach collection="reserveCodes" index="index" item="item" open="(" separator="," close=")">
						#{item}
					</foreach>
				</if>
				<if test="projectCodes != null ">
					AND a.project_code in
					<foreach collection="projectCodes" index="index" item="item" open="(" separator="," close=")">
						#{item}
					</foreach>
				</if>
				<if test="reserveProjectName != null and reserveProjectName != ''">
					AND a.reserve_project_name LIKE CONCAT(CONCAT('%',#{reserveProjectName},'%'))
				</if>
				<if test="projectName != null and projectName != ''">
					AND a.project_name LIKE CONCAT(CONCAT('%',#{projectName},'%'))
				</if>
				<if test="reserveCode != null and reserveCode != ''">
					AND a.reserve_code LIKE CONCAT(CONCAT('%',#{reserveCode},'%'))
				</if>
				<if test="projectCode != null and projectCode != ''">
					AND a.project_code LIKE CONCAT(CONCAT('%',#{projectCode},'%'))
				</if>
				<if test="projectDefine != null and projectDefine !=''">
					AND a.project_define LIKE CONCAT(CONCAT('%',#{projectDefine},'%'))
				</if>
				<if test="projectAttribute != null and projectAttribute != ''">
					AND a.project_attribute LIKE CONCAT(CONCAT('%',#{projectAttribute},'%'))
				</if>
				<if test="projectBatch != null and projectBatch != ''">
					AND a.project_batch LIKE CONCAT(CONCAT('%',#{projectBatch},'%'))
				</if>
				<if test="suppliesBatch != null and suppliesBatch !=''">
					AND a.supplies_batch LIKE CONCAT(CONCAT('%',#{suppliesBatch},'%'))
				</if>
			</if>
			<if test="projectStatus != '' ">
				<if test="reserveCodes != null ">
					AND a.reserve_code in
					<foreach collection="reserveCodes" index="index" item="item" open="(" separator="," close=")">
						#{item}
					</foreach>
				</if>
				<if test="projectCodes != null ">
					AND a.project_code in
					<foreach collection="projectCodes" index="index" item="item" open="(" separator="," close=")">
						#{item}
					</foreach>
				</if>
				<if test="reserveProjectName != null and reserveProjectName != ''">
					AND a.reserve_project_name =  #{reserveProjectName}
				</if>
				<if test="projectName != null and projectName != ''">
					AND a.project_name = #{projectName}
				</if>
				<if test="reserveCode != null and reserveCode != ''">
					AND a.reserve_code = #{reserveCode}
				</if>
				<if test="projectCode != null and projectCode != ''">
					AND a.project_code = #{projectCode}
				</if>
				<if test="projectDefine != null and projectDefine !=''">
					AND a.project_define = #{projectDefine}
				</if>
				<if test="projectAttribute != null and projectAttribute != ''">
					AND a.project_attribute = #{projectAttribute}
				</if>
				<if test="projectBatch != null and projectBatch != ''">
					AND a.project_batch = #{projectBatch}
				</if>
				<if test="suppliesBatch != null and suppliesBatch !=''">
					AND a.supplies_batch = #{suppliesBatch}
				</if>
			</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="findByList" resultType="MaterialProject">
		SELECT
		<include refid="infoColumns"/>
		FROM xm_material_project a
		<where>
			a.reserve_project_name in
			<foreach collection="list" item="item" open="(" close=")" separator=",">
				#{item.reserveProjectName}
			</foreach>
		</where>
	</select>
	
	<select id="findAllList" resultType="MaterialProject" >
		SELECT 
			<include refid="infoColumns"/>
		FROM xm_material_project 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="findBatch" resultType="MaterialProject">
		SELECT DISTINCT(batch) AS suppliesBatch
		from  xm_material_standard
	</select>
	
	<insert id="insert">
		INSERT INTO xm_material_project(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			reserve_project_name,
		    project_name,
		    reserve_code,
		    project_code,
		    project_define,
		    project_attribute,
		    project_batch
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{reserveProjectName},
			#{projectName},
			#{reserveCode},
			#{projectCode},
			#{projectDefine},
			#{projectAttribute},
			#{projectBatch}
		)
	</insert>

	<insert id="insertList">
		INSERT INTO xm_material_project(
		id,
		create_by,
		create_date,
		update_by,
		update_date,
		remarks,
		del_flag,
		reserve_project_name,
		project_name,
		reserve_code,
		project_code,
		project_define,
		project_attribute,
		project_batch
		) 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.reserveProjectName},
			#{item.projectName},
			#{item.reserveCode},
			#{item.projectCode},
			#{item.projectDefine},
			#{item.projectAttribute},
			#{item.projectBatch}
		  )
		</foreach>
	</insert>

	<insert id="replaceList">
		replace INTO xm_material_project(
		id,
		create_by,
		create_date,
		update_by,
		update_date,
		remarks,
		del_flag,
		reserve_project_name,
		project_name,
		reserve_code,
		project_code,
		project_define,
		project_attribute,
		project_batch
		) 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.reserveProjectName},
			#{item.projectName},
			#{item.reserveCode},
			#{item.projectCode},
			#{item.projectDefine},
			#{item.projectAttribute},
			#{item.projectBatch}
			)
		</foreach>
	</insert>
	<update id="update">
		UPDATE xm_material_project SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			reserve_project_name = #{reserveProjectName},
			project_name = #{projectName},
			reserve_code = #{reserveCode},
			project_code = #{projectCode},
			project_define = #{projectDefine},
			project_attribute = #{projectAttribute},
			project_batch = #{projectBatch}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM xm_material_project
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE xm_material_project SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="TestNote" statementType="STATEMENT">
		select * FROM xm_material_project  where ${propertyName} = '${value}'
	</select>
	
</mapper>