<?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，namespace的值习惯上设置成包名+sql映射文件名，这样就能够保证namespace的值是唯一的,例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
-->               
<mapper namespace="com.fuzamei.mapperInterface.BusinessDeclareMapper"> 
	
	<!-- 支行业务申请，将新的数据插入到数据库中的业务申请表中 -->
	<insert id="insertBusinessDeclareInformationToDaoByBranchBank">
		insert into 
			business_declare(business_id,
							 bank_id,
							 business_name,
							 business_approval_id,
							 responsible_investigation_id,
							 product_approval_id,
							 basic_data_id,
							 declare_time,
							 status_id,
							 hash)
		values(#{businessId},
			   #{bank},
			   #{businessName},
			   #{busineeApprovalId},
			   #{responsibleInvestigationId},
			   #{productApprovalId},
			   #{basicDataId},
			   #{declareTime},
			   #{statusId},
			   #{hash})
	</insert>
	<!-- 将附件信息插入附件表中 -->
	<insert id="insertAttachmentInformationToDaoByBranchBank">
		insert into 
			attachment(attachment_id,
					   attachment_name,
					   url,
					   upload_person_id,
					   create_time)
		values(#{attachmentId},
			   #{attachmentName},
			   #{url},
			   #{userId},
			   #{createTime})
	</insert>
	
	<!-- 当经办支行进行业务申报的时候往操作记录表中插入一条数据 -->
	<insert id="insertOperationHistoryInformationToDaoByBranchBank">
		insert into 
			operation_history(operator_type_id,
							  operator_account,
							  operator_role,
							  operator_person,
							  operator_time,
							  hash)
		values(#{operatorTypeId},
			   #{operatorAccount},
			   #{operatorRole},
			   #{operatorPerson},
			   #{operatorTime},
			   #{hash})
	</insert>
	
	<!-- 返回总页数 -->
	<select id="findAllInfromations" resultType="int">
		select 
			count(*) 
		from 
			business_declare
		<where>
			<if test="businessName!='' and businessName!=null">
				business_name=#{businessName} 
			</if>
			<if test="startTime!=null and endTime!=null">
				and declare_time between #{startTime} and #{endTime}
			</if>
		</where>
	</select>
	
	<!-- 根据条件或不根据条件查询业务申请信息，并返回给前端，需要进行6表关联查询，1张业务申请表，4张附件表，1张状态表 -->
	<select id="queryBusinessDeclareInformation" resultType="com.fuzamei.entity.BusinessDeclare">
		select 
			bd.business_id,
			bd.bank_id,
			bd.business_name,
			a1.attachment_name as business_approval_name,
			a1.url as business_approval_url,
			a2.attachment_name as responsible_investigation_name,
			a2.url as responsible_investigation_url,
			a3.attachment_name as product_approval_name,
			a3.url as product_approval_url,
			a4.attachment_name as basic_data_name,
			a4.url as basic_data_url,
			bd.declare_time,
			s.status_name,
			bd.hash
		from 
			business_declare bd
		left join 
			attachment a1
		on 
			a1.attachment_id=bd.business_approval_id
		left join 
			attachment a2
		on 
			a2.attachment_id=bd.responsible_investigation_id
		left join 
			attachment a3
		on 
			a3.attachment_id=bd.product_approval_id
		left join 
			attachment a4
		on 
			a4.attachment_id=bd.basic_data_id
		left join 
			status s
		on 
			s.status_id=bd.status_id
		<where>
			<if test="businessName!='' and businessName!=null">
				bd.business_name=#{businessName} 
			</if>
			<if test="startTime!=null and endTime!=null">
				and bd.declare_time between #{startTime} and #{endTime}
			</if>
		</where>
		order by 
			bd.declare_time desc 
		limit 
			#{startPage},#{rowNum}
	</select>
	
	<!-- 返回省分行查询时的总条数 -->
	<select id="findAllInfromationsByPBB" resultType="int">
		select 
			count(*) 
		from 
			business_declare 
		<where>
			<!-- 省分行能看到的要排除待二级分行审核和二级分行审核不通过的业务 -->
			status_id not in (1,2)
			<if test="businessName!='' and businessName!=null">
				and business_name=#{businessName} 
			</if>
			<if test="startTime!=null and endTime!=null">
				and declare_time between #{startTime} and #{endTime}
			</if>
		</where>
	</select>
	
	<!-- 当省分行来查看业务申报表的时候给展现的数据，要除去状态1和状态2【分别对应待二级分行审核和二级分行审核不通过】 -->
	<select id="queryBusinessDeclareInformationByPBB" resultType="com.fuzamei.entity.BusinessDeclare">
		select 
			bd.business_id,
			bd.bank_id,
			bd.business_name,
			a1.attachment_name as business_approval_name,
			a1.url as business_approval_url,
			a2.attachment_name as responsible_investigation_name,
			a2.url as responsible_investigation_url,
			a3.attachment_name as product_approval_name,
			a3.url as product_approval_url,
			a4.attachment_name as basic_data_name,
			a4.url as basic_data_url,
			bd.declare_time,
			s.status_name,
			bd.hash
		from 
			business_declare bd
		left join 
			attachment a1
		on 
			a1.attachment_id=bd.business_approval_id
		left join 
			attachment a2
		on 
			a2.attachment_id=bd.responsible_investigation_id
		left join 
			attachment a3
		on 
			a3.attachment_id=bd.product_approval_id
		left join 
			attachment a4
		on 
			a4.attachment_id=bd.basic_data_id
		left join 
			status s
		on 
			s.status_id=bd.status_id
		<where>
			<!-- 省分行能看到的要排除待二级分行审核和二级分行审核不通过的业务 -->
			bd.status_id not in (1,2)
			<if test="businessName!='' and businessName!=null">
				and bd.business_name=#{businessName} 
			</if>
			<if test="startTime!=null and endTime!=null">
				and bd.declare_time between #{startTime} and #{endTime}
			</if>
		</where>
		order by 
			bd.declare_time desc 
		limit 
			#{startPage},#{rowNum}
	</select>
	
	<!-- 查看审核记录的sql语句 -->
	<select id="queryBusinessCheckInformation" resultType="com.fuzamei.entity.BusinessCheck">
		select
			bc.check_department,
			bc.check_person,
			bc.check_business_id,
			bc.check_opinion,
			bc.check_time,
			bc.result,
			s.status_name as task
		from
			(select 
				bc.check_department,
				bc.check_person,
				bc.check_business_id,
				bc.check_opinion,
				bc.check_time,
				s.status_name as result,
				bc.task
			from 
				business_check bc
			left join 
				status s
			on 
				bc.check_result=s.status_id) bc
		left join 
			status s
		on
			bc.task=s.status_id
		where 
			bc.check_business_id=#{businessId}
		order by 
			bc.check_time desc
	</select>
	
	<!-- 将审核意见插入业务审核表中去 -->
	<insert id="businessDeclareCheckedBy">
		insert into 
			business_check(check_department,
						   check_person,
						   check_business_id,
						   check_opinion,
						   check_time,
						   check_result,
						   task)
		values(#{organizationName},
			   #{checkPerson},
			   #{businessId},
			   #{checkOpinion},
			   #{checkTime},
			   #{checkResult},
			   #{task})
	</insert>
	
	<!-- 将审核结果意见在插入审核表的同时，也将审核结果更新到业务申报表中去，根据业务id（business_id）修改指定的业务 -->
	<update id="updateBusinessDeclareStatus">
		update 
			business_declare
		set
			status_id=#{statusId}
		where 
			business_id=#{businessId}
	</update>
	
	
	<!-- 省分行业务部门第一次上传合同文件到合同提交记录表中去 -->
	<insert id="insertContractInformationToContractRecord">
		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>
	
	<!-- 省分行业务部门第一次上传合同文件到合同状态跟踪表中去 -->
	<insert id="insertContractInformationToContractStatusTracking">
		insert into 
			contract_status_tracking(business_id,
									 contract_id,
									 contract_party,
									 update_time,
									 status_id)
		values(#{businessId},
			   #{contractId},
			   #{contractParty},
			   #{updateTime},
			   #{statusId})
	</insert>
	
	<!-- 将上传的合同文件信息插入到附件表中 -->
	<insert id="insertContractInformationToAttachment">
		insert into 
			attachment(attachment_id,
					   attachment_name,
					   url,
					   upload_person_id,
					   create_time)
		values(#{attachmentId},
			   #{attachmentName},
			   #{url},
			   #{userId},
			   #{createTime})
	</insert>
	
	<!-- 对业务id进行查重 -->
	<select id="queryIfHasTheSameBusinessId" resultType="int">
		select count(*) from business_declare where business_id=#{businessId}
	</select>
	
	<!-- 根据业务id查询状态id值 -->
	<select id="getStatusIdByBusinessId" resultType="int">
		select status_id from business_declare where business_id=#{businessId}
	</select>
</mapper>
