<?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.AccountMaintenanceMapper"> 
    <!--查询账户开立信息维护   列表-->
	<select id="selectAccountMaintenanceInfo" parameterType="java.util.Map" resultType="com.fuzamei.entity.AccountOpeningRecord">
	 select  DISTINCT
			customer_id,
			customer_name,
			bank_account,
			f.attachment_name as basic_data_id,
			f.url as basicUrl,
			f1.attachment_name as letter_of_commitment_id,
			f1.url as letterUrl,
			f2.attachment_name as application_id,
			f2.url  as applicationUrl,
			update_time,
			s.status_name 
		from  
			      account_opening_record a
		LEFT JOIN  attachment f
		
			on a.basic_data_id=f.attachment_id 
	  	LEFT JOIN	attachment f1
	  	on	a.letter_of_commitment_id = f1.attachment_id 
	  	LEFT JOIN attachment f2
	  	on	a.application_id= f2.attachment_id 
	  	LEFT JOIN status s
	  	on a.status_id = s.status_id	
	 <where>
		    <if test="customer_name!='' and customer_name!=null ">
			  customer_name=#{customer_name}
			</if>
			<if test="bank_account!='' and bank_account!=null">
			  and bank_account=#{bank_account}
			</if>
		   	<if test="startTime!=null and endTime!=null">
			  and a.update_time  between #{startTime} and #{endTime}
			</if>
	 </where>
	             order by update_time desc
	            limit #{startPage},#{rowNum}
	</select>
	
	<!--查询账户维护开立信息总页数  返回一个int类型     待条件查询出来的也要分页-->
	<select id="selectCountAccountMaintenancePage" resultType="int">
	  select count(*) from account_opening_record
	  	 <where>
		    <if test="customer_name!='' and customer_name!=null ">
			  customer_name=#{customer_name}
			</if>
			<if test="bank_account!='' and bank_account!=null">
			  and bank_account=#{bank_account}
			</if>
		   	<if test="startTime!=null and endTime!=null">
			  and update_time  between #{startTime} and #{endTime}
			</if>
	 </where>
	</select>
	
	<!-- 添加上传账户维护信息表  添加到账户维护开立表 -->
    <insert id="insertIntoAccountMaintenanceInfo" parameterType="java.util.Map">
    	   insert into account_opening_record(
	              customer_id,
	              customer_name,
	              bank_account,
	              basic_data_id,
	              letter_of_commitment_id,
	              application_id,
	              update_time,
	              status_id,
	              hash)
	       values(
	            #{customer_id},
	            #{customer_name},
	            #{bank_account},
	            #{basicAttachment_id},
	            #{letterAttachment_id},
	            #{applicationAttachment_id},
	            #{update_time},
	            #{status_id},
	            #{hash})
	           
    </insert>
    
    <!-- 上传其他凭证，承诺书，开户申请书    经办支行添加创建账号和客户名称  上传到附件表--> 
	<insert id="insertAccountMaintenanceInfoFuJianBiao" parameterType="java.util.Map">
	       <!--  插入到附件表-->
		   insert into attachment (
		                attachment_id,
		                attachment_name,
		                url,
		                upload_person_id,
		                create_time) 
		          values(
		               #{attachment_id},
		               #{attachment_name},
		               #{url},
		               #{upload_person_id},
		               #{create_time} )
	</insert>
	
	<!-- 当经办支行进行账户开立维护上传的时候往（操作记录表)中插入一条数据    可公用-->
	<insert id="insertAccountMaintenanceInfoOperationHistory">
			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="selectAccountMaintenanceNoPage" parameterType="java.util.Map" resultType="com.fuzamei.entity.AccountOpeningRecord">
	select   DISTINCT
			customer_id,
			customer_name,
			bank_account,
			f.attachment_name as basic_data_id,
			f.url   as basicUrl,
			f1.attachment_name as letter_of_commitment_id,
			f1.url as letterUrl ,
			f2.attachment_name as application_id,
			f2.url  as applicationUrl,
			update_time
		
		from  
			account_opening_record a
			left  join  attachment f
			on a.basic_data_id=f.attachment_id 
	     	left  join	attachment f1
	    	on	a.letter_of_commitment_id = f1.attachment_id 
	  	    left  join attachment f2
	  	    on	a.application_id= f2.attachment_id 
		 where  customer_id=#{customer_id}
    </select>
   

</mapper>