<?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.FansConsumReturnMapper">

	   	<insert id="saveFansConsumReturn" parameterType="cn.pluss.platform.entity.FansConsumReturn">
        	insert into tb_pluss_fans_consum_return(logo,nickName,consumMony,merchantCode,merchantName,returnMoney,consumTime,orderNumber,createDt)VALUES(#{logo},#{nickName},#{consumMony},#{merchantCode},#{merchantName},#{returnMoney},#{consumTime},#{orderNumber},#{createDt})
    	</insert>

	   	<insert id="saveFansConsumReturnBatch">
        	insert into tb_pluss_fans_consum_return(logo,nickName,consumMony,merchantCode,merchantName,returnMoney,consumTime,orderNumber,createDt) VALUES 
    			<foreach collection="list" item="item" index="index" separator=",">
			(#{logo},#{nickName},#{consumMony},#{merchantCode},#{merchantName},#{returnMoney},#{consumTime},#{orderNumber},#{createDt}		)
				</foreach>
    	</insert>

		<select id="queryFansConsumReturn" parameterType="cn.pluss.platform.entity.FansConsumReturn" resultType="cn.pluss.platform.entity.FansConsumReturn">
			SELECT * from tb_pluss_fans_consum_return 
				<where> 
					<if test="id!= null"> 
					 	and id = #{id}
					 </if> 
					<if test="logo!= null"> 
					 	and logo = #{logo}
					 </if> 
					<if test="nickName!= null"> 
					 	and nickName = #{nickName}
					 </if> 
					<if test="consumMony!= null"> 
					 	and consumMony = #{consumMony}
					 </if> 
					<if test="merchantCode!= null"> 
					 	and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 	and merchantName = #{merchantName}
					 </if> 
					<if test="returnMoney!= null"> 
					 	and returnMoney = #{returnMoney}
					 </if> 
					  <if test="orderNumber!= null"> 
					 	and orderNumber = #{orderNumber}
					 </if>
					 <if test="createDt!= null"> 
					 	and createDt = #{createDt}
					 </if> 
					 and consumTime &gt; #{startTime} and consumTime &lt; #{endTime}
				 </where> 
		</select>

		<select id="queryFansConsumReturnList" parameterType="cn.pluss.platform.entity.FansConsumReturn" resultType="cn.pluss.platform.entity.FansConsumReturn">
			SELECT * from tb_pluss_fans_consum_return 
				<where> 
					<if test="id!= null"> 
					 and 	id = #{id}
					 </if> 
					<if test="logo!= null"> 
					 and 	logo = #{logo}
					 </if> 
					<if test="nickName!= null"> 
					 and 	nickName = #{nickName}
					 </if> 
					<if test="consumMony!= null"> 
					 and 	consumMony = #{consumMony}
					 </if> 
					<if test="merchantCode!= null"> 
					 and 	merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 and 	merchantName = #{merchantName}
					 </if> 
					<if test="returnMoney!= null"> 
					 and 	returnMoney = #{returnMoney}
					 </if> 
					<if test="consumTime!= null"> 
					 and 	consumTime = #{consumTime}
					 </if> 
					  <if test="orderNumber!= null"> 
					 	and orderNumber = #{orderNumber}
					 </if>
					 <if test="createDt!= null"> 
					 	and createDt = #{createDt}
					 </if> 
				 </where> 
		</select>

		<update id="updateFansConsumReturn" parameterType="cn.pluss.platform.entity.FansConsumReturn" >
			update tb_pluss_fans_consum_return 
				  <set> 
					<if test="logo!= null"> 
					 	logo = #{logo},
					 </if> 
					<if test="nickName!= null"> 
					 	nickName = #{nickName},
					 </if> 
					<if test="consumMony!= null"> 
					 	consumMony = #{consumMony},
					 </if> 
					<if test="merchantCode!= null"> 
					 	merchantCode = #{merchantCode},
					 </if> 
					<if test="merchantName!= null"> 
					 	merchantName = #{merchantName},
					 </if> 
					<if test="returnMoney!= null"> 
					 	returnMoney = #{returnMoney},
					 </if> 
					<if test="consumTime!= null"> 
					 	consumTime = #{consumTime}
					 </if> 
					  <if test="orderNumber!= null"> 
					 	orderNumber = #{orderNumber},
					 </if>
					 <if test="createDt!= null"> 
					 	createDt = #{createDt},
					 </if> 
				</set>
			where id=#{id} 
		</update>

		<delete id="deleteFansConsumReturn" parameterType="cn.pluss.platform.entity.FansConsumReturn" >
			DELETE FROM tb_pluss_fans_consum_return where id=#{id}
		</delete>

		<select id="queryFansConsumReturnPage" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.FansConsumReturn">
			SELECT * from tb_pluss_fans_consum_return 
				<where> 
					<if test="id!= null"> 
					and id = #{id}
					 </if> 
					<if test="logo!= null"> 
					and logo = #{logo}
					 </if> 
					<if test="nickName!= null"> 
					and nickName like CONCAT("%",#{nickName},"%")
					 </if> 
					<if test="consumMony!= null"> 
					and consumMony = #{consumMony}
					 </if> 
					<if test="merchantCode!= null"> 
					and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					and merchantName = #{merchantName}
					 </if> 
					<if test="returnMoney!= null"> 
					and returnMoney = #{returnMoney}
					 </if> 
					  <if test="orderNumber!= null"> 
					 	and orderNumber = #{orderNumber}
					 </if>
					<if test="startTime!= null"> 
                         <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')>=DATE_FORMAT(#{startTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
                     </if> 
                    <if test="endTime!= null"> 
                         <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
                     </if> 
				 </where> 
			order by id desc  limit #{pageSize}  offset  #{offset}  
		</select>

		<select id="queryFansConsumReturnPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
			SELECT count(*) from tb_pluss_fans_consum_return 
					<where> 
					<if test="id!= null"> 
					 and id = #{id}
					 </if> 
					<if test="logo!= null"> 
					 and logo = #{logo}
					 </if> 
					<if test="nickName!= null"> 
					 and nickName like CONCAT("%",#{nickName},"%")
					 </if> 
					<if test="consumMony!= null"> 
					 and consumMony = #{consumMony}
					 </if> 
					<if test="merchantCode!= null"> 
					 and merchantCode = #{merchantCode}
					 </if> 
					<if test="merchantName!= null"> 
					 and merchantName = #{merchantName}
					 </if> 
					<if test="returnMoney!= null"> 
					 and returnMoney = #{returnMoney}
					 </if> 
					  <if test="orderNumber!= null"> 
					 	and orderNumber = #{orderNumber}
					 </if>
					<if test="startTime!= null"> 
                         <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')>=DATE_FORMAT(#{startTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
                     </if> 
                    <if test="endTime!= null"> 
                         <![CDATA[
                            and  DATE_FORMAT(createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
                     </if> 
				</where>
		</select>
		
		<!-- 统计粉丝消费金额  统计粉丝消费奖励金额 -->
		<select id="sumFansConsumReturnInfo" resultType="cn.pluss.platform.vo.FansInfoCountVO">
            select (SELECT IFNULL(SUM(consumMoney),0) FROM tb_pluss_fans WHERE lastMerchantCode=#{merchantCode} AND  date(createDt) = curdate()) AS  fansConsumMonyNowDayCount,(select IFNULL(SUM(shareMoney),0) from tb_pluss_fans where lastMerchantCode=#{merchantCode} AND date(createDt) = curdate()) as fansReturnMoneyNowDayCount
           
        </select>
        
        <!-- 可提现总金额（提现规则  超过一个月的时间）  -->
        <select id="sumFansAllowCashMoney"  parameterType="java.util.Map" resultType="java.lang.Double">
             select IFNULL(sum(shareMoney),0) as returnMoney from tb_pluss_fans where lastMerchantCode=#{merchantCode} and createDt &lt;= (SELECT DATE_ADD(now(),INTERVAL -1 DAY))
        </select>
        
        <!--不可提现金额 （为超过一个月 不可提现） -->
         <select id="sumFansNotAllowCashMoney" parameterType="java.util.Map" resultType="java.lang.Double">
             select IFNULL(sum(shareMoney),0) as returnMoney from tb_pluss_fans where lastMerchantCode=#{merchantCode} and createDt &gt; (SELECT DATE_ADD(now(),INTERVAL -1 DAY))
        </select>
		
		<!-- 今日分润 -->
		<select id="sumNowDayShareMoney"  parameterType="java.util.Map" resultType="java.lang.Double">
		   select IFNULL(sum(shareMoney),0) as returnMoney From tb_pluss_fans where lastMerchantCode=#{merchantCode} and createDt &gt; #{startTime} and createDt &lt; #{endTime}
        </select>
        
        <!-- 今日奖励 -->
		<select id="sumNowReturnMoney" parameterType="java.util.Map"  resultType="java.lang.Double">
		   select IFNULL(sum(shareMoney),0) as returnMoney From tb_pluss_fans where lastMerchantCode=#{merchantCode} and createDt &gt; #{startTime} and createDt &lt; #{endTime}
        </select>
        
        <!-- 累计总分润  -->
        <select id="sumAllShareMoney" parameterType="java.util.Map" resultType="java.lang.Double">
           select IFNULL(sum(shareMoney),0) as returnMoney From tb_pluss_fans where lastMerchantCode=#{merchantCode}
        </select>
        
        <!-- 累计总分润  -->
        <select id="sumShareMoney" parameterType="java.util.Map" resultType="java.lang.Double">
           select IFNULL(sum(returnMoney),0) as returnMoney From tb_pluss_fans_consum_return
        </select>
        
        <select id="sumReturnMoneyByTime" parameterType="java.util.Map" resultType="java.lang.Double">
			SELECT sum(returnMoney) from tb_pluss_fans_consum_return 
					<where> 
					<if test="startTime!= null"> 
					 and consumTime &gt; #{startTime}
					 </if> 
					 <if test="endTime!= null"> 
					 and consumTime &lt; #{endTime}
					 </if> 
					 <if test="childList!= null"> 
					 and merchantCode in
					 (
					 <foreach collection="childList" item="item" index="index" separator=",">
						#{merchantCode}
					 </foreach>
					 )
					 </if> 
				</where>
		</select>
        

</mapper>
