<?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.yonge.cooleshow.admin.dao.OmsOrderDao">
    <resultMap id="orderDetailResultMap" type="com.yonge.cooleshow.admin.dto.OmsOrderDetail" extends="com.yonge.cooleshow.mbg.mapper.OmsOrderMapper.BaseResultMap">
        <collection property="orderItemList" resultMap="com.yonge.cooleshow.mbg.mapper.OmsOrderItemMapper.BaseResultMap" columnPrefix="item_"/>
        <collection property="historyList" resultMap="com.yonge.cooleshow.mbg.mapper.OmsOrderOperateHistoryMapper.BaseResultMap" columnPrefix="history_"/>
    </resultMap>

    <resultMap id="orderResultMap" type="com.yonge.cooleshow.admin.dto.OrderVo" extends="com.yonge.cooleshow.mbg.mapper.OmsOrderMapper.BaseResultMap">
        <result column="userId"  property="userId" />
        <result column="username"  property="username" />
        <result column="phone"  property="phone" />
        <result column="userType"  property="userType" />
    </resultMap>
    <select id="getList" resultMap="orderResultMap">
        SELECT o.*,
                um.id as userId,
                um.username as username,
                um.phone as phone,
                o.platform_type as userType

        FROM
        oms_order o
        left join ums_member um on um.id = member_id
        WHERE
        o.delete_status = 0
        <if test="queryParam.orderSn!=null and queryParam.orderSn!=''">
            AND o.order_sn = #{queryParam.orderSn}
        </if>
        <if test="queryParam.status!=null">
            AND o.`status` = #{queryParam.status}
        </if>
        <if test="queryParam.sourceType!=null">
            AND o.source_type = #{queryParam.sourceType}
        </if>
        <if test="queryParam.orderType!=null">
            AND o.order_type = #{queryParam.orderType}
        </if>
        <if test="queryParam.createTime!=null and queryParam.createTime!=''">
            AND o.create_time LIKE concat(#{queryParam.createTime},"%")
        </if>
        <if test="queryParam.receiverKeyword!=null and queryParam.receiverKeyword!=''">
            AND (
            o.receiver_name LIKE concat("%",#{queryParam.receiverKeyword},"%")
            OR o.receiver_phone LIKE concat("%",#{queryParam.receiverKeyword},"%")
            )
        </if>
        order by o.id desc
    </select>
    <update id="delivery">
        UPDATE oms_order
        SET
        delivery_sn = CASE id
        <foreach collection="list" item="item">
            WHEN #{item.orderId} THEN #{item.deliverySn}
        </foreach>
        END,
        delivery_company = CASE id
        <foreach collection="list" item="item">
            WHEN #{item.orderId} THEN #{item.deliveryCompany}
        </foreach>
        END,
        delivery_time = CASE id
        <foreach collection="list" item="item">
            WHEN #{item.orderId} THEN now()
        </foreach>
        END,
        `status` = CASE id
        <foreach collection="list" item="item">
            WHEN #{item.orderId} THEN 2
        </foreach>
        END
        WHERE
        id IN
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item.orderId}
        </foreach>
        AND `status` = 1
    </update>
    <select id="getDetail" resultMap="orderDetailResultMap">
        SELECT o.*,
            oi.id item_id,
            oi.product_id item_product_id,
            oi.product_sn item_product_sn,
            oi.product_pic item_product_pic,
            oi.product_name item_product_name,
            oi.product_brand item_product_brand,
            oi.product_price item_product_price,
            oi.product_quantity item_product_quantity,
            oi.product_attr item_product_attr,
            oh.id history_id,
            oh.operate_man history_operate_man,
            oh.create_time history_create_time,
            oh.order_status history_order_status,
            oh.note history_note
        FROM
            oms_order o
            LEFT JOIN oms_order_item oi ON o.id = oi.order_id
            LEFT JOIN oms_order_operate_history oh ON o.id = oh.order_id
        WHERE
            o.id = #{id}
        ORDER BY oi.id ASC,oh.create_time DESC
    </select>

    <select id="getUserOrderInfoList" resultType="java.util.Map">
        select
            member_id as userId,
            min(payment_time) as receiveTime
        from oms_order
        <where>
            <if test="startTime != null and startTime !=''">
                and payment_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and payment_time &lt;= #{endTime}
            </if>
            <if test="userIdList != null and userIdList.size() != 0">
                and member_id in
                <foreach collection="userIdList" separator="," close=")" open="(" item="item">
                    #{item}
                </foreach>
            </if>
        </where>
        group by member_id
    </select>

    <select id="selectOrderStatusNum" resultType="com.yonge.cooleshow.admin.dto.HomeStatistical">

        select
            sum(if(status = 0 or status = 6,1,0)) as waitPayOrder,
            sum(if(status = 1,1,0)) as waitOutOrder,
            sum(if(status = 2,1,0)) as outOrder,
            sum(if(status = 3,1,0)) as successOrder,
            sum(if(status = 2 and confirm_status = 0,1,0)) as waitReceiptOrder
        from oms_order where delete_status = 0
    </select>

    <select id="selectOrderCountAndAmount" resultType="com.yonge.cooleshow.admin.dto.OrderStatistical">

        select
        <choose>
            <when test="dateFormat == 'DAY'">
                date_format(create_time,'%Y-%m-%d') as date,
            </when>
            <when test="dateFormat == 'WEEK'">
                YEARWEEK(date_format(create_time,'%Y-%m-%d'),7) as date,
            </when>
            <when test="dateFormat == 'MONTH'">
                date_format(create_time,'%Y-%m')  as date,
            </when>
        </choose>
        count(1) as orderNum,
        sum(pay_amount) as orderAmount
        from oms_order
        where status in (1,2,3) and create_time between #{startTime} and  #{endTime}

        group by

        <choose>
            <when test="dateFormat == 'DAY'">
                date_format(create_time,'%Y-%m-%d')
            </when>
            <when test="dateFormat == 'WEEK'">
                YEARWEEK(date_format(create_time,'%Y-%m-%d'),7)
            </when>
            <when test="dateFormat == 'MONTH'">
                date_format(create_time,'%Y-%m')
            </when>
        </choose>

        order by
        <choose>
            <when test="dateFormat == 'DAY'">
                date_format(create_time,'%Y-%m-%d')
            </when>
            <when test="dateFormat == 'WEEK'">
                YEARWEEK(date_format(create_time,'%Y-%m-%d'),7)
            </when>
            <when test="dateFormat == 'MONTH'">
                date_format(create_time,'%Y-%m')
            </when>
        </choose>
    </select>
</mapper>