<?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.projectcontentinfo.dao.ProjectReportRecordDao">
    
	<sql id="projectReportRecordColumns">
		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.report_id AS "report.id",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.record_num AS "recordNum",
		a.file_num AS "fileNum",
		a.name AS "name",
		a.record_date AS "recordDate",
		a.status AS "status",
		a.number_count AS "numberCount",
		a.process_instance_id AS "processInstanceId",
		a.sign_cost_one AS "signCostOne.id",
		a.sign_cost_two AS "signCostTwo.id",
		u.name AS "createBy.name",
		d.name AS "report.name",
		d.number AS "report.number"
	</sql>
	
	<sql id="projectReportRecordJoins">
		JOIN project_report_data d ON d.id = a.report_id
		JOIN sys_user u ON u.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="ProjectReportRecord" >
		SELECT
		<include refid="projectReportRecordColumns"/>,
		wci.contract_num AS "contractNum",
		wci.name AS "contractName",
		wct.name AS "clientName",
		d.report_date AS "report.reportDate",
		r.project_name AS "projectName",
		r.project_id AS "projectNum"
		FROM project_report_record a
		<include refid="projectReportRecordJoins"/>
		LEFT JOIN project_records r ON r.id = d.project_id
		LEFT JOIN work_contract_info wci on r.contract_id = wci.id
		LEFT JOIN work_client_info wct on wci.client_id = wct.id
		WHERE a.id = #{id}
	</select>
	<select id="getNumber" resultType="String" >
		SELECT
			number_count
		FROM project_report_record a
		WHERE a.report_id = #{reportId}
		ORDER BY a.number_count DESC
		limit 1
	</select>

	<select id="findList" resultType="ProjectReportRecord" >
		SELECT 
			<include refid="projectReportRecordColumns"/>,
		wci.contract_num AS "contractNum",
		wci.name AS "contractName",
		wct.name AS "clientName"
		FROM project_report_record a
		<include refid="projectReportRecordJoins"/>
		LEFT JOIN project_records r ON r.id = d.project_id
		LEFT JOIN work_contract_info wci on r.contract_id = wci.id
		LEFT JOIN work_client_info wct on wci.client_id = wct.id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="report!=null and report.id != null and report.id != ''">
				AND d.id = #{report.id}
			</if>
			<if test="report!=null and report.name != null and report.name != ''">
				AND d.name = #{report.name}
			</if>
			<if test="report!=null and report.number != null and report.number != ''">
				AND d.number = #{report.number}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="officeId != null and officeId != ''">
				AND a.office_id = #{officeId}
			</if>
			<if test="recordNum != null and recordNum != ''">
				AND a.record_num = #{recordNum}
			</if>
			<if test="fileNum != null and fileNum != ''">
				AND a.file_num = #{fileNum}
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.create_date &gt;= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND a.create_date &lt;= #{endDate}
			</if>
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="recordDate != null and recordDate != ''">
				AND a.record_date = #{recordDate}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				AND a.create_by = #{createBy.id}
			</if>
			<if test="contractNum != null and contractNum != ''">
				AND wci.contract_num like
				<if test="dbName == 'oracle'">'%'||#{contractNum}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{contractNum}, '%')</if>
			</if>
			<if test="contractName != null and contractName != ''">
				AND wci.name like
				<if test="dbName == 'oracle'">'%'||#{contractName}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{contractName}, '%')</if>
			</if>
			<if test="clientName != null and clientName != ''">
				AND wct.name like
				<if test="dbName == 'oracle'">'%'||#{clientName}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{clientName}, '%')</if>
			</if>
			${sqlMap.dsf}
		</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="ProjectReportRecord" >
		SELECT 
			<include refid="projectReportRecordColumns"/>
		FROM project_report_record a
		<include refid="projectReportRecordJoins"/>
		<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_report_record(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			report_id,
			company_id,
			office_id,
			record_num,
			file_num,
			name,
			record_date,
			status,
			number_count,
			sign_cost_one,
			sign_cost_two,
			process_instance_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{report.id},
			#{companyId},
			#{officeId},
			#{recordNum},
			#{fileNum},
			#{name},
			#{recordDate},
			#{status},
			#{numberCount},
			#{signCostOne.id},
			#{signCostTwo.id},
			#{processInstanceId}
		)
	</insert>
	
	<update id="update">
		UPDATE project_report_record SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			report_id = #{report.id},
			company_id = #{companyId},
			office_id = #{officeId},
			record_num = #{recordNum},
			file_num = #{fileNum},
			name = #{name},
			record_date = #{recordDate},
			status = #{status},
			number_count = #{numberCount},
			sign_cost_one = #{signCostOne.id},
			sign_cost_two = #{signCostTwo.id},
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM project_report_record
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE project_report_record SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="ProjectReportRecord" statementType="STATEMENT">
		select * FROM project_report_record  where ${propertyName} = '${value}'
	</select>

	<update id="updateProcessInstanceId">
		UPDATE project_report_record SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="ProjectReportRecord">
		SELECT
		<include refid="projectReportRecordColumns"/>
		FROM project_report_record a
		<include refid="projectReportRecordJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>
</mapper>