SkuMapper.xml 13.9 KB
<?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.sibu.orderHelper.integral.web.dao.IMSkuDao">

	<resultMap id="skuResponse" type="com.sibu.orderHelper.integral.reponse.SkuResponse">
		<result property="skuId" column="sku_id" javaType="java.lang.String" jdbcType="VARCHAR"/>
		<result property="imProductId" column="im_product_id" javaType="java.lang.String" jdbcType="VARCHAR"/>
		<result property="erpCode" column="erp_code" javaType="java.lang.String" jdbcType="VARCHAR"/>
		<result property="stockNum" column="stock_num" javaType="java.lang.Integer" jdbcType="INTEGER"/>
		<result property="exchangeIntegral" column="exchange_integral" javaType="java.lang.Integer" jdbcType="INTEGER"/>
		<result property="memberPrice" column="member_price" />
		<result property="retailPrice" column="retail_price" />
		<result property="marketPrice" column="market_price" />
		<result property="isShow" column="is_show" javaType="java.lang.Boolean" jdbcType="VARCHAR"/>
		<collection property="values" ofType="com.sibu.orderHelper.integral.reponse.SkuValueResponse" column="sku_Id">
			<result property="attrValueId" column="attr_value_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
			<result property="attrId" column="attr_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
			<result property="attrName" column="attr_name" javaType="java.lang.String" jdbcType="VARCHAR"/>
		</collection>
	</resultMap>
	
	<!-- API返回类 -->
	<resultMap id="skuApiResponse" type="com.sibu.orderHelper.integral.reponse.SkuApiResponse">
		<result property="skuId" column="sku_id" javaType="java.lang.String" jdbcType="VARCHAR"/>
		<result property="erpCode" column="erp_code" javaType="java.lang.String" jdbcType="VARCHAR"/>
		<result property="stockNum" column="stock_num" javaType="java.lang.Integer" jdbcType="INTEGER"/>
		<result property="exchangeIntegral" column="exchange_integral" javaType="java.lang.Integer" jdbcType="INTEGER"/>
		<result property="retailPrice" column="retail_price"/>
		<result property="marketPrice" column="market_price"/>
		<result property="memberPrice" column="member_price"/>
		<collection property="values" ofType="com.sibu.orderHelper.integral.reponse.SkuValueResponse" column="sku_Id">
			<result property="attrValueId" column="attr_value_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
			<result property="attrId" column="attr_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
			<result property="attrName" column="attr_name" javaType="java.lang.String" jdbcType="VARCHAR"/>
			<result property="valueName" column="attr_value" javaType="java.lang.String" jdbcType="VARCHAR"/>
			<result property="sortIndex" column="sort_index" javaType="java.lang.Integer" jdbcType="TINYINT"/>
		</collection>
	</resultMap>
	
	<resultMap type="com.sibu.orderHelper.integral.model.SkuBean" id="skuBean">
		<result property="skuId" column="sku_id" javaType="java.lang.String" jdbcType="VARCHAR"/>
		<result property="imProductId" column="im_product_id" javaType="java.lang.String" jdbcType="VARCHAR"/>
		<result property="erpCode" column="erp_code" javaType="java.lang.String" jdbcType="VARCHAR"/>
		<result property="stockNum" column="stock_num" javaType="java.lang.Integer" jdbcType="INTEGER"/>
		<result property="exchangeIntegral" column="exchange_integral" javaType="java.lang.Integer" jdbcType="INTEGER"/>
		<result property="retailPrice" column="retail_price" />
		<result property="marketPrice" column="market_price" />
		<result property="isShow" column="is_show" javaType="java.lang.Boolean" jdbcType="VARCHAR"/>
	</resultMap>
	
	<!-- 查询product sku返回 -->
	<select id="selectSkuByProductId" parameterType="java.lang.String" resultMap="skuResponse">
			SELECT s.sku_id,s.im_product_id,s.erp_code,s.stock_num,s.exchange_integral,
			s.member_price,s.retail_price,s.market_price,
			s.is_show,ref.attr_value_id,n.attr_id,n.attr_name FROM sku s 
			INNER JOIN attr_ref ref ON s.sku_id =ref.sku_id
			INNER JOIN attr_value v ON v.value_id =ref.attr_value_id
			INNER JOIN attr_name n ON n.attr_id =v.attr_id
			where s.im_product_id =#{productId}
	</select>
	
	<!-- 查询sku bean -->
	<select id="listSkuBeanByProductId" parameterType="java.lang.String" resultMap="skuBean">
		select sku_id,im_product_id,erp_code,stock_num,exchange_integral,retail_price,market_price,is_show from sku where im_product_id=#{productId}
	</select>
	
	<!-- 查询product sku返回 -->
	<select id="selectApiSkuByProductId" parameterType="java.lang.String" resultMap="skuApiResponse">
			SELECT s.sku_id,s.erp_code,s.stock_num,s.exchange_integral,s.retail_price,s.market_price,s.member_price,
			s.is_show,ref.attr_value_id,n.attr_id,n.attr_name,v.attr_value ,n.sort_index FROM sku s 
			INNER JOIN attr_ref ref ON s.sku_id =ref.sku_id
			INNER JOIN attr_value v ON v.value_id =ref.attr_value_id
			INNER JOIN attr_name n ON n.attr_id =v.attr_id
			WHERE s.im_product_id =#{productId} 
			ORDER BY n.sort_index
	</select>
	<!-- 保存实体 -->
	<insert id="save" parameterType="com.sibu.orderHelper.integral.request.ProductSpecRequest">
		INSERT INTO sku (
			sku_id,im_product_id,erp_code,stock_num,exchange_integral,member_price,retail_price,market_price,create_date,update_date,is_show
		) VALUES (
			#{skuId},#{productId},#{erpCode},#{stockNum},#{integral},
			#{memberPrice},#{price},#{marketPrice},now(),now(),#{isShow}
		)
	</insert>
	
	<!-- 批量保存 -->
	<insert id="batchSave" parameterType="java.util.List">
		INSERT INTO sku (
			sku_id,im_product_id,erp_code,stock_num,exchange_integral,member_price,retail_price,market_price,create_date,update_date,is_show
		) VALUES 
		<foreach collection="list" item="sku"  separator=",">
			(#{sku.skuId},#{sku.productId},#{sku.erpCode},#{sku.stockNum},#{sku.integral},
			#{sku.memberPrice},#{sku.price},#{sku.marktPrice},now(),now(),#{sku.isShow})
		</foreach>
	</insert>
	
	
		<!-- 批量保存 -->
	<insert id="batchSaveSkuValue" parameterType="java.util.List">
		insert into attr_ref (sku_id,attr_value_id) values 
		<foreach collection="list" item="item"  separator=",">
			(#{item.skuId},#{item.attrValueId})
		</foreach>
	</insert>
	
	<update id="updateSku" parameterType="com.sibu.orderHelper.integral.request.ProductSpecRequest">
		UPDATE sku
		<trim prefix="SET" suffixOverrides=",">
			<if test="null != erpCode">
				erp_code=#{erpCode},
			</if>
			<if test="null != stockNum">
				stock_num=#{stockNum},
			</if>
			<if test="null != integral">
				exchange_integral=#{integral},
			</if>
			<if test="null != memberPrice">
				member_price=#{memberPrice},
			</if>
			<if test="null != price">
				retail_price=#{price},
			</if>
			<if test="null != marktPrice">
				market_price=#{marktPrice},
			</if>
			<if test="null != isShow">
				is_show=#{isShow},
			</if>
				update_date=now()
		</trim>
		WHERE sku_id = #{skuId}
	</update>
	
	<!-- 同步sku库存和价格 -->
	<update id="syncSkuStockAndPrice" parameterType="com.sibu.orderHelper.integral.model.SkuBean">
		update sku
		<trim prefix="SET" suffixOverrides=",">
			<if test="null != stockNum">
				stock_num=#{stockNum},
			</if>
			<if test="null != exchangeIntegral">
				exchange_integral=#{exchangeIntegral},
			</if>
			<if test="null != memberPrice">
				member_price=#{memberPrice},
			</if>
			<if test="null != retailPrice">
				retail_price=#{retailPrice},
			</if>
			<if test="null != marketPrice">
				market_price=#{marketPrice},
			</if>
				update_date=now()
		</trim>
		WHERE erp_code=#{erpCode}
	</update>
	
	<!-- 删除sku实体 -->
	<delete id="delete" parameterType="java.lang.String">
		DELETE FROM sku WHERE sku_id = #{skuId}
	</delete>
	<!-- 批量删除SKU实体 -->
	<delete id="batchDelete" parameterType="java.util.List">
		DELETE FROM sku WHERE sku_id in
		<foreach collection="list" item="item"  open="(" separator="," close=")">
			#{item}
		</foreach>
	</delete>
	<!-- 批量删除 -->
	<delete id="batchDeleteSkuValue" parameterType="java.util.List">
		delete from attr_ref where sku_id in
		<foreach collection="list" item="item"  open="(" separator="," close=")">  
			#{item} 
		</foreach>
	</delete>
	<!-- 通过skuId批量删除对象 -->
	<delete id="batchDeleteSkuValueBySkuId" parameterType="java.util.List">
		delete from attr_ref where sku_id in
		<foreach collection="list" item="item"  open="(" separator="," close=")">  
			#{item} 
		</foreach>
	</delete>
	
	<!-- 查出所有的产品SKU主键 -->
	<select id="selectSkuIds" parameterType="java.util.Map" resultType="java.lang.String">
		select sku_id from sku where im_product_id=#{productId}
	</select>

	<resultMap type="com.sibu.orderHelper.integral.model.AttrNameBean" id="attrNameBeanList">
		<result property="attrId" column="attr_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
		<result property="categoryId" column="category_id" javaType="java.lang.String" jdbcType="VARCHAR"/>
		<result property="attrName" column="attr_name" javaType="java.lang.String" jdbcType="VARCHAR"/>
	</resultMap>
	<resultMap type="com.sibu.orderHelper.integral.model.AttrValueBean" id="attrValueBeanList">
		<result property="valueId" column="value_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
		<result property="attrValue" column="attr_value" javaType="java.lang.String" jdbcType="VARCHAR"/>
		<result property="attrId" column="attr_id" javaType="java.lang.Long" jdbcType="BIGINT"/>
	</resultMap>
	<!-- 查出所有的产品规格属性值 -->
	<select id="selectFirstAttrValue" parameterType="java.util.Map" resultMap="attrValueBeanList">
		SELECT value_id,attr_value,attr_id
		FROM attr_value WHERE attr_value=rtrim(ltrim(#{attrValue}))  AND attr_id=rtrim(ltrim(#{attrNameId}))
		Limit 0,1
	</select>
	<!-- 查出所有的产品规格属性名称-->
	<select id="selectFirstAttrName" parameterType="java.util.Map" resultMap="attrNameBeanList">
		SELECT attr_id,category_id ,attr_name
		FROM attr_name WHERE attr_name=rtrim(ltrim(#{attrName})) AND category_id=rtrim(ltrim(#{categoryId}))
		Limit 0,1
	</select>
	<!-- 判断该规格是否已经存在 -->
	<select id="selectSkuByErpCodeAndProductId" parameterType="java.util.Map" resultMap="skuBean">
		SELECT s.sku_id,s.im_product_id,s.erp_code,s.stock_num,s.exchange_integral,s.retail_price,s.market_price,
			s.is_show FROM sku s
			WHERE s.im_product_id =#{productId} AND s.erp_code=#{erpCode}
			Limit 0,1
	</select>
	<!-- 判断属性联合键是否已经存在 -->
	<select id="selectSkuListProductId" parameterType="java.util.Map" resultType="com.sibu.orderHelper.integral.vo.ImportSkuExist">
		SELECT  t0.sku_id AS skuId,t0.erp_code AS erpCode,t0.is_show AS isShow,
		GROUP_CONCAT(t2.attr_value  ORDER BY t2.attr_value DESC) AS attrValue,
		t0.stock_num AS stockNum,t0.exchange_integral AS exchangeIntegral,t0.retail_price AS retailPrice
		FROM sku t0
		INNER JOIN attr_ref t1 ON t1.sku_id=t0.sku_id
		INNER JOIN attr_value t2 ON t2.value_id=t1.attr_value_id
		INNER JOIN attr_name t3 ON t3.attr_id=t2.attr_id
		WHERE t0.im_product_id=#{productId}
		GROUP BY t0.sku_id,t0.erp_code,t0.stock_num,t0.exchange_integral,t0.retail_price,t0.is_show
		ORDER BY t0.sku_id ASC,t2.attr_value ASC,t0.erp_code ASC,t3.attr_name ASC
	</select>

	<!-- 单个保存 -->
	<insert id="saveSku"  parameterType="com.sibu.orderHelper.integral.request.ProductSpecRequest2">
		INSERT INTO sku (
		sku_id,im_product_id,erp_code,stock_num,exchange_integral,member_price,retail_price,market_price,create_date,update_date,is_show
		) VALUES
		(#{skuId},#{productId},#{erpCode},#{stockNum},#{integral},#{memberPrice},#{price}
		,#{marktPrice},now(),now(),#{isShow})
	</insert>

	<!-- 单个保存 -->
	<insert id="saveSkuValue" parameterType="com.sibu.orderHelper.integral.model.AttrRefBean">
		insert into attr_ref (sku_id,attr_value_id) values
		(#{skuId},#{attrValueId})
	</insert>

	<!-- 批量保存 -->
	<insert id="batchSave2" parameterType="java.util.List">
		INSERT INTO sku (
		sku_id,im_product_id,erp_code,stock_num,exchange_integral,member_price,retail_price,market_price,create_date,update_date,is_show
		) VALUES
		<foreach collection="list" item="sku"  separator=",">
			(#{sku.skuId},#{sku.productId},#{sku.erpCode},#{sku.stockNum},#{sku.integral},#{sku.memberPrice},#{sku.price},#{sku.marktPrice},now(),now(),#{sku.isShow})
		</foreach>
	</insert>

	<!-- 同步修改相同产品的库存 -->
	<select id="selectSkuProductIdAndErpCode" parameterType="java.util.Map"  resultMap="skuBean">
		SELECT s.sku_id,s.im_product_id,s.erp_code,s.stock_num,s.exchange_integral,s.retail_price,s.market_price,s.is_show
		FROM sku s WHERE s.`erp_code`=#{erpCode}  AND  s.`im_product_id` IN (
		SELECT p.`im_product_id` FROM im_product p WHERE p.`im_product_id` != #{productId}
		AND  p.`erp_code`=  (SELECT p.`erp_code` FROM im_product p WHERE p.`im_product_id`=#{productId})
		)
	</select>


	<!-- 同步不同分类的相同产品SKU的库存 -->
	<update id="syncSkuStock" parameterType="com.sibu.orderHelper.integral.model.SkuBean">
		update sku
		<trim prefix="SET" suffixOverrides=",">
			<if test="null != erpCode">
				erp_code=#{erpCode},
			</if>
			<if test="null != exchangeIntegral">
				exchange_integral=#{exchangeIntegral},
			</if>
			<if test="null != memberPrice">
				member_price=#{memberPrice},
			</if>
			<if test="null != retailPrice">
				retail_price=#{retailPrice},
			</if>
			<if test="null != memberPrice">
				market_price=#{memberPrice},
			</if>
			<if test="null != stockNum">
				stock_num=#{stockNum},
			</if>
			update_date=now()
		</trim>
		WHERE erp_code=#{erpCode} AND im_product_id=#{imProductId}
	</update>

	<!-- 同步修改相同产品的库存 -->
	<select id="getSkuById" parameterType="java.lang.String"  resultMap="skuBean">
		SELECT s.sku_id,s.im_product_id,s.erp_code,s.stock_num,s.exchange_integral,s.retail_price,s.market_price,s.is_show
		FROM sku s WHERE s.sku_id=#{skuId}
	</select>

	<update id="updateSkuByStock" parameterType="com.sibu.orderHelper.integral.model.SkuBean">
		UPDATE sku
		<trim prefix="SET" suffixOverrides=",">
			<if test="null != stockNum">
				stock_num=#{stockNum},
			</if>
			<if test="null != isShow">
				is_show=#{isShow},
			</if>
			update_date=now()
		</trim>
		WHERE sku_id = #{skuId}
	</update>

</mapper>