<?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.ValuationAccountingMapper"> 
	
	<!-- 分页查询所有数据量，用于分页使用 -->
	<select id="findAllInformations" resultType="int">
		select 
			count(*) 
		from 
			valuation_accounting
		<where>
			<if test="fundName!='' and fundName!=null">
				fund_name=#{fundName}
			</if>
			<if test="battleResult!='' and battleResult!=null">
				and battle_result=#{battleResult}
			</if>
			<if test="startTime!=null and endTime!=null">
				and update_time between #{startTime} and #{endTime}
			</if>
		</where>
	</select>
	
	<!-- 根据条件查询估值核算的信息 -->
	<select id="queryValuationAccountingInformation" resultType="com.fuzamei.entity.ValuationAccount">
		select 
			temp.fund_id,
			temp.fund_name,
			temp.assets_under_custody,
			temp.attachment_name attachment_name_of_custodian,
			att.attachment_name attachment_name_of_bank,
			temp.url url_of_custodian,
			att.url url_of_bank,
			temp.update_time,
			temp.battle_result
		from
			(select 
				va.fund_id,
				va.fund_name,
				va.assets_under_custody,
				att.attachment_name,
				va.bank_valuation_id,
				va.battle_result,
				att.url,
				va.update_time
			 from 
				valuation_accounting va
			 left join 
			 	attachment att
			 on 
			 va.custodian_valuation_id=att.attachment_id) temp 
		left join 
			attachment att
		on 
			temp.bank_valuation_id = att.attachment_id
		<where>
			<if test="fundName!='' and fundName!=null">
				temp.fund_name=#{fundName}
			</if>
			<if test="battleResult!='' and battleResult!=null">
				and temp.battle_result=#{battleResult}
			</if>
			<if test="startTime!=null and endTime!=null">
				and temp.update_time between #{startTime} and #{endTime}
			</if>
		</where>
		order by 
			temp.update_time asc
		limit 
			#{startPage},#{rowNum}
	</select>
	
	<!-- 管理岗上传管理人估值报表的时候插入的数据，分别插入valuation_accounting和attachment表，但是注意这个时候银行估值报表暂时不进行插入 -->
	<insert id="insertInformationIntoTableByAdmin">
		insert into 
			valuation_accounting(fund_id,
								 fund_name,
								 assets_under_custody,
								 custodian_valuation_id,
								 update_time,
								 battle_result)
		values(#{fundId},
			   #{fundName},
			   #{assets},
			   #{attachmentId},
			   #{updateTime},
			   #{battleResult})
	</insert>
	
	<!-- 将上文的文件信息插入到附件表中去 -->
	<insert id="insertInformationToAttachment">
		insert into 
			attachment(attachment_id,
					   attachment_name,
					   url,
					   upload_person_id,
					   create_time)
		values(#{attachmentId},
			   #{attachmentName},
			   #{url},
			   #{userId},
			   #{addTime})
	</insert>
	
	<!-- 银行上传一份估值核算文件后更新之前管理员上传文件插入的那条信息 -->
	<update id="updateInformationByBank">
		update
			valuation_accounting 
		set 
			bank_valuation_id=#{attachmentId},
			update_time=#{updateTime},
			battle_result=#{battleResult}
		where 
				fund_id=#{fundId}
	</update>
	
	<!-- 省分行上传估值核算文件，在操作记录中要插入这个数据 -->
	<insert id="insertOperationHistoryInformationToDao">
		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="checkoutDownloadInformation" resultType="com.fuzamei.entity.ValuationAccount">
		select 
			temp.fund_id,
			temp.fund_name,
			temp.assets_under_custody,
			temp.attachment_name attachment_name_of_custodian,
			att.attachment_name attachment_name_of_bank,
			temp.url url_of_custodian,
			att.url url_of_bank,
		from
			(select 
				va.fund_id,
				va.fund_name,
				va.assets_under_custody,
				att.attachment_name,
				va.bank_valuation_id,
				va.battle_result,
				att.url,
				va.update_time
			 from 
				valuation_accounting va
			 left join 
			 	attachment att
			 on 
			 va.custodian_valuation_id=att.attachment_id
			 where 
			 	va.fund_id=#{fundId}) temp 
		left join 
			attachment att
		on 
			temp.bank_valuation_id = att.attachment_id
		order by 
			temp.update_time asc
	</select>
	
	<!-- 对是否有重复的fundId进行查询并返回重复结果 -->
	<select id="queryIfHasTheSameFundId" resultType="int">
		select count(*) from valuation_accounting where fund_id=#{fundId}
	</select>

	<!-- 对是否有重复的fundId进行查询并返回重复结果 -->
	<select id="queryIfHasTheSameFundName" resultType="int">
		select count(*) from valuation_accounting where fund_name=#{fundName}
	</select>
	
	
	
	<!-- 对是否有存在基金数据进行查询并返回查询到的结果 -->
	<select id="checkIfFundExists" resultType="com.fuzamei.entity.ValuationAccount">
		select 
			temp.fund_id,
			temp.fund_name,
			temp.assets_under_custody,
			temp.custodian_valuation_id attachment_id_of_custodian,
			temp.bank_valuation_id attachment_id_of_bank,
			temp.attachment_name attachment_name_of_custodian,
			att.attachment_name attachment_name_of_bank,
			temp.url url_of_custodian,
			att.url url_of_bank,
			temp.update_time,
			temp.battle_result
		from
			(select 
				va.fund_id,
				va.fund_name,
				va.assets_under_custody,
				att.attachment_name,
				va.custodian_valuation_id,
				va.bank_valuation_id,
				va.battle_result,
				att.url,
				va.update_time
			 from 
				valuation_accounting va
			 left join 
			 	attachment att
			 on 
			 va.custodian_valuation_id=att.attachment_id) temp 
		left join 
			attachment att
		on 
			temp.bank_valuation_id = att.attachment_id
		where
			temp.fund_name=#{fundName} and temp.fund_id=#{fundId}
	</select>
	
	<!-- 管理员再次上传估值文件时修改总表中的信息 -->
	<update id="updateInformationByAdmin">
		update
			valuation_accounting
		set 
			update_time=#{updateTime},
			battle_result=#{battleResult}
		where 
			fund_id=#{fundId}
	</update>
	
	<!-- 管理人修改附件信息 -->
	<update id="updateAttachmentInformationByAdmin">
		update
			attachment
		set 
			attachment_name=#{attachmentName},
			url=#{url},
			upload_person_id=#{userId},
			create_time=#{addTime}
		where 
			attachment_id=#{attachmentId}
	</update>
	
	<!-- 银行修改附件信息 -->
	<update id="updateAttachmentInformationByBank">
		update
			attachment
		set 
			attachment_name=#{attachmentName},
			url=#{url},
			upload_person_id=#{userId},
			create_time=#{addTime}
		where 
			attachment_id=#{attachmentId}
	</update>
	
</mapper>
