<?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.project.mapper.ItemMapper">
    
	<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.item_name AS "itemName",
		a.item_no AS "itemNo",
		a.unit AS "unit",
	    a.project_id AS "projectId",
		a.parent_node AS "parentNode",
		a.type AS "type"
	</sql>

	
    
	<select id="get" resultType="WbsItem" >
		SELECT 
			<include refid="infoColumns"/>
		FROM sg_wbs_item a
		WHERE a.id = #{id}
	</select>
	
	<select id="getList" resultType="WbsItem" parameterType="com.jeeplus.modules.sg.project.entity.WbsProject">
		SELECT 
			<include refid="infoColumns"/>
		FROM sg_wbs_item a
		<where>
			 a.del_flag = '0'
			<if test="itemName != null and itemName != ''">
				AND a.item_name LIKE CONCAT(CONCAT('%',#{itemName},'%'))
			</if>
			<if test="type != null  and type != ''">
				AND a.type LIKE CONCAT(CONCAT('%',#{type},'%'))
			</if>
			<if test="unit != null  and unit != ''">
				AND a.unit LIKE CONCAT(CONCAT('%',#{unit},'%'))
			</if>
			<if test="itemStartDate != null and itemEndDate != ''">
				AND a.create_date &gt;= #{itemStartDate}
			</if>
			<if test="itemEndDate != null and itemEndDate != ''">
				AND a.create_date &lt;= #{itemEndDate}
			</if>
			<if test="projectId != null and projectId != ''">
				AND a.project_id = #{projectId}
			</if>
		</where>
		 ORDER BY a.update_date DESC

	</select>
	
	<select id="findAllList" resultType="WbsItem" >
		SELECT 
			<include refid="infoColumns"/>
		FROM sg_wbs_item a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</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 sg_wbs_item(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			item_name,
			unit,
			item_no,
			type,
			project_id,
			parent_node
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{itemName},
			#{unit},
			#{itemNo},
			#{type},
			#{projectId},
			#{parentNode}
		)
	</insert>
	
	<update id="update">
		UPDATE sg_wbs_item SET
			item_name = #{itemName},
			unit = #{unit},
			type = #{type},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sg_wbs_item
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sg_wbs_item SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="Goods" statementType="STATEMENT">
		select * FROM sg_wbs_item  where ${propertyName} = '${value}'
	</select>

	<select id="findItemByName" parameterType="String" resultType="com.jeeplus.modules.sg.project.entity.WbsItem">
		SELECT
		id AS id,
		unit AS unit,
		item_name AS itemName,
		project_id AS projectId,
		parent_node AS parentNode,
		create_date AS createDate
		FROM sg_wbs_item
		WHERE item_name = #{itemName} and project_id =#{projectId} and del_flag = 0
	</select>

	<!--<delete id="deleteItem" parameterType="String">-->
		<!--DELETE FROM sg_wbs_item where id = #{id}-->
	<!--</delete>-->



	<!--1-->

	<update id="updateItem" parameterType="WbsItem">
		update sg_wbs_item set del_flag = #{delFlag} where id=#{id}
	</update>
	<!-- 根据工程id查找项目 -->
	<select id="findItem" parameterType="String" resultType="WbsItem">
		select * From sg_wbs_item where project_id = #{id}
	</select>

	<!--根据工程id逻辑删除项目-->
	<update id = "deleteItem">
		update sg_wbs_item set del_flag = 1 where project_id = #{parentId}
	</update>

	<!--根据项目 id 删除结算信息-->
	<update id="deleteCount">
		update sg_sett_count set del_flag = 1 where id = #{itemId}
	</update>

	<!--根据项目 id 删除其他信息-->
	<update id="deleteAdjust">
		update sg_fee_adjust_jkxl set del_flag = 1 where id = #{itemId}
	</update>

	<!--根据项目 id 删除差额-->
	<update id="deleteDetail">
		update sg_fee_detail_jkxl set del_flag = 1 where id = #{itemId}
	</update>

	<!--根据项目id查找当前项目所有信息-->
	<select id="findItemById" parameterType="String" resultType="com.jeeplus.modules.sg.project.entity.WbsItem">
		SELECT
		id AS id,
		unit AS unit,
		item_name AS itemName,
		project_id AS projectId,
		parent_node AS parentNode,
		create_date AS createDate
		FROM sg_wbs_item
		WHERE id=#{id}
	</select>
</mapper>