<?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.projectcontroltable.dao.ProjectControlTableDao">
    
	<sql id="projectControlTableColumns">
		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.t_name AS "tName",
		a.t_type AS "tType",
		(select process_name from work_process_detail where process_number = a.t_type) AS "processName",
		a.t_user AS "tUser.id",
		a.pf_id AS "pfId",
		tUser.name AS "tUser.name"
	</sql>
	
	<sql id="projectControlTableJoins">
		LEFT JOIN sys_user tUser ON tUser.id = a.t_user
	</sql>
	
    
	<select id="get" resultType="ProjectControlTable" >
		SELECT 
			<include refid="projectControlTableColumns"/>
		FROM project_control_table a
		<include refid="projectControlTableJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="ProjectControlTable" >
		SELECT 
			<include refid="projectControlTableColumns"/>
		FROM project_control_table a
		<include refid="projectControlTableJoins"/>
		<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>
	
	<select id="findAllList" resultType="ProjectControlTable" >
		SELECT 
			<include refid="projectControlTableColumns"/>
		FROM project_control_table a
		<include refid="projectControlTableJoins"/>
		<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 project_control_table(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			t_name,
			t_type,
			t_user,
			pf_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{tName},
			#{tType},
			#{tUser.id},
			#{pfId}
		)
	</insert>
	
	<update id="update">
		UPDATE project_control_table SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			t_name = #{tName},
			t_type = #{tType},
			t_user = #{tUser.id},
			pf_id = #{pfId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM project_control_table
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE project_control_table SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="ProjectControlTable" statementType="STATEMENT">
		select * FROM project_control_table  where ${propertyName} = '${value}'
	</select>


	<!-- 获取所有的过程数据 -->
	<select id="getControlData" resultType="ProjectControlTable">
		select
		<include refid="projectControlTableColumns"/>
		FROM project_control_table a
		<include refid="projectControlTableJoins"/>
		 where a.pf_id = #{pfId}
	</select>
	<!-- 删除临时数据 -->
	<delete id="delControlData">
		DELETE FROM project_control_table where pf_id =#{pfId}
	</delete>

	<!-- 依据id获取数据 -->
	<select id="findListById" resultType="ProjectControlTable" >
		SELECT
		<include refid="projectControlTableColumns"/>
		FROM project_control_table a
		<include refid="projectControlTableJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND  a.id = #{id}
		</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>

    <update id="updatePfId">
        update project_control_table SET pf_id = #{pfId} where id = #{id}
    </update>
</mapper>