<?php

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

use app\admin\model\school\activity\Activity;
use think\Model;
use traits\model\SoftDelete;

class OrderDetail extends Model
{

    use SoftDelete;

    

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

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

    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text',
        'sign_start_time_text',
        'sign_end_time_text',
        'recommend_text',
        'hot_text',
        'new_text',
        'add_type_text',
        'feel_text'
    ];
    

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

    
    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 getAddTypeList()
    {
        return ['1' => __('Add_type 1'), '2' => __('Add_type 2')];
    }

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


    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 getSignStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_start_time']) ? $data['sign_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSignEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_end_time']) ? $data['sign_end_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 getAddTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['add_type']) ? $data['add_type'] : '');
        $list = $this->getAddTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFeelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['feel']) ? $data['feel'] : '');
        $list = $this->getFeelList();
        return isset($list[$value]) ? $list[$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 setSignStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

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


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


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


    public function activity()
    {
        return $this->belongsTo(Activity::class, 'activity_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
