<?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_DISCOUNT productDiscount, 
		PRODUCT_PRICE productPrice, 
		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
	</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>

	<sql id="allChildTypeField">
		child_type_id typeId,
		child_type_name typeName,
		child_type_detilas typeDetilas,
		child_type_create_date typeCreateDate,
		child_type_update_date typeUpdateDate,
		child_type_state typeState
	</sql>
	<!-- 商品模糊查询 -->
	<select id="getProductByFuzzy" parameterType="String" resultType="product">
		select
		<include refid="allField"/>
		from TB_IAMBERRY_PRODUCT_INFO 
		<where>
			<if test="PRODUCT_NAME!=null">
			 	PRODUCT_NAME like '%#{productName}%' 
			</if>
		</where>
		ORDER BY PRODUCT_ID DESC
	</select>
	
	<!-- 查询所有的产品并分页 -->
	<select id="selectAllProductInfo" resultType="product" parameterType="product">
		SELECT
			<include refid="allField"></include>,
			child_type_id "productType.typeId",
			child_type_name "productType.typeName"
		FROM TB_IAMBERRY_PRODUCT_INFO 
		JOIN tb_iamberry_product_child_type ON tb_iamberry_product_child_type.child_type_id = TB_IAMBERRY_PRODUCT_INFO.product_child_type
		<where>
			<if test="productName != null and productName != ''">
				PRODUCT_NAME LIKE CONCAT('%',#{productName},'%')
			</if>
			<if test="minMoney != null and minMoney != ''">
				<![CDATA[AND PRODUCT_DISCOUNT >= #{minMoney} ]]>
			</if>
			<if test="maxMoney != null and maxMoney != ''">
				<![CDATA[AND PRODUCT_DISCOUNT <= #{maxMoney} ]]>
			</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>
	
	<!-- 根据productId查询单个产品 -->
	<select id="selectProductByProductId" resultType="product" parameterType="int">
		SELECT 
			<include refid="allField"></include>,
			child_type_id "productType.typeId",
			child_type_name "productType.typeName"
		FROM TB_IAMBERRY_PRODUCT_INFO
		JOIN tb_iamberry_product_child_type ON tb_iamberry_product_child_type.child_type_id = TB_IAMBERRY_PRODUCT_INFO.product_child_type
		WHERE PRODUCT_ID=#{productId}
	</select>
	
	<!-- 根据productId查询单个产品 -->
	<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="minMoney != null and minMoney != ''">
				<![CDATA[AND PRODUCT_DISCOUNT >= #{minMoney} ]]>
			</if>
			<if test="maxMoney != null and maxMoney != ''">
				<![CDATA[AND PRODUCT_DISCOUNT <= #{maxMoney} ]]>
			</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>
	
	<!-- 添加一个产品 -->
	<insert id="addProduct" parameterType="product" 
				useGeneratedKeys="true" keyProperty="productId">
		INSERT INTO TB_IAMBERRY_PRODUCT_INFO
		(
			PRODUCT_NAME ,
			PRODUCT_ABBREVIATION , 
			PRODUCT_DISCOUNT , 
			PRODUCT_PRICE , 
			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},#{productDiscount},#{productPrice},
			#{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="productDiscount!=null and productDiscount!=''">
				PRODUCT_DISCOUNT=#{productDiscount},
			</if>
			<if test="productPrice!=null and productPrice!=''">
				PRODUCT_PRICE=#{productPrice},
			</if>
			<if test="productSalesnum!=null and productSalesnum!=''">
				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="productColor!=null and productColor!=''">
				PRODUCT_COLOR=#{productColor},
			</if>
			<if test="productType.typeId !=null and productType.typeId !=''">
				PRODUCT_TYPE=#{productType.typeId}
			</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="allChildTypeField"></include>
		FROM
		tb_iamberry_product_child_type
	</select>
	<!-- 						【根据id查询一个产品返利类型】							 -->
	<select id="selectOneById" parameterType="Integer" resultType="ProductType">
		SELECT
			<include refid="allChildTypeField"></include>
		FROM
		tb_iamberry_product_child_type
		WHERE child_type_id = #{typeId}
	</select>
	
	<!-- 						【添加产品返利类型】							 -->
	<insert id="insertProductType" parameterType="ProductType" 
		useGeneratedKeys="true" keyProperty="typeId">
		INSERT INTO tb_iamberry_product_child_type
			(child_type_name ,child_type_detilas ,child_type_create_date ,child_type_update_date ,child_type_state)
		values
			(#{typeName},#{typeDetilas},#{typeCreateDate},#{typeUpdateDate},#{typeState})
	</insert>
	
	<!-- 						【根据类型id修改一个产品返利类型】							 -->
	<update id="updateProductTypeById" parameterType="ProductType">
		UPDATE tb_iamberry_product_child_type
		SET
		child_type_update_date = NOW(),
		<if test="typeName != null and typeName != ''">
			child_type_name = #{typeName},
		</if>
		<if test="typeDetilas != null and typeDetilas != ''">
			child_type_detilas = #{typeDetilas}
		</if>
		WHERE child_type_id = #{typeId}
	</update>
	
	<update id="updateProductTypeStatusById" parameterType="ProductType">
		UPDATE tb_iamberry_product_child_type
		SET child_type_state = #{typeState},
			child_type_update_date = NOW()
		WHERE child_type_id = #{typeId}
	</update>
</mapper>