<?php

namespace App\Services\Order;

use App\Consts\BaseConst;
use App\Consts\ErrorConst;
use App\Dao\Order\OrderDao;
use App\Facade\Site;
use App\Models\Order\Order;
use App\Libs\Utils;
use App\Services\OpenApi\OpenService;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Response;
use Vinkla\Hashids\Facades\Hashids;
use App\Exceptions\ApiException;

class OrderService
{
    protected $orderDao;
    protected $openService;

    public function __construct(
        OrderDao    $orderDao,
        OpenService $openService
    )
    {
        $this->orderDao    = $orderDao;
        $this->openService = $openService;
    }

    /**
     * @param $data
     * @return mixed
     * @throws ApiException
     */
    public function channelList($data)
    {
        $channel_id = Site::getChannelId();
        $result = DB::table('distribution_channels as dc')->leftJoin('channel_users as cu', 'cu.id', 'dc.channel_user_id')->whereIn('dc.id', $channel_id)
            ->select('dc.id as channel_id', 'dc.channel_type', 'cu.account', 'cu.nickname as account_nickname')
            ->orderBy('dc.id')->get()->map(function ($value) {
                return (array)$value;
            })->toArray();
        foreach ($result as &$item) {
            $item['channel_type'] = $item['channel_type'] == 'PERIOD' ? '会员制' : '充送制';
        }

        return $result;
    }

    /**
     * @param $data
     * @return mixed
     */
    public function userList($data) {
        $channel_id = Site::getChannelId();
        $uid = getProp($data, 'uid');
        $param_channel_id   = getProp($data, 'channel_id');
        $send_order_id      = getProp($data, 'send_order_id');
        $date_range         = getProp($data, 'register_time');

        $query = DB::table('users as u')
            ->whereIn('u.distribution_channel_id', $channel_id)->where('u.is_lock', 0)
            ->select('u.*', DB::raw("(select created_at from channel_copy_user_mappings where uid = u.id order by created_at desc limit 1) as bind_send_order_time"));

        //用户id筛选
        if ($uid) {
            $query->where('u.id', $uid);
        }

        //站点id筛选
        if ($param_channel_id) {
            $query->where('u.distribution_channel_id', $param_channel_id);
        }

        //派单id筛选
        if ($send_order_id) {
            $query->where('u.send_order_id', $send_order_id);
        }

        //时间周期筛选
        if ($date_range) {
            $date_range = explode(',', $date_range);
            if (count($date_range) == 2) {
                if ($date_range[0]) {
                    $start = date('Y-m-d 00:00:00', strtotime($date_range[0]));
                    $query->where('u.created_at', '>=', $start);
                }

                if ($date_range[1]) {
                    $end = date('Y-m-d 23:59:59', strtotime($date_range[1]));
                    $query->where('u.created_at', '<=', $end);
                }
            }
        }

        return $query->paginate(1000);
    }

    /**
     * @param $data
     * @return mixed
     */
    public function sendOrderList($data) {
        $channel_id = Site::getChannelId();
        $param_channel_id = getProp($data, 'channel_id');
        $send_order_id = getProp($data, 'send_order_id');

        $query = DB::table('send_orders')->whereIn('distribution_channel_id', $channel_id)->where('is_enable', 1);

        //站点筛选
        if ($param_channel_id) {
            $query->where('distribution_channel_id', $param_channel_id);
        }

        //派单id筛选
        if ($send_order_id) {
            $query->where('id', $send_order_id);
        }

        return $query->paginate(1000);
    }

    // 订单列表
    public function orderList($data)
    {
        $channel_id              = Site::getChannelId();                    // 绑定的权限站点id集
        $uid                     = getProp($data, 'uid');                   // 用户id
        $send_order_id           = getProp($data, 'send_order_id');         // 派单id
        $promotionid             = getProp($data, 'promotion_id');          // 广告id
        $param_channel_id        = getProp($data, 'channel_id');            // 站点id
        $register_date_range     = getProp($data, 'register_time');         // 注册时间
        $pay_date_range          = getProp($data, 'pay_time');              // 支付时间

        // 列表
        $query = DB::table('orders')->leftJoin('users', 'users.id', 'orders.uid')
            ->leftJoin('dy_report_logs', 'dy_report_logs.trade_no', 'orders.trade_no')
            ->leftJoin('send_orders', 'send_orders.id', 'orders.send_order_id')
            ->leftJoin('channel_copy_user_mappings', 'channel_copy_user_mappings.trade_no', 'orders.trade_no')
            ->leftJoin('book_configs', 'book_configs.bid', 'orders.from_bid')
            ->whereIn('send_orders.distribution_channel_id', $channel_id)
            ->where('orders.status', '<>', 'UNPAID')
            ->select('orders.id', 'orders.trade_no', 'orders.uid', 'users.created_at as register_time', 'channel_copy_user_mappings.created_at as bind_send_order_time',
                'orders.price', 'orders.status', 'orders.created_at', 'orders.pay_end_at as pay_time', 'send_orders.distribution_channel_id', 'dy_report_logs.id as report_id',
                'orders.send_order_id', 'dy_report_logs.advertiserid', 'dy_report_logs.promotionid', 'dy_report_logs.callback_result', 'orders.pay_num',
                'dy_report_logs.remark', 'dy_report_logs.updated_at as report_time', 'book_configs.book_name', 'orders.order_type', 'orders.transaction_id');

        if ($uid) {
            $query->where('orders.uid', $uid);
        }

        if ($send_order_id) {
            $query->where('orders.send_order_id', $send_order_id);
        }

        if ($promotionid) {
            $query->where('dy_report_logs.promotionid', $promotionid);
        }

        if ($param_channel_id) {
            $query->where('send_orders.distribution_channel_id', $param_channel_id);
        }

        // 注册时间周期筛选
        if ($register_date_range) {
            $date_range = explode(',', $register_date_range);
            if (count($date_range) == 2) {
                if ($date_range[0]) {
                    $start = date('Y-m-d 00:00:00', strtotime($date_range[0]));
                    $query->where('users.created_at', '>=', $start);
                }

                if ($date_range[1]) {
                    $end = date('Y-m-d 23:59:59', strtotime($date_range[1]));
                    $query->where('users.created_at', '<=', $end);
                }
            }
        }

        // 支付时间周期筛选
        if ($pay_date_range) {
            $date_range = explode(',', $pay_date_range);
            if (count($date_range) == 2) {
                if ($date_range[0]) {
                    $start = date('Y-m-d 00:00:00', strtotime($date_range[0]));
                    $query->where('orders.pay_end_at', '>=', $start);
                }

                if ($date_range[1]) {
                    $end = date('Y-m-d 23:59:59', strtotime($date_range[1]));
                    $query->where('orders.pay_end_at', '<=', $end);
                }
            }
        }

        // 获取分页数据
        return $query->orderBy('orders.pay_end_at', 'desc')->orderBy('users.created_at', 'desc')->paginate();
    }
}
