<?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="cn.iocoder.yudao.module.member.dal.mysql.storeorder.StoreOrderMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="getStoreOrderPageNew"
            resultType="cn.iocoder.yudao.module.member.controller.admin.storeorder.vo.StoreOrderPageVO">
        select so.order_id,so.total_price,so.create_time,mu.nickname,mu.mobile,so.reduction_price,so.carteen_id,so.status
        from member_store_order so
        left join member_user mu on so.user_id = mu.id
        <where>
            so.deleted = false
            <if test="vo.orderId != null">
                and so.order_id = #{vo.orderId}
            </if>
            <if test="vo.nickName != null and vo.nickName != ''">
                and mu.nickname like concat('%',#{vo.nickName},'%')
            </if>
            <if test="vo.mobile != null and vo.mobile != ''">
                and mu.mobile like concat('%',#{vo.mobile},'%')
            </if>
            <if test="vo.startDate != null and vo.startDate != '' and vo.endDate != null and vo.endDate != '' ">
                and date_format(so.create_time,'%Y-%m-%d') between #{vo.startDate} and #{vo.endDate}
            </if>
            <if test="vo.carteenId != null ">
                and so.carteen_id = #{vo.carteenId}
            </if>
            <if test="vo.status != null ">
                and so.status = #{vo.status}
            </if>
        </where>
        order by so.order_id DESC
    </select>

    <select id="getStoreOrderPageNewList"
            resultType="cn.iocoder.yudao.module.member.controller.admin.storeorder.vo.StoreOrderPageVO">
        select so.order_id,so.total_price,so.create_time,mu.nickname,mu.mobile,so.reduction_price,so.carteen_id,so.status
        from member_store_order so
        left join member_user mu on so.user_id = mu.id
        <where>
            so.deleted = false
            <if test="vo.orderId != null">
                and so.order_id = #{vo.orderId}
            </if>
            <if test="vo.nickName != null and vo.nickName != ''">
                and mu.nickname like concat('%',#{vo.nickName},'%')
            </if>
            <if test="vo.mobile != null and vo.mobile != ''">
                and mu.mobile like concat('%',#{vo.mobile},'%')
            </if>
            <if test="vo.startDate != null and vo.startDate != '' and vo.endDate != null and vo.endDate != '' ">
                and date_format(so.create_time,'%Y-%m-%d') between #{vo.startDate} and #{vo.endDate}
            </if>
            <if test="vo.carteenId != null ">
                and so.carteen_id = #{vo.carteenId}
            </if>
        </where>
        order by so.order_id DESC
    </select>

    <update id="updateByIdBillingExist">
        update member_store_order SET billing_exist = #{exist}
        WHERE order_id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateByIdBillingNum">
        update member_store_order SET billing_num = #{num}
        WHERE order_id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="countOrder" resultType="cn.iocoder.yudao.module.member.controller.admin.storeorder.vo.StoreOrderCountVO">
        select *,
               CASE
                   WHEN HOUR (mo.create_time) BETWEEN 0 AND 9 THEN '早上'
                   WHEN HOUR(mo.create_time) BETWEEN 10 AND 15 THEN '中午'
                   WHEN HOUR(mo.create_time) BETWEEN 16 AND 23 THEN '晚上'
                   ELSE '未知'
               END AS timeSlot
        from member_store_order mo
        where DATE_FORMAT(mo.create_time, '%Y%m%d') between #{startTime} and #{endTime}
                and mo.status not in (3, 4);
    </select>
</mapper>