<?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.CapitalTransferMapper"><!-- 找接口 --> 
    <!--查询资金划转表 列表 管理人发起后    管理人和指令业务岗能看见 -->
	<select id="selectCapitalTransferTable" parameterType="java.util.Map" resultType="com.fuzamei.entity.CapitalTransfer">
       select 	z.fund_id,
                y.person_name,
                z.fund_name,
                y.account_balance,
                z.sum_of_money,
                f.attachment_name,
                f.url,
                y.drawing_account,
                z.apply_time,
                z.handle_time,
                s.status_name 
        from 
              capital_transfer z left join 	t_user y  
        on    z. custodian=y.user_id  
      left  join   attachment f
        on   z.basis_of_payment=f.attachment_id  
      left  join  status s  on z.status_id=s.status_id
       <where>
	      <if test="fund_name!='' and fund_name!= null">
	           fund_name=#{fund_name}
	      </if>
	      <if test="status_name!='' and status_name!=null">
	          and status_name=#{status_name}
	      </if>
	      <if test="startTime!=null and endTime!=null">
		     and apply_time  between #{startTime} and #{endTime}
		  </if>
	   </where>
	        order by apply_time desc
	        limit #{startPage},#{rowNum}
	</select>
	<!--  ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||/////////////////////////////-->
	 <!--查询资金划转表 列表  指令业务岗审核通过后 核算业务岗  才能看见之前的记录  状态不在12和13 -->
	<select id="selectCapitalTransferTable2" parameterType="java.util.Map" resultType="com.fuzamei.entity.CapitalTransfer">
       select 	z.fund_id,
                y.person_name,
                z.fund_name,
                y.account_balance,
                z.sum_of_money,
                f.attachment_name,
                f.url,
                y.drawing_account,
                z.apply_time,
                z.handle_time,
                s.status_name 
        from 
              capital_transfer z left join 	t_user y  
        on    z. custodian=y.user_id  
      left  join   attachment f
        on   z.basis_of_payment=f.attachment_id  
      left  join  status s  on z.status_id=s.status_id
       <where>
            z.status_id  not in(12,13)
	      <if test="fund_name!='' and fund_name!= null">
	            and fund_name=#{fund_name}
	      </if>
	      <if test="status_name!='' and status_name!=null">
	          and status_name=#{status_name}
	      </if>
	      <if test="startTime!=null and endTime!=null">
		     and apply_time  between #{startTime} and #{endTime}
		  </if>
	   </where>
	        order by apply_time desc
	        limit #{startPage},#{rowNum}
	</select>
	<!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////  -->
	 <!--查询资金划转表 列表  指令业务岗和核算业务岗审核通过后 核算主管岗  才能看见之前的记录  状态不在12和13和14和15 -->
	<select id="selectCapitalTransferTable3" parameterType="java.util.Map" resultType="com.fuzamei.entity.CapitalTransfer">
       select 	z.fund_id,
                y.person_name,
                z.fund_name,
                y.account_balance,
                z.sum_of_money,
                f.attachment_name,
                f.url,
                y.drawing_account,
                z.apply_time,
                z.handle_time,
                s.status_name 
        from 
              capital_transfer z left join 	t_user y  
        on    z. custodian=y.user_id  
      left  join   attachment f
        on   z.basis_of_payment=f.attachment_id  
      left  join  status s  on z.status_id=s.status_id
       <where>
            z.status_id  not in(12,13,14,15)
	      <if test="fund_name!='' and fund_name!= null">
	            and fund_name=#{fund_name}
	      </if>
	      <if test="status_name!='' and status_name!=null">
	          and status_name=#{status_name}
	      </if>
	      <if test="startTime!=null and endTime!=null">
		     and apply_time  between #{startTime} and #{endTime}
		  </if>
	   </where>
	        order by apply_time desc
	        limit #{startPage},#{rowNum}
	</select>
	
	<!-- ///////////////////////////////分页//////////////////////////////////////////////////////////////////////////////////// -->
	<!--查询资金划转表   返回查询总条数   管理人和指令业务岗可以查看-->
    <select id="selectCapitalTransferPage" resultType="int" parameterType="java.util.Map">
    	   <!-- SELECT count(*) from  capital_transfer z left  join  status s  on z.status_id = s.status_id  -->
    	    select  count(*)  from  capital_transfer z  
    	 <where>
	    	<if test="fund_name != null and fund_name!=''">
	    		 fund_name=#{fund_name}
	    	</if>
	    	<if test="status_name!= null and status_name!=''">
	    		and  status_name=#{status_name}
	    	</if>
	    	<if test="startTime!=null and endTime!=null">
				and apply_time between #{startTime} and #{endTime}
		    </if>
    	 </where> 
    </select>
  <!-- ////////////////////////////////////////////////////////////////////////////////////////////////// -->
	<!--查询资金划转表   返回查询总条数   指令业务岗审核通过后核算业务岗才能看见之前审核的列表信息 -->
    <select id="selectCapitalTransferPage2" resultType="int" parameterType="java.util.Map">
    	   <!-- SELECT count(*) from  capital_transfer z left  join  status s  on z.status_id = s.status_id  -->
    	    select  count(*)  from  capital_transfer z  
    	 <where>
    	        z.status_id  not in(12,13)
	    	<if test="fund_name != null and fund_name!=''">
	    		and  fund_name=#{fund_name}
	    	</if>
	    	<if test="status_name != null and status_name !=''">
	    		and  status_name=#{status_name}
	    	</if>
	    	<if test="startTime!=null and endTime!=null">
				and apply_time between #{startTime} and #{endTime}
		    </if>
    	 </where> 
    </select>

<!--/////////////////////////////////////////////////////////////////  -->
	<!--查询资金划转表   返回查询总条数   指令业务岗和核算业务岗审核通过后核算主管岗才能看见之前的2级审核的列表的信息 -->
	  <select id="selectCapitalTransferPage3" resultType="int" parameterType="java.util.Map">
    	   <!-- SELECT count(*) from  capital_transfer z left  join  status s  on z.status_id = s.status_id  -->
    	    select  count(*)  from  capital_transfer z  
    	 <where>
    	        z.status_id  not in(12,13,14,15)
	    	<if test="fund_name!= null and fund_name!=''">
	    		and  fund_name=#{fund_name}
	    	</if>
	    	<if test="status_name!= null and status_name!=''">
	    		and  status_name=#{status_name}
	    	</if>
	    	<if test="startTime!=null and endTime!=null">
				and apply_time between #{startTime} and #{endTime}
		    </if>
    	 </where> 
    </select>
	


  <!--************************************************************************************************  -->  
  <!--查看  资金划转详情     根据前端给我返回的id   在把那一条数据查出来-->
  <select id="selectChaKanZiJinHuaZhuan" parameterType="java.util.Map" resultType="com.fuzamei.entity.CapitalTransfer">
   	 	select   y.person_name,
  	             sj.fund_name, 
			     y.account_balance,
  	             sj.sum_of_money,
  	             f.attachment_name,
			     f.url,
  	             y.drawing_account,
  	             sj.apply_time   
       from      capital_transfer sj  
       left join 
                 t_user y 
         on     
                 sj.custodian= y.user_id 
      left join 
                 attachment f 
         on      
                 sj.basis_of_payment=f.attachment_id  
    <where>
        <if test="fund_id!='' and fund_id!=null">
          fund_id=#{fund_id}
        </if>
     </where> 
  </select>
  <!--*************************************************************************************************************  -->
    <!-- 点击查看 (划转资金审核记录表)  框框详情记录 --> 
	<select id="selectCapitalTransferRecord" parameterType="java.util.Map" resultType="com.fuzamei.entity.CapitalTransferCheck">
        
 	select
	    bc.fund_id,
			bc.check_department,
			bc.check_person,
			bc.check_opinion,
			bc.check_time,
			bc.check_result ,
			s.status_name as relation
		from
			(select 
				bc.check_department,
				bc.check_person,
				bc.fund_id,
				bc.check_opinion,
				bc.check_time,
				s.status_name as check_result,
				bc.relation
			from 
				capital_transfer_check bc
			left join 
				status s
			on 
				bc.check_result=s.status_id) bc
		left join 
			status s
		on
			bc.relation=s.status_id
    
         where fund_id=#{fund_id}
         order by 
		  bc.check_time desc
			
        <where>
        <if test="fund_id!='' and fund_id!=null">
          fund_id=#{fund_id}
        </if>
     </where>  -
	</select>
	<!--////////////////////  -->
	<!-- 根据资金划转id查询状态id值  -->
	<select id="getStatusIdByCapitalId" resultType="int">
		select status_id from capital_transfer where fund_id=#{fund_id}
	</select>
	<!--===========================================以下===================================================================================  -->
	<!-- 管理人  发起  划转资金 插入到划转资金表*******************************///暂没用////////////////////////////////////////////////////// --> 
	<insert id="insertCapitalTransfer" parameterType="java.util.Map">
		insert  into  
		capital_transfer(fund_id,
                         custodian,
                         fund_name,
                         sum_of_money,
                         basis_of_payment,
                         apply_time,
                         handle_time,
                         status_id)
                  values(#{fund_id},
                         #{custodian},
                         #{fund_name},
                         #{sum_of_money},
                         #{basis_of_payment},
                         #{apply_time},
                         #{handle_time},
                         #{status_id})
	</insert>
  <!--******************************************//**////***///************************************************************************************ -->
	<!-- 将划转资金   上传依据到 ————>    的附件信息插入附件表中   （多用此方法）   -->
	<insert id="insertAttachmentInfo" 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="insertOperationHistory">
		insert into 
			operation_history(operator_type_id,
							  operator_account,
							  operator_role,
							  operator_person,
							  operator_time,
							  hash)
		values(#{operator_type_id},
			   #{operator_account},
			   #{operator_role},
			   #{operator_person},
			   #{operator_time},
			   #{hash})
	</insert>
    <!--*******************************************************************************************************  -->
    <!-- 查询划转资金表  几级审核  审核意见  详情  --><!-- 要排除状态13   状态为：   指令业务岗审核不通过就到不了核算业务岗这里审核 --><!--资金划转点击查看详情  -->
    <select id="selectCheckOpinion" parameterType="java.util.Map" resultType="com.fuzamei.entity.CapitalTransfer">
         select 
                y.person_name,
                z.fund_name,
                y.account_balance,
                z.sum_of_money,
                f.attachment_name,
                f.url,
                y.drawing_account,
                z.apply_time
          from 
               capital_transfer z left join 	t_user y  
          on    
               z. custodian=y.user_id  
         left  join   
               attachment f  
          on   z.basis_of_payment=f.attachment_id 
         left  join   
               status s 
           on  s.status_id=z.status_id 
       <where>
       			z.status_id not in (13)
       		<if test="fund_id != null and fund_id != ''">
       		  and  fund_id=#{fund_id}
       		</if>
       </where>
    </select>
    <!--*************************************************************************************************  -->
    <!--将审核意见插入到(划转资金审核记录表)里  -->
    <insert id="insertCheckTransfer" parameterType="java.util.Map">
	    insert into capital_transfer_check(fund_id,
			          check_department,
						   check_person,
						   check_opinion,
						   check_time,
						   check_result,
						   relation) 		
				values(#{fund_id},
				       #{check_department},
				       #{check_person},
				       #{check_opinion},
				       #{check_time},
				       #{check_result},
				       #{relation})
    </insert>
    <!--审核意见 查询审核记录表    (暂没用)-->
    <!-- <select id="">
    	select  s.fund_id,
    	        s.check_department,
    	        s.check_person,
    	        s.check_opinion,
    	        s.check_time,
    	        y.status_name as check_result,
				zt.status_name as relation
		 from  
		        capital_transfer_check s 
	   left join   
	           status y on  s.check_result=y.status_id  
	   left join 
	           status  zt  on  s.relation=zt.status_id	
	    	
    
    </select> -->
    <!-- 将划转审核结果意见在插入划转资金表的同时，也将审核结果更新到划转资金表中去，根据基金id  ->fund_id定位到指定的业务 -->
	<update id="updateCapitaltransferStatus" parameterType="java.util.Map">
		update 
			capital_transfer
		set
			status_id=#{statusId}
		where 
			fund_id=#{fund_id}  
	</update>
	
	<!--根据user_id 去修改账户余额  -->
	<update id="updateTUserAccountBalance"><!--用户表的账户余额  要减掉-->
       update  t_user set account_balance=#{account_balance}   where user_id=#{user_id}
	</update>
	
	<!--根据账户表的账户id来修改钱  -->
	<update id="updateAccountMoney"><!-- 账户表的账户 (钱)要加起来 -->
       update  account set money=#{money}   where account_id=#{account_id}
	</update>
	
	<!-- 查询 根据用户id查询出账户表id值--><!--暂没用  -->
	<!-- <select id="queryAccountIdByUserId" resultType="com.fuzamei.entity.UserAccount">
		select  account_id  from  user_account where  user_id=#{user_id}
	</select> -->
	
	<!--查询所有(账户名称)  返给前端    -->
	<select id="queryAccountWhereUserId" parameterType="int" resultType="com.fuzamei.entity.AccountInfo">
		select  account_name  from account_info WHERE  account_id  in
		(select account_id  from  user_account  where user_id=#{user_id})
	</select>
	
	
	
</mapper>