<?php

namespace app\common\model;

use think\Model;

class AppointmentTicket extends Model
{
    protected $autoWriteTimestamp = false;

    const STATUS_OPEN = 1;
    const STATUS_CLOSE = 0;


    //appointment_daytime
    public function getAppointmentDaytimeAttr($value, $data)
    {
        return $value ? date('Y-m-d', $value) : '';
    }

    //morning_use
    public function getMorningUseAttr($value, $data)
    {
        return (new AppointmentApplication())->where([
            'appointment_period' => AppointmentApplication::APPOINTMENT_PERIOD_MORNING,
            'appointment_ticket_id' => $this->id,
            'status' => ['<>', AppointmentApplication::STATUS_CANCEL]
        ])->count();
    }

    //afternoon_use
    public function getAfternoonUseAttr($value, $data)
    {
        return (new AppointmentApplication())->where([
            'appointment_period' => AppointmentApplication::APPOINTMENT_PERIOD_AFTERNOON,
            'appointment_ticket_id' => $this->id,
            'status' => ['<>', AppointmentApplication::STATUS_CANCEL]
        ])->count();
    }

    //night_use
    public function getNightUseAttr($value, $data)
    {
        return (new AppointmentApplication())->where([
            'appointment_period' => AppointmentApplication::APPOINTMENT_PERIOD_NIGHT,
            'appointment_ticket_id' => $this->id,
            'status' => ['<>', AppointmentApplication::STATUS_CANCEL]
        ])->count();
    }


    //关联专家
    public function specialist()
    {
        return $this->belongsTo('Specialist', 'provider_id');
    }

    //关联放号配置
    public function appointment()
    {
        return $this->belongsTo('Appointment');
    }

    //关联地址
    public function address()
    {
        return $this->belongsTo('Address');
    }
}
