<?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.workreceiptsrevise.dao.WorkReceiptsReviseDao">
    
	<sql id="workReceiptsReviseColumns">
		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.office_name AS "officeName",
		a.office_num AS "officeNum",
		a.revise_date AS "reviseDate",
		a.user_name AS "userName",
		a.revise_type AS "reviseType",
		a.contract_info_id AS "workContractInfo.id",
		a.inrevise_office AS "inreviseOffice.id",
		a.revise_money AS "reviseMoney",
		a.process_instance_id AS "processInstanceId",
		a.invalid_process_instance_id AS "invalidProcessInstanceId",
		a.invalid_status AS "invalidStatus",
		a.office_id AS "officeId",
		a.company_id AS "companyId",
		workContractInfo.name AS "workContractInfo.name",
		so.name AS "inreviseOffice.name",
		su.name AS "createBy.name",
		o.top_company AS "office.name",
		a.status AS "status"
	</sql>
	
	<sql id="workReceiptsReviseJoins">
		LEFT JOIN work_contract_info workContractInfo ON workContractInfo.id = a.contract_info_id
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		left JOIN sys_user su on su.id = a.create_by
		left join sys_office so on so.id = a.inrevise_office
	</sql>
	
    
	<select id="get" resultType="WorkReceiptsRevise" >
		SELECT 
			<include refid="workReceiptsReviseColumns"/>
		FROM work_receipts_revise a
		<include refid="workReceiptsReviseJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkReceiptsRevise" >
		SELECT 
			<include refid="workReceiptsReviseColumns"/>
		FROM work_receipts_revise a
		<include refid="workReceiptsReviseJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="officeNum != null and officeNum != ''">
				AND a.office_num = #{officeNum}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="reviseType != null and reviseType != ''">
				AND a.revise_type = #{reviseType}
			</if>
			<if test="officeName != null and officeName != ''">
				AND o.name LIKE
				<if test="dbName == 'oracle'">'%'||#{officeName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{officeName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{officeName},'%')</if>
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.revise_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</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.revise_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findListInvalid" resultType="WorkReceiptsRevise" >
		SELECT
		<include refid="workReceiptsReviseColumns"/>
		FROM work_receipts_revise a
		<include refid="workReceiptsReviseJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and a.invalid_status is not null
			<if test="officeNum != null and officeNum != ''">
				AND a.office_num = #{officeNum}
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.revise_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</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.revise_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkReceiptsRevise" >
		SELECT 
			<include refid="workReceiptsReviseColumns"/>
		FROM work_receipts_revise a
		<include refid="workReceiptsReviseJoins"/>
		<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 work_receipts_revise(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_name,
			office_num,
			revise_date,
			user_name,
			revise_type,
			contract_info_id,
			inrevise_office,
			revise_money,
			process_instance_id,
			invalid_process_instance_id,
			invalid_status,
			office_id,
			company_id,
			status
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{officeName},
			#{officeNum},
			#{reviseDate},
			#{userName},
			#{reviseType},
			#{workContractInfo.id},
			#{inreviseOffice.id},
			#{reviseMoney},
			#{processInstanceId},
			#{invalidProcessInstanceId},
			#{invalidStatus},
			#{officeId},
			#{companyId},
			#{status}
		)
	</insert>
	
	<update id="update">
		UPDATE work_receipts_revise SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_name = #{officeName},
			office_num = #{officeNum},
			revise_date = #{reviseDate},
			user_name = #{userName},
			revise_type = #{reviseType},
			contract_info_id = #{workContractInfo.id},
			inrevise_office = #{inreviseOffice.id},
			revise_money = #{reviseMoney},
			process_instance_id = #{processInstanceId},
			invalid_process_instance_id = #{invalidProcessInstanceId},
			invalid_status = #{invalidStatus},
			office_id = #{officeId},
			company_id = #{companyId},
			status = #{status}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_receipts_revise
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_receipts_revise SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkReceiptsRevise" statementType="STATEMENT">
		select * FROM work_receipts_revise  where ${propertyName} = '${value}'
	</select>

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

	<update id="updateInvalidProcessInstanceId">
		UPDATE work_receipts_revise SET
		invalid_process_instance_id = #{invalidProcessInstanceId}
		WHERE id = #{id}
	</update>
	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkReceiptsRevise">
		SELECT
		<include refid="workReceiptsReviseColumns"/>
		FROM work_receipts_revise a
		<include refid="workReceiptsReviseJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

	<!-- 获取流程ID -->
	<select id="getByInvalidProcessInstanceId" resultType="WorkReceiptsRevise">
		SELECT
		<include refid="workReceiptsReviseColumns"/>
		FROM work_receipts_revise a
		<include refid="workReceiptsReviseJoins"/>
		WHERE a.invalid_process_instance_id = #{invalidProcessInstanceId}
	</select>
</mapper>