<?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.xmomen.module.wx.module.order.mapper.MyOrderMapper" >
    <!-- 订单结果映射 -->
    <resultMap type="com.xmomen.module.wx.module.order.model.OrderModel" id="orderModel">
        <id column="ID" property="id"></id>
        <result column="ORDER_NO" property="orderNo"></result>
        <result column="ORDER_TYPE" property="orderType"></result>
        <result column="ORDER_TYPE_DESC" property="orderTypeDesc"></result>
        <result column="ORDER_SOURCE" property="orderSource"></result>
        <result column="ORDER_SOURCE_DESC" property="orderSourceDesc"></result>
        <result column="PAYMENT_MODE" property="paymentMode"></result>
        <result column="PAYMENT_MODE_DESC" property="paymentModeDesc"></result>
        <result column="OTHER_PAYMENT_MODE_DESC" property="otherPaymentModeDesc"></result>
        <result column="ORDER_STATUS" property="orderStatus"></result>
        <result column="ORDER_STATUS_DESC" property="orderStatusDesc"></result>
        <result column="PAY_STATUS" property="payStatus"></result>
        <result column="PAY_STATUS_DESC" property="payStatusDesc"></result>
        <result column="TOTAL_AMOUNT" property="totalAmount"></result>
        <result column="CREATE_TIME" property="createTime"></result>
        <result column="APPOINTMENT_TIME" property="appointmentTime"></result>
        <collection property="products" resultMap="orderProductItem"></collection>
    </resultMap>
    
    <!-- 订单详情结果映射 -->
    <resultMap type="com.xmomen.module.wx.module.order.model.OrderDetailModel" id="orderDetailModel">
        <id column="ID" property="id"></id>
        <result column="ORDER_NO" property="orderNo"></result>
        <result column="ORDER_TYPE" property="orderType"></result>
        <result column="ORDER_TYPE_DESC" property="orderTypeDesc"></result>
        <result column="ORDER_SOURCE" property="orderSource"></result>
        <result column="ORDER_SOURCE_DESC" property="orderSourceDesc"></result>
        <result column="PAYMENT_MODE" property="paymentMode"></result>
        <result column="PAYMENT_MODE_DESC" property="paymentModeDesc"></result>
        <result column="OTHER_PAYMENT_MODE" property="otherPaymentMode"></result>
        <result column="OTHER_PAYMENT_MODE_DESC" property="otherPaymentModeDesc"></result>
        <result column="ORDER_STATUS" property="orderStatus"></result>
        <result column="ORDER_STATUS_DESC" property="orderStatusDesc"></result>
        <result column="PAY_STATUS" property="payStatus"></result>
        <result column="PAY_STATUS_DESC" property="payStatusDesc"></result>
        <result column="CONSIGNEE_PHONE" property="consigneePhone"></result>
        <result column="CONSIGNEE_NAME" property="consigneeName"></result>
        <result column="CONSIGNEE_ADDRESS" property="consigneeAddress"></result>
        <result column="POSTCODE" property="postcode"></result>
        <result column="TRANSPORT_MODE" property="transportMode"></result>
        <result column="TRANSPORT_MODE_DESC" property="transportModeDesc"></result>
        <result column="REMARK" property="remark"></result>
        <result column="TOTAL_AMOUNT" property="discountPrice"></result>
        <result column="TOTAL_AMOUNT" property="totalAmount"></result>
        <result column="CREATE_TIME" property="createTime"></result>
        <result column="APPOINTMENT_TIME" property="appointmentTime"></result>
        <result column="OUT_DATE" property="outDate"></result>
        <result column="SHOU_HUO_DATE" property="shouHuoDate"></result>
        <result column="IS_REJECT" property="isReject"></result>
        <result column="EXPRESS_NAME" property="expressName"></result>
        <result column="EXPRESS_MEMBER_NAME" property="expressMemberName"></result>
        <result column="EXPRESS_MEMBER_PHONE" property="expressMemberPhone"></result>
        <collection property="products" resultMap="orderProductItem"></collection>
    </resultMap>
    
    <!-- 订单商品结果映射 -->
    <resultMap type="com.xmomen.module.wx.module.order.model.OrderProductItem" id="orderProductItem">
        <result column="ITEM_ID" property="itemId"></result>
        <result column="ITEM_NAME" property="itemName"></result>
        <result column="ITEM_CODE" property="itemCode"></result>
        <result column="ITEM_PRICE" property="itemPrice"></result>
        <result column="ITEM_QTY" property="itemQty"></result>
        <result column="ITEM_UNIT" property="itemUnit"></result>
        <result column="PIC_URL" property="picUrl"></result>
    </resultMap>
    
    <!-- 根据会员编号查询订单 -->
    <!-- <select id="selectOrdersByMemberCode" parameterType="String" resultMap="orderModel">
        SELECT 
        tb_order.ID,
        tb_order.ORDER_NO,
        tb_order.ORDER_TYPE,
        tb_order.ORDER_SOURCE,
        tb_order.PAYMENT_MODE,
        tb_order.ORDER_STATUS,
        tb_order.PAY_STATUS,
        tb_order.TOTAL_AMOUNT,
        tb_order.CREATE_TIME,
        tb_order.APPOINTMENT_TIME,
        order_item.ITEM_ID,
        order_item.ITEM_NAME,
        order_item.ITEM_CODE,
        order_item.ITEM_PRICE,
        order_item.ITEM_QTY,
        order_item.ITEM_UNIT
        FROM tb_order
        LEFT JOIN tb_order_item order_item
        ON
        	tb_order.ORDER_NO = order_item.ORDER_NO
        WHERE tb_order.MEMBER_CODE = #{memberCode}
    </select> -->

	<!-- 查询订单 -->
	<select id="selectOrders" parameterType="com.xmomen.module.wx.module.order.model.MyOrderQuery" resultMap="orderModel">
        SELECT 
        tb_order.ID,
        tb_order.ORDER_NO,
        tb_order.ORDER_TYPE,
        sp.SHOW_VALUE AS ORDER_TYPE_DESC,
        ssp.SHOW_VALUE AS ORDER_STATUS_DESC,
        paymentp.SHOW_VALUE AS PAYMENT_MODE_DESC,
        paymentp2.`SHOW_VALUE` as OTHER_PAYMENT_MODE_DESC,
        osp.SHOW_VALUE AS ORDER_SOURCE_DESC,
        case tb_order.PAY_STATUS
            WHEN 1 THEN '已支付'
            WHEN 0 THEN '待付款'
            WHEN 2 THEN '待付款'
        END as PAY_STATUS_DESC,
        tb_order.ORDER_SOURCE,
        tb_order.PAYMENT_MODE,
        tb_order.OTHER_PAYMENT_MODE,
        tb_order.ORDER_STATUS,
        tb_order.PAY_STATUS,
        tb_order.TOTAL_AMOUNT,
        tb_order.CREATE_TIME,
        tb_order.APPOINTMENT_TIME,
        order_item.ITEM_ID,
        order_item.ITEM_NAME,
        order_item.ITEM_CODE,
        order_item.ITEM_PRICE,
        order_item.ITEM_QTY,
        order_item.ITEM_UNIT,
        cr.PATH as PIC_URL
        FROM tb_order
        LEFT JOIN tb_order_item order_item
        ON
        	tb_order.ORDER_NO = order_item.ORDER_NO
        LEFT JOIN sys_dictionary s on s.DICTIONARY_CODE='ORDER_TYPE'
        LEFT JOIN sys_dictionary_parameter sp on sp.SYS_DICTIONARY_ID=s.ID and tb_order.ORDER_TYPE = sp.REAL_VALUE
        LEFT JOIN sys_dictionary ss on ss.DICTIONARY_CODE='ORDER_STATUS'
        LEFT JOIN sys_dictionary_parameter ssp on ssp.SYS_DICTIONARY_ID=ss.ID and tb_order.ORDER_STATUS = ssp.REAL_VALUE
        LEFT JOIN sys_dictionary payment on payment.DICTIONARY_CODE='PAYMENT_MODE'
        LEFT JOIN sys_dictionary_parameter paymentp on paymentp.SYS_DICTIONARY_ID=payment.ID and tb_order.PAYMENT_MODE = paymentp.REAL_VALUE

		LEFT JOIN sys_dictionary payment2 on payment2.DICTIONARY_CODE='PAYMENT_MODE'
        LEFT JOIN sys_dictionary_parameter paymentp2 on paymentp2.SYS_DICTIONARY_ID=payment2.ID and tb_order.`OTHER_PAYMENT_MODE` = paymentp2.REAL_VALUE

        LEFT JOIN sys_dictionary oss on oss.DICTIONARY_CODE='ORDER_SOURCE'
        LEFT JOIN sys_dictionary_parameter osp on osp.SYS_DICTIONARY_ID=oss.ID and tb_order.ORDER_SOURCE = osp.REAL_VALUE
        LEFT JOIN cd_resource cr on 
            cr.ENTITY_TYPE = 'cd_item' and cr.entity_id = order_item.ITEM_ID
            and cr.IS_DEFAULT = 1
        <where>
        	tb_order.MEMBER_CODE = #{userId}
       	    <choose>
       	        <when test="status == 0 or status == 2">
       	            AND tb_order.PAY_STATUS = 0
       	        </when>
       	        <when test="status == 1">
       	             AND ssp.REAL_VALUE = 5
       	        </when>
       	        <otherwise></otherwise>
       	    </choose>
        	<if test="minOrderTime">
        		<![CDATA[ AND tb_order.CREATE_TIME >= #{minOrderTime}]]>
        		
        	</if>
        	<if test="maxOrderTime">
        		<![CDATA[ AND tb_order.CREATE_TIME < #{maxOrderTime}]]>
        		
        	</if>
        </where>
        order by tb_order.CREATE_TIME desc
    </select>
    
    <!-- 查询订单详情 -->
    <select id="getOrderDetail" parameterType="com.xmomen.module.wx.module.order.model.MyOrderQuery" resultMap="orderDetailModel">
        SELECT 
        tb_order.*,
        order_item.ITEM_ID,
        order_item.ITEM_NAME,
        order_item.ITEM_CODE,
        order_item.ITEM_PRICE,
        order_item.ITEM_QTY,
        order_item.ITEM_UNIT,
        sp.SHOW_VALUE AS ORDER_TYPE_DESC,
        ssp.SHOW_VALUE AS ORDER_STATUS_DESC,
        paymentp.SHOW_VALUE AS PAYMENT_MODE_DESC,
        paymentp2.`SHOW_VALUE` as OTHER_PAYMENT_MODE_DESC,
        osp.SHOW_VALUE AS ORDER_SOURCE_DESC,
        case tb_order.PAY_STATUS
            WHEN 1 THEN '已支付'
            WHEN 0 THEN '待付款'
            WHEN 2 THEN '待付款'
        END as PAY_STATUS_DESC,
        ce.EXPRESS_NAME,
        cem.MEMBER_NAME as EXPRESS_MEMBER_NAME,
        cem.PHONE as EXPRESS_MEMBER_PHONE,
        cr.PATH as PIC_URL
        FROM tb_order
        LEFT JOIN tb_order_item order_item
        ON
        	tb_order.ORDER_NO = order_item.ORDER_NO
        
        LEFT JOIN sys_dictionary s on s.DICTIONARY_CODE='ORDER_TYPE'
        LEFT JOIN sys_dictionary_parameter sp on sp.SYS_DICTIONARY_ID=s.ID and tb_order.ORDER_TYPE = sp.REAL_VALUE
        LEFT JOIN sys_dictionary ss on ss.DICTIONARY_CODE='ORDER_STATUS'
        LEFT JOIN sys_dictionary_parameter ssp on ssp.SYS_DICTIONARY_ID=ss.ID and tb_order.ORDER_STATUS = ssp.REAL_VALUE
        LEFT JOIN sys_dictionary payment on payment.DICTIONARY_CODE='PAYMENT_MODE'
        LEFT JOIN sys_dictionary_parameter paymentp on paymentp.SYS_DICTIONARY_ID=payment.ID and tb_order.PAYMENT_MODE = paymentp.REAL_VALUE

		LEFT JOIN sys_dictionary payment2 on payment2.DICTIONARY_CODE='PAYMENT_MODE'
        LEFT JOIN sys_dictionary_parameter paymentp2 on paymentp2.SYS_DICTIONARY_ID=payment2.ID and tb_order.`OTHER_PAYMENT_MODE` = paymentp2.REAL_VALUE

        LEFT JOIN sys_dictionary oss on oss.DICTIONARY_CODE='ORDER_SOURCE'
        LEFT JOIN sys_dictionary_parameter osp on osp.SYS_DICTIONARY_ID=oss.ID and tb_order.ORDER_SOURCE = osp.REAL_VALUE
        LEFT JOIN cd_express ce on tb_order.despatch_express_id = ce.id
        LEFT JOIN cd_express_member cem on tb_order.express_member_id = cem.id
        LEFT JOIN cd_resource cr on 
            cr.ENTITY_TYPE = 'cd_item' and cr.entity_id = order_item.ITEM_ID
            and cr.IS_DEFAULT = 1
        <where>
        	<if test="orderId">
        	    tb_order.ID = #{orderId}
        	</if>
        	<if test="orderNo">
        	    tb_order.ORDER_NO = #{orderNo}
        	</if>
        </where>
        
    </select>
    
    <!-- 查询订单统计 -->
    <select id="getOrderStatistic" parameterType="Integer" resultType="com.xmomen.module.wx.module.order.model.OrderStatisticModel">
    	SELECT
    	COUNT(tb_order.id) as count, 
    	tb_order.order_status as orderStatus,
    	tb_order.pay_status as payStatus,
    	case tb_order.PAY_STATUS
            WHEN 1 THEN '已支付'
            ELSE '待付款'
        END as payStatusDesc,
        s.show_value as orderStatusDesc from tb_order
		JOIN sys_dictionary_parameter s
			ON s.sys_dictionary_id = 5 and s.real_value = tb_order.order_status 
		WHERE tb_order.order_source = 1 and tb_order.member_code = #{userId}
		and tb_order.order_status != 9
		GROUP BY order_status, pay_status
    	
    </select>
</mapper>