<?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.ProductColorMapper">
	<sql id="allField">
		COLOR_ID colorId,
		COLOR_PRODUCT_ID colorProductId,
		COLOR_NAME colorName,
		COLOR_DISCOUNT colorDiscount,
		COLOR_PRICE colorPrice,
		
		COLOR_69CODE color69Code,
		COLOR_CRETAE_TIME colorCretaeTime,
		COLOR_UPDATE_TIME colorUpdateTime
	</sql>
	
	<!-- 添加一个产品颜色 -->
	<insert id="insertProductColor" parameterType="ProductColor" 
		useGeneratedKeys="true" keyProperty="colorId">
		INSERT INTO tb_iamberry_product_color
			(	
				COLOR_PRODUCT_ID ,
				COLOR_NAME ,
				COLOR_DISCOUNT ,
				COLOR_PRICE ,
				COLOR_69CODE,
				COLOR_CRETAE_TIME ,
				COLOR_UPDATE_TIME 
			)
		VALUES
			(
				#{colorProductId},#{colorName},#{colorDiscount},#{colorPrice},#{color69Code},NOW(),NOW()
			)
	</insert>
	
	<!-- 批量添加产品颜色 -->
	<insert id="insertProductColorList" parameterType="ProductColor">
		INSERT INTO TB_IAMBERRY_PRODUCT_COLOR
			(	
				COLOR_PRODUCT_ID ,
				COLOR_NAME ,
				COLOR_DISCOUNT ,
				COLOR_PRICE ,
				COLOR_69CODE,
				COLOR_CRETAE_TIME ,
				COLOR_UPDATE_TIME 
			)
		VALUES
		<foreach collection="list" item="color"  separator=",">
			(
				#{color.colorProductId},#{color.colorName},#{color.colorDiscount},
				#{color.colorPrice},#{color.color69Code},NOW(),NOW()
			)
		</foreach>
	</insert>
	<!-- 查询租赁产品颜色 -->
	<select id="getLeaseProductColor" parameterType="java.lang.Integer"  resultType="ProductColor">
		SELECT 
			<include refid="allField" />
		FROM 
			tb_iamberry_product_color 
		WHERE 
			color_product_id = #{colorProductId}
	</select>
	
	<!-- 分页获取产品颜色列表 -->
	<select id="selectProductColorList" parameterType="ProductColor" resultType="ProductColor">
		SELECT 
			<include refid="allField"></include>,
			PRODUCT_NAME colorProductName
		FROM TB_IAMBERRY_PRODUCT_COLOR PC
		JOIN TB_IAMBERRY_PRODUCT_INFO PI
		ON PC.COLOR_PRODUCT_ID = PI.PRODUCT_ID
		ORDER BY COLORID ASC
		<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="selectProductColorCount" parameterType="ProductColor" resultType="int">
		SELECT 
			COUNT(COLOR_ID)
		FROM TB_IAMBERRY_PRODUCT_COLOR PC
		JOIN TB_IAMBERRY_PRODUCT_INFO PI
		ON PC.COLOR_PRODUCT_ID = PI.PRODUCT_ID
	</select>
	
	<!-- 根据id查询单个产品颜色 -->
	<select id="selectProductColorById" parameterType="Integer" resultType="ProductColor">
		SELECT 
			<include refid="allField"></include>,
			PRODUCT_NAME colorProductName,
			PRODUCT_INTRODUCE_IMG colorProductPic,
			PRODUCT_TYPE colorProductType 
		FROM TB_IAMBERRY_PRODUCT_COLOR PC JOIN TB_IAMBERRY_PRODUCT_INFO PI
		ON PC.COLOR_PRODUCT_ID = PI.PRODUCT_ID
		WHERE COLOR_ID=#{colorId}
	</select>
	
	<!-- 根据产品id，获取产品颜色 -->
	<select id="getColor" parameterType="Integer" resultType="ProductColor">
		SELECT 
			<include refid="allField"/>
		FROM 
			TB_IAMBERRY_PRODUCT_COLOR
		WHERE
			COLOR_PRODUCT_ID = #{productId}
	</select>
	
	<!-- 根据id修改一个产品颜色信息 -->
	<update id="updateProductColorById" parameterType="ProductColor">
		UPDATE TB_IAMBERRY_PRODUCT_COLOR
		<set>
 			<if test="colorProductId !=null and colorProductId !=''">
	 			COLOR_PRODUCT_ID=#{colorProductId},
 			</if>
 			<if test="colorName !=null and colorName !=''">
	 			COLOR_NAME=#{colorName},
 			</if>
 			<if test="colorDiscount !=null and colorDiscount !=''">
	 			COLOR_DISCOUNT=#{colorDiscount},
 			</if>
 			<if test="colorPrice !=null and colorPrice !=''">
	 			COLOR_PRICE = #{colorPrice},
 			</if>
 			<if test="color69Code !=null and color69Code !=''">
	 			COLOR_69CODE=#{color69Code},
 			</if>
 			COLOR_UPDATE_TIME=NOW()
 		</set>
		WHERE COLOR_ID = #{colorId}
	</update>
</mapper>