<?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.ms.dal.mapper.PurchaseOrderItemMapper">

    <resultMap id="BaseResultMap" type="com.ms.dal.entity.PurchaseOrderItem">
            <id property="purchaseOrderItemId" column="purchase_order_item_id" jdbcType="BIGINT"/>
            <result property="purchaseOrderId" column="purchase_order_id" jdbcType="BIGINT"/>
            <result property="shopId" column="shop_id" jdbcType="BIGINT"/>
            <result property="purOrderId" column="pur_order_id" jdbcType="VARCHAR"/>
            <result property="subItemId" column="sub_item_id" jdbcType="VARCHAR"/>
            <result property="orderId" column="order_id" jdbcType="BIGINT"/>
            <result property="wareId" column="ware_id" jdbcType="BIGINT"/>
            <result property="skuId" column="sku_id" jdbcType="BIGINT"/>
            <result property="purchaseUrl" column="purchase_url" jdbcType="VARCHAR"/>
            <result property="purchasePrice" column="purchase_price" jdbcType="DECIMAL"/>
            <result property="purchaseNum" column="purchase_num" jdbcType="SMALLINT"/>
            <result property="status" column="status" jdbcType="VARCHAR"/>
            <result property="gmtCreate" column="gmt_create" jdbcType="TIMESTAMP"/>
            <result property="gmtModified" column="gmt_modified" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        purchase_order_item_id,purchase_order_id,shop_id,
        pur_order_id,sub_item_id,order_id,ware_id,sku_id,
        purchase_url,purchase_price,purchase_num,
        status,gmt_create,gmt_modified
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from purchase_order_item
        where  purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT} 
    </select>
    <select id="getListByPurchaseOrderId" resultType="com.ms.dal.entity.PurchaseOrderItem">
        select
        <include refid="Base_Column_List" />
        from purchase_order_item
        where purchase_order_id = #{purchaseOrderId,jdbcType=BIGINT}
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete from purchase_order_item
        where  purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT} 
    </delete>
    <insert id="insert" keyColumn="purchase_order_item_id" keyProperty="purchaseOrderItemId" parameterType="com.ms.dal.entity.PurchaseOrderItem" useGeneratedKeys="true">
        insert into purchase_order_item
        ( purchase_order_item_id,purchase_order_id,shop_id
        ,pur_order_id,sub_item_id,order_id,ware_id,sku_id
        ,purchase_url,purchase_price,purchase_num
        ,status,gmt_create,gmt_modified
        )
        values (#{purchaseOrderItemId,jdbcType=BIGINT},#{purchaseOrderId,jdbcType=BIGINT},#{shopId,jdbcType=BIGINT}
        ,#{purOrderId,jdbcType=VARCHAR},#{subItemId,jdbcType=VARCHAR},#{orderId,jdbcType=BIGINT},#{wareId,jdbcType=BIGINT},#{skuId,jdbcType=BIGINT}
        ,#{purchaseUrl,jdbcType=VARCHAR},#{purchasePrice,jdbcType=DECIMAL},#{purchaseNum,jdbcType=SMALLINT}
        ,#{status,jdbcType=VARCHAR},#{gmtCreate,jdbcType=TIMESTAMP},#{gmtModified,jdbcType=TIMESTAMP}
        )
    </insert>
    <update id="insertOrUpdate"  keyColumn="purchase_order_item_id" keyProperty="purchaseOrderItemId" parameterType="com.ms.dal.entity.PurchaseOrderItem"
        useGeneratedKeys="true">
        insert into purchase_order_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="purchaseOrderItemId != null">purchase_order_item_id,</if>
                <if test="purchaseOrderId != null">purchase_order_id,</if>
                <if test="shopId != null">shop_id,</if>
                <if test="purOrderId != null">pur_order_id,</if>
                <if test="subItemId != null">sub_item_id,</if>
                <if test="orderId != null">order_id,</if>
                <if test="wareId != null">ware_id,</if>
                <if test="skuId != null">sku_id,</if>
                <if test="purchaseUrl != null">purchase_url,</if>
                <if test="purchasePrice != null">purchase_price,</if>
                <if test="purchaseNum != null">purchase_num,</if>
                <if test="status != null">status,</if>
                <if test="gmtCreate != null">gmt_create,</if>
                <if test="gmtModified != null">gmt_modified,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="purchaseOrderItemId != null">#{purchaseOrderItemId,jdbcType=BIGINT},</if>
                <if test="purchaseOrderId != null">#{purchaseOrderId,jdbcType=BIGINT},</if>
                <if test="shopId != null">#{shopId,jdbcType=BIGINT},</if>
                <if test="purOrderId != null">#{purOrderId,jdbcType=VARCHAR},</if>
                <if test="subItemId != null">#{subItemId,jdbcType=VARCHAR},</if>
                <if test="orderId != null">#{orderId,jdbcType=BIGINT},</if>
                <if test="wareId != null">#{wareId,jdbcType=BIGINT},</if>
                <if test="skuId != null">#{skuId,jdbcType=BIGINT},</if>
                <if test="purchaseUrl != null">#{purchaseUrl,jdbcType=VARCHAR},</if>
                <if test="purchasePrice != null">#{purchasePrice,jdbcType=DECIMAL},</if>
                <if test="purchaseNum != null">#{purchaseNum,jdbcType=SMALLINT},</if>
                <if test="status != null">#{status,jdbcType=VARCHAR},</if>
                <if test="gmtCreate != null">#{gmtCreate,jdbcType=TIMESTAMP},</if>
                <if test="gmtModified != null">#{gmtModified,jdbcType=TIMESTAMP},</if>
        </trim>
        ON DUPLICATE KEY UPDATE
        <trim  suffixOverrides=",">
                <if test="purchaseOrderId != null">
                    purchase_order_id = #{purchaseOrderId,jdbcType=BIGINT},
                </if>
                <if test="shopId != null">
                    shop_id = #{shopId,jdbcType=BIGINT},
                </if>
                <if test="purOrderId != null">
                    pur_order_id = #{purOrderId,jdbcType=VARCHAR},
                </if>
                <if test="subItemId != null">
                    sub_item_id = #{subItemId,jdbcType=VARCHAR},
                </if>
                <if test="orderId != null">
                    order_id = #{orderId,jdbcType=BIGINT},
                </if>
                <if test="wareId != null">
                    ware_id = #{wareId,jdbcType=BIGINT},
                </if>
                <if test="skuId != null">
                    sku_id = #{skuId,jdbcType=BIGINT},
                </if>
                <if test="purchaseUrl != null">
                    purchase_url = #{purchaseUrl,jdbcType=VARCHAR},
                </if>
                <if test="purchasePrice != null">
                    purchase_price = #{purchasePrice,jdbcType=DECIMAL},
                </if>
                <if test="purchaseNum != null">
                    purchase_num = #{purchaseNum,jdbcType=SMALLINT},
                </if>
                <if test="status != null">
                    status = #{status,jdbcType=VARCHAR},
                </if>
                <if test="gmtCreate != null">
                    gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
                </if>
                <if test="gmtModified != null">
                    gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
                </if>
        </trim>
    </update>
    <insert id="insertSelective" keyColumn="purchase_order_item_id" keyProperty="purchaseOrderItemId" parameterType="com.ms.dal.entity.PurchaseOrderItem" useGeneratedKeys="true">
        insert into purchase_order_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="purchaseOrderItemId != null">purchase_order_item_id,</if>
                <if test="purchaseOrderId != null">purchase_order_id,</if>
                <if test="shopId != null">shop_id,</if>
                <if test="purOrderId != null">pur_order_id,</if>
                <if test="subItemId != null">sub_item_id,</if>
                <if test="orderId != null">order_id,</if>
                <if test="wareId != null">ware_id,</if>
                <if test="skuId != null">sku_id,</if>
                <if test="purchaseUrl != null">purchase_url,</if>
                <if test="purchasePrice != null">purchase_price,</if>
                <if test="purchaseNum != null">purchase_num,</if>
                <if test="status != null">status,</if>
                <if test="gmtCreate != null">gmt_create,</if>
                <if test="gmtModified != null">gmt_modified,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="purchaseOrderItemId != null">#{purchaseOrderItemId,jdbcType=BIGINT},</if>
                <if test="purchaseOrderId != null">#{purchaseOrderId,jdbcType=BIGINT},</if>
                <if test="shopId != null">#{shopId,jdbcType=BIGINT},</if>
                <if test="purOrderId != null">#{purOrderId,jdbcType=VARCHAR},</if>
                <if test="subItemId != null">#{subItemId,jdbcType=VARCHAR},</if>
                <if test="orderId != null">#{orderId,jdbcType=BIGINT},</if>
                <if test="wareId != null">#{wareId,jdbcType=BIGINT},</if>
                <if test="skuId != null">#{skuId,jdbcType=BIGINT},</if>
                <if test="purchaseUrl != null">#{purchaseUrl,jdbcType=VARCHAR},</if>
                <if test="purchasePrice != null">#{purchasePrice,jdbcType=DECIMAL},</if>
                <if test="purchaseNum != null">#{purchaseNum,jdbcType=SMALLINT},</if>
                <if test="status != null">#{status,jdbcType=VARCHAR},</if>
                <if test="gmtCreate != null">#{gmtCreate,jdbcType=TIMESTAMP},</if>
                <if test="gmtModified != null">#{gmtModified,jdbcType=TIMESTAMP},</if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.ms.dal.entity.PurchaseOrderItem">
        update purchase_order_item
        <set>
                <if test="purchaseOrderId != null">
                    purchase_order_id = #{purchaseOrderId,jdbcType=BIGINT},
                </if>
                <if test="shopId != null">
                    shop_id = #{shopId,jdbcType=BIGINT},
                </if>
                <if test="purOrderId != null">
                    pur_order_id = #{purOrderId,jdbcType=VARCHAR},
                </if>
                <if test="subItemId != null">
                    sub_item_id = #{subItemId,jdbcType=VARCHAR},
                </if>
                <if test="orderId != null">
                    order_id = #{orderId,jdbcType=BIGINT},
                </if>
                <if test="wareId != null">
                    ware_id = #{wareId,jdbcType=BIGINT},
                </if>
                <if test="skuId != null">
                    sku_id = #{skuId,jdbcType=BIGINT},
                </if>
                <if test="purchaseUrl != null">
                    purchase_url = #{purchaseUrl,jdbcType=VARCHAR},
                </if>
                <if test="purchasePrice != null">
                    purchase_price = #{purchasePrice,jdbcType=DECIMAL},
                </if>
                <if test="purchaseNum != null">
                    purchase_num = #{purchaseNum,jdbcType=SMALLINT},
                </if>
                <if test="status != null">
                    status = #{status,jdbcType=VARCHAR},
                </if>
                <if test="gmtCreate != null">
                    gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
                </if>
                <if test="gmtModified != null">
                    gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
                </if>
        </set>
        where   purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT} 
    </update>
    <update id="updateByPrimaryKey" parameterType="com.ms.dal.entity.PurchaseOrderItem">
        update purchase_order_item
        set 
            purchase_order_id =  #{purchaseOrderId,jdbcType=BIGINT},
            shop_id = #{shopId,jdbcType=BIGINT},
            pur_order_id =  #{purOrderId,jdbcType=VARCHAR},
            sub_item_id =  #{subItemId,jdbcType=VARCHAR},
            order_id =  #{orderId,jdbcType=BIGINT},
            ware_id =  #{wareId,jdbcType=BIGINT},
            sku_id =  #{skuId,jdbcType=BIGINT},
            purchase_url =  #{purchaseUrl,jdbcType=VARCHAR},
            purchase_price =  #{purchasePrice,jdbcType=DECIMAL},
            purchase_num =  #{purchaseNum,jdbcType=SMALLINT},
            status =  #{status,jdbcType=VARCHAR},
            gmt_create =  #{gmtCreate,jdbcType=TIMESTAMP},
            gmt_modified =  #{gmtModified,jdbcType=TIMESTAMP}
        where   purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT} 
    </update>
    <insert id="insertBatch" keyColumn="purchase_order_item_id" keyProperty="purchaseOrderItemId" useGeneratedKeys="true">
        insert into purchase_order_item
        (purchase_order_item_id,purchase_order_id,shop_id
        ,sub_item_id,order_id,pur_order_id,ware_id,sku_id,purchase_url
        ,purchase_price,purchase_num,status
        ,gmt_create,gmt_modified)
        values
        <foreach item="item" collection="list" separator="," >
            (#{item.purchaseOrderItemId,jdbcType=BIGINT},#{item.purchaseOrderId,jdbcType=BIGINT},#{item.shopId,jdbcType=BIGINT}
            ,#{item.subItemId,jdbcType=VARCHAR},#{item.orderId,jdbcType=BIGINT}
            ,#{item.purOrderId,jdbcType=VARCHAR},#{item.wareId,jdbcType=BIGINT},#{item.skuId,jdbcType=BIGINT},#{item.purchaseUrl,jdbcType=VARCHAR}
            ,#{item.purchasePrice,jdbcType=DECIMAL},#{item.purchaseNum,jdbcType=SMALLINT},#{item.status,jdbcType=VARCHAR}
            ,#{item.gmtCreate,jdbcType=TIMESTAMP},#{item.gmtModified,jdbcType=TIMESTAMP})
        </foreach>
    </insert>
    <update id="updateStatusByPurchaseOrderIds">
        update purchase_order_item
        set
        status =  #{status,jdbcType=VARCHAR},
        gmt_modified = now()
        where 1=1
        <choose>
            <when test="purchaseOrderIds!=null and purchaseOrderIds.size>0">
                and purchase_order_id in
                <foreach collection="purchaseOrderIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                AND 1=0
            </otherwise>
        </choose>
    </update>
</mapper>
