<?php

namespace app\admin\model\school\activity\order;

use app\admin\model\user\withdrawal\UserwithdrawalLog;
use think\Model;
use traits\model\SoftDelete;

class SettleLog extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'school_activity_settle_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'settletime_text',
        'canceltime_text',
        'unfreezetime_text'
    ];
    

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '-1' => __('Status -1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSettletimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['settletime']) ? $data['settletime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCanceltimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['canceltime']) ? $data['canceltime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUnfreezetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['unfreezetime']) ? $data['unfreezetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setSettletimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCanceltimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUnfreezetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function order()
    {
        return $this->belongsTo(Order::class, 'activity_order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function log()
    {
        return $this->belongsTo(UserwithdrawalLog::class, 'withdrawal_log_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function touser()
    {
        return $this->belongsTo('app\common\model\User', 'to_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function payuser()
    {
        return $this->belongsTo('app\common\model\User', 'pay_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
