<?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.diaoyun.zion.chinafrica.dao.TbCfItemDetailDao">

	<resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfItemDetailEntity" id="tbCfItemDetailMap">
		<result property="itemId" column="item_id"/>
		<result property="sourceItemId" column="source_item_id"/>
		<result property="stationId" column="station_id"/>
		<result property="stationType" column="station_type"/>
		<result property="itemTitle" column="item_title"/>
		<result property="itemNum" column="item_num"/>
		<result property="itemImg" column="item_img"/>
		<result property="itemPrice" column="item_price"/>
		<result property="itemCategory" column="item_category"/>
		<result property="itemSku" column="item_sku"/>
		<result property="shopId" column="shop_id"/>
		<result property="shopName" column="shop_name"/>
		<result property="shopUrl" column="shop_url"/>
		<result property="createTime" column="create_time"/>
	</resultMap>

	<select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemDetailEntity">
		select
			`item_id`,
			`source_item_id`,
			`station_id`,
			`station_type`,
			`item_title`,
			`item_num`,
			`item_img`,
			`item_price`,
			`item_category`,
			`item_sku`,
			`shop_id`,
			`shop_name`,
			`shop_url`,
			`create_time`
		from tb_cf_item_detail
		where item_id = #{id}
	</select>
	<select id="queryByTitle" resultType="com.diaoyun.zion.chinafrica.vo.TbCfItemDetailVo">
		select
			`item_id`,
			`source_item_id`,
			`station_id`,
			`station_type`,
			`item_title`,
			`item_num`,
			`item_img`,
			`item_price`,
			`item_category`,
			`item_sku`,
			`shop_id`,
			`shop_name`,
			`shop_url`,
			`create_time`
		from tb_cf_item_detail
		where item_title = #{itemTitle}
	</select>
	<select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemDetailEntity">
		select
		`item_id`,
		`source_item_id`,
		`station_id`,
		`station_type`,
		`item_title`,
		`item_num`,
		`item_img`,
		`item_price`,
		`item_category`,
		`item_sku`,
		`shop_id`,
		`shop_name`,
		`shop_url`,
		`create_time`
		from tb_cf_item_detail
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND name LIKE concat('%',#{name},'%')
		</if>
		<choose>
			<when test="sidx != null and sidx.trim() != ''">
				order by ${sidx} ${order}
			</when>
			<otherwise>
				order by item_id desc
			</otherwise>
		</choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

	<select id="queryTotal" resultType="int">
		select count(*) from tb_cf_item_detail
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND name LIKE concat('%',#{name},'%')
		</if>
	</select>
	<insert id="save" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfItemDetailEntity">
		insert into tb_cf_item_detail(
			`item_id`,
			`source_item_id`,
			`station_id`,
			`station_type`,
			`item_title`,
			`item_num`,
			`item_img`,
			`item_price`,
			`item_category`,
			`item_sku`,
			`shop_id`,
			`shop_name`,
			`shop_url`,
			`create_time`)
		values(
			#{itemId},
			#{sourceItemId},
			#{stationId},
			#{stationType},
			#{itemTitle},
			#{itemNum},
			#{itemImg},
			#{itemPrice},
			#{itemCategory},
			#{itemSku},
			#{shopId},
			#{shopName},
			#{shopUrl},
			#{createTime})
	</insert>

	<update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfItemDetailEntity">
		update tb_cf_item_detail
		<set>
			<if test="sourceItemId != null">`source_item_id` = #{sourceItemId}, </if>
			<if test="stationId != null">`station_id` = #{stationId}, </if>
			<if test="stationType != null">`station_type` = #{stationType}, </if>
			<if test="itemTitle != null">`item_title` = #{itemTitle}, </if>
			<if test="itemNum != null">`item_num` = #{itemNum}, </if>
			<if test="itemImg != null">`item_img` = #{itemImg}, </if>
			<if test="itemPrice != null">`item_price` = #{itemPrice}, </if>
			<if test="itemCategory != null">`item_category` = #{itemCategory}, </if>
			<if test="itemSku != null">`item_sku` = #{itemSku}, </if>
			<if test="shopId != null">`shop_id` = #{shopId}, </if>
			<if test="shopName != null">`shop_name` = #{shopName}, </if>
			<if test="shopUrl != null">`shop_url` = #{shopUrl},</if>
			<if test="createTime != null">`create_time` = #{createTime}</if>
		</set>
		where item_id = #{itemId}
	</update>

	<delete id="delete">
		delete from tb_cf_item_detail where item_id = #{value}
	</delete>

	<delete id="deleteBatch">
		delete from tb_cf_item_detail where item_id in
		<foreach item="itemId" collection="array" open="(" separator="," close=")">
			#{itemId}
		</foreach>
	</delete>

	<!--获取用户购物车内商品-->
	<select id="getCartItemList" resultType="com.diaoyun.zion.chinafrica.vo.TbCfCartItemDetailVo">
		select t1.cart_record_id,t1.check_flag,t2.* from tb_cf_cart_record_r t1,tb_cf_item_detail t2 where t1.item_id=t2.item_id
		and t1.user_id=#{userId} and t2.item_price is not null
		<if test="checkFlag != null"> and t1.check_flag = #{checkFlag}</if>
		and t1.enable_flag=1 order by t2.create_time desc
	</select>

	<!--改变购物车的商品勾选状态-->
	<update id="changeItemState">
        update tb_cf_cart_record_r set check_flag=#{checkFlag} where cart_record_id=#{cartRecordId}
    </update>


</mapper>