<?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.materialinfo.mapper.MaterialInfoMapper">
    
	<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.project_name AS "projectName",
		a.material_code AS "materialCode",
		a.material_name AS "materialName",
		a.extend_description AS "extendDescription",
		a.unit AS "unit",
		a.total_count AS "totalCount",
		a.weight_kg AS "weightKg",
		a.weight_ton AS "weightTon",
		a.supply_type AS "supplyType",
		a.note AS "note",
		a.state AS "state"
	</sql>

    
	<select id="get" resultType="MaterialInfo" >
		SELECT 
			<include refid="infoColumns"/>
		FROM xm_material_data a
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="MaterialInfo" >
		SELECT 
			<include refid="infoColumns"/>
		FROM xm_material_data a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="projectNames != null ">
				AND project_name in
				<foreach collection="projectNames" index="index" item="item" open="(" separator="," close=")">
					#{item}
				</foreach>
			</if>
			<if test="projectName != null and projectName != '' ">
				AND project_name LIKE CONCAT(CONCAT('%',#{projectName},'%'))
			</if>
		</where>
		ORDER BY a.material_code DESC
		<!--<choose>-->
			<!--<when test="page !=null and page.orderBy != null and page.orderBy != ''">-->
				<!--ORDER BY a.update_date DESC-->
			<!--</when>-->
			<!--<otherwise>-->
				<!--ORDER BY a.update_date DESC-->
			<!--</otherwise>-->
		<!--</choose>-->
	</select>
	
	<select id="findAllList" resultType="MaterialInfo" >
		SELECT 
			<include refid="infoColumns"/>
		FROM xm_material_data 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_data(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			project_name,
		    material_code,
		    material_name,
		    extend_description ,
		    unit ,
		    total_count,
		     weight_kg,
		    weight_ton,
		    supply_type,
		    note
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{projectName},
			#{materialCode},
			#{materialName},
			#{extendDescription},
			#{unit},
			#{totalCount},
			#{weightKg},
			#{weightTon},
			#{supplyType},
			#{note}
		)
	</insert>

    <insert id="insertList">
        replace INTO xm_material_data(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
		    project_name,
		    material_code,
		    material_name,
		    extend_description ,
		    unit ,
		    total_count,
		    weight_kg,
		    weight_ton,
		    supply_type,
		    note,
		    state
		) 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.projectName},
			#{item.materialCode},
			#{item.materialName},
			#{item.extendDescription},
			#{item.unit},
			#{item.totalCount},
			#{item.weightKg},
			#{item.weightTon},
			#{item.supplyType},
			#{item.note},
			#{item.state}
		)
        </foreach>
	</insert>
	
	<update id="update">
		UPDATE xm_material_data SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			start_date = #{startDate},
			project_id = #{projectId},
			end_date = #{endDate},
			approval_number = #{approvalNumber},
			building_fee = #{buildingFee},
			install_fee = #{installFee},
			equipment_fee = #{equipmentFee},
			material_fee = #{materialFee},
			design_fee = #{designFee},
			supervision_fee = #{supervisionFee},
			preliminaryWork_fee = #{preliminaryWorkFee},
			damages = #{damages},
			management_fee = #{managementFee},
			total_fee = #{totalFee}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM xm_material_data
		WHERE id = #{id}
	</update>

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


	<select id="findSat" resultType="MaterialInfo" parameterType="String">
		select a.material_code as materialCode,
		a.material_description as materialName,
		a.extend_description as extendDescription
		from xm_material_standard a
		<where>
			a.batch = #{batch}
		</where>
	</select>

	<select id="findProjectName" resultType="MaterialInfo" parameterType="String">
		SELECT DISTINCT(project_name) AS projectName
		<where>
			project_name = #{projectName}
		</where>
	</select>

	<select id="findByList" resultType="MaterialInfo">
		SELECT
		   <include refid="infoColumns"/>
		FROM xm_material_data a
		<where>
		 a.project_name in
		<foreach collection="list" item="item" open="(" close=")" separator=",">
			#{item.reserveProjectName}
		</foreach>
		</where>
	</select>
</mapper>