<?php

namespace app\manystore\model\school\classes;

use app\manystore\model\Manystore;
use think\Model;
use traits\model\SoftDelete;

class ClassesLib extends Model
{

    use SoftDelete;

    

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

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

    // 追加属性
    protected $append = [
        'add_type_text',
        'type_text',
        'address_type_text',
        'status_text',
        'auth_status_text',
        'auth_time_text',
        'recommend_text',
        'hot_text',
        'new_text',
        'selfhot_text',
        'classes_cate_title',
        'classes_label_title',
        'has_expire',
    ];

    public function getHasExpireList()
    {
        return ['1' => __('Has_expire 1'), '2' => __('Has_expire 2')];
    }


    public function getHasExpireAttr($value, $data)
    {
        $end_time =  (isset($data['end_time']) ? $data['end_time'] : '');
        if(!$end_time) return '2';

        if( $end_time < time()) {
            return '1';
        }else{
            return '2';
        }

    }

    public function getClassesCateTitleAttr($value, $data)
    {
        $classes_cate_ids =  (isset($data['classes_cate_ids']) ? $data['classes_cate_ids'] : '');
        if(!$classes_cate_ids) return '';
        //$classes_cate_ids 查询分类表 names 已逗号拼接返回
        $classes_cate_title = Cate::where('id','in',$classes_cate_ids)->column('name');
        return implode(',',$classes_cate_title);
    }

    public function getClassesLabelTitleAttr($value, $data)
    {
        $classes_cate_ids =  (isset($data['classes_label_ids']) ? $data['classes_label_ids'] : '');
        if(!$classes_cate_ids) return '';
        //$classes_cate_ids 查询分类表 names 已逗号拼接返回
        $classes_cate_title = Label::where('id','in',$classes_cate_ids)->column('name');
        return implode(',',$classes_cate_title);
    }

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
//            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => 0]);
        });
    }


    public function getClassesTypeList(){
         return \app\admin\model\school\classes\Type::column("name", 'id');
    }


    
    public function getAddTypeList()
    {
        return ['1' => __('Add_type 1'), '2' => __('Add_type 2')];
    }

    public function getTypeList()
    {
        return ['out' => __('Type out'), 'in' => __('Type in')];
    }

    public function getAddressTypeList()
    {
        return ['1' => __('Address_type 1'), '2' => __('Address_type 2')];
    }

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

    public function getAuthStatusList()
    {
        return ['0' => __('Auth_status 0'), '1' => __('Auth_status 1'), '2' => __('Auth_status 2')];
    }

    public function getRecommendList()
    {
        return ['0' => __('Recommend 0'), '1' => __('Recommend 1')];
    }

    public function getHotList()
    {
        return ['0' => __('Hot 0'), '1' => __('Hot 1')];
    }

    public function getNewList()
    {
        return ['0' => __('New 0'), '1' => __('New 1')];
    }

    public function getSelfhotList()
    {
        return ['0' => __('Selfhot 0'), '1' => __('Selfhot 1')];
    }


    public function getAddTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['add_type']) ? $data['add_type'] : '');
        $list = $this->getAddTypeList();
        return isset($list[$value]) ? $list[$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 getAddressTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['address_type']) ? $data['address_type'] : '');
        $list = $this->getAddressTypeList();
        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 getAuthStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['auth_status']) ? $data['auth_status'] : '');
        $list = $this->getAuthStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


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


    public function getRecommendTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['recommend']) ? $data['recommend'] : '');
        $list = $this->getRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['hot']) ? $data['hot'] : '');
        $list = $this->getHotList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getNewTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['new']) ? $data['new'] : '');
        $list = $this->getNewList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSelfhotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['selfhot']) ? $data['selfhot'] : '');
        $list = $this->getSelfhotList();
        return isset($list[$value]) ? $list[$value] : '';
    }

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


    public function manystore()
    {
        return $this->belongsTo(Manystore::class, 'manystore_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function manystoreshop()
    {
        return $this->belongsTo('app\manystore\model\ManystoreShop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


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


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