<?php

namespace addons\webman\model;

use addons\webman\traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\SoftDeletes;
use support\Cache;

/**
 * Class Channel
 * @property int id 主键
 * @property string name 渠道名称
 * @property string domain 渠道域名
 * @property string lang 语言
 * @property string currency 币别代码
 * @property int department_id 所属部门
 * @property int user_id 管理员id
 * @property int site_id 渠道编号
 * @property int status 状态(0:禁用,1:启用)
 * @property string telegram_url telegram地址
 * @property string package_url 安装包地址
 * @property int recharge_status 平台充值(0:禁用,1:启用)
 * @property int withdraw_status 提现(0:禁用,1:启用)
 * @property int web_login_status web登录状态(0:禁用,1:启用)
 * @property int wallet_action_status 钱包操作功能(0:禁用,1:启用)
 * @property float recharge_amount 总充值点数
 * @property float withdraw_amount 总提现点数
 * @property string deleted_at 删除时间
 * @property string created_at 创建时间
 * @property string updated_at 最后一次修改时间
 * @property int promotion_status 推广员功能(0:禁用,1:启用)
 * @property int pay_type 支付方式
 * @property int game_id 游戏id
 * @property int create_id 创建人id
 *
 * @property AdminDepartment department
 * @property AdminUser user
 * @property Player player
 * @property PlayerPlatformCash wallet
 * @package addons\webman\model
 */
class Channel extends Model
{
    use HasDateTimeFormatter, SoftDeletes;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->setTable(plugin()->webman->config('database.channel_table'));
    }

    /**
     * 部门
     * @return BelongsTo
     */
    public function department(): BelongsTo
    {
        return $this->belongsTo(plugin()->webman->config('database.department_model'), 'department_id')->withTrashed();
    }

    /**
     * 管理员用户
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(plugin()->webman->config('database.user_model'), 'user_id')->withTrashed();
    }

    /**
     * 管理员用户
     * @return hasMany
     */
    public function player(): hasMany
    {
        return $this->hasMany(plugin()->webman->config('database.player_model'), 'department_id', 'department_id');
    }

    /**
     * 管理员用户
     * @return hasManyThrough
     */
    public function wallet(): hasManyThrough
    {
        return $this->hasManyThrough(plugin()->webman->config('database.player_platform_cash_model'), plugin()->webman->config('database.player_model'), 'department_id', 'player_id', 'department_id', 'id');
    }

    /**
     * 模型的 "booted" 方法
     *
     * @return void
     */
    protected static function booted()
    {
        static::created(function (Channel $channel) {
            $cacheKey = "channel_" . $channel->site_id;
            Cache::set($cacheKey, $channel->toArray());
            // 创建渠道系统配置
            SystemSetting::insert([
                [
                    'department_id' => $channel->department_id,
                    'feature' => 'marquee',
                    'created_at' => date('Y-m-d H:i:s'),
                ],
                [
                    'department_id' => $channel->department_id,
                    'feature' => 'machine_marquee',
                    'created_at' => date('Y-m-d H:i:s'),
                ]
            ]);
        });
        static::deleted(function (Channel $channel) {
            $cacheKey = "channel_" . $channel->site_id;
            Cache::delete($cacheKey);
        });
        static::updated(function (Channel $channel) {
            $cacheKey = "channel_" . $channel->site_id;
            Cache::set($cacheKey, $channel->toArray());
        });
    }
}
