<?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.fuzamei.mapperInterface.ContractManagementMapper">

	<!-- 根据条件查询所有符合条件的合同状态跟踪(contract_status_tracking)信息 -->
	<select id="queryContractStatusTracking" resultType="com.fuzamei.entity.ContractStatusTracking">
		select
			sct.id,
			sct.business_id,
			sct.contract_id,
			att.attachment_name as contract_name,
			att.url as url,
			sct.contract_party,
			sct.update_time,
			sct.status_id,
			s.status_name
		from
			contract_status_tracking sct
		left join
			status s
		on
			sct.status_id=s.status_id
		left join 
			attachment att
		on 
			sct.contract_id=att.attachment_id
		<where>
			<if test="businessId!=null">
				sct.business_id=#{businessId}
			</if>
			<if test="contractParty!='' and contractParty!=null">
				and sct.contract_party=#{contractParty}
			</if>
			<if test="startTime!=null and endTime!=null">
				and sct.update_time between #{startTime} and #{endTime}
			</if>
		</where>
		order by
			sct.update_time desc
		limit
			 #{startPage},#{rowNum}
	</select>
	
	<!-- 根据条件查询所有符合条件的合同状态跟踪信息(contract_status_tracking)条数 -->
	<select id="findAllContractStatusTrackingInformation" resultType="int">
		select
			count(*)
		from
			contract_status_tracking
		<where>
			<if test="businessId!=null">
				business_id=#{businessId}
			</if>
			<if test="contractParty!='' and contractParty!=null">
				and contract_party=#{contractParty}
			</if>
			<if test="startTime!=null and endTime!=null">
				and update_time between #{startTime} and #{endTime}
			</if>
		</where>
	</select>
	
	<!-- 根据条件查询所有符合条件的合同记录表(contract_record)表信息 -->
	<select id="queryContractRecord" resultType="com.fuzamei.entity.ContractRecord">
		select 
			cr.id,
			cr.business_id,
			cr.contract_id,
			cr.contract_name,
			cr.version,
			cr.contract_party,
			cr.upload_department,
			cr.upload_person,
			cr.modify_opinion,
			cr.unconfirm_department,
			s.status_name,
			cr.status_id,
			cr.commit_time,
			cr.hash
		from 
			contract_record cr 
		left join 
			status s
		on 
			s.status_id=cr.status_id
		where 
			cr.business_id=#{businessId}
		order by
			cr.commit_time desc
		limit
			 #{startPage},#{rowNum}
	</select>
	
	<!-- 根据条件查询所有符合条件的合同记录表(contract_record)信息条数 -->
	<select id="findAllContractRecordInformation" resultType="int">
		select 
			count(*)
		from 
			contract_record
		where 
			business_id=#{businessId}
	</select>
	
	<!-- 根据业务id查询ContractStatusTracking的详细信息 -->
	<select id="getContractStatusTrackingByBusinessId" resultType="com.fuzamei.entity.ContractStatusTracking">
		select
			sct.id,
			sct.business_id,
			sct.contract_id,
			att.attachment_name as contract_name,
			att.url as url,
			sct.contract_party,
			sct.update_time,
			sct.status_id,
			s.status_name
		from
			contract_status_tracking sct
		left join
			status s
		on
			sct.status_id=s.status_id
		left join 
			attachment att
		on 
			sct.contract_id=att.attachment_id
		where
			sct.business_id=#{businessId}
	</select>
	
	<!-- 将数据插入附件表中 -->
	<insert id="insertInformationToAttachment">
		insert into 
			attachment(attachment_id,
					   attachment_name,
					   url,
					   upload_person_id,
					   create_time)
		values(#{attachmentId},
			   #{attachmentName},
			   #{url},
			   #{userId},
			   #{createTime})
	</insert>
	
	<!-- 将信息插入到操作记录表中 -->
	<insert id="insertInformationToOperationHistory">
		insert into 
			operation_history(operator_type_id,
							  operator_account,
							  operator_role,
							  operator_person,
							  operator_time,
							  hash)
		values(#{operatorTypeId},
			   #{operatorAccount},
			   #{operatorRole},
			   #{operatorPerson},
			   #{operatorTime},
			   #{hash})
	</insert>
	
	<!-- 根据业务id号获取最新的合同记录表中的一条信息 -->
	<select id="getLatestContractRecordByBusinessId" resultType="com.fuzamei.entity.ContractRecord">
		select 
			cr.id,
			cr.business_id,
			cr.contract_id,
			cr.contract_name,
			cr.version,
			cr.contract_party,
			cr.upload_department,
			cr.upload_person,
			cr.modify_opinion,
			cr.unconfirm_department,
			s.status_name,
			cr.status_id,
			cr.commit_time,
			cr.hash
		from 
			contract_record cr 
		left join 
			status s
		on 
			s.status_id=cr.status_id
		where 
			cr.business_id=#{businessId}
		order by
			cr.version desc
		limit 1
	</select>
	
	<!-- 将合同记录信息插入到合同记录表中 -->
	<insert id="insertInformationToContractRecord">
		insert into 
			contract_record(business_id,
							contract_id,
							contract_name,
							version,
							contract_party,
							upload_department,
							upload_person,
							modify_opinion,
							unconfirm_department,
							status_id,
							commit_time,
							hash)
		values(#{businessId},
			   #{contractId},
			   #{contractName},
			   #{version},
			   #{contractParty},
			   #{uploadDepartment},
			   #{uploadPerson},
			   #{modifyOpinion},
			   #{uncomfirmDepartment},
			   #{statusId}
			   #{commitTime},
			   #{hash})
	</insert>
	
	<!-- 对合同状态跟踪表中的更新时间和合同id号进行修改 -->
	<update id="updateContractStatusTracking">
		update 
			contract_status_tracking
		set 
			contract_id=#{contractId},
			update_time=#{updateTime}
		where
			business_id=#{businessId}
	</update>
	
	<!-- 根据业务id和版本号获取当前的一条合同记录详细信息 -->
	<select id="getCurrentContractRecordByBusinessId" resultType="com.fuzamei.entity.ContractRecord">
		select 
			cr.id,
			cr.business_id,
			cr.contract_id,
			cr.contract_name,
			cr.version,
			cr.contract_party,
			cr.upload_department,
			cr.upload_person,
			cr.modify_opinion,
			cr.unconfirm_department,
			s.status_name,
			cr.status_id,
			cr.commit_time,
			cr.hash
		from 
			contract_record cr 
		left join 
			status s
		on 
			s.status_id=cr.status_id
		where 
			cr.business_id=#{businessId} 
			and cr.version=#{version}
	</select>
	
	<!-- 更新合同记录表的信息 -->
	<update id="updateContractRecord">
		update 
			contract_record
		set
			commit_time=#{commitTime},
			unconfirm_department=#{unconfirmed},
			status_id=#{statusId}
		where
			business_id=#{businessId}
			and version=#{version}
	</update>
	
	<!-- 在指定业务id下将除了目标version以外的所有信息全部置为作废 -->
	<update id="updateOtherContractRecord">
		update 
			contract_record
		set
			status_id=#{statusId},
			commit_time=#{commitTime}
		where
			business_id=#{businessId}
			and version not in(#{version})
	</update>
	
	<!-- 当整个合同被确认以后，更新合同状态跟踪表中的状态，时间和合同id号 -->
	<update id="updateContractStatusTrackingStatusAndContractId">
		update 
			contract_status_tracking
		set 
			contract_id=#{contractId},
			update_time=#{updateTime},
			status_id=#{statusId}
		where
			business_id=#{businessId}
	</update>
</mapper>