<?php

namespace app\admin\controller;

use app\BaseController;
use Carbon\Carbon;
use Exception;
use app\admin\model\Operation as OperationModel;
use app\admin\model\User;
use app\admin\model\Recharge;
use app\admin\model\Withdraw;

class Operation extends BaseController
{
    /**
     * 用户报表
     */
    function exchangeList()
    {
        try {
            $params = $this->request->param();
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;

            $query = new User();
            $admin_id = $this->admin_id;
            // if ($admin_id != 1 && Config::adminOpenAllData($admin_id) === false) {
            //     $user_code = $this->user_code;   
            //     $query->where('user_code', $user_code);
            // }

            if (isset($params['user_id'])) {
                $user_id = $params['user_id'];
                $query->where(function ($query1) use ($user_id) {
                    $query1->where('user_id', $user_id)
                        ->orWhere('realname', 'like', "%{$user_id}%");
                });
            }

            $count = $query->count();
            $list = $query->order("last_login_time",'desc')
                ->field(['id', 'user_id', 'realname', 'money','remark'])
                ->limit($limit)
                ->page($page)
                ->select();
            
            $start = !empty($params['start_time']) ? "{$params['start_time']} 00:00:00" : null;
            $start = $start ? strtotime($start) : null;
            
            $end = !empty($params['end_time']) ? "{$params['end_time']} 23:59:59" : null;
            $end = $end ? strtotime($end) : null;

            foreach ($list as &$item) {
                $item['recharge'] = number_format(Recharge::where('user_id', $item['user_id'])
                    ->where('status', 1)
                    ->where(function ($query1) use ($start, $end) {
                        if ($start && $end) {
                            $query1->where('create_time', '>=', $start)
                                ->where('create_time', '<=', $end);
                        }
                    })
                    ->sum('amount'), 2);
                $item['withdraw'] = number_format(Withdraw::where('user_id', $item['user_id'])
                    ->where('status', 1)
                    ->where(function ($query1) use ($start, $end) {
                        if ($start && $end) {
                            $query1->where('create_time', '>=', $start)
                                ->where('create_time', '<=', $end);
                        }
                    })
                    ->sum('amount'), 2);
                $item['price_count'] = '0.00';
                $item['order_price'] = '0.00';
            }

        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['count' => $count, 'list' => $list]);

    }

    /**
     * 运营数据
     * @apiParam {String{'day','week','month','all'}} type 类型
     * - day 今日
     * - week 本周
     * - month 本月
     * - all 全部
     */
    function index()
    {
        try {
            $params = $this->request->param();
            
            $data = [
                'recharge' => 0,
                'withdraw' => 0,
                'total_price' => 0,
            ];
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;
            $query = new OperationModel;
            $query1 = new OperationModel;
            if (!empty($params['start_time'])) {
                $query = $query->where('date', '>=', $params['start_time'])
                    ->where('date', '<=', $params['end_time']);
                $query1 = $query1->where('date', '>=', $params['start_time'])
                    ->where('date', '<=', $params['end_time']);
            } else if ($params['type'] != 'all') {
                switch ($params['type']) {
                    case 'day':
                        $date = date('Y-m-d');
                        break;
                    case "week":
                        $date = date('Y-m-d', strtotime('this week'));
                        break;
                    case "month":
                        $date = date('Y-m-d', strtotime('this month'));
                        break;
                }
                $query = $query->where('date', '>=', $date);
                $query1 = $query1->where('date', '>=', $date);
            }
            $data['recharge'] = $query1->sum('recharge');
            $data['withdraw'] = $query1->sum('withdraw');
            $data['total_price'] = $query1->sum('total_price');
            $count = $query->count();
            $list = $query->limit($limit)
                ->page($page)
                ->order('date','desc')
                ->select();

        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['count' => $count, 'list' => $list, 'data' => $data]);
    }
}