<?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="cn.pluss.platform.mapper.MerchantIncomeMapper">

	   	<insert id="saveMerchantIncome" parameterType="cn.pluss.platform.entity.MerchantIncome">
        	insert into tb_pluss_merchant_income(userId,merchantCode,merchantName,storeId,storeName,dealType,dealFee,incomeFee,createTime,payTypeCode,payTypeName,memberCode,memberName,memberPic,incomeType,plateformProfit,promoterProfit,dealStatus,dealCode)VALUES(#{userId},#{merchantCode},#{merchantName},#{storeId},#{storeName},#{dealType},#{dealFee},#{incomeFee},now(),#{payTypeCode},#{payTypeName},#{memberCode},#{memberName},#{memberPic},#{incomeType},#{plateformProfit},#{promoterProfit},#{dealStatus},#{dealCode})
    	</insert>

	   	<insert id="saveMerchantIncomeBatch">
        	insert into tb_pluss_merchant_income(userId,merchantCode,merchantName,storeId,storeName,dealType,dealFee,incomeFee,createTime,payTypeCode,payTypeName,memberCode,memberName,memberPic,incomeType,plateformProfit,promoterProfit,dealStatus,dealCode) VALUES 
    			<foreach collection="list" item="item" index="index" separator=",">
			(#{userId},#{merchantCode},#{merchantName},#{storeId},#{storeName},#{dealType},#{dealFee},#{incomeFee},#{createTime},#{payTypeCode},#{payTypeName},#{memberCode},#{memberName},#{memberPic},#{incomeType},#{plateformProfit},#{promoterProfit},#{dealStatus},#{dealCode}		)
				</foreach>
    	</insert>

		<select id="queryMerchantIncome" parameterType="cn.pluss.platform.entity.MerchantIncome" resultType="cn.pluss.platform.entity.MerchantIncome">
			SELECT * from tb_pluss_merchant_income 
				<where> 
					<if test="id!= null"> 
					 	and id = #{id}
					 </if> 
					<if test="userId!= null"> 
					 	and userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					 	and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 	and merchantName = #{merchantName}
					 </if> 
					<if test="storeId!= null"> 
					 	and storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					 	and storeName = #{storeName}
					 </if> 
					<if test="dealType!= null"> 
					 	and dealType = #{dealType}
					 </if> 
					<if test="dealFee!= null"> 
					 	and dealFee = #{dealFee}
					 </if> 
					<if test="incomeFee!= null"> 
					 	and incomeFee = #{incomeFee}
					 </if> 
					<if test="createTime!= null"> 
					 	and createTime = #{createTime}
					 </if> 
					<if test="payTypeCode!= null"> 
					 	and payTypeCode = #{payTypeCode}
					 </if> 
					<if test="payTypeName!= null"> 
					 	and payTypeName = #{payTypeName}
					 </if> 
					<if test="memberCode!= null"> 
					 	and memberCode = #{memberCode}
					 </if> 
					<if test="memberName!= null"> 
					 	and memberName = #{memberName}
					 </if> 
					<if test="memberPic!= null"> 
					 	and memberPic = #{memberPic}
					 </if> 
					<if test="incomeType!= null"> 
					 	and incomeType = #{incomeType}
					 </if> 
					<if test="plateformProfit!= null"> 
					 	and plateformProfit = #{plateformProfit}
					 </if> 
					<if test="promoterProfit!= null"> 
					 	and promoterProfit = #{promoterProfit}
					 </if> 
					 <if test="dealStatus!= null"> 
					 	and dealStatus = #{dealStatus}
					 </if> 
					 <if test="dealCode!= null"> 
					 	and dealCode = #{dealCode}
					 </if> 
				 </where> 
		</select>

		<select id="queryMerchantIncomeList" parameterType="cn.pluss.platform.entity.MerchantIncome" resultType="cn.pluss.platform.entity.MerchantIncome">
			SELECT * from tb_pluss_merchant_income 
				<where> 
					<if test="id!= null"> 
					 and 	id = #{id}
					 </if> 
					<if test="userId!= null"> 
					 and 	userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					 and 	merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 and 	merchantName = #{merchantName}
					 </if> 
					<if test="storeId!= null"> 
					 and 	storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					 and 	storeName = #{storeName}
					 </if> 
					<if test="dealType!= null"> 
					 and 	dealType = #{dealType}
					 </if> 
					<if test="dealFee!= null"> 
					 and 	dealFee = #{dealFee}
					 </if> 
					<if test="incomeFee!= null"> 
					 and 	incomeFee = #{incomeFee}
					 </if> 
					<if test="createTime!= null"> 
					 and 	createTime = #{createTime}
					 </if> 
					<if test="payTypeCode!= null"> 
					 and 	payTypeCode = #{payTypeCode}
					 </if> 
					<if test="payTypeName!= null"> 
					 and 	payTypeName = #{payTypeName}
					 </if> 
					<if test="memberCode!= null"> 
					 and 	memberCode = #{memberCode}
					 </if> 
					<if test="memberName!= null"> 
					 and 	memberName = #{memberName}
					 </if> 
					<if test="memberPic!= null"> 
					 and 	memberPic = #{memberPic}
					 </if> 
					<if test="incomeType!= null"> 
					 and 	incomeType = #{incomeType}
					 </if> 
					<if test="plateformProfit!= null"> 
					 and 	plateformProfit = #{plateformProfit}
					 </if> 
					<if test="promoterProfit!= null"> 
					 and 	promoterProfit = #{promoterProfit}
					 </if> 
					 <if test="dealStatus!= null"> 
					 and 	dealStatus = #{dealStatus}
					 </if> 
					 <if test="dealCode!= null"> 
					 and 	dealCode = #{dealCode}
					 </if> 
				 </where> 
		</select>

		<update id="updateMerchantIncome" parameterType="cn.pluss.platform.entity.MerchantIncome" >
			update tb_pluss_merchant_income 
				  <set> 
					<if test="userId!= null"> 
					 	userId = #{userId},
					 </if> 
					<if test="merchantCode!= null"> 
					 	merchantCode = #{merchantCode},
					 </if> 
					<if test="merchantName!= null"> 
					 	merchantName = #{merchantName},
					 </if> 
					<if test="storeId!= null"> 
					 	storeId = #{storeId},
					 </if> 
					<if test="storeName!= null"> 
					 	storeName = #{storeName},
					 </if> 
					<if test="dealType!= null"> 
					 	dealType = #{dealType},
					 </if> 
					<if test="dealFee!= null"> 
					 	dealFee = #{dealFee},
					 </if> 
					<if test="incomeFee!= null"> 
					 	incomeFee = #{incomeFee},
					 </if> 
					<if test="createTime!= null"> 
					 	createTime = #{createTime},
					 </if> 
					<if test="payTypeCode!= null"> 
					 	payTypeCode = #{payTypeCode},
					 </if> 
					<if test="payTypeName!= null"> 
					 	payTypeName = #{payTypeName},
					 </if> 
					<if test="memberCode!= null"> 
					 	memberCode = #{memberCode},
					 </if> 
					<if test="memberName!= null"> 
					 	memberName = #{memberName},
					 </if> 
					<if test="memberPic!= null"> 
					 	memberPic = #{memberPic},
					 </if> 
					<if test="incomeType!= null"> 
					 	incomeType = #{incomeType},
					 </if> 
					<if test="plateformProfit!= null"> 
					 	plateformProfit = #{plateformProfit},
					 </if> 
					<if test="promoterProfit!= null"> 
					 	promoterProfit = #{promoterProfit},
					 </if> 
					 <if test="dealStatus!= null"> 
					 	dealStatus = #{dealStatus},
					 </if> 
					 <if test="dealCode!= null"> 
					 	dealCode = #{dealCode}
					 </if> 
				</set>
			where id=#{id} 
		</update>

		<delete id="deleteMerchantIncome" parameterType="cn.pluss.platform.entity.MerchantIncome" >
			DELETE FROM tb_pluss_merchant_income where id=#{id}
		</delete>

		<select id="queryMerchantIncomePage" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.MerchantIncome">
			SELECT * from tb_pluss_merchant_income 
				<where> 
					<if test="id!= null"> 
					and id = #{id}
					 </if> 
					<if test="userId!= null"> 
					and userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					and merchantName = #{merchantName}
					 </if> 
					<if test="storeId!= null"> 
					and storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					and storeName = #{storeName}
					 </if> 
					<if test="dealType!= null"> 
					and dealType = #{dealType}
					 </if> 
					<if test="dealFee!= null"> 
					and dealFee = #{dealFee}
					 </if> 
					<if test="incomeFee!= null"> 
					and incomeFee = #{incomeFee}
					 </if> 
					<if test="createTime!= null"> 
					and createTime = #{createTime}
					 </if> 
					<if test="payTypeCode!= null"> 
					and payTypeCode = #{payTypeCode}
					 </if> 
					<if test="payTypeName!= null"> 
					and payTypeName = #{payTypeName}
					 </if> 
					<if test="memberCode!= null"> 
					and memberCode = #{memberCode}
					 </if> 
					<if test="memberName!= null"> 
					and memberName = #{memberName}
					 </if> 
					<if test="memberPic!= null"> 
					and memberPic = #{memberPic}
					 </if> 
					<if test="incomeType!= null"> 
					and incomeType = #{incomeType}
					 </if> 
					<if test="plateformProfit!= null"> 
					and plateformProfit = #{plateformProfit}
					 </if> 
					<if test="promoterProfit!= null"> 
					and promoterProfit = #{promoterProfit}
					 </if> 
					 <if test="dealStatus!= null"> 
					and dealStatus = #{dealStatus}
					 </if> 
					 <if test="dealCode!= null"> 
					and dealCode = #{dealCode}
					 </if> 
				 </where> 
			order by id desc  limit #{pageSize}  offset  #{offset}  
		</select>

		<select id="queryMerchantIncomePageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
			SELECT count(*) from tb_pluss_merchant_income 
					<where> 
					<if test="id!= null"> 
					 and id = #{id}
					 </if> 
					<if test="userId!= null"> 
					 and userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					 and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 and merchantName = #{merchantName}
					 </if> 
					<if test="storeId!= null"> 
					 and storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					 and storeName = #{storeName}
					 </if> 
					<if test="dealType!= null"> 
					 and dealType = #{dealType}
					 </if> 
					<if test="dealFee!= null"> 
					 and dealFee = #{dealFee}
					 </if> 
					<if test="incomeFee!= null"> 
					 and incomeFee = #{incomeFee}
					 </if> 
					<if test="createTime!= null"> 
					 and createTime = #{createTime}
					 </if> 
					<if test="payTypeCode!= null"> 
					 and payTypeCode = #{payTypeCode}
					 </if> 
					<if test="payTypeName!= null"> 
					 and payTypeName = #{payTypeName}
					 </if> 
					<if test="memberCode!= null"> 
					 and memberCode = #{memberCode}
					 </if> 
					<if test="memberName!= null"> 
					 and memberName = #{memberName}
					 </if> 
					<if test="memberPic!= null"> 
					 and memberPic = #{memberPic}
					 </if> 
					<if test="incomeType!= null"> 
					 and incomeType = #{incomeType}
					 </if> 
					<if test="plateformProfit!= null"> 
					 and plateformProfit = #{plateformProfit}
					 </if> 
					<if test="promoterProfit!= null"> 
					 and promoterProfit = #{promoterProfit}
					 </if> 
					 <if test="dealStatus!= null"> 
					 and dealStatus = #{dealStatus}
					 </if> 
					 <if test="dealCode!= null"> 
					 and dealCode = #{dealCode}
					 </if> 
				</where>
		</select>
		
		
		<!-- 查询最近五条收入记录 -->
		<select id="queryLastFiveMerchantIncome" parameterType="cn.pluss.platform.entity.MerchantIncome" resultType="cn.pluss.platform.entity.MerchantIncome">
			SELECT * from tb_pluss_merchant_income 
				<where> 
					<if test="id!= null"> 
					 and 	id = #{id}
					 </if> 
					<if test="userId!= null"> 
					 and 	userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					 and 	merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 and 	merchantName = #{merchantName}
					 </if> 
					<if test="storeId!= null"> 
					 and 	storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					 and 	storeName = #{storeName}
					 </if> 
					<if test="dealType!= null"> 
					 and 	dealType = #{dealType}
					 </if> 
					<if test="dealFee!= null"> 
					 and 	dealFee = #{dealFee}
					 </if> 
					<if test="incomeFee!= null"> 
					 and 	incomeFee = #{incomeFee}
					 </if> 
					<if test="createTime!= null"> 
					 and 	createTime = #{createTime}
					 </if> 
					<if test="payTypeCode!= null"> 
					 and 	payTypeCode = #{payTypeCode}
					 </if> 
					<if test="payTypeName!= null"> 
					 and 	payTypeName = #{payTypeName}
					 </if> 
					<if test="memberCode!= null"> 
					 and 	memberCode = #{memberCode}
					 </if> 
					<if test="memberName!= null"> 
					 and 	memberName = #{memberName}
					 </if> 
					<if test="memberPic!= null"> 
					 and 	memberPic = #{memberPic}
					 </if> 
					<if test="incomeType!= null"> 
					 and 	incomeType = #{incomeType}
					 </if> 
					<if test="plateformProfit!= null"> 
					 and 	plateformProfit = #{plateformProfit}
					 </if> 
					<if test="promoterProfit!= null"> 
					 and 	promoterProfit = #{promoterProfit}
					 </if> 
					 <if test="dealStatus!= null"> 
					 and 	dealStatus = #{dealStatus}
					 </if> 
					 <if test="dealCode!= null"> 
					 and 	dealCode = #{dealCode}
					 </if> 
					 order by createTime DESC limit 5
				 </where> 
		</select>
		
		<!-- 根据时间查询商家收益明细 -->
		<select id="queryMerchantIncomePageByTime" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.MerchantIncome">
			SELECT * from tb_pluss_merchant_income 
				<where> 
					<if test="id!= null"> 
					and id = #{id}
					 </if> 
					<if test="userId!= null"> 
					and userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					and merchantName = #{merchantName}
					 </if> 
					<if test="storeId!= null"> 
					and storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					and storeName = #{storeName}
					 </if> 
					<if test="dealType!= null"> 
					and dealType = #{dealType}
					 </if> 
					<if test="dealFee!= null"> 
					and dealFee = #{dealFee}
					 </if> 
					<if test="incomeFee!= null"> 
					and incomeFee = #{incomeFee}
					 </if> 
					<if test="createTime!= null"> 
					and createTime = #{createTime}
					 </if> 
					<if test="payTypeCode!= null"> 
					and payTypeCode = #{payTypeCode}
					 </if> 
					<if test="payTypeName!= null"> 
					and payTypeName = #{payTypeName}
					 </if> 
					<if test="memberCode!= null"> 
					and memberCode = #{memberCode}
					 </if> 
					<if test="memberName!= null"> 
					and memberName = #{memberName}
					 </if> 
					<if test="memberPic!= null"> 
					and memberPic = #{memberPic}
					 </if> 
					<if test="incomeType!= null"> 
					and incomeType = #{incomeType}
					 </if> 
					<if test="plateformProfit!= null"> 
					and plateformProfit = #{plateformProfit}
					 </if> 
					<if test="promoterProfit!= null"> 
					and promoterProfit = #{promoterProfit}
					 </if> 
					 <if test="dealStatus!= null"> 
					and dealStatus = #{dealStatus}
					 </if> 
					 <if test="dealCode!= null"> 
					and dealCode = #{dealCode}
					 </if> 
				 </where> 
			and createTime &gt; #{startTime} and createTime &lt; #{endTime}  order by createTime desc  limit #{pageSize}  offset  #{offset}  
		</select>
		
		
		<!-- 根据时间查询商家收益数量 -->
		<select id="queryMerchantIncomePageCountByTime" parameterType="java.util.Map" resultType="java.lang.Integer">
			SELECT count(*) from tb_pluss_merchant_income 
					<where> 
					<if test="id!= null"> 
					 and id = #{id}
					 </if> 
					<if test="userId!= null"> 
					 and userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					 and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 and merchantName = #{merchantName}
					 </if> 
					<if test="storeId!= null"> 
					 and storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					 and storeName = #{storeName}
					 </if> 
					<if test="dealType!= null"> 
					 and dealType = #{dealType}
					 </if> 
					<if test="dealFee!= null"> 
					 and dealFee = #{dealFee}
					 </if> 
					<if test="incomeFee!= null"> 
					 and incomeFee = #{incomeFee}
					 </if> 
					<if test="createTime!= null"> 
					 and createTime = #{createTime}
					 </if> 
					<if test="payTypeCode!= null"> 
					 and payTypeCode = #{payTypeCode}
					 </if> 
					<if test="payTypeName!= null"> 
					 and payTypeName = #{payTypeName}
					 </if> 
					<if test="memberCode!= null"> 
					 and memberCode = #{memberCode}
					 </if> 
					<if test="memberName!= null"> 
					 and memberName = #{memberName}
					 </if> 
					<if test="memberPic!= null"> 
					 and memberPic = #{memberPic}
					 </if> 
					<if test="incomeType!= null"> 
					 and incomeType = #{incomeType}
					 </if> 
					<if test="plateformProfit!= null"> 
					 and plateformProfit = #{plateformProfit}
					 </if> 
					<if test="promoterProfit!= null"> 
					 and promoterProfit = #{promoterProfit}
					 </if> 
					 <if test="dealStatus!= null"> 
					 and dealStatus = #{dealStatus}
					 </if> 
					 <if test="dealCode!= null"> 
					 and dealCode = #{dealCode}
					 </if> 
					 and createTime &gt; #{startTime} and createTime &lt; #{endTime}
				</where>
		</select>
		
		
		<!-- 根据时间查询商家总收益 -->
		<select id="queryMerchantTotalIncomeByTime" parameterType="java.util.Map" resultType="java.lang.Double">
			SELECT sum(incomeFee) from tb_pluss_merchant_income 
					<where> 
					<if test="id!= null"> 
					 and id = #{id}
					 </if> 
					<if test="userId!= null"> 
					 and userId = #{userId}
					 </if> 
					<if test="merchantCode!= null"> 
					 and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 and merchantName = #{merchantName}
					 </if> 
					<if test="storeId!= null"> 
					 and storeId = #{storeId}
					 </if> 
					<if test="storeName!= null"> 
					 and storeName = #{storeName}
					 </if> 
					<if test="dealType!= null"> 
					 and dealType = #{dealType}
					 </if> 
					<if test="dealFee!= null"> 
					 and dealFee = #{dealFee}
					 </if> 
					<if test="incomeFee!= null"> 
					 and incomeFee = #{incomeFee}
					 </if> 
					<if test="createTime!= null"> 
					 and createTime = #{createTime}
					 </if> 
					<if test="payTypeCode!= null"> 
					 and payTypeCode = #{payTypeCode}
					 </if> 
					<if test="payTypeName!= null"> 
					 and payTypeName = #{payTypeName}
					 </if> 
					<if test="memberCode!= null"> 
					 and memberCode = #{memberCode}
					 </if> 
					<if test="memberName!= null"> 
					 and memberName = #{memberName}
					 </if> 
					<if test="memberPic!= null"> 
					 and memberPic = #{memberPic}
					 </if> 
					<if test="incomeType!= null"> 
					 and incomeType = #{incomeType}
					 </if> 
					<if test="plateformProfit!= null"> 
					 and plateformProfit = #{plateformProfit}
					 </if> 
					<if test="promoterProfit!= null"> 
					 and promoterProfit = #{promoterProfit}
					 </if> 
					 <if test="dealStatus!= null"> 
					 and dealStatus = #{dealStatus}
					 </if> 
					 <if test="dealCode!= null"> 
					 and dealCode = #{dealCode}
					 </if> 
					 and createTime &gt; #{startTime} and createTime &lt; #{endTime}
				</where>
		</select>

</mapper>
