<?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.ProjectReportChangeDao">
    
	<sql id="projectReportDataColumns">
		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.company_id AS "companyId",
		a.office_id AS "officeId",
		a.project_id AS "project.id",
		a.name AS "name",
		a.number AS "number",
		a.type AS "type",
		a.achievement_type AS "achievementType",
		a.review_standard AS "reviewStandard",
		a.sign_cost_one AS "signCostOne.id",
		a.sign_cost_two AS "signCostTwo.id",
		a.master AS "master.id",
		a.report_date AS "reportDate",
		a.file_status AS "fileStatus",
		a.status AS "status",
		a.process_instance_id AS "processInstanceId",
		m.name AS "master.name",
		a.report_id AS "reportId",
		a.before_count AS "beforeCount",
		a.before_id AS "beforeEntity.id",
		a.report_type AS "reportType",
		a.number_path as "numberPath",
		a.review_fee AS "reviewFee",
		a.approval_fee AS "approvalFee",
		a.contract_fee AS "contractFee",
		a.verify_fee AS "verifyFee",
		a.verify_rate AS "verifyRate",
		a.consult_fee AS "consultFee",
		a.building_fee AS "buildingFee",
		a.install_fee AS "installFee"
	</sql>
	
	<sql id="projectReportDataJoins">
		LEFT JOIN sys_user m ON m.id = a.master
	</sql>
	
    
	<select id="get" resultType="ProjectReportData" >
		SELECT
			<include refid="projectReportDataColumns"/>
		FROM project_report_change a
		<include refid="projectReportDataJoins"/>
		WHERE a.id = #{id}
	</select>
	<select id="findByBefore" resultType="ProjectReportData" >
		SELECT
			<include refid="projectReportDataColumns"/>
		FROM project_report_change a
		<include refid="projectReportDataJoins"/>
		WHERE a.report_id = #{reportId} AND a.status = '5'
		ORDER BY before_count DESC
		limit 1
	</select>

	<select id="getNumber" resultType="ProjectReportData" >
		SELECT
		a.id AS "id",
		a.number AS "number"
		FROM project_report_change a
		WHERE
		a.del_flag = #{DEL_FLAG_NORMAL}
		AND a.project_id = #{project.id}
		ORDER BY a.number DESC
		limit 1
	</select>
	
	<select id="findList" resultType="ProjectReportData" >
		SELECT 
			<include refid="projectReportDataColumns"/>
		FROM project_report_change a
		<include refid="projectReportDataJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} AND a.report_id is NOT NULL
			<if test="project != null and project.id != null and project.id != ''">
				AND a.project_id = #{project.id}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="name != null and name != ''">
				AND a.name like
				<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
			</if>
			<if test="number != null and number != ''">
				AND a.number = #{number}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="master!=null and master.id != null and master.id != ''">
				AND a.master = #{master.id}
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.report_date &gt;= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND a.report_date &lt;= #{endDate}
			</if>
		</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="ProjectReportData" >
		SELECT 
			<include refid="projectReportDataColumns"/>
		FROM project_report_change a
		<include refid="projectReportDataJoins"/>
		<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_change(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			office_id,
			project_id,
			name,
			number,
			type,
			achievement_type,
			review_standard,
			sign_cost_one,
			sign_cost_two,
			master,
			report_date,
			file_status,
			status,
			report_id,
			before_count,
			before_id,
			report_type,
			number_path,
			review_fee,
		    approval_fee,
		    contract_fee,
		    verify_fee,
		    verify_rate,
		    consult_fee,
		    building_fee,
		    install_fee
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{officeId},
			#{project.id},
			#{name},
			#{number},
			#{type},
			#{achievementType},
			#{reviewStandard},
			#{signCostOne.id},
			#{signCostTwo.id},
			#{master.id},
			#{reportDate},
			#{fileStatus},
			#{status},
			#{reportId},
			#{beforeCount},
			#{beforeEntity.id},
			#{reportType},
			#{numberPath},
			#{reviewFee},
			#{approvalFee},
			#{contractFee},
			#{verifyFee},
			#{verifyRate},
			#{consultFee},
			#{buildingFee},
			#{installFee}
		)
	</insert>
	
	<update id="update">
		UPDATE project_report_change SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			project_id = #{project.id},
			name = #{name},
			number = #{number},
			type = #{type},
			achievement_type = #{achievementType},
			review_standard = #{reviewStandard},
			sign_cost_one = #{signCostOne.id},
			sign_cost_two = #{signCostTwo.id},
			master = #{master.id},
			report_date = #{reportDate},
			status = #{status},
			report_id = #{reportId},
			report_type = #{reportType},
			number_path = #{numberPath},
			review_fee = #{reviewFee},
			approval_fee = #{approvalFee},
			contract_fee = #{contractFee},
			verify_fee = #{verifyFee},
			verify_rate = #{verifyRate},
			consult_fee = #{consultFee},
			building_fee = #{buildingFee},
			install_fee = #{installFee}
		WHERE id = #{id}
	</update>

	<update id="saveInvalid">
		UPDATE project_report_change SET
		update_by = #{updateBy.id},
		update_date = #{updateDate},
		remarks = #{remarks},
		<if test="invalidStatus != null and invalidStatus != ''">
			invalid_status = #{invalidStatus},
		</if>
		status = #{status}
		WHERE id = #{id}
	</update>

	<update id="changeProjectReportData">
		UPDATE project_report_data
		SET update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			office_id = #{officeId},
			project_id = #{project.id},
			name = #{name},
			number = #{number},
			type = #{type},
			achievement_type = #{achievementType},
			review_standard = #{reviewStandard},
			sign_cost_one = #{signCostOne.id},
			sign_cost_two = #{signCostTwo.id},
			master = #{master.id},
			report_date = #{reportDate},
			status = #{status}
		WHERE id = #{id}
	</update>

	<update id="changeBerforProjectReportData">
		UPDATE project_report_data
		SET update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			office_id = #{officeId},
			project_id = #{project.id},
			name = #{name},
			number = #{number},
			type = #{type},
			achievement_type = #{achievementType},
			review_standard = #{reviewStandard},
			sign_cost_one = #{signCostOne.id},
			sign_cost_two = #{signCostTwo.id},
			master = #{master.id},
			report_date = #{reportDate},
			status = #{status},
			file_status = #{fileStatus}
		WHERE id = #{id}
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM project_report_change
		WHERE id = #{id}
	</update>

	<update id="deleteTree">
		DELETE FROM project_content_change
		WHERE project_id = #{project.id} and info_id = #{id}
	</update>

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

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

	<update id="deleteReportDataInfo">
		DELETE FROM project_report_content WHERE report_id = #{reportId} AND type = '2'
	</update>
	<update id="deleteReportDataBased">
		DELETE FROM project_report_content WHERE report_id = #{reportId} AND type = '4'
	</update>
	<update id="deleteContentChange">
		DELETE FROM project_content_change WHERE info_id = #{infoId}
	</update>

	<update id="reportNumber">
		UPDATE project_report_change SET
		number = #{number}
		WHERE id = #{reportId}
	</update>

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="ProjectReportData" >
		SELECT
		<include refid="projectReportDataColumns"/>
		FROM project_report_change a
		<include refid="projectReportDataJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>
	
</mapper>