Appointment.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Model;
  5. use time\DateHelper;
  6. class Appointment extends Model
  7. {
  8. protected $updateTime = false;
  9. public function getStartTimeAttr($value, $data)
  10. {
  11. return $value ? date('Y-m-d', $value) : '';
  12. }
  13. public function getEndTimeAttr($value, $data)
  14. {
  15. return $value ? date('Y-m-d', $value) : '';
  16. }
  17. //生成号源配置
  18. public function createTicket()
  19. {
  20. if (!$this->ifExitsTicket()) {
  21. $daylist = DateHelper::getDateList($this->start_time, $this->end_time);
  22. foreach ($daylist as $datetime) {
  23. (new AppointmentTicket())->save([
  24. 'appointment_id' => $this->id,
  25. 'appointment_daytime' => strtotime($datetime),
  26. 'address_id' => $this->specialist->address_id
  27. ]);
  28. }
  29. $this->save(['ticket_status' => 1]);
  30. }
  31. }
  32. //是否已生成号源配置
  33. public function ifExitsTicket()
  34. {
  35. return $this->ticket_status > 0;
  36. }
  37. //是否已存在预约订单
  38. public function ifExitsAppointmentApplication()
  39. {
  40. }
  41. /**
  42. * 按地点获取预约列表
  43. * @return false|\PDOStatement|string|\think\Collection
  44. */
  45. public static function listByAddress()
  46. {
  47. //未结束的预约都保证生成了号源
  48. $all = (new Appointment())->where('end_time', '>', time())->select();
  49. /** @var $item Appointment */
  50. foreach ($all as $item) {
  51. $item->createTicket();
  52. }
  53. $addressIds = (new AppointmentTicket())->where('appointment_daytime', '>=', strtotime(date('Y-m-d')))->distinct('true')->column('address_id');
  54. return (new Address())->where('id', 'in', $addressIds)->select();
  55. }
  56. //关联专家
  57. public function specialist()
  58. {
  59. return $this->belongsTo('Specialist', 'provider_id');
  60. }
  61. }