<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
 <mapper namespace="com.iamberry.wechat.service.mapper.RebackInfoMapper">
 
 <sql id="AllField">
		t.REBACK_ID id,
		t.REBACK_TO_OPENID toOpenid,
		t.REBACK_FROM_OPENID fromOpenid,
		t.REBACK_ORDERID orderId,
		t.REBACK_USER_TYPE userType,
		t.REBACK_MONEY money,
		t.REBACK_CREATE_DATE createDate,
		t.REBACK_UPDATE_DATE updateDate,
		t.REBACK_FINISH_DATE finishDate,
		t.REBACK_STATUS status
	</sql>
 
  <select id="getOneById" parameterType="integer" resultType="RebackInfo">
		SELECT 
		<include refid="AllField"/> 
        FROM tb_iamberry_reback t WHERE t.REBACK_id = #{id} 
	</select>
 
 <select id="selectAll" resultType="RebackInfo" parameterType="RebackInfo">
		select 
		<include refid="AllField"/>
        from tb_iamberry_reback t
		<where>
			<if test="status>0">
			  t.REMARK_STATUS=${status}
			</if>
			<if test="userType>0">
			  and t.REBACK_USER_TYPE=${userType}
			</if>
			<if test="fromOpenid!=null and fromOpenid!=''">
			  and t.REBACK_FROM_OPENID=#{fromOpenid}
			</if>
			<if test="toOpenid!=null and toOpenid!=''">
			  and t.REBACK_TO_OPENID=#{toOpenid}
			</if>
	    </where>
	    order by REMARK_CREATE_DATE desc
	    <if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
	     limit ${page.recordBegin},${page.pageSize}
	    </if>
	     <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
	     limit ${page.pageSize}
	    </if>  
    </select>
  
  <select id="getAllCount" resultType="integer">
	 select count(t.REBACK_ID) from tb_iamberry_reback t
	 	<where>
			<if test="status>0">
			  t.REMARK_STATUS=${status}
			</if>
			<if test="userType>0">
			  and t.REBACK_USER_TYPE=${userType}
			</if>
			<if test="fromOpenid!=null and fromOpenid!=''">
			  and t.REBACK_FROM_OPENID=#{fromOpenid}
			</if>
			<if test="toOpenid!=null and toOpenid!=''">
			  and t.REBACK_TO_OPENID=#{toOpenid}
			</if>
	    </where>
	</select>
	
	<insert id="insert" >
 	  insert into 
 	  tb_iamberry_REBACK(
		REBACK_TO_OPENID ,
		REBACK_FROM_OPENID ,
		REBACK_ORDERID ,
		REBACK_USER_TYPE ,
		REBACK_MONEY ,
		REBACK_CREATE_DATE ,
		REBACK_STATUS ,
		REBACK_ROLE,
		REBACK_USER_ID) 
      values(#{toOpenid},#{fromOpenid},#{orderId},#{userType},#{money},
      #{createDate},#{status},#{roleId},#{userId})
 	</insert>
   
   <insert id="batchInsert" parameterType="ArrayList">
 	insert into 
 	  tb_iamberry_REBACK(
		REBACK_TO_OPENID ,
		REBACK_FROM_OPENID ,
		REBACK_ORDERID ,
		REBACK_USER_TYPE ,
		REBACK_MONEY ,
		REBACK_CREATE_DATE ,
		REBACK_STATUS
		) 
 	 values  
    <foreach collection="list" item="obj" index="index" separator="," >  
       (#{toOpenid},#{fromOpenid},#{orderId},#{userType},#{money},
      #{createDate},#{status})  
    </foreach>  
 	</insert>

     <delete id="delete" parameterType="string">
	      delete from tb_iamberry_reback
	      where REBACK_ID = #{id}
      </delete>
      
      <update id="update"  parameterType="RebackInfo" >
        update tb_iamberry_REBACK
        <set>
	        <if test="money>0">
	         set REBACK_MONEY = ${money}
	        </if>
	        <if test="status>0">
	         , REBACK_STATUS=${status}
	        </if>
	        <if test="updateDate!=null and updateDate!=''">
	         , REBACK_UPDATE_DATE=#{updateDate}
	        </if>
	        <if test="finishDate!=null and finishDate!=''">
	         , REBACK_FINISH_DATE=#{finishDate}
	        </if>
        </set>
         where REBACK_ID=${id}
       </update>
      
      
      <!-- 更新订单对应的状态 -->
       <update id="updateStatus"  parameterType="RebackInfo" >
        update tb_iamberry_REBACK
        set REBACK_STATUS=${status}, 
        REBACK_UPDATE_DATE=NOW(), 
        REBACK_FINISH_DATE=NOW() 
        where REBACK_ORDERID=#{orderId}
       </update>
      
      
     <!-- 根据查询条件  分页查询所有的订单  -->
	<select id="loadOrder" parameterType="Order" resultType="Order">
		SELECT 
			SALES_ORDERID salesOrderid,
			SALES_OPENID salesOpenid,
			SALES_STATUS salesStatus,
			SALES_YET_AMOUNT salesYetAmount,
			SALES_CREATE_DATE salesCreateDate,
			SALES_PROXY_REWARD salesProxyReward
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER 
		<where>
			<if test="beginDate!=null">
			   SALES_CREATE_DATE &gt; ${beginDate}
			</if>
			<if test="endDate!=null">
			  and SALES_CREATE_DATE &lt; ${endDate}
			</if>  
		</where>	
		  order by SALES_CREATE_DATE DESC  
	    <if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
	     limit ${page.recordBegin},${page.pageSize}
	    </if>
	     <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
	     limit ${page.pageSize}
	    </if> 
	</select>
	
	<!-- 计算加载的数量 -->
	<select id="loadOrderCount" parameterType="Order" resultType="Integer">
		SELECT 
			count(SALES_ORDERID)
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER 
		<where>
			<if test="beginDate!=null">
			   SALES_CREATE_DATE &gt; ${beginDate}
			</if>
			<if test="endDate!=null">
			  and SALES_CREATE_DATE &lt; ${endDate}
			</if>  
		</where>	
	</select>
 
    <select id="getOrderById"  parameterType="string" resultType="Order">
    	SELECT 
			SALES_ORDERID salesOrderid,
			SALES_OPENID salesOpenid,
			SALES_STATUS salesStatus,
			SALES_YET_AMOUNT salesYetAmount,
			SALES_CREATE_DATE salesCreateDate,
			SALES_CREATE_DATE salesCreateDate,
			SALES_PROXY_REWARD salesProxyReward
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER 
		where 
		    SALES_ORDERID=#{id}
    </select>
 
 
  <!-- 获取某个订单的所有产品的总个数 -->
  <select id="getItemsProductNums"  parameterType="string" resultType="Integer">
    select IFNULL(sum(g.item_num),0) from tb_iamberry_shop_order_item g 
    where g.item_sales_orderid=#{id}
  </select>
  
  <!-- 获取分销商未入账的钱 -->
  <select id="getUnIncomeMoney" parameterType="string" resultType="Integer">
      select 
      IFNULL(sum(t.REBACK_MONEY),0)  
      from TB_IAMBERRY_REBACK  t
      where  t.REBACK_STATUS=1
       and t.reback_user_type=2
      and t.REBACK_TO_OPENID=#{toOpenid}
  </select>
  <select id="getUnIncomeMoney2" parameterType="map" resultType="Integer">
      select 
      IFNULL(sum(t.REBACK_MONEY),0)  
      from TB_IAMBERRY_REBACK  t 
      left join TB_IAMBERRY_REBACK g on t.reback_orderid=g.reback_orderid
      where  t.REBACK_STATUS=1
      and t.REBACK_TO_OPENID=#{openid}
      and g.reback_to_openid in (select place_openid from tb_iamberry_place_info where place_pid=#{pid})
  </select>
 
 <!-- ===================================== 微信端 staticInfo ================================ -->
 
   <select id="getRecentIncome7Day" parameterType="java.util.Map" resultType="ObjRecord">
   select DATE_FORMAT(t.createDate,'%Y%m%d') name,
	DATE_FORMAT(t.createDate,'%m.%d') strDate,
	sum(t.money) amount
   from 
   (
	   select t.REBACK_ORDERID orderId,
	    t.REBACK_CREATE_DATE createDate,
	    t.REBACK_MONEY  money
	    from TB_IAMBERRY_REBACK t
	    where t.REBACK_STATUS in (1,2,12)
	    and  t.REBACK_TO_OPENID=#{toOpenid}
	    and t.REBACK_USER_TYPE=2
   ) t
    group by strDate
    order by  t.createDate DESC
    limit 0,7
  </select>
  <select id="getRecentIncome6Month" parameterType="java.util.Map" resultType="ObjRecord">
   select DATE_FORMAT(t.createDate,'%Y%m') name,
	DATE_FORMAT(t.createDate,'%Y.%m') strDate,
	sum(t.money) amount
   from 
   (
	   select t.REBACK_ORDERID orderId,
	    t.REBACK_CREATE_DATE createDate,
	    t.REBACK_MONEY  money
	    from TB_IAMBERRY_REBACK t
	    where t.REBACK_STATUS in (1,2,12)
	    and  t.REBACK_TO_OPENID=#{toOpenid}
	    and t.REBACK_USER_TYPE=2
   ) t
     group by strDate
    order by  t.createDate DESC
    limit 0,12
  </select>
 
 <!-- 分销商查看下级分销商列表统计的 -->
  <select id="getUnderlingStatisc" parameterType="Map" resultType="StoreStatiscInfo">
  select
       t.openid openid,
       DATE_FORMAT(t.createDate,'%Y-%m-%d %H:%i:%s') strDate,
       t.name  name,
       (select role_name from tb_iamberry_place_role where role_id=roleId ) roleName,
       b.user_head  img
       from
	(select 
	  place_openid openid,
	  place_create_date createDate ,
	  place_role_id  roleId,
	  place_name name
	  from tb_iamberry_place_info 
	  where place_openid in ${openids}
	  <if  test="roleId!=null">
	  and PLACE_ROLE_ID = #{roleId}
	  </if>
	  order by place_create_date desc
	  limit ${recordBegin},${pageSize}
	 ) t left join tb_iamberry_user_userinfo b 
	on b.user_openid=t.openid
    
  </select>
  
  <select id="getRolesList"  parameterType="integer" resultType="RolesListDto">
   select role_name roleName,role_id roleId from tb_iamberry_place_role where role_id in (
   select distinct Place_role_id from tb_iamberry_place_info where place_pid=#{pid} )
  </select>
  
  <!-- 导购，奶粉厂商，特殊渠道者，分销员 查看线下的  ${recordBegin}-->
  <select id="getUnderlingStatisc2" parameterType="Map" resultType="StoreStatiscInfo">
     select 
      DATE_FORMAT(b.user_sub_date,'%Y-%m-%d %H:%i:%s') strDate,
      b.user_nickname name, b.user_head img ,
      b.user_id userId,
      b.user_openid openid ,
      if(user_identity=1,"会员","会员") as roleName 
	 from TB_IAMBERRY_USER_USERINFO b  where 
	 b.user_openid in ${openids}
	 <if test="endNo!=null and endNo!=''">
		<![CDATA[ and b.USER_ID <  #{endNo} ]]>
	 </if>
	<if test="beginNo!=null and beginNo!=''">
		<![CDATA[ and b.USER_ID > #{begindNo} ]]>
	</if>
     order by b.user_id desc
     limit 0,${pageSize}
  </select>
  
  <!-- 查询分销商在用户表的下线 -->
  <select id="getUnderlingUserIds" parameterType="map" resultType="String">
      select b.user_openid
       from TB_IAMBERRY_USER_USERINFO b 
       where user_res_type=3 
       and user_dealers in (
       select Dealer_id from tb_iamberry_dealers
       where Dealer_second=#{userDealer} 
       union all 
       select Dealer_id from tb_iamberry_dealers
       where Dealer_third=#{userDealer} 
       union all 
       select Dealer_id from tb_iamberry_dealers
       where Dealer_fourth=#{userDealer}
       )
  </select>
  
  <!-- 此方法废弃 -->
   <select id="getUnderlingUserIds2" parameterType="map" resultType="String">
      select b.user_openid
       from TB_IAMBERRY_USER_USERINFO b 
       where user_res_type=3 
       and user_dealers in
       (select place_id from tb_iamberry_place_info where place_openid in ${openids})
  </select>
  

   <select id="getUnInComeRecord" parameterType="Map" resultType="ObjRecord" >
      select 
	       t.amount amount ,
	       t.createDate createDate,
	       DATE_FORMAT(t.createDate,'%Y-%m-%d %H:%i:%s') strDate,
	       b.user_head img,
	       b.user_nickname  name
	       from
		(select 
		   t.reback_from_openid openid,
		   t.reback_create_date createDate,
		   t.reback_money   amount
		from TB_IAMBERRY_REBACK t 
		where
		 t.reback_status=1
		 and t.reback_user_type=2
		<![CDATA[ and t.reback_money > 0 ]]>
		and t.reback_to_openid=#{openid}
		) t left join TB_IAMBERRY_USER_USERINFO b 
		on b.USER_OPENID=t.openid
    order by t.createDate desc
   limit ${recordBegin},${pageSize}
   </select>
   
   <select id="getDrpUnInComeRecord" parameterType="Map" resultType="ObjRecord2" >
    select 
   m.amount  amount, 
   b.place_create_date createDate,
   DATE_FORMAT(b.place_create_date,'%Y-%m-%d %H:%i:%s') strDate,
   m.reback2 openid,
   b.place_username name, 
(select user_head from tb_iamberry_user_userinfo where user_openid=m.reback2) img ,
(select Role_name from tb_iamberry_place_role where role_id=b.Place_role_id) roleName
from (	
 select sum(t.reback_money) amount, t.reback_to_openid , g.reback_to_openid reback2  from  tb_iamberry_reback  t left join  tb_iamberry_reback g 
 on t.reback_orderid=g.reback_orderid 
 where t.reback_to_openid=#{openid} 
 and t.reback_user_type=2 and t.reback_status=1 
 and g.reback_to_openid in(select place_openid from tb_iamberry_place_info where place_pid=#{pid}) 
 group by g.reback_to_openid
) 
m left join tb_iamberry_place_info b on b.place_openid=m.reback2 
 limit ${recordBegin},${pageSize}
   </select>
  
  <!-- 查找代理的下一级 对于省代的贡献值 --> 
  <select id="getDrpUnInComeRecord2" parameterType="Map" resultType="ObjRecord2" >
   select 
   m.amount  amount, 
   b.place_create_date createDate,
   DATE_FORMAT(b.place_create_date,'%Y-%m-%d %H:%i:%s') strDate,
   m.reback_to_openid openid,
   b.place_username name, 
(select user_head from tb_iamberry_user_userinfo where user_openid=m.reback_to_openid) img ,
(select Role_name from tb_iamberry_place_role where role_id=b.Place_role_id) roleName
from (	
select sum(g.reback_money) amount, t.reback_to_openid  from  tb_iamberry_reback  t left join  tb_iamberry_reback g 
 on t.reback_orderid=g.reback_orderid 
 where t.reback_to_openid  in (select place_openid from tb_iamberry_place_info where place_pid=#{pid})
 and  g.reback_to_openid=#{openid} 
 and  t.reback_status=1 
 and  t.reback_user_type=2
 group by t.reback_to_openid)
m left join tb_iamberry_place_info b on b.place_openid=m.reback_to_openid  
 limit ${recordBegin},${pageSize}
   </select>
   
   <select id="alreadyIn" parameterType="String" resultType="Integer"> 
    select IFNULL(count(t.reback_id),0)  from TB_IAMBERRY_REBACK  t
      where t.REBACK_ORDERID=#{orderId}
   </select>
   
   
   <!-- ======================================后台统计的======================================= -->
  <select id="getSearchInComeRecord" parameterType="Map" resultType="IncomeRecord" >
  	select c.orderId orderNo,
          c.createDate createDate,
          DATE_FORMAT(c.createDate,'%Y-%m-%d %H:%i:%s') strDate,
           c.money amount,
           c.name name,
           c.isReward isReward
   from (
       select t.REBACK_ORDERID orderId,
	    t.REBACK_CREATE_DATE createDate,
	    t.REBACK_MONEY  money,
	    b.user_name name ,
	    t.REBACK_STATUS isReward
	    from TB_IAMBERRY_REBACK t
	    left join tb_iamberry_user_userinfo b
	    on t.REBACK_FROM_OPENID=b.USER_OPENID
	    where 
         t.REBACK_TO_OPENID=#{toOpenid}
	    and t.REBACK_USER_TYPE=2
	    <if test="status>0">
	     and t.REBACK_STATUS=${status}
	    </if>
      ) c
     order by createDate DESC
    limit ${recordBegin},${pageSize}
  </select>
  
  
   <select id="getSearchInComeRecordCount" parameterType="Map" resultType="Integer" >
   select count(t.REBACK_ORDERID)
   from TB_IAMBERRY_REBACK t
    where 
        t.REBACK_TO_OPENID=#{toOpenid}
	    and t.REBACK_USER_TYPE=2
	    <if test="status>0">
	    and t.REBACK_STATUS=${status}
	    </if>
  </select>
  
	<!-- 获取某一个微代理在规定的时间内,某一个产品类型销售达到X台的用户以及销售数量，已返利 -->
 	<select id="getByDate" parameterType="PaperBean" resultType="com.iamberry.wechat.core.entity.task.TaskModel">
 		SELECT
 			temp.*
 		FROM
 			(
	 		SELECT 
	 			REBACK_TO_OPENID openID, SUM(I.ITEM_NUM) count 
	 		FROM 
	 			TB_IAMBERRY_REBACK 
	 		RIGHT JOIN 
	 			TB_IAMBERRY_SHOP_ORDER_ITEM I 
	 		ON 
	 			I.ITEM_SALES_ORDERID = REBACK_ORDERID AND I.ITEM_PRODUCT_TYPE = #{otherInt}
	 		WHERE 
	 		<![CDATA[
	 			REBACK_USER_TYPE = 1
	 				AND 
	 			REBACK_STATUS = 2 
	 				AND 
	 			REBACK_MONEY != 0
	 				AND
	 			REBACK_CREATE_DATE >= #{beginDate}
	 				AND 
	 			REBACK_CREATE_DATE <= #{endDate}
	 		]]>
	 		GROUP BY 
	 			REBACK_TO_OPENID
	 	) temp
	 WHERE
	 	temp.count >= #{hid}
 	</select>
 </mapper>