Goods.php 4.6 KB
<?php

namespace app\admin\model;

use think\Model;


class Goods extends Model
{

    

    

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

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

    // 追加属性
    protected $append = [
        'type_text',
//        'view_time_text',
        'start_time_text',
        'end_time_text',
        'sex_text',
        'status_text',
        'user_is_up_text',
        'admin_is_up_text',
        'user_deletetime_text',
        'admin_deletetime_text'
    ];



    public function getTypeList()
    {
        return ['3' => __('图文'), '2' => __('音频'), '1' => __('视频')];
    }

    public function getSexList()
    {
        return ['2' => __('女'), '1' => __('男'), '0' => __('不限')];
    }

    public function getStatusList()
    {
        return ['3' => __('审核失败'), '2' => __('审核通过'), '1' => __('审核中')];
    }

    public function getUserIsUpList()
    {
        return ['1' => __('上架'), '0' => __('下架')];
    }

    public function getAdminIsUpList()
    {
        return ['1' => __('上架'), '0' => __('下架')];
    }


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


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


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


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


    public function getSexTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
        $list = $this->getSexList();
        return isset($list[$value]) ? $list[$value] : '';
    }


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


    public function getUserIsUpTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user_is_up']) ? $data['user_is_up'] : '');
        $list = $this->getUserIsUpList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAdminIsUpTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['admin_is_up']) ? $data['admin_is_up'] : '');
        $list = $this->getAdminIsUpList();
        return isset($list[$value]) ? $list[$value] : '';
    }


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


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

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

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

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

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

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


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}