<?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.PlaceLogsMapper">
 
 <sql id="logsAllField">
	t.LOGS_ID  id ,
	t.LOGS_PLACE_ID placeId,
	t.LOGS_NUM  nums,
	t.LOGS_TYPE  type,
	t.LOGS_INTRODUCTION introduction,
	t.LOGS_RES_TYPE  resType,
	t.LOGS_CREATE_DATE createDate
 </sql>
 
 <select id="getOnePlaceLogs" parameterType="string" resultType="PlaceLogs">
	SELECT 
	<include refid="logsAllField"/>
       FROM tb_iamberry_place_logs t WHERE t.LOGS_ID = #{appid}
 </select>
 
   <select id="selectAll" resultType="PlaceLogs" parameterType="PlaceLogs">
		  select 
		<include refid="logsAllField"/>,p.place_name drpName
          from tb_iamberry_place_logs t
          left join tb_iamberry_place_info p
          on p.place_id=t.LOGS_PLACE_ID
		<where>
			<if test="placeId!=null and placeId!=''" >
			   t.LOGS_PLACE_ID=#{placeId}
			</if>
			<if test="drpName!=null and drpName!=''" >
			 and p.place_name like CONCAT('%',#{drpName},'%') 
			</if>
			<if test="type!=null and type!=''" >
			  and t.LOGS_TYPE =#{type}
			</if>
			<if test="introduction!=null and introduction!=''" >
			  and t.LOGS_INTRODUCTION =#{introduction}
			</if>
			<if test="resType!=null and resType!=''" >
			  and t.LOGS_RES_TYPE =#{resType}
			</if>
			<if test="beginDate!=null and beginDate!=''">
			  and p.PLACE_CREATE_DATE &gt; #{beginDate}
			</if>
			<if test="endDate!=null and endDate!=''">
			  and p.PLACE_CREATE_DATE &lt; #{endDate}
			</if>  
	    </where>
	    ORDER BY t.LOGS_ID 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.LOGS_ID) from tb_iamberry_place_logs t
	  left join tb_iamberry_place_info p
          on p.place_id=t.LOGS_PLACE_ID
		<where>
			<if test="placeId!=null and placeId!=''" >
			   t.LOGS_PLACE_ID=#{placeId}
			</if>
			<if test="drpName!=null and drpName!=''" >
			 and p.place_name like CONCAT('%',#{drpName},'%') 
			</if>
			<if test="type!=null and type!=''" >
			  and t.LOGS_TYPE =#{type}
			</if>
			<if test="introduction!=null and introduction!=''" >
			  and t.LOGS_INTRODUCTION =#{introduction}
			</if>
			<if test="resType!=null and resType!=''" >
			  and t.LOGS_RES_TYPE =#{resType}
			</if>
			<if test="beginDate!=null and beginDate!=''">
			  and p.PLACE_CREATE_DATE &gt; #{beginDate}
			</if>
			<if test="endDate!=null and endDate!=''">
			  and p.PLACE_CREATE_DATE &lt; #{endDate}
			</if>  
	    </where>
	</select>
	
 
   <insert id="insert">
 	  insert into 
 	  tb_iamberry_place_logs(
		LOGS_PLACE_ID ,
		LOGS_NUM ,
		LOGS_TYPE ,
		LOGS_INTRODUCTION ,
		LOGS_RES_TYPE ,
		LOGS_CREATE_DATE ) 
      values(
	    #{placeId},
	   #{nums},
	   #{type},
	   #{introduction},
	   #{resType},
	  #{createDate})
 	</insert>
 	
 	<!-- 根据返利人id和单号模糊查询返利日志信息 -->
 	<select id="selectPlaceLogInfo" parameterType="PlaceLogs" resultType="PlaceLogs">
 		SELECT 
 			<include refid="logsAllField"/>
 		FROM 
 			TB_IAMBERRY_PLACE_LOGS t 
 		WHERE 
 			t.LOGS_PLACE_ID = #{placeId} 
 		AND 
 			t.LOGS_INTRODUCTION LIKE CONCAT('%',#{introduction},'%') 
 	</select>
 	
 	<!-- 修改了商户层返利日志表 -->
 	<update id="updatePlaceLogs" parameterType="PlaceLogs">
 		update tb_iamberry_place_logs 
	     <set>
	     	<if test="placeId!=null and placeId!=''">
	          LOGS_PLACE_ID =#{placeId},
	        </if>
	        <if test="nums!=null and nums!=''">
	          LOGS_NUM =(LOGS_NUM + #{nums}),
	        </if>
	        <if test="type!=null and type!=''">
	          LOGS_TYPE =#{type},
	        </if>
	        <if test="introduction!=null and introduction!=''">
	          LOGS_INTRODUCTION =#{introduction},
	        </if>
	        <if test="resType!=null and resType!=''">
	          LOGS_RES_TYPE =#{resType},
	        </if>
	     </set> 
	     where LOGS_ID = #{id}
 	</update>
 
    <update id="update" >
	     update tb_iamberry_place_logs 
	     <set>
	     	<if test="placeId!=null and placeId!=''">
	          LOGS_PLACE_ID =#{placeId},
	        </if>
	        <if test="nums!=null and nums!=''">
	          LOGS_NUM =#{nums},
	        </if>
	        <if test="type!=null and type!=''">
	          LOGS_TYPE =#{type},
	        </if>
	        <if test="introduction!=null and introduction!=''">
	          LOGS_INTRODUCTION =#{introduction},
	        </if>
	        <if test="resType!=null and resType!=''">
	          LOGS_RES_TYPE =#{resType},
	        </if>
	     </set> 
	     where LOGS_ID = #{id}
     </update>
 
 
      <delete id="delete" parameterType="integer">
	      delete from tb_iamberry_place_logs  
	      where WHERE LOGS_ID = #{appid}
      </delete>
  
   <!-- 获取分销商下的所有用户 -->
  <select id="getUnInComeOrderUserTop" parameterType="integer" resultType="java.lang.String" >
    select user_openid userOpenid
    from TB_IAMBERRY_USER_USERINFO 
    where USER_DEALER=#{id} 
    ORDER BY USER_ID DESC
  </select>


	<select id="selectNumberBacklog" resultType="NumberBacklogDto">
		SELECT
		(CASE WHEN a.sendTheGoods is null THEN 0 ELSE a.sendTheGoods END) as sendTheGoods,
		(CASE WHEN b.returnGoods is null THEN 0 ELSE b.returnGoods END) as returnGoods,
		(CASE WHEN c.returnRefund is null THEN 0 ELSE c.returnRefund END) as returnRefund,
		(CASE WHEN d.exchange is null THEN 0 ELSE d.exchange END) as exchange
		FROM
		(
		(
		SELECT
		sum(1) AS sendTheGoods
		FROM
		tb_iamberry_shop_sales_order
		WHERE
		sales_status = 2
		) AS a
		CROSS JOIN (
		SELECT
		sum(1) AS returnGoods
		FROM
		tb_iamberry_shop_sales_order
		WHERE
		sales_status = 3
		) AS b
		CROSS JOIN (
		SELECT
		sum(1) AS returnRefund
		FROM
		tb_iamberry_shop_sales_order
		WHERE
		sales_status = 6
		) AS c
		CROSS JOIN (
		SELECT
		sum(1) AS exchange
		FROM
		tb_iamberry_shop_sales_order
		WHERE
		sales_status = 11
		) AS d
		)
	</select>

 </mapper>