<?php

namespace app\admin\model\order;

use think\Model;


class Order extends Model
{





    // 表名
    protected $name = 'order';

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

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

    // 追加属性
    protected $append = [
        'status_text',
        'is_deleted_text',
        'paytime_text',
        'type_text',
        'pay_type_text'
    ];



    public function getStatusList()
    {
        return ['-1' => __('已取消'), '0' => __('未支付'), '1' => __('已完成')];
    }

    public function getIsDeletedList()
    {
        return ['0' => __('是'), '1' => __('否')];
    }

    public function getTypeList()
    {
        return ['0' => __('书籍'), '1' => __('视频')];
    }

    public function getPayTypeList()
    {
        return ['0' => __('微信'), '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 getIsDeletedTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_deleted']) ? $data['is_deleted'] : '');
        $list = $this->getIsDeletedList();
        return isset($list[$value]) ? $list[$value] : '';
    }


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


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

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

    public function goods(){
        return $this->hasMany(OrderGoods::class,'order_id','id');
    }


}
