<?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>

	<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
				`order_id`,
				`order_no`,
				`user_name`,
				`delivery_address`,
				`delivery_name`,
				`delivery_phone`
			from tb_cf_order
			where order_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_issue_coupon t1 left join tb_cf_coupon t2 on t2.coupon_id=t1.coupon_id where t1.coupon_issuetable_id=#{iss_coupon_id}
	 </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="getUserOrderList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfOrderEntity">
        select * from tb_cf_order where user_id=#{userId} and enable_flag=1
		<if test="orderStatus != null"> and order_status = #{orderStatus} </if>
        order by order_time desc
    </select>

    <!--根据订单id，获取订单内商品详情-->
    <select id="getOrderItemList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemDetailEntity">
		SELECT
		t1.*,
		t3.descripition_name 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
		WHERE
		t2.enable_flag = 1 and t3.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,
		t1.item_category,
		t3.descripition_name 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
		WHERE
		t2.enable_flag = 1 and t3.order_id=#{order_id}
    </select>
	<!--获取付款订单-->
	<select id="getOrderListByTime" resultType="com.diaoyun.zion.chinafrica.entity.TbCfOrderEntity">
		select user_id,order_id,deal_time from tb_cf_order where pay_status=20 and 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 o.user_id,o.order_id,p.create_time deliveryTime,o.delivery_address,
		p.p_express_number expressId,i.item_id itemId,d.descripition_name descripitionName from tb_cf_order o left join tb_cf_platform_order p on o.order_id=p.order_id left join tb_cf_station_item i on p.item_id=i.item_id left join tb_cf_descripiton d on d.descripition_id=i.item_descrition_id where o.delivery_flag=20 and o.enable_flag=1
		and p.create_time <![CDATA[ >= ]]> #{beginDate} and p.create_time <![CDATA[ < ]]> #{endDate}
	</select>

</mapper>