<?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.ItemProductionDao">
    <resultMap id="ResultMap" type="com.zm.order.tool.server.model.ItemProduction">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="biz_type" jdbcType="INTEGER" property="bizType"/>
		<result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
		<result column="user_id" jdbcType="BIGINT" property="userId"/>
		<result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
		<result column="store_id" jdbcType="BIGINT" property="storeId"/>
		<result column="spu_id" jdbcType="BIGINT" property="spuId"/>
		<result column="sku_id" jdbcType="VARCHAR" property="skuId"/>
		<result column="production_sign" jdbcType="VARCHAR" property="productionSign"/>
		<result column="num" jdbcType="INTEGER" property="num"/>
		<result column="success_num" jdbcType="INTEGER" property="successNum"/>
		<result column="status" jdbcType="INTEGER" property="status"/>
		<result column="pdc_type" jdbcType="INTEGER" property="pdcType"/>
		<result column="order_item_id" jdbcType="BIGINT" property="orderItemId"/>
		<result column="production_at_start" jdbcType="TIMESTAMP" property="productionAtStart"/>
		<result column="production_at_end" jdbcType="TIMESTAMP" property="productionAtEnd"/>
		<result column="operate_at" jdbcType="TIMESTAMP" property="operateAt"/>
		<result column="production_no" jdbcType="VARCHAR" property="productionNo"/>
		<result column="version" jdbcType="INTEGER" property="version"/>
		<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">
        item_productions
    </sql>

    <sql id="cols_all">
        id,
        biz_type,
		order_no,
		user_id,
		seller_id,
		store_id,
		spu_id,
		sku_id,
		production_sign,
		num,
		status,
		order_item_id,
		production_at_start,
		production_at_end,
		operate_at,
		production_no,
		version,
		created_at,
		updated_at,
		is_deleted,
		features,
		pdc_type,
		success_num
    </sql>

    <sql id="values">
    	#{id,jdbcType=BIGINT},
    	#{bizType,jdbcType=INTEGER},
		#{orderNo,jdbcType=VARCHAR},
		#{userId,jdbcType=BIGINT},
		#{sellerId,jdbcType=BIGINT},
		#{storeId,jdbcType=BIGINT},
		#{spuId,jdbcType=BIGINT},
		#{skuId,jdbcType=VARCHAR},
		#{productionSign,jdbcType=VARCHAR},
		#{num,jdbcType=INTEGER},
		#{status,jdbcType=INTEGER},
		#{orderItemId},
		#{productionAtStart,jdbcType=TIMESTAMP},
		#{productionAtEnd,jdbcType=TIMESTAMP},
		#{operateAt,jdbcType=TIMESTAMP},
		#{productionNo},
		0,
		now(),
		now(),
		0,
		#{features,jdbcType=LONGVARCHAR},
		#{pdcType},
		#{successNum}
    </sql>

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

	<insert id="insertList" parameterType="com.zm.order.tool.server.model.ItemProduction">
		insert into
		<include refid="tb"/>
		(<include refid="cols_all"/>)
		values
		<foreach collection="list" item="each" index="index" separator=",">
			(
			#{each.id,jdbcType=BIGINT},
			#{each.bizType,jdbcType=INTEGER},
			#{each.orderNo,jdbcType=VARCHAR},
			#{each.userId,jdbcType=BIGINT},
			#{each.sellerId,jdbcType=BIGINT},
			#{each.storeId,jdbcType=BIGINT},
			#{each.spuId,jdbcType=BIGINT},
			#{each.skuId,jdbcType=VARCHAR},
			#{each.productionSign,jdbcType=VARCHAR},
			#{each.num,jdbcType=INTEGER},
			#{each.status,jdbcType=INTEGER},
			#{each.orderItemId,jdbcType=BIGINT},
			#{each.productionAtStart,jdbcType=TIMESTAMP},
			#{each.productionAtEnd,jdbcType=TIMESTAMP},
			#{each.operateAt,jdbcType=TIMESTAMP},
			#{each.productionNo},
			0,
			now(),
			now(),
			0,
			#{each.features,jdbcType=LONGVARCHAR},
			#{each.pdcType},
			#{each.successNum}
			)
		</foreach>
	</insert>

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

	<select id="selectOne" parameterType="map" resultMap="ResultMap">
		SELECT
		<include refid="cols_all"/>
		FROM
		<include refid="tb"/>
		<where>
			user_id = #{userId}
			and order_no = #{orderNo}
			and production_sign = #{productionSign}
		</where>
		limit 1
	</select>

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

	<select id="selectByNo" parameterType="map" resultMap="ResultMap">
		SELECT
		<include refid="cols_all"/>
		FROM
		<include refid="tb"/>
		<where>
			production_no = #{productionNo}
		</where>
		limit 200
	</select>

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

    <update id="update" parameterType="com.zm.order.tool.server.model.ItemProduction">
        UPDATE
        <include refid="tb"/>
        <set>
			<if test="status != null">
				status = #{status,jdbcType=INTEGER},
			</if>
			<if test="successNum != null">
				success_num = #{successNum,jdbcType=INTEGER},
			</if>
			<if test="operateAt != null">
				operate_at = #{operateAt,jdbcType=TIMESTAMP},
			</if>
			<if test="features != null">
				features = #{features,jdbcType=LONGVARCHAR},
			</if>
			<if test="productionAtStart != null">
				production_at_start = #{productionAtStart,jdbcType=TIMESTAMP},
			</if>
			<if test="productionAtEnd != null">
				production_at_end = #{productionAtEnd,jdbcType=TIMESTAMP},
			</if>
			version = version + 1,
			updated_at = now()
        </set>
		<where>
			id = #{id} and user_id = #{userId}
			<if test="version != null">
				AND version = #{version}
			</if>
		</where>
    </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>