<?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.orderspacecapsule.OrderSpaceCapsuleMapper">

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


    <select id="compDay"
            resultType="cn.iocoder.yudao.module.member.controller.admin.orderspacecapsule.vo.OrderSpaceCapsuleStatisticsVo">
        SELECT
            SUM(money) AS totalMoney,
            COUNT(id) AS totalOrder,
            SUM(combo_num * combo_minutes) AS totalMinute,
            SUM(refund_money) AS refundMoney,
            SUM(combo_num) AS totalPackageSum,
            carteen_id AS carteenId,
            carteen_name AS carteenName,
            COUNT(DISTINCT user_id) AS totalCustomer,
            ROUND(SUM(money) / COUNT(id)) AS avgOrderMoney
        FROM
            t_order_space_capsule
        WHERE
            `status` = #{status}
          AND DATE(create_time) BETWEEN #{start} AND #{end} and carteen_id=#{carteenId}
        GROUP BY
            carteen_id, carteen_name;
    </select>

    <select id="compMoney"
            resultType="cn.iocoder.yudao.module.member.controller.admin.orderspacecapsule.vo.OrderSpaceCapsuleStatisticsComVo">
        SELECT
            SUM( total_money ) AS totalMoney
        FROM
            t_statistics_space_capsule_order
        where create_time between #{start} and #{end}

    </select>

    <select id="compDay1"
            resultType="cn.iocoder.yudao.module.member.controller.admin.statisticsspacecapsuleorder.vo.SpaceCapsuleOrderNameRespVO">
        SELECT
            IFNULL(SUM(a.money), 0) AS totalMoney,
            IFNULL(COUNT(a.id), 0) AS totalOrder,
            IFNULL(SUM(a.combo_num * a.combo_minutes), 0) AS totalMinute,
            IFNULL(SUM(a.refund_money), 0) AS refundMoney,
            IFNULL(SUM(a.combo_num), 0) AS totalPackageSum,
            IFNULL(a.carteen_id, 0) AS carteenId,
            IFNULL(a.carteen_name, 'Unknown') AS carteenName,
            IFNULL(COUNT(DISTINCT a.user_id), 0) AS totalCustomer,
            IFNULL(ROUND(SUM(a.money) / NULLIF(COUNT(a.id), 0)), 0) AS avgOrderMoney,
            a.space_capsule_id AS spId,
            b.space_name AS spName
        FROM
            t_order_space_capsule AS a
                LEFT JOIN t_space_capsule AS b ON a.space_capsule_id = b.id
        WHERE
            a.`status` = = #{status}
          AND DATE(a.create_time) BETWEEN #{start} AND #{end} and a.space_capsule_id=#{spId}
        GROUP BY
            a.carteen_id, a.carteen_name, a.space_capsule_id, b.space_name;
    </select>

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