<?php

namespace addons\webman\model;

use addons\webman\traits\DataPermissions;
use addons\webman\traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Webman\Event\Event;

/**
 * Class PlayerRechargeRecord
 * @property int id 主键
 * @property int player_id 玩家id
 * @property int department_id 部门/渠道id
 * @property int setting_id 充值账号配置id
 * @property string tradeno 单号
 * @property string external_reference 商户订单号
 * @property int status 状态
 * @property int type 类型
 * @property string payment_method 支付方式
 * @property string player_name 玩家名称
 * @property float money 金额
 * @property float inmoney 实际金额
 * @property float $coins 充值点数
 * @property float gift_coins 赠送coins
 * @property float $chip_amount 打码量
 * @property string currency 币种
 * @property string player_tag 忘记标注
 * @property string remark 备注
 * @property string reject_reason 拒绝原因
 * @property int user_id 管理员id
 * @property string user_name 管理员
 * @property string notify_result 回调数据
 * @property string certificate 付款凭证
 * @property string account 银行账户
 * @property string bank_name 银行
 * @property string sub_bank 支行
 * @property string owner 户名
 * @property float rate 汇率
 * @property string finish_time 完成时间
 * @property string cancel_time 取消时间
 * @property string created_at 创建时间
 * @property string updated_at 最后一次修改时间
 *
 * @property Player player 玩家
 * @property Channel channel 渠道
 * @property ChannelRechargeMethod channel_recharge_setting 充值账户
 * @package addons\webman\model
 */
class PlayerRechargeRecord extends Model
{
    use HasDateTimeFormatter, DataPermissions;

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

    const STATUS_WAIT = 0; // 充值中
    const STATUS_RECHARGING = 1; // 待审核
    const STATUS_RECHARGED_SUCCESS = 2; // 充值成功(管理员通过)
    const STATUS_RECHARGED_FAIL = 3; // 充值失败
    const STATUS_RECHARGED_CANCEL = 4; // 充值取消(玩家取消)
    const STATUS_RECHARGED_REJECT = 5; // 拒绝(管理员拒绝)
    const STATUS_RECHARGED_SYSTEM_CANCEL = 6; // 已关闭(系统取消)

    const TYPE_REGULAR = 1; // 普通充值
    const TYPE_ACTIVITY = 2; // 活动充值
    const TYPE_ARTIFICIAL = 4; // 人工充值

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

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

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

    /**
     * 充值配置信息
     * @return BelongsTo
     */
    public function channel_recharge_setting(): BelongsTo
    {
        return $this->belongsTo(plugin()->webman->config('database.channel_recharge_setting_model'), 'setting_id')->withTrashed();
    }

    /**
     * 获取器 - 标签id
     * @param $value
     * @return false|string[]
     */
    public function getPlayerTagAttribute($value)
    {
        return array_filter(explode(',', $value));
    }

    /**
     * 修改器 - 标签id
     * @param $value
     * @return string
     */
    public function setPlayerTagAttribute($value): string
    {
        return $this->attributes['player_tag'] = implode(',', $value);
    }

    /**
     * 金额
     *
     * @param $value
     * @return float
     */
    public function getMoneyAttribute($value): float
    {
        return floatval($value);
    }

    /**
     * 时间金额
     *
     * @param $value
     * @return float
     */
    public function getInmoneyAttribute($value): float
    {
        return floatval($value);
    }

    /**
     * 游戏点数
     *
     * @param $value
     * @return float
     */
    public function getCoinsAttribute($value): float
    {
        return floatval($value);
    }

    /**
     * 模型的 "booted" 方法
     *
     * @return void
     */
    protected static function booted()
    {
        if (config('app.profit', 'task') == 'event') {
            static::updated(function (PlayerRechargeRecord $playerRechargeRecord) {
                $oldStatus = $playerRechargeRecord->getOriginal('status'); // 原始值
                $newStatus = $playerRechargeRecord->status;
                // 游戏结束并且产生盈亏后计算分润
                if ($oldStatus != $newStatus && $newStatus == PlayerRechargeRecord::STATUS_RECHARGED_SUCCESS) {
                    Event::emit('promotion.playerRecharge', $playerRechargeRecord);
                }
            });
        }
    }
}
