<?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.ProductMapper">
	<sql id="allField">
		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_COLOR productColor,
		PRODUCT_RENT_TYPE productRentType,
		PRODUCT_DISCOUNT productDiscount,
		PRODUCT_PRICE productPrice,
		PRODUCT_STATUS productStatus

	</sql>
	<!-- 产品类型的所有字段 -->
	<sql id="allTypeField">
		TYPE_ID typeId,
		TYPE_NAME typeName,
		TYPE_DETILAS typeDetilas,
		TYPE_CREATE_DATE typeCreateDate,
		TYPE_UPDATE_DATE typeUpdateDate,
		TYPE_STATE typeState
	</sql>

	<!-- 查询所有的产品并分页 -->
	<select id="selectAllProductInfo" resultMap="productColorMap" parameterType="product">
		SELECT
			PRODUCT_ID, 
			PRODUCT_NAME,
			PRODUCT_ABBREVIATION, 
			PRODUCT_SALESNUM, 
			PRODUCT_ACCESSNUM, 
			PRODUCT_STATUS, 
			PRODUCT_INTRODUCE_IMG, 
			PRODUCT_PARAMETER_IMG,
			PRODUCT_CREATE_DATE, 
			PRODUCT_SORT_NUM,
			PRODUCT_COLOR_DES,
			PRODUCT_REMARK,
			PRODUCT_RENT_TYPE,
			TYPE_ID,
			TYPE_NAME 
		FROM TB_IAMBERRY_PRODUCT_INFO 
		JOIN TB_IAMBERRY_PRODUCT_TYPE ON PRODUCT_TYPE = TYPE_ID
		<where>
			<if test="productName != null and productName != ''">
				PRODUCT_NAME LIKE CONCAT('%',#{productName},'%')
			</if>
			<if test="productType != null and productType.typeId !=null and productType.typeId !=''">
				and PRODUCT_TYPE=#{productType.typeId}
			</if>
			<if test="beginDate != null and beginDate != ''">
				<![CDATA[AND DATE_FORMAT(PRODUCT_CREATE_DATE, '%Y-%m-%d') >= DATE_FORMAT(#{beginDate},'%Y-%m-%d')]]>
			</if>
			<if test="endDate != null and endDate != ''">
				<![CDATA[AND DATE_FORMAT(PRODUCT_CREATE_DATE, '%Y-%m-%d') <= DATE_FORMAT(#{endDate},'%Y-%m-%d')]]>
			</if>
		</where>
		ORDER BY product_sort_num
		<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="Product" id="productColorMap">
		<id column="PRODUCT_ID" property="productId"/>
		<result column="PRODUCT_NAME" property="productName"/>
		<result column="PRODUCT_ABBREVIATION" property="productAbbreviation"/>
		<result column="PRODUCT_SALESNUM" property="productSalesnum"/>
		<result column="PRODUCT_ACCESSNUM" property="productAccessnum"/>
		<result column="PRODUCT_STATUS" property="productStatus"/>
		<result column="PRODUCT_CREATE_DATE" property="productCreateDate"/>
		<result column="PRODUCT_SORT_NUM" property="productSortNum"/>
		<result column="PRODUCT_COLOR_DES" property="productColorDes"/>
		<result column="TYPE_NAME" property="productType.typeName"/>
		<result column="PRODUCT_INTRODUCE_IMG" property="productIntroduceImg"/>
		<result column="PRODUCT_PARAMETER_IMG" property="productParameterImg"/>
		<result column="PRODUCT_REMARK" property="productRemark"/>
		<result column="PRODUCT_TYPE" property="productType.typeId"/>
		<result column="PRODUCT_RENT_TYPE" property="productRentType"/>
		<collection property="colorList" column="PRODUCT_ID" ofType="ProductColor" select="selectAllColorInfo"/>
	</resultMap>
	
	<select id="selectProductIdAndName" resultType="Product"> 
		SELECT
			PRODUCT_ID, 
			PRODUCT_NAME
		FROM TB_IAMBERRY_PRODUCT_INFO
	</select>
	
	<select id="selectAllColorInfo" parameterType="Integer" resultType="ProductColor">
		SELECT 
			COLOR_ID colorId,
			COLOR_PRODUCT_ID colorProductId,
			COLOR_NAME colorName,
			COLOR_DISCOUNT colorDiscount,
			COLOR_PRICE colorPrice,
			COLOR_69CODE color69Code 
		FROM 
			TB_IAMBERRY_PRODUCT_COLOR 
		WHERE 
			COLOR_PRODUCT_ID = #{colorProductId} 
		ORDER BY COLOR_DISCOUNT ASC
	</select>
	
	<!-- 查所有产品个数  -->
	<select id="selectProductCount" resultType="int" parameterType="product">
		SELECT 
			COUNT(product_id)
		FROM TB_IAMBERRY_PRODUCT_INFO 
		<where>
			<if test="productName != null and productName != ''">
				PRODUCT_NAME LIKE CONCAT('%',#{productName},'%')
			</if>
			<if test="productType != null and productType.typeId !=null and productType.typeId !=''">
				and PRODUCT_TYPE=#{productType.typeId}
			</if>
			<if test="beginDate != null and beginDate != ''">
				<![CDATA[AND DATE_FORMAT(PRODUCT_CREATE_DATE, '%Y-%m-%d') >= DATE_FORMAT(#{beginDate},'%Y-%m-%d')]]>
			</if>
			<if test="endDate != null and endDate != ''">
				<![CDATA[AND DATE_FORMAT(PRODUCT_CREATE_DATE, '%Y-%m-%d') <= DATE_FORMAT(#{endDate},'%Y-%m-%d')]]>
			</if>
		</where>
	</select>
	
	<!-- 根据productId查询单个产品 -->
	<select id="selectProductByProductId" resultMap="productResult" parameterType="int" >
		SELECT 
			<include refid="allField"></include>,
			TYPE_ID "productType.typeId",
			TYPE_NAME "productType.typeName",
			imagetext_id "productImageText.imagetextId",
			imagetext_produect_id "productImageText.imagetextProduectId",
			imagetext_context "productImageText.imagetextContext",
			PICTURE_ID,
			PICTURE_PRODUECT_ID,
			PICTURE_URL
		FROM TB_IAMBERRY_PRODUCT_INFO
		LEFT JOIN TB_IAMBERRY_PRODUCT_TYPE ON PRODUCT_TYPE = TYPE_ID
		LEFT JOIN TB_IAMBERRY_PRODUCT_IMAGETEXT ON PRODUCT_ID = IMAGETEXT_PRODUECT_ID
		LEFT JOIN TB_IAMBERRY_PRODUCT_PICTURE ON PRODUCT_ID = PICTURE_PRODUECT_ID
		WHERE PRODUCT_ID=#{productId}
	</select>
	
	<resultMap type="Product" id="productResult" autoMapping="true">
		<id property="productId" column="PRODUCT_ID"/>
		<collection property="pictureList" ofType="com.iamberry.wechat.core.entity.product.ProductPicture">
			<id property="pictureId" column="PICTURE_ID"/>
			<result property="pictureProductId" column="picture_produect_id"/>
			<result property="pictureUrl" column="picture_url"/>
		</collection>
	</resultMap>
	<!-- 添加一个产品 -->
	<insert id="addProduct" parameterType="product" 
				useGeneratedKeys="true" keyProperty="productId">
		INSERT INTO TB_IAMBERRY_PRODUCT_INFO
		(
			PRODUCT_NAME ,
			PRODUCT_ABBREVIATION , 
			PRODUCT_SALESNUM , 
			PRODUCT_ACCESSNUM , 
			PRODUCT_STATUS , 
			PRODUCT_INTRODUCE_IMG , 
			PRODUCT_PARAMETER_IMG ,
			PRODUCT_CREATE_DATE , 
			PRODUCT_SORT_NUM ,
			PRODUCT_COLOR_DES ,
			PRODUCT_REMARK ,
			PRODUCT_COLOR ,
			PRODUCT_TYPE
		)
		VALUES
		(
			#{productName},#{productAbbreviation},
			#{productSalesnum},#{productAccessnum},#{productStatus},#{productIntroduceImg},
			#{productParameterImg},#{productCreateDate},#{productSortNum},#{productColorDes},
			#{productRemark},#{productColor},#{productType.typeId}
		)
	</insert>
	
	<!-- 修改一个产品的信息 -->
	<update id="updateProductByProductId" parameterType="product">
		UPDATE TB_IAMBERRY_PRODUCT_INFO
		<set>
			<if test="productName!=null and productName!=''">
				PRODUCT_NAME=#{productName},
			</if>
			<if test="productAbbreviation!=null and productAbbreviation!=''">
				PRODUCT_ABBREVIATION=#{productAbbreviation},
			</if>
			<if test="productSalesnum!=null">
				PRODUCT_SALESNUM=#{productSalesnum},
			</if>
			<if test="productAccessnum!=null and productAccessnum!=''">
				PRODUCT_ACCESSNUM=#{productAccessnum},
			</if>
			<if test="productStatus!=null and productStatus!=''">
				PRODUCT_STATUS=#{productStatus},
			</if>
			<if test="productIntroduceImg!=null and productIntroduceImg!=''">
				PRODUCT_INTRODUCE_IMG=#{productIntroduceImg},
			</if>
			<if test="productParameterImg!=null and productParameterImg!=''">
				PRODUCT_PARAMETER_IMG=#{productParameterImg},
			</if>
			<if test="productCreateDate!=null and productCreateDate!=''">
				PRODUCT_CREATE_DATE=#{productCreateDate},
			</if>
			<if test="productSortNum!=null and productSortNum!=''">
				PRODUCT_SORT_NUM=#{productSortNum},
			</if>
			<if test="productColorDes!=null and productColorDes!=''">
				PRODUCT_COLOR_DES=#{productColorDes},
			</if>
			<if test="productRemark!=null and productRemark!=''">
				PRODUCT_REMARK=#{productRemark},
			</if>
			<if test="productType.typeId !=null and productType.typeId !=''">
				PRODUCT_TYPE=#{productType.typeId},
			</if>
			<if test="productRentType != null">
				PRODUCT_RENT_TYPE = #{productRentType}
			</if>
		</set>
		WHERE PRODUCT_ID=#{productId}
	</update>
	
	<update id="updateProductStatusByProductId" parameterType="product">
		UPDATE TB_IAMBERRY_PRODUCT_INFO
		SET PRODUCT_STATUS=#{productStatus}
		WHERE PRODUCT_ID=#{productId}
	</update>
	
	<!-- 						【产品返利类型】							 -->
	<select id="getAll" resultType="ProductType">
		SELECT
			<include refid="allTypeField"></include>
		FROM
			TB_IAMBERRY_PRODUCT_TYPE
	</select>
	<!-- 						【根据id查询一个产品返利类型】							 -->
	<select id="selectOneById" parameterType="Integer" resultType="ProductType">
		SELECT
			<include refid="allTypeField"></include>
		FROM
			TB_IAMBERRY_PRODUCT_TYPE
		WHERE TYPE_ID = #{typeId}
	</select>
	
	<!-- 						【添加产品返利类型】							 -->
	<insert id="insertProductType" parameterType="ProductType" 
		useGeneratedKeys="true" keyProperty="typeId">
		INSERT INTO TB_IAMBERRY_PRODUCT_TYPE 
			(TYPE_NAME ,TYPE_DETILAS ,TYPE_CREATE_DATE ,TYPE_UPDATE_DATE ,TYPE_STATE) 
		values
			(#{typeName},#{typeDetilas},#{typeCreateDate},#{typeUpdateDate},#{typeState})
	</insert>
	
	<!-- 						【根据类型id修改一个产品返利类型】							 -->
	<update id="updateProductTypeById" parameterType="ProductType">
		UPDATE TB_IAMBERRY_PRODUCT_TYPE
		SET 
			TYPE_UPDATE_DATE = NOW(),
		<if test="typeName != null and typeName != ''">
			TYPE_NAME = #{typeName},
		</if>
		<if test="typeDetilas != null and typeDetilas != ''">
			TYPE_DETILAS = #{typeDetilas}
		</if>
		WHERE TYPE_ID = #{typeId}
	</update>
	
	<update id="updateProductTypeStatusById" parameterType="ProductType">
		UPDATE TB_IAMBERRY_PRODUCT_TYPE
		SET TYPE_STATE = #{typeState}, 
			TYPE_UPDATE_DATE = NOW()
		WHERE TYPE_ID = #{typeId}
	</update>
</mapper>