<?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.OrderMapper">
	
	<!-- 添加订单信息 -->
	<insert id="insertOrderInfo" parameterType="Order">
		INSERT INTO 
		 	TB_IAMBERRY_SHOP_SALES_ORDER
		 		(
			 		SALES_ORDERID, SALES_OPENID, SALES_YET_AMOUNT, SALES_AMOUNT,
			 		SALES_INTEGRAL_NUM, SALES_RATE_AMOUNT, SALES_CREATE_DATE,
			 		SALES_REWARD_INTEGRAL_NUM, SALES_ADDRESS_INFO, SALES_ADDRESS_POSTNUM,
			 		SALES_ADDRESS_TEL, SALES_ADDRESS_NAME, SALES_USER_REMARK, SALES_STATUS, 
			 		SALES_PROXY_REWARD, SALES_REWARD_NUM,SALES_COUPON_RECEIVE_ID, Sales_balance_amount
				)
		 VALUES 
		  	( 
			  #{salesOrderid},  
			  #{salesOpenid},
			  #{salesYetAmount}, 
			  #{salesAmount},
			  #{salesIntegralNum},
			  #{salesRateAmount},
			  #{salesCreateDate},
			  #{salesRewardIntegralNum},
			  #{salesAddressInfo},
			  #{salesAddressPostnum},
			  #{salesAddressTel},
			  #{salesAddressName},
			  #{salesUserRemark},
			  #{salesStatus},
			  1,
			  #{salesRewardNum},
			  #{couponId},
			  #{balancePayAmount}
		  	)
	</insert>
	
	<!-- 插入订单条目信息 -->
	<insert id="insertOrderItemList" parameterType="java.util.List">
		INSERT INTO 
		 	TB_IAMBERRY_SHOP_ORDER_ITEM
		 		(
			 		ITEM_SALES_ORDERID, ITEM_PRODUCT_ID, ITEM_NUM, ITEM_TOTAL, ITEM_CREATE_DATE,
			 		ITEM_PRODUCT_NAME, ITEM_PRODUCT_DISCOUNT, ITEM_PRODUCT_PRICE, ITEM_PRODUCT_PIC,
			 		ITEM_PRODUCT_TYPE, ITEM_COLOR_ID, ITEM_PRODUCT_COLOR, ITEM_PRODUCT_CHILD_TYPE,
					ITEM_IS_SOURCE, ITEM_GIFT_SOURCE
				)
		 VALUES 
		  	<foreach collection="list" item="item" separator=",">
				(
					#{item.itemSalesOrderid}, #{item.itemProductId}, #{item.itemNum}, 
					#{item.itemTotal}, NOW(), #{item.itemProductName}, #{item.itemProductDiscount},
					#{item.itemProductPrice}, #{item.itemProductPic}, #{item.itemProductType},
					#{item.itemColorId}, #{item.itemProductColor}, #{item.itemProductChildType},
					#{item.itemIsSource}, #{item.itemGiftSource}
				)
	        </foreach>
	</insert>
	
	<!-- 修改订单信息 -->
	<update id="updateOrderStatus" parameterType="Order">
		UPDATE 
			TB_IAMBERRY_SHOP_SALES_ORDER 
		SET 
			SALES_STATUS = #{salesStatus}, SALES_TRANSACTION_ID = #{salesTransactionId},
			SALES_TRANSACTION_DATE = #{salesTransactionDate}
		WHERE
			SALES_STATUS = 1 AND SALES_ORDERID = #{salesOrderid}
	</update>
	
	<!-- 根据订单号，获取订单的详细信息 -->
	<select id="selectOrderStatusByOrderId" parameterType="String" resultType="Integer">
		SELECT
			SALES_STATUS
		FROM
			TB_IAMBERRY_SHOP_SALES_ORDER
		WHERE
			SALES_ORDERID = #{salesOrderid}
		LIMIT
			0, 1
	</select>
	
	<!-- 根据orderId查询订单信息 -->
	<select id="selectOrderInfoById" parameterType="Order" resultType="Order">
		SELECT 
			SALES_ORDERID salesOrderid, SALES_STATUS salesStatus,
			SALES_TRANSACTION_DATE salesTransactionDate, SALES_REWARD_INTEGRAL_NUM salesRewardIntegralNum,
			SALES_INTEGRAL_NUM salesIntegralNum, SALES_AMOUNT salesAmount ,
			SALES_BALANCE_AMOUNT balancePayAmount,
			SALES_OPENID  salesOpenid,
			SALES_COUPON_RECEIVE_ID couponId
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER 
		WHERE 
			SALES_ORDERID = #{salesOrderid} 
		<if test="salesOpenid!=null and salesOpenid!=''" >
		   AND SALES_OPENID = #{salesOpenid}
		</if>	
			
	</select>
	
	<!-- 根据单号查询该订单的产品个数 -->
	<select id="getItemNumByOrderId" parameterType="String" resultType="Integer">
		SELECT 
			SUM(ITEM_NUM) itemNum 
		FROM 
			TB_IAMBERRY_SHOP_ORDER_ITEM
		WHERE ITEM_SALES_ORDERID = #{orderid}
	</select>
	
	<!-- 查找订单的产品类型数量 -->
	<select id="getItemProdutTypeStatic" parameterType="String" resultType="java.util.Map">
	select item_product_type as productType, sum(item_num) as num from tb_iamberry_shop_order_item where 
	item_sales_orderid=#{orderId}
	group by item_product_type 
    </select>
	
	<!-- 获取最近的购买订单的地址 -->
	<select id="getOrderAddressInfo" parameterType="String" resultType="Order">
	select sales_orderid  salesOrderid ,
	sales_address_name salesAddressName,
	sales_address_info salesAddressInfo,
	sales_address_tel  salesAddressTel,
	sales_address_postnum salesAddressPostnum
	from 
	tb_iamberry_shop_sales_order
    where  
    sales_openid=#{openid}
    order by sales_create_date desc
    limit 0,1
	</select>
	
	
	
</mapper>