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

	<select id="getAgentOrderListByOpenId" parameterType="AgentOrderDto" resultMap="agentOrderMap">
		SELECT 
			RE.REBACK_ORDERID,
			RE.REBACK_MONEY,
			RE.REBACK_STATUS,
			O.SALES_CREATE_DATE 
		FROM TB_IAMBERRY_REBACK RE 
		JOIN TB_IAMBERRY_SHOP_SALES_ORDER O ON RE.REBACK_ORDERID = O.SALES_ORDERID 
		WHERE RE.REBACK_TO_OPENID = #{orderOpenid} 
		<![CDATA[AND RE.REBACK_MONEY > 0 ]]>
		<if test="rebackStatus != null and rebackStatus != '' and rebackStatus == '4'.toString()">
			AND (RE.REBACK_STATUS = 2 OR RE.REBACK_STATUS = 3) 
		</if>
		<if test="rebackStatus != null and rebackStatus != '' and rebackStatus != '4'.toString()">
			AND RE.REBACK_STATUS = 1 
		</if>
		ORDER BY O.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>
	
	<resultMap type="AgentOrderDto" id="agentOrderMap">
		<id column="REBACK_ORDERID" property="orderId"/>
		<result column="REBACK_MONEY" property="cashLogsNum"/>
		<result column="REBACK_STATUS" property="rebackStatus"/>
		<result column="SALES_CREATE_DATE" property="orderCreateDate"/>
		<collection property="orderItemList" column="REBACK_ORDERID" ofType="com.iamberry.wechat.core.entity.order.OrderItem" select="selectRebackOrderItem"/>
	</resultMap>
	
	<select id="selectRebackOrderItem" parameterType="String" resultType="com.iamberry.wechat.core.entity.order.OrderItem">
		SELECT I.ITEM_ID itemId, 
			I.ITEM_SALES_ORDERID itemSalesOrderid, I.ITEM_PRODUCT_NAME itemProductName, 
			I.ITEM_NUM itemNum, I.ITEM_PRODUCT_TYPE itemProductType 
		FROM 
			TB_IAMBERRY_SHOP_ORDER_ITEM I 
		WHERE 
			I.ITEM_SALES_ORDERID = #{itemSalesOrderid} 
		ORDER BY I.ITEM_ID DESC
	</select>
	
	<select id="getAgentOrderCountByUserId" resultType="Integer" parameterType="AgentOrderDto">
		SELECT 
			count(o.sales_orderid) countOrder
		FROM tb_iamberry_user_userinfo u
		JOIN tb_iamberry_shop_sales_order o ON u.user_openid=o.sales_openid
		WHERE o.sales_status = 12
		<!-- AND u.user_refereeid=#{userId} -->
	</select>
	
	<select id="getProductCountByOpenId" parameterType="String" resultType="Integer">
		SELECT SUM(OI.ITEM_NUM)
		FROM TB_IAMBERRY_REBACK RE
		JOIN TB_IAMBERRY_SHOP_ORDER_ITEM OI ON RE.REBACK_ORDERID = OI.ITEM_SALES_ORDERID
		WHERE RE.REBACK_TO_OPENID = #{openId}
		AND RE.REBACK_USER_TYPE = 1
	</select>
	
	<!-- 根据openid查询 完成的订单总数 -->
	<select id="selectCountOrderByOpenId" parameterType="String" resultType="Integer">
		SELECT COUNT(SALES_ORDERID) 
		FROM TB_IAMBERRY_SHOP_SALES_ORDER 
		WHERE SALES_STATUS = 12
		AND SALES_OPENID=#{openId}
	</select>

	<!-- 根据openid 查询 状态是：已支付、已发货、换货完成 的 第一条订单 -->	
	<select id="getPayedOrderByOpenId" parameterType="String" resultType="String">
		SELECT T.SALES_ORDERID ORDERID
		FROM TB_IAMBERRY_SHOP_SALES_ORDER T
		WHERE SALES_STATUS IN (2,5,10)
		AND SALES_OPENID=#{openId}
		ORDER BY T.SALES_CREATE_DATE ASC
		LIMIT 1
	</select>
	
	<update id="updateOrderLockTypeByOrderId" parameterType="String">
		UPDATE TB_IAMBERRY_SHOP_SALES_ORDER
		SET SALES_LOCK_TYPE=2
		WHERE SALES_ORDERID=#{orderid};
	</update>
</mapper>
