<?php

namespace addons\webman\model;

use addons\webman\traits\DataPermissions;
use addons\webman\traits\HasDateTimeFormatter;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Notice
 * @property int id 主键
 * @property int department_id 渠道id
 * @property int player_id 玩家id
 * @property int source_id 来源id
 * @property int type 类型
 * @property string title 标题
 * @property string content 内容
 * @property int status 状态
 * @property int receiver 接受方，1=玩家, 2=总后台, 2=子站
 * @property int is_private 是否私人消息
 * @property int admin_id 管理员id
 * @property string admin_name 管理员名称
 * @property string created_at 创建时间
 * @property string updated_at 最后一次修改时间
 * @property string deleted_at 删除时间
 *
 * @property AdminUser adminUser 管理员
 * @property Channel channel 渠道
 * @property Player player 玩家
 * @package addons\webman\model
 */
class Notice extends Model
{
    use SoftDeletes, HasDateTimeFormatter, DataPermissions;

    //数据权限字段
    protected $dataAuth = ['department_id' => 'department_id'];

    const TYPE_SYSTEM = 1; // 系统
    const TYPE_EXAMINE_RECHARGE = 2; // 充值审核
    const TYPE_EXAMINE_WITHDRAW = 3; // 提现审核
    const TYPE_PAY = 4; // 三方充值
    const TYPE_WITHDRAW = 5; // 三方提现

    const RECEIVER_PLAYER = 1; // 玩家
    const RECEIVER_ADMIN = 2; // 总站
    const RECEIVER_DEPARTMENT = 3; // 子站

    /**
     * 时间转换
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setTable(plugin()->webman->config('database.notice_table'));
    }

    /**
     * 游戏类别
     * @return BelongsTo
     */
    public function adminUser(): BelongsTo
    {
        return $this->belongsTo(plugin()->webman->config('database.user_model'), 'admin_id');
    }

    /**
     * 渠道信息
     * @return BelongsTo
     */
    public function channel(): BelongsTo
    {
        return $this->belongsTo(plugin()->webman->config('database.channel_model'), 'department_id', 'department_id')->withTrashed();
    }

    /**
     * 玩家信息
     * @return BelongsTo
     */
    public function player(): BelongsTo
    {
        return $this->belongsTo(plugin()->webman->config('database.player_model'), 'player_id', 'department_id')->withTrashed();
    }

    /**
     * 模型的 "booted" 方法
     *
     * @return void
     */
    protected static function booted()
    {
        static::created(function (Notice $notice) {
            if ($notice->is_private == 1) {
                sendSocketMessage('player-' . $notice->player_id, [
                    'msg_type' => 'player_notice_num',
                    'notice_num' => Notice::query()
                        ->where('player_id', $notice->player_id)
                        ->where('receiver', Notice::RECEIVER_PLAYER)
                        ->where('is_private', 1)
                        ->where('status', 0)
                        ->count('*'),
                ]);
            }
        });
    }
}
