123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Exception;
- use think\Model;
- use time\DateHelper;
- class Appointment extends Model
- {
- protected $updateTime = false;
- const STATUS_OPEN = 1;
- const STATUS_CLOSE = 0;
- const WEEK_TEXTS = ['', '周一', '周二', '周三', '周四', '周五', '周六', '周日'];
- //appointment_date_text
- public function getAppointmentDateTextAttr($value, $data)
- {
- if ($this->isWeekMode()) {
- $weeks = explode(',', $this->weeks);
- $week_texts = [];
- foreach ($weeks as $week) {
- $week_texts[] = self::WEEK_TEXTS[$week]??'';
- }
- return '每' . implode(',', $week_texts);
- } else {
- return $this->start_time . ' ~ ' . $this->end_time;
- }
- }
- //start_time
- public function getStartTimeAttr($value, $data)
- {
- return $value ? date('Y-m-d', $value) : '';
- }
- //end_time
- public function getEndTimeAttr($value, $data)
- {
- return $value ? date('Y-m-d', $value) : '';
- }
- //是否为工作日排号模式
- public function isWeekMode()
- {
- return $this->weeks ? true : false;
- }
- //生成工作日号源
- public static function createWeekTicketAll($daytime)
- {
- $daytimestamp = strtotime($daytime);
- if (strtotime(date('Y-m-d')) > $daytimestamp) {
- throw new Exception("该时间已过期,请重新选择时间");
- }
- $daytimeWeek = date('w', $daytimestamp);
- $appoints = (new Appointment())->where('weeks', '<>', '')->where('status', self::STATUS_OPEN)->select();
- foreach ($appoints as $appoint) {
- $weeks = explode(',', $appoint->weeks);
- if (in_array($daytimeWeek, $weeks)) {
- //生成号源记录
- if (0 == (new AppointmentTicket())->where(['appointment_id' => $appoint->id, 'appointment_daytime' => $daytimestamp])->count()) {
- (new AppointmentTicket())->allowField(true)->save([
- 'appointment_id' => $appoint->id,
- 'appointment_daytime' => $daytimestamp,
- 'address_id' => $appoint->address_id,
- 'provider_id' => $appoint->provider_id,
- 'status' => AppointmentTicket::STATUS_OPEN
- ]);
- }
- }
- }
- }
- //关联专家
- public function specialist()
- {
- return $this->belongsTo('Specialist', 'provider_id');
- }
- }
|