<?php

    /**
     * 状态码说明
     * 200 成功
     * 201 未登录
     * 202 失败
     * 203 空值
     * 204 无结果
     */

	// 页面编码
	header("Content-type:application/json");

	// 判断登录状态
    session_start();
    if(isset($_SESSION["yinliubao"])){
        
        $loginUser = $_SESSION["yinliubao"];
        
        // 已登录
        // 引入数据库配置
        include '../../../../Db.php';
        $servername = $config['db_host'];
        $username = $config['db_user'];
        $password = $config['db_pass'];
        $dbname = $config['db_name'];
        
        // 连接数据库
        try {
            
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            
            // 设置 PDO 错误模式为异常
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
            // 获取今天的开始和结束时间
            $today_start = date('Y-m-d 00:00:00');
            $today_end = date('Y-m-d 23:59:59');
        
            // 获取昨天的开始和结束时间
            $yesterday_start = date('Y-m-d 00:00:00', strtotime('-1 day'));
            $yesterday_end = date('Y-m-d 23:59:59', strtotime('-1 day'));
        
            // 查询今天的支付订单总金额
            $stmt_today = $conn->prepare("SELECT SUM(order_amount) as total_amount FROM ylb_ChatroomPayOrders WHERE order_time BETWEEN :today_start AND :today_end AND Action_User = '$loginUser'");
            $stmt_today->bindParam(':today_start', $today_start);
            $stmt_today->bindParam(':today_end', $today_end);
            $stmt_today->execute();
            $today_total_amount = $stmt_today->fetch(PDO::FETCH_ASSOC)['total_amount'];
        
            // 查询昨天的支付订单总金额
            $stmt_yesterday = $conn->prepare("SELECT SUM(order_amount) as total_amount FROM ylb_ChatroomPayOrders WHERE order_time BETWEEN :yesterday_start AND :yesterday_end AND Action_User = '$loginUser'");
            $stmt_yesterday->bindParam(':yesterday_start', $yesterday_start);
            $stmt_yesterday->bindParam(':yesterday_end', $yesterday_end);
            $stmt_yesterday->execute();
            $yesterday_total_amount = $stmt_yesterday->fetch(PDO::FETCH_ASSOC)['total_amount'];
        
            // 查询所有时间的支付订单总金额
            $stmt_all_time = $conn->prepare("SELECT SUM(order_amount) as total_amount FROM ylb_ChatroomPayOrders WHERE order_status=1 AND Action_User = '$loginUser'");
            $stmt_all_time->execute();
            $all_time_total_amount = $stmt_all_time->fetch(PDO::FETCH_ASSOC)['total_amount'];
        
            // 格式化金额到两位小数
            $today_total_amount = number_format((float)$today_total_amount, 2, '.', '');
            $yesterday_total_amount = number_format((float)$yesterday_total_amount, 2, '.', '');
            $all_time_total_amount = number_format((float)$all_time_total_amount, 2, '.', '');
            
            // 获取成功
            $result = array(
    		    'code' => 200,
    		    'msg' => '获取成功',
    		    'today_total_amount' => $today_total_amount,
                'yesterday_total_amount' => $yesterday_total_amount,
                'all_time_total_amount' => $all_time_total_amount
		    );
        } catch(PDOException $e) {
            
            // 失败
            $result = array(
    		    'code' => 202,
    		    'msg' => '收款汇总获取失败：' . $e->getMessage()
		    );
        }
        
        // 断开连接
        $conn = null;
    }else {
        
        // 未登录
        $result = array(
			'code' => 201,
            'msg' => '未登录'
		);
    }
    
    // 输出JSON
	echo json_encode($result,JSON_UNESCAPED_UNICODE);

?>
