<?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.zm.order.tool.server.dao.OrderItemBatchingDao">
    <resultMap id="ResultMap" type="com.zm.order.tool.server.model.OrderItemBatching">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
		<result column="store_id" jdbcType="BIGINT" property="storeId"/>
		<result column="user_id" jdbcType="BIGINT" property="userId"/>
		<result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
		<result column="batching_id" jdbcType="VARCHAR" property="batchingId"/>
		<result column="batching_name" jdbcType="VARCHAR" property="batchingName"/>
		<result column="code" jdbcType="VARCHAR" property="code"/>
		<result column="num" jdbcType="INTEGER" property="num"/>
		<result column="price" jdbcType="DECIMAL" property="price"/>
		<result column="order_item_id" jdbcType="BIGINT" property="orderItemId"/>
		<result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
		<result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
		<result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
		<result column="features" jdbcType="VARCHAR" property="features"/>
	</resultMap>

	<sql id="tb">
        order_item_batching
    </sql>

    <sql id="cols_all">
        id,
		seller_id,
		store_id,
		user_id,
		order_no,
		batching_id,
		batching_name,
		code,
		num,
		price,
		order_item_id,
		created_at,
		updated_at,
		is_deleted,
		features
    </sql>

    <sql id="values">
    	#{id,jdbcType=BIGINT},
		#{sellerId,jdbcType=BIGINT},
		#{storeId,jdbcType=BIGINT},
		#{userId,jdbcType=BIGINT},
		#{orderNo,jdbcType=VARCHAR},
		#{batchingId,jdbcType=VARCHAR},
		#{batchingName,jdbcType=VARCHAR},
		#{code,jdbcType=VARCHAR},
		#{num,jdbcType=INTEGER},
		#{price,jdbcType=DECIMAL},
		#{orderItemId,jdbcType=BIGINT},
		#{createdAt,jdbcType=TIMESTAMP},
		#{updatedAt,jdbcType=TIMESTAMP},
		#{isDeleted,jdbcType=INTEGER},
		#{features,jdbcType=LONGVARCHAR}
    </sql>

    <insert id="insert" parameterType="com.zm.order.tool.server.model.OrderItemBatching"
                        keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        (<include refid="values"/>)
    </insert>

	<select id="selectById" parameterType="map" resultMap="ResultMap">
		select
		<include refid="cols_all"/>
		from
		<include refid="tb"/>
		where id = #{id} and user_id = #{userId}
	</select>

	<update id="updateUpdatedAt" parameterType="map">
		update
		<include refid="tb"/>
		<set>
			updated_at = now()
		</set>
		where id = #{id}
		and user_id = #{userId}
	</update>

	<insert id="insertList" parameterType="list">
		insert into
		<include refid="tb"/>
		(<include refid="cols_all"/>)
		values
		<foreach collection="list" item="batching" index="index" separator=",">
			(
			#{batching.id,jdbcType=BIGINT},
			#{batching.sellerId,jdbcType=BIGINT},
			#{batching.storeId,jdbcType=BIGINT},
			#{batching.userId,jdbcType=BIGINT},
			#{batching.orderNo,jdbcType=VARCHAR},
			#{batching.batchingId,jdbcType=VARCHAR},
			#{batching.batchingName,jdbcType=VARCHAR},
			#{batching.code,jdbcType=VARCHAR},
			#{batching.num,jdbcType=INTEGER},
			#{batching.price,jdbcType=DECIMAL},
			#{batching.orderItemId,jdbcType=BIGINT},
			now(),
			now(),
			0,
			#{batching.features,jdbcType=LONGVARCHAR}
			)
		</foreach>
	</insert>

	<insert id="inserts" parameterType="list">
		insert into
		<include refid="tb"/>
		(<include refid="cols_all"/>)
		values
		<foreach collection="list" item="batching" index="index" separator=",">
			(
			#{batching.id,jdbcType=BIGINT},
			#{batching.sellerId,jdbcType=BIGINT},
			#{batching.storeId,jdbcType=BIGINT},
			#{batching.userId,jdbcType=BIGINT},
			#{batching.orderNo,jdbcType=VARCHAR},
			#{batching.batchingId,jdbcType=VARCHAR},
			#{batching.batchingName,jdbcType=VARCHAR},
			#{batching.code,jdbcType=VARCHAR},
			#{batching.num,jdbcType=INTEGER},
			#{batching.price,jdbcType=DECIMAL},
			#{batching.orderItemId,jdbcType=BIGINT},
			#{batching.createdAt},
			#{batching.updatedAt},
			#{batching.isDeleted},
			#{batching.features,jdbcType=LONGVARCHAR}
			)
		</foreach>
	</insert>


	<select id="selectList" parameterType="map" resultMap="ResultMap">
		SELECT
		<include refid="cols_all"/>
		FROM
		<include refid="tb"/>
		<where>
			user_id = #{userId}
			<if test="orderNo != null">
				and order_no = #{orderNo}
			</if>
			<if test="sellerId != null">
				and seller_id = #{sellerId}
			</if>
			<if test="storeId != null">
				and store_id = #{storeId}
			</if>
			<if test="orderItemId != null">
				and order_item_id = #{orderItemId}
			</if>
			and is_deleted = 0
		</where>
		<if test="start != null">
			limit ${start}, ${pageSize}
		</if>
	</select>

	<select id="selectByOrderNo" parameterType="map" resultMap="ResultMap">
		SELECT
		<include refid="cols_all"/>
		FROM
		<include refid="tb"/>
		<where>
			user_id = #{userId}
			and order_no = #{orderNo}
			and is_deleted = 0
		</where>
	</select>

	<update id="deletes" parameterType="map">
		update
		<include refid="tb"/>
		set is_deleted = #{deleteType},
		updated_at = now()
		where user_id = #{userId}
		and id in
		<foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</update>

	<update id="updateSellerUser" parameterType="map">
		update
		<include refid="tb"/>
		<set>
			seller_id = #{sellerId},
			store_id = #{storeId},
			user_id = #{newUserId},
			updated_at= now()
		</set>
		where
		user_id = #{oldUserId}
		and order_no = #{orderNo}
	</update>

	<update id="deleteByOrderNo" parameterType="map">
		update
		<include refid="tb"/>
		set is_deleted = -1,
		updated_at = now()
		where user_id = #{userId}
		and order_no = #{orderNo}
	</update>
</mapper>