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

<mapper namespace="com.iamberry.wechat.service.mapper.ProductInfoMapper">
	<sql id="product">
		PRODUCT_ID productId, PRODUCT_NAME productName, PRODUCT_ABBREVIATION productAbbreviation,  
		 PRODUCT_SALESNUM productSalesnum, PRODUCT_ACCESSNUM productAccessnum, PRODUCT_STATUS productStatus, 
		PRODUCT_INTRODUCE_IMG productIntroduceImg, PRODUCT_PARAMETER_IMG productParameterImg, PRODUCT_CREATE_DATE productCreateDate, 
		PRODUCT_SORT_NUM productSortNum, PRODUCT_COLOR_DES productColorDes, PRODUCT_REMARK productRemark,
		PRODUCT_TYPE "productType.typeId", PRODUCT_RENT_TYPE productRentType 
	</sql>
	
	<sql id="userGift">
		USER_ID userId,
		USER_PRODUCT_ID userProductId,
		USER_COLOR_ID userColorId, 
		USER_GIFT_ID userGiftId,
		USER_GIFT_COLOR userGiftColor,
		USER_LEVEL userLevel,
		USER_DISCOUNT userDiscount,
		USER_GIFT_COUNT userGiftCount,
		USER_STATUS userStatus,
		USER_CREATE_TIME userCreateTime 
	</sql>
	
	<sql id="color">
		COLOR_ID colorId,
		COLOR_PRODUCT_ID colorProductId,
		COLOR_NAME colorName,
		COLOR_DISCOUNT colorDiscount,
		COLOR_PRICE colorPrice,
		COLOR_69CODE color69Code 
	</sql>
	
	<!-- 分页查询商品信息 -->
	<select id="selectPageProduct" parameterType="Product" resultType="Product">
		SELECT 
			<include refid="product" />,
			COLOR_DISCOUNT productDiscount,
			COLOR_PRICE productPrice, 
			COLOR_NAME productColor, 
			COLOR_ID colorId,
			color_img colorImg
		FROM 
			TB_IAMBERRY_PRODUCT_INFO 
		LEFT JOIN TB_IAMBERRY_PRODUCT_COLOR ON PRODUCT_ID = COLOR_PRODUCT_ID 
		WHERE 
			PRODUCT_STATUS = 1 
			<if test="productName != null and productName != ''">
				AND PRODUCT_NAME like CONCAT('%',#{productName},'%') 
			</if>
			<if test="productType.typeId != null and productType.typeId != 2">
				AND PRODUCT_TYPE = #{productType.typeId} 
			</if>
			<if test="productType.typeId != null and productType.typeId == 2">
				AND PRODUCT_TYPE in (2,6,7,8,9,10) 
			</if>
			<if test="productRentType != null and productRentType != ''">
				AND PRODUCT_RENT_TYPE = #{productRentType} 
			</if>
			ORDER BY color_sort
			<if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
		     	LIMIT ${page.recordBegin},${page.pageSize}
		    </if>
	</select>
	
	<!-- 根据条件查询产品集合 -->
	<select id="listSelectProduct" parameterType="Product" resultType="Product">
		SELECT 
			<include refid="product" />,
			COLOR_DISCOUNT productDiscount,
			COLOR_PRICE productPrice, 
			COLOR_NAME productColor, 
			COLOR_ID colorId  
		FROM 
			TB_IAMBERRY_PRODUCT_INFO 
		LEFT JOIN TB_IAMBERRY_PRODUCT_COLOR ON PRODUCT_ID = COLOR_PRODUCT_ID 
		<where>
			<if test="productStatus != null and productStatus != ''">
				PRODUCT_STATUS = #{productStatus} 
			</if>
			<if test="productRentType != null and productRentType != ''">
				AND PRODUCT_RENT_TYPE = #{productRentType} 
			</if>
		</where>
	</select>
	
	<select id="selectColorList" parameterType="Integer" resultType="ProductColor">
		SELECT 
			<include refid="color" />
		FROM 
			TB_IAMBERRY_PRODUCT_COLOR 
		WHERE 
			COLOR_PRODUCT_ID = #{colorProductId} 
		ORDER BY COLOR_DISCOUNT ASC
	</select>
	
	<select id="selectProductCount" resultType="Integer" parameterType="Product">
		SELECT 
			COUNT(PRODUCT_ID) 
		FROM 
			TB_IAMBERRY_PRODUCT_INFO 
		WHERE 
			PRODUCT_STATUS = 1 
			<if test="productName != null and productName != ''">
				AND PRODUCT_NAME LIKE CONCAT('%',#{productName},'%') 
			</if>
			<if test="productType.typeId != null and productType.typeId != 2">
				AND PRODUCT_TYPE = #{productType.typeId} 
			</if>
			<if test="productType.typeId != null and productType.typeId == 2">
				AND PRODUCT_TYPE in (2,6,7,8,9,10) 
			</if>
			<if test="productRentType != null and productRentType != ''">
				AND PRODUCT_RENT_TYPE = #{productRentType} 
			</if>
	</select>
	
	<!-- 查询首页产品 -->
	<select id="selectIndexPageProduct" resultType="Product">
		SELECT 
			PRODUCT_ID productId, 
			PRODUCT_COLOR_DES productColorDes,
			PRODUCT_INTRODUCE_IMG productIntroduceImg,
			PRODUCT_NAME productName, 
			PRODUCT_TYPE "productType.typeId", 
			COLOR_DISCOUNT productDiscount,
			COLOR_PRICE productPrice, 
			COLOR_NAME productColor, 
			COLOR_ID colorId  
		FROM 
			TB_IAMBERRY_PRODUCT_INFO 
		LEFT JOIN TB_IAMBERRY_PRODUCT_COLOR ON PRODUCT_ID = COLOR_PRODUCT_ID 
		WHERE 
			PRODUCT_STATUS = 1 
		AND 
			PRODUCT_RENT_TYPE = 1 
		LIMIT 
			0,10
	</select>
	
	<!-- 根据productId查询商品信息 -->
	<select id="getProductByProductId" parameterType="java.lang.Integer" resultType="Product">
		SELECT 
			PRODUCT_ID productId, 
			PRODUCT_NAME productName, 
			PRODUCT_INTRODUCE_IMG productIntroduceImg, 
			PRODUCT_PARAMETER_IMG productParameterImg, 
			PRODUCT_SALESNUM productSalesnum, 
			COLOR_69CODE productRemark, 
			PRODUCT_TYPE "productType.typeId", 
			PRODUCT_RENT_TYPE productRentType,
			PRODUCT_STATUS productStatus,
			COLOR_DISCOUNT productDiscount,
			COLOR_PRICE productPrice, 
			COLOR_NAME productColor, 
			COLOR_ID colorId  
		FROM 
			TB_IAMBERRY_PRODUCT_INFO 
		LEFT JOIN TB_IAMBERRY_PRODUCT_COLOR ON PRODUCT_ID = COLOR_PRODUCT_ID 
		WHERE 
			PRODUCT_ID = #{0} 
		AND 
			COLOR_ID = #{1} 
	</select>
	
	<!-- 根据商品名称查询商品信息 -->
	<select id="getProductByProductName" parameterType="java.lang.String" resultType="Product">
		SELECT 
			<include refid="product"/>
		FROM 
			TB_IAMBERRY_PRODUCT_INFO 
		<where>
			<if test="_parameter != null">
				PRODUCT_NAME like #{productName} 
			</if>
		</where>
		ORDER BY PRODUCT_ID DESC
	</select>
	
	<update id="updateProductNumList" parameterType="java.util.List">
		<foreach collection="list" item="item" index="index" separator=";" open="" close="">
		UPDATE 
			TB_IAMBERRY_PRODUCT_INFO 
		SET 
			PRODUCT_SALESNUM = (PRODUCT_SALESNUM + #{item.itemNum,jdbcType=INTEGER}) 
		WHERE 
			PRODUCT_ID = #{item.itemProductId,jdbcType=INTEGER}
			</foreach>
	</update>
	
	<!-- 根据商品id查询礼品信息 -->
	<select id="selectUserGift" parameterType="UserGiftDto" resultType="UserGiftDto">
		SELECT 
			<include refid="userGift"/> 
		FROM 
			TB_IAMBERRY_SALES_USER_GIFT
		WHERE 
			USER_STATUS = 1 
		<if test="userProductId != null and userProductId != ''">
			AND USER_PRODUCT_ID = #{userProductId} 
		</if>
		<if test="userColorId != null and userColorId != ''">
			AND USER_COLOR_ID = #{userColorId} 
		</if>
		<if test="userLevel != null and userLevel != ''">
			AND USER_LEVEL = #{userLevel}
		</if>
	</select>
	
	<!-- 查询租赁产品信息 -->
	<select id="getLeaseProduct" parameterType="java.lang.Integer" resultType="Product">
		SELECT 
			<include refid="product" />
		FROM 
			TB_IAMBERRY_PRODUCT_INFO 
		WHERE 
			PRODUCT_ID = #{productId}
			and
			PRODUCT_RENT_TYPE = 2
	</select>
</mapper>