Appointment.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, true);
  22. foreach ($daylist as $datetime) {
  23. (new AppointmentTicket())->save([
  24. 'appointment_id' => $this->id,
  25. 'appointment_daytime' => strtotime($datetime),
  26. 'provider_id' => $this->provider_id,
  27. 'address_id' => $this->specialist->address_id,
  28. 'morning_num' => $this->morning_num,
  29. 'afternoon_num' => $this->afternoon_num,
  30. 'night_num' => $this->night_num,
  31. ]);
  32. }
  33. $this->save(['ticket_status' => 1]);
  34. }
  35. }
  36. //是否已生成号源配置
  37. public function ifExitsTicket()
  38. {
  39. return $this->ticket_status > 0;
  40. }
  41. /**
  42. * 生成号源,保证预约取号正常运行
  43. */
  44. public static function createTicketFromAllAppointment()
  45. {
  46. $all = (new Appointment())->where('end_time', '>', time())->where('ticket_status', 0)->select();
  47. /** @var $item Appointment */
  48. foreach ($all as $item) {
  49. $item->createTicket();
  50. }
  51. }
  52. /**
  53. * 获取可预约地点列表
  54. * @return array
  55. */
  56. public static function availableAddressIds()
  57. {
  58. self::createTicketFromAllAppointment();
  59. $addressIds = (new AppointmentTicket())->where('appointment_daytime', '>=', strtotime(date('Y-m-d')))->distinct('true')->column('address_id');
  60. return $addressIds;
  61. }
  62. //按时间预约列表接口
  63. public static function listByDayTime()
  64. {
  65. self::createTicketFromAllAppointment();
  66. }
  67. //是否已存在预约订单
  68. public function ifExitsAppointmentApplication()
  69. {
  70. }
  71. //关联专家
  72. public function specialist()
  73. {
  74. return $this->belongsTo('Specialist', 'provider_id');
  75. }
  76. }