<?php

namespace app\admin\model;

use think\Model;
use think\model\relation\HasMany;

/**
 * Bank
 */
class Bank extends Model
{
    // 表名
    protected $name = 'bank';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    protected static function onAfterInsert($model): void
    {
        if (is_null($model->weigh)) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }

    public function getTxInAttr($value): ?float
    {
        return is_null($value) ? null : (float)$value;
    }

    public function getTxOutAttr($value): ?float
    {
        return is_null($value) ? null : (float)$value;
    }

    public function getFundInAttr($value): ?float
    {
        return is_null($value) ? null : (float)$value;
    }

    public function getFundOutAttr($value): ?float
    {
        return is_null($value) ? null : (float)$value;
    }

    public function MoneyLog(): hasMany
    {
        return $this->hasMany(UserMoneyLog::class, 'bank_id');
    }
}