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

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfOrderEntity" id="tbCfOrderMap">
        <result property="orderId" column="order_id"/>
        <result property="orderNo" column="order_no"/>
        <result property="orderName" column="order_name"/>
        <result property="orderTime" column="order_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="dealTime" column="deal_time"/>
        <result property="closeTime" column="close_time"/>
        <result property="orderStatus" column="order_status"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="deliveryAddressId" column="delivery_address_id"/>
        <result property="deliveryAddress" column="delivery_address"/>
        <result property="deliveryName" column="delivery_name"/>
        <result property="deliveryPhone" column="delivery_phone"/>
        <result property="itemsPrice" column="items_price"/>
        <result property="totalPrice" column="total_price"/>
        <result property="realityPay" column="reality_pay"/>
        <result property="deliveryFlag" column="delivery_flag"/>
        <result property="deliveryTime" column="delivery_time"/>
        <result property="expressCost" column="express_cost"/>
        <result property="couponId" column="coupon_id"/>
        <result property="couponTitle" column="coupon_title"/>
        <result property="couponPrice" column="coupon_price"/>
        <result property="fee" column="fee"/>
        <result property="tax" column="tax"/>
        <result property="payId" column="pay_id"/>
        <result property="payStatus" column="pay_status"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="descripitionName" column="descripition_name"></result>
    </resultMap>
    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfExchangeEntity" id="tbCfExchangeMap">
        <result property="id" column="id"/>
        <result property="type" column="type"/>
        <result property="currency" column="currency"/>
        <result property="exchangeCurrency" column="exchange_currency"/>
        <result property="exchangeRate" column="exchange_rate"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfOrderEntity">
		select
			`order_id`,
			`order_no`,
			`order_name`,
			`order_time`,
			`update_time`,
			`deal_time`,
			`close_time`,
			`order_status`,
			`user_id`,
			`user_name`,
			`delivery_address_id`,
			`delivery_address`,
			`delivery_name`,
			`delivery_phone`,
			`items_price`,
			`total_price`,
			`reality_pay`,
			`delivery_flag`,
			`delivery_time`,
			`express_cost`,
			`coupon_id`,
			`coupon_title`,
			`coupon_price`,
			`fee`,
			`tax`,
			`pay_id`,
			`pay_status`,
			`enable_flag`,
			`descripition_name`
		from tb_cf_order
		where order_id = #{id}
	</select>
    <select id="getOrder" resultType="com.diaoyun.zion.chinafrica.entity.OrderEntity">
			select
				r.order_item_id order_id,
				o.order_no,
				o.user_name,
				o.delivery_address,
				o.delivery_name,
				o.delivery_phone
			from tb_cf_order o left join tb_cf_item_order_r r on r.order_id=o.order_id
			where r.order_item_id = #{id}
	</select>

    <select id="getOrders" resultType="com.diaoyun.zion.chinafrica.entity.OrderEntity">
			select
				r.order_item_id order_id,
				o.order_no,
				o.user_name,
				o.delivery_address,
				o.delivery_name,
				o.delivery_phone
			from tb_cf_order o left join tb_cf_item_order_r r on r.order_id=o.order_id
			where r.order_item_id = #{id}
	</select>

    <select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfOrderEntity">
        select
        `order_id`,
        `order_no`,
        `order_name`,
        `order_time`,
        `update_time`,
        `deal_time`,
        `close_time`,
        `order_status`,
        `user_id`,
        `user_name`,
        `delivery_address_id`,
        `delivery_address`,
        `delivery_name`,
        `delivery_phone`,
        `items_price`,
        `total_price`,
        `reality_pay`,
        `delivery_flag`,
        `delivery_time`,
        `express_cost`,
        `coupon_id`,
        `coupon_title`,
        `coupon_price`,
        `fee`,
        `tax`,
        `pay_id`,
        `pay_status`,
        `enable_flag`,
        `descripition_name`
        from tb_cf_order
        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_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_order
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
    </select>
    <select id="queryCouponPrice" resultType="com.diaoyun.zion.chinafrica.entity.TbCfCouponEntity">
		 select t2.* from tb_cf_toicoupon t1 left join tb_cf_coupon t2 on t2.coupon_id=t1.coupon_id where t1.toitable_id=#{toitableId}
	 </select>
    <select id="currencyConversion" resultMap="tbCfExchangeMap">
       SELECT * from tb_cf_exchange  where exchange_currency=#{exchangeCurrency}
	</select>
    <insert id="save" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfOrderEntity">
		insert into tb_cf_order(
			`order_id`,
			`order_no`,
			`order_name`,
			`order_time`,
			`update_time`,
			`deal_time`,
			`close_time`,
			`order_status`,
			`user_id`,
			`user_name`,
			`delivery_address_id`,
			`delivery_address`,
			`delivery_name`,
			`delivery_phone`,
			`items_price`,
			`total_price`,
			`reality_pay`,
			`delivery_flag`,
			`delivery_time`,
			`express_cost`,
			`coupon_id`,
			`coupon_title`,
			`coupon_price`,
			`fee`,
			`tax`,
			`pay_id`,
			`pay_status`,
			`enable_flag`,
			`descripition_name`)
		values(
			#{orderId},
			#{orderNo},
			#{orderName},
			#{orderTime},
			#{updateTime},
			#{dealTime},
			#{closeTime},
			#{orderStatus},
			#{userId},
			#{userName},
			#{deliveryAddressId},
			#{deliveryAddress},
			#{deliveryName},
			#{deliveryPhone},
			#{itemsPrice},
			#{totalPrice},
			#{realityPay},
			#{deliveryFlag},
			#{deliveryTime},
			#{expressCost},
			#{couponId},
			#{couponTitle},
			#{couponPrice},
			#{fee},
			#{tax},
			#{payId},
			#{payStatus},
			#{enableFlag},
			#{descripitionName})
	</insert>

    <update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfOrderEntity">
        update tb_cf_order
        <set>
            <if test="orderNo != null">`order_no` = #{orderNo},</if>
            <if test="orderName != null">`order_name` = #{orderName},</if>
            <if test="orderTime != null">`order_time` = #{orderTime},</if>
            <if test="updateTime != null">`update_time` = #{updateTime},</if>
            <if test="dealTime != null">`deal_time` = #{dealTime},</if>
            <if test="closeTime != null">`close_time` = #{closeTime},</if>
            <if test="orderStatus != null">`order_status` = #{orderStatus},</if>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="userName != null">`user_name` = #{userName},</if>
            <if test="deliveryAddressId != null">`delivery_address_id` = #{deliveryAddressId},</if>
            <if test="deliveryAddress != null">`delivery_address` = #{deliveryAddress},</if>
            <if test="deliveryName != null">`delivery_name` = #{deliveryName},</if>
            <if test="deliveryPhone != null">`delivery_phone` = #{deliveryPhone},</if>
            <if test="itemsPrice != null">`items_price` = #{itemsPrice},</if>
            <if test="totalPrice != null">`total_price` = #{totalPrice},</if>
            <if test="realityPay != null">`reality_pay` = #{realityPay},</if>
            <if test="deliveryFlag != null">`delivery_flag` = #{deliveryFlag},</if>
            <if test="deliveryTime != null">`delivery_time` = #{deliveryTime},</if>
            <if test="expressCost != null">`express_cost` = #{expressCost},</if>
            <if test="couponId != null">`coupon_id` = #{couponId},</if>
            <if test="couponTitle != null">`coupon_title` = #{couponTitle},</if>
            <if test="couponPrice != null">`coupon_price` = #{couponPrice},</if>
            <if test="fee != null">`fee` = #{fee},</if>
            <if test="tax != null">`tax` = #{tax},</if>
            <if test="payId != null">`pay_id` = #{payId},</if>
            <if test="payStatus != null">`pay_status` = #{payStatus},</if>
            <if test="enableFlag != null">`enable_flag` = #{enableFlag},</if>
            <if test="descripitionName != null">`descripition_name` = #{descripitionName}</if>
        </set>
        where order_id = #{orderId}
    </update>

    <delete id="delete">
		delete from tb_cf_order where order_id = #{value}
	</delete>

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

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

    <!--获取用户订单数据-->
    <select id="getUserOrderList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfOrderEntity">
        select
        o.order_id,
        o.order_no,
        o.order_name,
        o.order_time,
        o.deal_time,
        o.user_id,
        o.user_name,
        o.delivery_address_id,
        o.delivery_address,
        o.delivery_name,
        o.delivery_phone,
        o.items_price,
        o.total_price,
        o.reality_pay,
        o.express_cost,
        o.coupon_id,
        o.coupon_title,
        o.coupon_price,
        o.order_status,
        o.fee,
        o.tax,
        o.pay_id,
        o.pay_status,
        o.enable_flag,
        o.descripition_name
        from tb_cf_order o
        where o.user_id=#{userId} and o.enable_flag=1
        <if test="orderStatus  != null">and o.order_status = #{orderStatus}</if>
        order by o.order_time desc
    </select>
    <select id="getOrdersId" resultType="String">
        	select r.order_item_id ordersId from tb_cf_item_order_r r where   r.order_id=#{orderId} and r.item_id=#{itemId}
    </select>
    <!--根据订单id，获取订单内商品详情-->
    <select id="getOrderItemList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemDetailEntity">
		SELECT
		t1.*
		FROM
		tb_cf_item_detail t1
		LEFT JOIN tb_cf_item_order_r t2 ON t1.item_id = t2.item_id
		LEFT JOIN tb_cf_order  t3 on t2.order_id=t3.order_id
		WHERE
		t2.enable_flag=1 and t2.order_id=#{order_id}
    </select>
    <!--根据订单id，获取订单内商品详情-->
    <select id="getItemList" resultType="com.diaoyun.zion.chinafrica.entity.ItemEntity">
			SELECT
		t1.item_id,
		t1.item_title,
		t1.item_num,
		t1.item_price,
		t5.template_id descripitionId,
		t5.template_title descripitionName
		FROM
		tb_cf_item_detail t1
		LEFT JOIN tb_cf_item_order_r t2 ON t1.item_id = t2.item_id
		LEFT JOIN tb_cf_order t3 ON t2.order_id = t3.order_id
		LEFT JOIN tb_cf_platform_order t4 on  t2.order_item_id=t4.relative_id
		LEFT JOIN tb_cf_express_template t5 on t5.template_id=t4.descripition_id
		WHERE
		t2.enable_flag = 1 and t2.order_item_id=#{orderId}
    </select>
    <!--获取付款订单-->
    <select id="getOrderListByTime" resultType="com.diaoyun.zion.chinafrica.entity.TbCfOrderEntity">
		select o.user_id,r.order_item_id order_id,o.deal_time
		from tb_cf_order o  left join tb_cf_item_order_r  r on r.order_id=o.order_id
		where pay_status=20 and r.enable_flag=1
		and deal_time <![CDATA[ >= ]]> #{beginDate} and deal_time <![CDATA[ < ]]> #{endDate}
	</select>

    <select id="getDeliveryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemShippedEntity">
		select
		DISTINCT
		r.order_item_id orderId,
		o.delivery_name deliveryName,
		o.delivery_address deliveryAddress,
		o.user_id userId,
		p.p_express_number expressId,
        i.item_id itemId,
        i.item_num itemNum,
        a.address_country_name country,
        p.descripition_id
		from tb_cf_order o
		INNER JOIN  tb_cf_item_order_r r on r.order_id=o.order_id
		INNER JOIN   tb_cf_platform_order p on r.order_item_id=p.relative_id
		INNER JOIN   tb_cf_item_detail i on r.item_id=i.item_id
		INNER JOIN   tb_cf_address a on a.user_id=o.user_id
		INNER JOIN   tb_cf_user_info  u on u.user_id=o.user_id
		INNER JOIN   tb_cf_express_template e on e.template_id=p.descripition_id
		where r.delivery_flag=10 and r.enable_flag=1 and r.order_status=40
		and p.create_time <![CDATA[ >= ]]> #{beginDate} and p.create_time <![CDATA[ < ]]> #{endDate}
	</select>
    <!--获取某段时间已发货订单数据-->
    <select id="getDeliveryLists" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemShippedEntity">
		select
		DISTINCT
		r.order_item_id orderId,
		p.p_express_number expressId,
		o.user_id userId,
		CONCAT_WS("_",u.nick,u.user_no) userInfo,
		p.create_time deliveryTime,
		a.address_country_name countryName,
		o.delivery_name deliveryName,
		o.delivery_phone deliveryPhone,
		o.delivery_address deliveryAddress,
		i.item_id itemId,
		i.item_title itemTitle,
		i.item_num itemNum,
		i.item_sku itemSku,
		CONCAT_WS("_",p.descripition_id,e.template_title) descripition
		from tb_cf_order o
		INNER JOIN  tb_cf_item_order_r r on r.order_id=o.order_id
		INNER JOIN   tb_cf_platform_order p on r.order_item_id=p.relative_id
		INNER JOIN   tb_cf_item_detail i on r.item_id=i.item_id
		INNER JOIN   tb_cf_address a on a.user_id=o.user_id
		INNER JOIN   tb_cf_user_info  u on u.user_id=o.user_id
		INNER JOIN   tb_cf_express_template e on e.template_id=p.descripition_id
		where r.delivery_flag=10 and r.enable_flag=1 and r.order_status=40
		and p.create_time <![CDATA[ >= ]]> #{beginDate} and p.create_time <![CDATA[ < ]]> #{endDate}
	</select>
</mapper>