<?php

namespace App\Dao\Order;

use App\Models\Order\Order;

class OrderDao
{
    /**
     * @param $param
     * @return mixed
     */
    public function orderList($param)
    {
        // 查询字段
        $fields = [
            'orders.id', 'orders.distribution_channel_id', 'orders.uid', 'orders.price', 'orders.status', 'orders.trade_no',
            'orders.from_bid', 'orders.order_type', 'orders.send_order_id', 'send_orders.name as send_order_name',
            'books.name as book_name', 'orders.created_at', 'orders.updated_at', 'users.created_at as register_time',
            'users.register_ip', 'channel_users.nickname', 'channel_copy_user_mappings.created_at as bind_send_order_time'
        ];
        $query  = Order::select($fields)
                       ->leftJoin('users', 'users.id', 'orders.uid')
                       ->leftJoin('books', 'books.id', 'orders.from_bid')
                       ->leftJoin('send_orders', 'send_orders.id', 'orders.send_order_id')
                       ->leftJoin('distribution_channels', 'distribution_channels.id', 'orders.distribution_channel_id')
                       ->leftJoin('channel_users', 'channel_users.id', 'distribution_channels.channel_user_id')
                       ->leftJoin('channel_copy_user_mappings', 'channel_copy_user_mappings.trade_no', 'orders.trade_no');

        // 站点
        $channelId = getProp($param, 'channel_id');
        if ($channelId) {
            $query->whereIn('orders.distribution_channel_id', $channelId);
        }

        // 订单编号
        $tradeNo = trim(getProp($param, 'trade_no'));
        if ($tradeNo) {
            $query->where('orders.trade_no', $tradeNo);
        }

        // 用户uid
        $uid = (int)getProp($param, 'uid');
        if ($uid) {
            $query->where('orders.uid', $uid);
        }

        // 派单id
        $sendOrderId = (int)getProp($param, 'send_order_id');
        if ($sendOrderId) {
            $query->where('orders.send_order_id', $sendOrderId);
        }

        // 充值类型
        $orderType = trim(getProp($param, 'order_type'));
        if (in_array($orderType, ['RECHARGE', 'YEAR', 'QUARTER', 'MONTH', 'WEEK'])) {
            $query->where('orders.order_type', $orderType);
        }

        // 提现状态,状态 PAID：已支付 UNPAID未支付; REFUND 退款;  FAIL失败
        $status = trim(getProp($param, 'status'));
        if ($status) {
            $query->where('orders.status', $status);
        }

        // 订单创建时间
        $dateRange = getProp($param, 'date_range');
        if ($dateRange) {
            [$startDate, $endDate] = explode(',', $dateRange);
            if ($startDate && $endDate && $startDate <= $endDate) {
                $query->where('orders.created_at', '>=', date('Y-m-d 00:00:00', strtotime($startDate)));
                $query->where('orders.created_at', '<=', date('Y-m-d 23:59:59', strtotime($endDate)));
            }
        }

        // 导出用
        $all = (int)getProp($param, 'all');
        if ($all) {
            return $query->orderBy('orders.created_at', 'desc')->get();
        }

        return $query->orderBy('orders.created_at', 'desc')->paginate(10);
    }

    /**
     * 站点充值总额
     *
     * @param $channelId
     * @return mixed
     */
    public function getChannelRechargeAmount($channelId)
    {
        return Order::where('distribution_channel_id', $channelId)
                    ->where('status', 'PAID')
                    ->sum('price');
    }

    /**
     * 站点充值总额（不含今日）
     *
     * @param $channelId
     * @return mixed
     */
    public function getChannelRechargeAmountWithoutToday($channelId)
    {
        return Order::where('distribution_channel_id', $channelId)
                    ->where('status', 'PAID')
                    ->where('created_at', '<', date('Y-m-d 00:00:00'))
                    ->sum('price');
    }

    /**
     * 站点月充值总额（不含今日）
     *
     * @param $channelId
     * @return mixed
     */
    public function getChannelMonthRechargeAmountWithoutToday($channelId)
    {
        return Order::where('distribution_channel_id', $channelId)
                    ->where('status', 'PAID')
                    ->where('created_at', '>=', date('Y-m-01 00:00:00'))
                    ->where('created_at', '<', date('Y-m-d 00:00:00'))
                    ->sum('price');
    }
}
