<?php

namespace addons\webman\model;

use addons\webman\traits\DataPermissions;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Class PlayerDeliveryRecord
 * @property int id 主键
 * @property int player_id 玩家id
 * @property string target 质料表
 * @property int target_id 质料id
 * @property int department_id 部门/渠道id
 * @property int user_id 管理员id
 * @property int user_name 管理员名称
 * @property int type 类型
 * @property string source 来源
 * @property float amount 点数
 * @property float amount_before 異動前金額
 * @property float amount_after 異動后金額
 * @property string tradeno 单号
 * @property string remark 备注
 * @property string created_at 创建时间
 * @property string updated_at 最后一次修改时间
 *
 * @property Player player 玩家
 * @package addons\webman\model
 */
class PlayerDeliveryRecord extends Model
{
    use HasFactory, DataPermissions;

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

    const TYPE_MODIFIED_AMOUNT_ADD = 1; // (管理后台)加点
    const TYPE_RECHARGE = 2; // 充值
    const TYPE_WITHDRAWAL = 3; // 提现
    const TYPE_MODIFIED_AMOUNT_DEDUCT = 4; // (管理后台)扣点
    const TYPE_WITHDRAWAL_BACK = 5; // 提现失败返还
    const TYPE_REGISTER_PRESENT = 6; // 注册赠送
    const TYPE_COMMISSION = 7; // 返佣
    const TYPE_SIGN = 8; // 签到
    const TYPE_GAME_OUT = 9; // 游戏转出
    const TYPE_GAME_IN = 10; // 游戏转入
    const TYPE_BET_REBATE = 11; // 打码量返水
    const TYPE_DAMAGE_REBATE = 12; // 客损返水
    const TYPE_RECHARGE_REWARD = 13; // 首充值奖励
    const TYPE_PROFIT = 14; // 推广员分润
    const TYPE_CANCELTRANSFER = 15; // 管理员取消转账

    protected $fillable = [
        'player_id',
        'target',
        'target_id',
        'department_id',
        'type',
        'source',
        'amount',
        'amount_after',
        'amount_before',
        'amount_platform_before',
        'amount_platform_after',
        'tradeno',
        'remark',
        'operator_audit',
        'operator_withdraw',
        'created_at',
        'updated_at',
    ];

    /**
     * 时间转换
     * @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.player_delivery_record_table'));
    }

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

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

    /**
     * 異動前金額
     *
     * @param $value
     * @return float
     */
    public function getAmountBeforeAttribute($value): float
    {
        return floatval($value);
    }

    /**
     * 異動后金額
     *
     * @param $value
     * @return float
     */
    public function getAmountAfterAttribute($value): float
    {
        return floatval($value);
    }
}
