<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2018/10/17
 * Time: 16:08
 */

namespace App\Modules\Subscribe\Services;

use App\Modules\Subscribe\Models\SubstituteOrder;
use App\Modules\User\Services\UserService;
use DB;
use Redis;

class SubstituteOrderService
{
    public static function createOrder(int $order_id, int $uid, int $pay_uid)
    {
        if (!self::getByOrderId($order_id)) {
            $date = date('Y-m-d');
            return SubstituteOrder::create(compact('order_id', 'uid', 'pay_uid', 'date'));
        }
        return null;
    }

    public static function getByOrderId(int $order_id)
    {
        return SubstituteOrder::where('order_id', $order_id)
            ->select('id', 'order_id', 'uid', 'pay_uid', 'created_at', 'updated_at', 'is_prize', 'prize_amount')
            ->first();
    }

    public static function SubstituteOrderPrize(int $order_id, int $amount)
    {
        $res = self::getByOrderId($order_id);
        if ($res && $res->is_prize == 0) {
            SubstituteOrder::where('id', $res->id)->update([
                'is_prize' => 1,
                'prize_amount' => $amount
            ]);
            UserService::addBalance($res->pay_uid, $amount, 0, $amount);
            return true;
        }
        return false;
    }

    public static function substituteButtonUvPv(int $uid, int $distribution_channel_id)
    {
        $day = date('Y-m-d');
        try {
            Redis::HINCRBY('substitutebuttonpvuv:distribution_channel_id:' . $distribution_channel_id, $day, 1);

            Redis::sadd(sprintf('substitutebuttonpvuv:distribution_channel_id:%s:date:%s', $distribution_channel_id, $day), $uid);
            Redis::sadd('substitutebuttonpvuv' . $day, $distribution_channel_id);
        } catch (\Exception $e) {
        }
    }

    public static function getSubstituteButtonUvPv($distribution_channel_id, $day)
    {
        return Redis::scard(sprintf('substitutebuttonpvuv:distribution_channel_id:%s:date:%s', $distribution_channel_id, $day));
    }

    public static function getSubstitutePageUvPv($distribution_channel_id, $day)
    {
        return Redis::scard(sprintf('substitutepagepvuv:distribution_channel_id:%s:date:%s', $distribution_channel_id, $day));
    }

    public static function substitutePageUvPv(int $uid, int $distribution_channel_id)
    {
        $day = date('Y-m-d');
        try {
            Redis::HINCRBY('substitutepagepvuv:distribution_channel_id:' . $distribution_channel_id, $day, 1);
            Redis::sadd(sprintf('substitutepagepvuv:distribution_channel_id:%s:date:%s', $distribution_channel_id, $day), $uid);
            Redis::sadd('substitutepagepvuv' . $day, $distribution_channel_id);
        } catch (\Exception $e) {
        }
    }

    public static function getSubstituteOrders(int $distribution_channel_id, $start_time = '', $end_time = '')
    {
        $result = SubstituteOrder::join('orders', 'orders.id', '=', 'substitute_orders.order_id')
            ->select('orders.price', 'orders.status', 'orders.pay_end_at')
            ->where('orders.distribution_channel_id', $distribution_channel_id);
        if ($start_time) {
            $result->where('substitute_orders.created_at', '>=', $start_time);
        }

        if ($end_time) {
            $result->where('substitute_orders.created_at', '<', $start_time);
        }
        return $result->orderBy('substitute_orders.id', 'desc')->paginate(20);
    }

    public static function substituteStats($isAll, string $start_date, string $end_date, $distribution_channel_id = '')
    {
        $search_obj = SubstituteOrder::join('orders', 'orders.id', '=', 'substitute_orders.order_id');
        if ($distribution_channel_id) {
            $search_obj->where('orders.distribution_channel_id', $distribution_channel_id);
        };
        $search_obj = $search_obj->whereBetween('substitute_orders.date', [$start_date, $end_date])
            ->select(
                'substitute_orders.date',
                'orders.distribution_channel_id',
                DB::raw('count(*) as num'),//代付订单
                DB::raw('count(distinct case when orders.status="PAID" then substitute_orders.pay_uid else null end) as users_num'),//代付人数
                DB::raw('count(case when orders.status="PAID" then substitute_orders.id else null end) as success_num'),//成功订单数
                DB::raw('sum(case when orders.status="PAID" then orders.price else null end) as amount')//代付金额
            )
            ->groupBy('substitute_orders.date')
            ->groupBy('orders.distribution_channel_id')
            ->orderBy('substitute_orders.date', 'desc');
        if ($isAll) {
            return $search_obj->get();
        } else {
            return $search_obj->paginate();
        }
    }
}