<?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.TbCfItemOrderRDao">

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfItemOrderREntity" id="tbCfItemOrderRMap">
        <result property="orderItemId" column="order_item_id"/>
        <result property="itemId" column="item_id"/>
        <result property="orderId" column="order_id"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="orderStatus" column="order_status"/>
        <result property="deliveryFlag" column="delivery_flag"/>
        <result property="deliveryTime" column="delivery_time"/>
        <result property="closeTime" column="close_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="isSend" column="is_send"/>
    </resultMap>

    <select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemOrderREntity">
		select
			`order_item_id`,
			`item_id`,
			`order_id`,
			`enable_flag`,
			`order_status`,
			`delivery_flag`,
			`delivery_time`,
			`close_time`,
			`is_send`,
			`update_time`
		from tb_cf_item_order_r
		where order_item_id = #{id}
	</select>

    <select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemOrderREntity">
        select
        `order_item_id`,
        `item_id`,
        `order_id`,
        `enable_flag`,
        `order_status`,
        `delivery_flag`,
        `delivery_time`,
        `close_time`,
        `is_send`,
        `update_time`
        from tb_cf_item_order_r
        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 order_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_order_r
        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.TbCfItemOrderREntity">
		insert into tb_cf_item_order_r(
			`order_item_id`,
			`item_id`,
			`order_id`,
			`enable_flag`,
			`order_status`,
			`delivery_flag`,
			`delivery_time`,
			`close_time`,
			`is_send`,
			`update_time`)
		values(
			#{orderItemId},
			#{itemId},
			#{orderId},
			#{enableFlag},
			#{orderStatus},
			#{deliveryFlag},
			#{deliveryTime},
			#{closeTime},
			#{isSend},
			#{updateTime})
	</insert>

    <update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfItemOrderREntity">
        update tb_cf_item_order_r
        <set>
            <if test="itemId != null">`item_id` = #{itemId},</if>
            <if test="orderId != null">`order_id` = #{orderId},</if>
            <if test="enableFlag != null">`enable_flag` = #{enableFlag},</if>
            <if test="orderStatus != null">`order_status` = #{orderStatus},</if>
            <if test="deliveryFlag != null">`delivery_flag` = #{deliveryFlag},</if>
            <if test="deliveryTime != null">`delivery_time` = #{deliveryTime},</if>
            <if test="closeTime != null">`close_time` = #{closeTime},</if>
            <if test="isSend != null">`is_send` = #{isSend},</if>
            <if test="updateTime != null">`update_time` = #{updateTime}</if>
        </set>
        where order_item_id = #{orderItemId}
    </update>
    <update id="updates" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfItemOrderREntity">
        update tb_cf_item_order_r
        <set>
            <if test="itemId != null">`item_id` = #{itemId},</if>
            <if test="orderId != null">`order_id` = #{orderId},</if>
            <if test="enableFlag != null">`enable_flag` = #{enableFlag},</if>
            <if test="orderStatus != null">`order_status` = #{orderStatus},</if>
            <if test="deliveryFlag != null">`delivery_flag` = #{deliveryFlag},</if>
            <if test="deliveryTime != null">`delivery_time` = #{deliveryTime},</if>
            <if test="closeTime != null">`close_time` = #{closeTime},</if>
            <if test="isSend != null">`is_send` = #{isSend},</if>
            <if test="updateTime != null">`update_time` = #{updateTime}</if>
        </set>
        where order_id = #{orderId}
    </update>

    <select id="getItemNum" resultType="int">
		select t2.item_num from tb_cf_item_order_r t1 left join tb_cf_item_detail t2 on t1.item_id=t2.item_id where t1.order_item_id=#{ordersId}
	</select>


    <delete id="delete">
		delete from tb_cf_item_order_r where order_item_id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from tb_cf_item_order_r where order_item_id in
        <foreach item="orderItemId" collection="array" open="(" separator="," close=")">
            #{orderItemId}
        </foreach>
    </delete>
    <select id="getOrderId" resultType="String">
		select order_id  from  tb_cf_item_order_r i where i.order_item_id=#{ordersId}
	</select>
    <select id="getItemId" resultType="String">
		select item_id  from  tb_cf_item_order_r i where i.order_item_id=#{ordersId}
	</select>

    <update id="updateDeliveryTime" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfItemOrderREntity">
        update tb_cf_item_order_r r
        <set>
            <if test="deliveryTime != null">r.delivery_time = #{deliveryTime},</if>
        </set>
        where r.order_item_id = #{orderItemId} and r.delivery_time is null
    </update>

    <select id="getOrderItemId" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemOrderREntity">
		select * from tb_cf_item_order_r where order_id=#{orderId} and item_id=#{itemId}
	</select>

    <select id="getOrderStatus">
		select order_status from tb_cf_item_order_r where order_item_id=#{ordersId}
	</select>

    <select id="findOrderStatus">
		select order_status from tb_cf_item_order_r where order_id=#{orderId}
	</select>

    <select id="queryByItemOrderR" resultType="String">
		select order_item_id from tb_cf_item_order_r where order_id=#{orderId}
	</select>

    <update id="updateById">
        UPDATE tb_cf_item_order_r
        SET enable_flag=0,update_time=now()
        WHERE order_item_id in
        <foreach collection="array" item="tbcfItemOrderR" open="(" separator="," close=")">
            #{tbcfItemOrderR}
        </foreach>
    </update>
    <update id="updateItemStatus">
        UPDATE tb_cf_item_order_r
        SET order_status=60,update_time=now()
        WHERE order_item_id in
        <foreach collection="array" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </update>
    <!--根据快递单号id，修改物流状态为已入中国仓库 -->
    <update id="updateExpressStatus">
        UPDATE tb_cf_order o,tb_cf_platform_order p,tb_cf_item_order_r i
        SET i.delivery_flag = 20
        WHERE o.order_id=p.order_id and i.order_id=o.order_id and i.delivery_flag=10 and p.p_express_number in
        <foreach collection="array" item="sids" open="(" separator="," close=")">
            #{sids}
        </foreach>
    </update>

    <!--根据快递单号id，修改物流状态为已入非洲仓库 -->
    <update id="updateExpressStatuss">
        UPDATE tb_cf_order o,tb_cf_platform_order p,tb_cf_item_order_r i
        SET i.delivery_flag = 40
        WHERE o.order_id=p.order_id and i.order_id=o.order_id and i.delivery_flag=20 and i.is_send=0 and
        p.p_express_number in
        <foreach collection="array" item="sids" open="(" separator="," close=")">
            #{sids}
        </foreach>
    </update>

    <!--根据快递单号id，修改物流状态为已签收 -->
    <update id="updateExpressStatusDelivery">
        UPDATE tb_cf_order o,tb_cf_platform_order p,tb_cf_item_order_r i
        SET i.delivery_flag = 50,i.order_status=50
        WHERE o.order_id=p.order_id and i.order_id=o.order_id and i.delivery_flag=40 and p.p_express_number in
        <foreach collection="array" item="sids" open="(" separator="," close=")">
            #{sids}
        </foreach>
    </update>
</mapper>