<?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.ProjectMapper">
    
	<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.number,
		b.name AS "userName"
	</sql>

	<select id="get" resultType="WbsProject" >
		SELECT 
			<include refid="infoColumns"/>
		FROM sg_wbs_project a
		left join sys_user b on a.create_by = b.id
		WHERE a.id = #{id}
	</select>

	<select id="findByName" parameterType="String" resultType="WbsProject">
		SELECT
		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.number AS "number"
		FROM sg_wbs_project a
		<where>
			a.project_name = #{projectName}
		</where>
		order by a.create_date DESC
	</select>
	
	<select id="findList" resultType="WbsProject">
		SELECT
			<include refid="infoColumns"/>
		FROM sg_wbs_project a
		left join sys_user b on a.create_by = b.id
		<where>
		   <if test="createBy != null">
				a.create_by = #{createBy.id} AND
		   </if>
		    <if test="projectId !=null and projectId != ''">
				a.id = #{projectId} AND
			</if>
			<if test="projectName != null and projectName != ''">
				a.project_name LIKE CONCAT(CONCAT('%',#{projectName},'%')) AND
			</if>
			<if test="creator != null  and creator != ''">
				 b.name LIKE CONCAT(CONCAT('%',#{creator},'%')) AND
			</if>
			<if test="projectStartDate != null and projectEndDate != ''">
				 a.create_date &gt;= #{projectStartDate} AND
			</if>
			<if test="projectEndDate != null and projectEndDate != ''">
				 a.create_date &lt;= #{projectEndDate} AND
			</if>
			a.del_flag = '0'
		</where>
		 ORDER BY a.update_date DESC

	</select>
	
	<select id="findAllList" resultType="WbsProject" >
		SELECT 
			<include refid="infoColumns"/>
		FROM sg_wbs_project 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_project(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			project_name,
			number,
			parent_node
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{projectName},
			#{number},
			#{parentNode}
		)
	</insert>
	
	<update id="update">
		UPDATE sg_wbs_project SET
			project_name = #{projectName},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>

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

	<!--1-->
	<!--逻辑删除工程id -->
	<update id="deleteProject" parameterType="WbsProject">
	  	update sg_wbs_project set del_flag = #{delFlag} where id = #{id}
	</update>
	<!--根据id查找当前工程所有信息-->
	<select id="findProjectById" parameterType="String" resultType="com.jeeplus.modules.sg.project.entity.WbsProject">
		select * from sg_wbs_project where id = #{id}
	</select>

	<!--根据工程id修改工程下项目数量-->
	<update id="updateProNumber" parameterType="WbsProject">
		update sg_wbs_project set number = #{number} where id=#{id}
	</update>
</mapper>