<?php

namespace App\Services;

use App\Services\BaseService;
use App\Models\BalanceLog;

// 余额额变动记录
class BalanceLogService extends BaseService
{
    /**
     * @description: 模型
     * @return {string}
     */
    public static function model(): string
    {
        return BalanceLog::class;
    }

    /**
     * @description: 枚举
     * @return {*}
     */
    public static function enum(): string
    {
        return '';
    }

    /**
     * @description: 获取查询条件
     * @param {array} $search 查询内容
     * @return {array}
     */
    public static function getWhere(array $search = []): array
    {
        $where = [];
        if (isset($search['coin']) && !empty($search['coin'])) {
            $where[] = ['coin', '=', $search['coin']];
        }
        if (isset($search['net']) && !empty($search['net'])) {
            $where[] = ['net', '=', $search['net']];
        }
        if (isset($search['address']) && !empty($search['address'])) {
            $where[] = ['address', '=', $search['address']];
        }
        if (isset($search['id']) && !empty($search['id'])) {
            $where[] = ['id', '=', $search['id']];
        }
        if (isset($search['member_id']) && !empty($search['member_id'])) {
            $where[] = ['member_id', '=', $search['member_id']];
        }
        return $where;
    }

    /**
     * @description: 查询单条数据
     * @param array $search
     * @return \App\Models\Coin|null
     */
    public static function findOne(array $search): ?BalanceLog
    {
        return self::model()::where(self::getWhere($search))->first();
    }

    /**
     * @description: 查询所有数据
     * @param array $search
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public static function findAll(array $search = [])
    {
        return self::model()::where(self::getWhere($search))->get();
    }

    /**
     * @description: 分页查询
     * @param array $search
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public static function paginate(array $search = [])
    {
        $limit = isset($search['limit']) ? $search['limit'] : 15;
        $paginator = self::model()::where(self::getWhere($search))
            ->orderBy('updated_at', 'desc')
            ->paginate($limit);
        return ['total' => $paginator->total(), 'data' => $paginator->items()];
    }

    /**
     * @description: 生成资金变动日志
     * @param {*} $memberId
     * @param {*} $amount
     * @param {*} $before_balance
     * @param {*} $after_balance
     * @param {*} $change_type
     * @param {*} $related_id
     * @param {*} $remark
     * @return {*}
     */
    public static function addLog($memberId, $amount, $before_balance, $after_balance, $change_type, $related_id, $remark, $room_id = null)
    {
        $data = [];
        $data['member_id'] = $memberId;
        $data['amount'] = $amount;
        $data['before_balance'] = $before_balance;
        $data['after_balance'] = $after_balance;
        $data['change_type'] = $change_type;
        $data['related_id'] = $related_id;
        $data['remark'] = $remark;
        if ($room_id) $data['room_id'] = $room_id;
        return self::model()::create($data);
    }

    /**
     * @description: 获取今日流水
     * @param int $memberId
     * @param string|null $date
     * @return {*}
     */
    public static function getTodayFlowing($memberId ,$date = null)
    {
        if (!$date) {
            $date = date('Y-m-d');
        }
        $startTime = date('Y-m-d H:i:s',strtotime($date . ' 00:00:00'));
        $endTime = date('Y-m-d H:i:s',strtotime($date . ' 23:59:59'));
        
        
        $flow = self::model()::where('member_id', $memberId)
            ->whereBetween('created_at', [$startTime, $endTime])
            ->where('change_type', '投注')
            ->sum('amount');
        $refund = self::model()::where('member_id', $memberId)
            ->whereBetween('created_at', [$startTime, $endTime])
            ->where('change_type', '返水')
            ->sum('amount');

        $profit = self::model()::where('member_id', $memberId)
            ->whereBetween('created_at', [$startTime, $endTime])
            ->where('change_type', '开奖') //嬴正数 输负数
            ->sum('amount');

        $walletInfo = WalletService::model()::where('member_id', $memberId)->first();
        $text = '';
        $text .= "当日流水: " . $flow . "\n";
        $text .= "当日返水: " . $refund . "\n";
        $text .= "当日盈利: " . ($profit + $refund + $flow)  . "\n";
        $text .= "余额: " . $walletInfo['available_balance'] . "\n";
        return [
            'chat_id' => $memberId,
            'text' => $text
        ];
    }

}
