<?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.CartMapper">
	<!-- 查询用户购物车所有信息 -->
	<select id="selectCartByOpenId" parameterType="String" resultType="CartDto">
		SELECT 
			C.CART_ID cartId, C.CART_PRODUCT_ID cartProductId,C.CART_COLOR_ID cartColorId, C.CART_NUM cartNum,
			P.PRODUCT_NAME productName, pc.COLOR_DISCOUNT productDiscount, P.PRODUCT_STATUS productStatus, 
			pc.COLOR_NAME productColor, P.PRODUCT_INTRODUCE_IMG productIntroduceImg, pc.COLOR_69CODE productRemark,
			P.PRODUCT_TYPE productType,pc.color_img
		FROM 
			TB_IAMBERRY_SHOP_CART C LEFT JOIN TB_IAMBERRY_PRODUCT_INFO P ON C.CART_PRODUCT_ID = P.PRODUCT_ID 
			LEFT JOIN TB_IAMBERRY_PRODUCT_COLOR pc ON c.CART_COLOR_ID = pc.COLOR_ID 
		WHERE
			C.CART_OPENID = #{openId} 
		ORDER BY C.CART_ID DESC
	</select>
	
	<select id="getCartNumByCartOpenId" parameterType="String" resultType="Integer">
		SELECT 
			COUNT(C.CART_ID) 
		FROM 
			TB_IAMBERRY_SHOP_CART C 
		WHERE
			C.CART_OPENID = #{0}
	</select>
	
	<!-- 修改购物车数量信息 -->
	<update id="updateCartByOpenId" parameterType="CartDto">
		UPDATE 
			TB_IAMBERRY_SHOP_CART 
		SET 
			CART_NUM = #{cartNum}
		WHERE
			CART_OPENID = #{cartOpenId} 
		AND 
			CART_PRODUCT_ID = #{cartProductId} 
		AND 
			CART_COLOR_ID = #{cartColorId} 
	</update>
	
	<!-- 根据商品id和openid查询购物车数量 -->
	<select id="getCartNumByOpenId" parameterType="CartDto" resultType="Integer">
		SELECT 
			CART_NUM cartNum 
		FROM 
			TB_IAMBERRY_SHOP_CART 
		WHERE 
			CART_OPENID = #{cartOpenId} 
		AND 
			CART_PRODUCT_ID = #{cartProductId} 
		AND 
			CART_COLOR_ID = #{cartColorId} 
	</select>

	<select id="getCartDtoList"  resultType="CartDto"  parameterType="CartDto" >
		select
		t.*
		from tb_iamberry_shop_cart t
		<where>
			<if test="cartId != null ">
				AND t.cart_id = #{cartId}
			</if >
			<if test="cartOpenId != null and cartOpenId != ''">
				AND t.cart_openid = #{cartOpenId}
			</if >
			<if test="cartProductId != null ">
				AND t.cart_product_id = #{cartProductId}
			</if >
			<if test="cartColorId != null ">
				AND t.cart_color_id = #{cartColorId}
			</if >
		</where>
	</select>

	<!-- 获取购物车信息看是否有记录该商品信息 -->
	<select id="getCartByCartProductId" parameterType="CartDto" resultType="CartDto">
		SELECT 
			CART_ID cartId, 
			CART_NUM cartNum 
		FROM 
			TB_IAMBERRY_SHOP_CART 
		WHERE 
			CART_OPENID = #{cartOpenId} 
		AND 
			CART_PRODUCT_ID = #{cartProductId} 
		AND 
			CART_COLOR_ID = #{cartColorId} 
	</select>
	
	<!-- 新增购物车数据 -->
	<insert id="addProductToCart" parameterType="CartDto" useGeneratedKeys="true" keyProperty="cartId">
		INSERT INTO 
			TB_IAMBERRY_SHOP_CART(CART_OPENID, CART_PRODUCT_ID, CART_NUM, CART_COLOR_ID,CART_CREATE_DATE) 
		VALUES 
			(#{cartOpenId}, #{cartProductId}, #{cartNum}, #{cartColorId}, NOW()) 
	</insert>
	
	<!-- 删除购物车物品信息 -->
	<delete id="deleteCartByCartId" parameterType="CartDto">
		DELETE FROM TB_IAMBERRY_SHOP_CART WHERE CART_PRODUCT_ID = #{cartProductId} AND CART_OPENID = #{cartOpenId} AND CART_COLOR_ID = #{cartColorId} 
	</delete>
	
	<!-- 根据openid查看销售订单表数据 -->
	<select id="getOrderList" parameterType="ShopSalesOrderDto" resultType="ShopSalesOrderDto">
		SELECT 
			SALES_ORDERID salesOrderid, 
			SALES_STATUS salesStatus 
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER 
		WHERE 
				SALES_OPENID = #{salesOpenid}
	</select>
	
	<!-- 分页查询销售订单表 -->
	<select id="selectShopOrder" parameterType="PaperBean" resultMap="orderListMap">
		SELECT DISTINCT O.SALES_ORDERID, O.SALES_STATUS, O.SALES_CREATE_DATE, 
			O.SALES_AMOUNT, O.SALES_INTEGRAL_NUM, O.SALES_RATE_AMOUNT, 
			O.SALES_ADDRESS_INFO, O.SALES_ADDRESS_NAME, 
			O.SALES_USER_REMARK, O.SALES_POST_NUM, O.SALES_POST_FIRM, 
			O.SALES_SEND_DATE, O.SALES_ADDRESS_POSTNUM, 
			O.SALES_ADDRESS_TEL ,  45*60-TIMESTAMPDIFF(SECOND,O.SALES_CREATE_DATE,NOW())   SALES_ADMIN_REMARK
		FROM 
			 TB_IAMBERRY_SHOP_SALES_ORDER O LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM I 
			 ON O.SALES_ORDERID = I.ITEM_SALES_ORDERID 
		WHERE 
			 O.SALES_OPENID = #{text} 
		AND 
			 O.SALES_ORDER_TYPE = 1 	
			<if test="status != null and status != ''">
				AND O.SALES_STATUS = #{status} 
			</if>
			<if test="str != null">
				AND (I.ITEM_PRODUCT_NAME LIKE CONCAT('%',#{str},'%') OR I.ITEM_SALES_ORDERID LIKE CONCAT('%',#{str},'%')) 
			</if>
			<if test="beginDate != null">
				AND DATE_FORMAT(O.SALES_CREATE_DATE, '%Y-%m-%d') = DATE_FORMAT(#{beginDate}, '%Y-%m-%d') 
			</if>
		ORDER BY O.SALES_CREATE_DATE DESC 
			LIMIT #{minNum}, #{maxNum} 
	</select>
	
	<resultMap type="ShopOrderAndItemDto" id="orderListMap">
		<id column="SALES_ORDERID" property="salesOrderid"/>
		<result column="SALES_STATUS" property="salesStatus"/>
		<result column="SALES_CREATE_DATE" property="salesCreateDate"/>
		<result column="SALES_AMOUNT" property="salesAmount"/>
		<result column="SALES_INTEGRAL_NUM" property="salesIntegralNum"/>
		<result column="SALES_RATE_AMOUNT" property="salesRateAmount"/>
		<result column="SALES_ADDRESS_INFO" property="salesAddressInfo"/>
		<result column="SALES_ADDRESS_NAME" property="salesAddressName"/>
		<result column="SALES_USER_REMARK" property="salesUserRemark"/>
		<result column="SALES_POST_NUM" property="salesPostNum"/>
		<result column="SALES_POST_FIRM" property="salesPostFirm"/>
		<result column="SALES_SEND_DATE" property="salesSendDate"/>
		<result column="SALES_ADMIN_REMARK" property="salesAdminRemark"/>
		<result column="SALES_ADDRESS_POSTNUM" property="salesAddressPostnum"/>
		<result column="SALES_ADDRESS_TEL" property="salesAddressTel"/>
		<collection property="orderItemList" column="SALES_ORDERID" ofType="OrderItem" select="getOrderItemByOrderId"/>
	</resultMap>
	
	<select id="getOrderItemByOrderId" parameterType="String" resultType="OrderItem">
		SELECT I.ITEM_ID itemId, 
			I.ITEM_SALES_ORDERID itemSalesOrderid, I.ITEM_PRODUCT_NAME itemProductName, 
			I.ITEM_PRODUCT_DISCOUNT itemProductDiscount, I.ITEM_PRODUCT_PRICE itemProductPrice,
			I.ITEM_PRODUCT_PIC itemProductPic, I.ITEM_NUM itemNum, I.ITEM_TOTAL itemTotal,
			c.COLOR_DISCOUNT productDiscount,c.COLOR_PRICE productPrice,I.ITEM_PRODUCT_TYPE itemProductType, 
			P.PRODUCT_STATUS productStatus,I.ITEM_PRODUCT_ID itemProductId,c.COLOR_NAME productColor, 
			I.ITEM_COLOR_ID itemColorId 
		FROM 
			TB_IAMBERRY_SHOP_ORDER_ITEM I 
		LEFT JOIN TB_IAMBERRY_PRODUCT_INFO P ON I.ITEM_PRODUCT_ID = P.PRODUCT_ID 
		LEFT JOIN TB_IAMBERRY_PRODUCT_COLOR c ON i.ITEM_COLOR_ID = c.COLOR_ID 
		WHERE 
			I.ITEM_SALES_ORDERID = #{itemSalesOrderid} 
		ORDER BY I.ITEM_ID DESC
	</select>
	
	<!-- 根据openid查询订单数量 -->
	<select id="getOrderNumByOpenId" parameterType="PaperBean" resultType="Integer">
		SELECT 
			COUNT(O.SALES_ORDERID) 
		FROM 
			TB_IAMBERRY_SHOP_ORDER_ITEM I  LEFT JOIN TB_IAMBERRY_SHOP_SALES_ORDER O 
			ON O.SALES_ORDERID = I.ITEM_SALES_ORDERID 
		WHERE 
			O.SALES_OPENID = #{text} 
			<if test="status != null and status != ''">
				AND O.SALES_STATUS = #{status} 
			</if>
			<if test="str != null">
				AND (I.ITEM_PRODUCT_NAME LIKE CONCAT('%',#{str},'%') OR I.ITEM_SALES_ORDERID LIKE CONCAT('%',#{str},'%')) 
			</if>
			<if test="beginDate != null">
				AND DATE_FORMAT(O.SALES_CREATE_DATE, '%Y-%m-%d') = DATE_FORMAT(#{beginDate}, '%Y-%m-%d') 
			</if>
	</select>
	
	<!--  -->
	<select id="getShopOrderByOrderId" parameterType="java.lang.String" resultType="ShopSalesOrderDto">
		SELECT O.SALES_STATUS salesStatus, O.SALES_ORDERID salesOrderid, O.SALES_CREATE_DATE salesCreateDate, 
			O.SALES_AMOUNT salesAmount, O.SALES_INTEGRAL_NUM salesIntegralNum, O.SALES_RATE_AMOUNT salesRateAmount, 
			O.SALES_ADDRESS_INFO salesAddressInfo, O.SALES_ADDRESS_NAME salesAddressName, O.SALES_YET_AMOUNT salesYetAmount,
			O.SALES_USER_REMARK salesUserRemark, O.SALES_POST_NUM salesPostNum, O.SALES_POST_FIRM salesPostFirm, 
			O.SALES_SEND_DATE salesSendDate, O.SALES_ADDRESS_POSTNUM salesAddressPostnum,O.SALES_ADDRESS_TEL salesAddressTel, 
			O.SALES_LOCK_TYPE salesLockType,O.SALES_COUPON_RECEIVE_ID salesCouponReceiveId, 
			O.SALES_BALANCE_AMOUNT balancePayAmount 
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER O 
		WHERE 
			O.SALES_ORDERID = #{0} 
		AND 
			O.SALES_OPENID = #{1} 
		AND 
			O.SALES_ORDER_TYPE = 1 
	</select>
	
	<!-- 根据订单id查询销售订单项 -->
	<select id="selectShopOrderItem" parameterType="java.lang.String" resultType="ShopOrderItemDto">
		SELECT I.ITEM_ID itemId, I.ITEM_SALES_ORDERID itemSalesOrderid, I.ITEM_PRODUCT_NAME itemProductName, 
			I.ITEM_PRODUCT_DISCOUNT itemProductDiscount, I.ITEM_PRODUCT_PRICE itemProductPrice,
			I.ITEM_PRODUCT_PIC itemProductPic, I.ITEM_NUM itemNum, I.ITEM_TOTAL itemTotal,P.PRODUCT_STATUS productStatus,
			I.ITEM_PRODUCT_ID itemProductId, c.COLOR_NAME productColor,I.ITEM_PRODUCT_TYPE itemProductType,
			I.ITEM_COLOR_ID itemColorId 
		FROM 
			TB_IAMBERRY_SHOP_ORDER_ITEM I 
		LEFT JOIN TB_IAMBERRY_PRODUCT_INFO P ON I.ITEM_PRODUCT_ID = P.PRODUCT_ID 
		LEFT JOIN TB_IAMBERRY_PRODUCT_COLOR c ON I.ITEM_COLOR_ID = c.COLOR_ID 
		WHERE 
			ITEM_SALES_ORDERID = #{itemSalesOrderid} 
		ORDER BY ITEM_ID
	</select>
	
	<!-- 根据优惠券id获取优惠券优惠价格 -->
	<select id="getCouponReduceByItemId" parameterType="CouponItem" resultType="CouponType">
		SELECT 
			T.COUPON_TYPE couponType, T.COUPON_REDUCE couponReduce 
		FROM 
			TB_IAMBERRY_COUPON_TYPE T LEFT JOIN  TB_IAMBERRY_COUPON_ITEM I
			ON T.COUPON_ID = I.COUPON_ID 
		WHERE 
			I.COUPON_ITEM_ID = #{couponItemId} 
		AND 
			I.COUPON_ITEM_USER_OPENID = #{couponItemUseropenid}
	</select>
	
	<!-- 根据购物车ID数组，批量获取购物车集合 -->
	<select id="selectCartItemByListId" parameterType="java.util.List" resultType="CartDto">
		SELECT 
			C.CART_ID cartId, G.PRODUCT_ID cartProductId, pc.COLOR_ID cartColorId, G.PRODUCT_STATUS productStatus, 
			C.CART_NUM cartNum, G.PRODUCT_NAME productName, pc.COLOR_DISCOUNT productPrice, 
			G.PRODUCT_INTRODUCE_IMG productIntroduceImg, pc.COLOR_PRICE productDiscount, PC.COLOR_69CODE productRemark, 
			pc.COLOR_NAME productColor, G.PRODUCT_TYPE productType ,G.product_color_des,PC.color_img AS colorImg
		FROM 
			TB_IAMBERRY_SHOP_CART C 
		LEFT JOIN TB_IAMBERRY_PRODUCT_INFO G ON C.CART_PRODUCT_ID = G.PRODUCT_ID 
		LEFT JOIN TB_IAMBERRY_PRODUCT_COLOR PC ON C.CART_COLOR_ID = PC.COLOR_ID 
		WHERE
			C.CART_ID IN (
				<foreach collection="array" item="item" separator=",">
					#{item} 
		        </foreach>) 
		 ORDER BY C.CART_ID DESC
	</select>
	
	<!-- 根据orderId查询订单状态，openid和发货时间 -->
	<select id="getOpenIdAndStatus" parameterType="String" resultType="ShopSalesOrderDto">
		SELECT 
			SALES_OPENID salesOpenid, SALES_STATUS salesStatus,SALES_SEND_DATE salesSendDate,
			SALES_LOCK_TYPE salesLockType,SALES_COUPON_RECEIVE_ID salesCouponReceiveId 
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER 
		WHERE 
			SALES_ORDERID = #{salesOrderid} 
	</select>
	
	<!-- 根据购物车ID，批量擅长 -->
	<delete id="deleteBatchCartListById" parameterType="java.util.List">
		DELETE FROM 
			TB_IAMBERRY_SHOP_CART 
		WHERE 
			CART_ID in (
				<foreach collection="list" item="item" separator=",">
					#{item}
		        </foreach>
			)
	</delete>
	
	<!-- 根据orderId修改订单状态 -->
	<update id="updateOrderByOrderId" parameterType="Order">
		UPDATE 
			TB_IAMBERRY_SHOP_SALES_ORDER 
		SET 
			SALES_STATUS = #{salesStatus},
			SALES_RETURN_DATE = NOW()
			<if test="couponId!=null and couponId!=''" >
			  , SALES_COUPON_RECEIVE_ID=#{couponId}
			</if>
			<if test="salesReturnDes!=null and salesReturnDes!=''" >
			  , SALES_RETURN_DES = #{salesReturnDes} 
			</if>
			<if test="salesChangeOrderid != null and salesChangeOrderid != ''">
				, SALES_CHANGE_ORDERID = #{salesChangeOrderid}
			</if>
		WHERE 
			SALES_ORDERID = #{salesOrderid} 
		<if test="oldStatus!=null" >
		AND 
			SALES_STATUS = #{oldStatus} 
		</if>
	</update>
	
	<!-- 根据openid和状态查询订单数量 -->
	<select id="selectOrderCount" parameterType="ShopSalesOrderDto" resultType="Integer">
		SELECT 
			COUNT(SALES_ORDERID) 
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER 
		WHERE 
			<![CDATA[SALES_STATUS >= #{salesStatus}]]> 
		AND 
			SALES_OPENID = #{salesOpenid}
	</select>
</mapper>