Appointment.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use time\DateHelper;
  7. class Appointment extends Model
  8. {
  9. protected $updateTime = false;
  10. const STATUS_OPEN = 1;
  11. const STATUS_CLOSE = 0;
  12. const WEEK_TEXTS = ['', '周一', '周二', '周三', '周四', '周五', '周六', '周日'];
  13. //appointment_date_text
  14. public function getAppointmentDateTextAttr($value, $data)
  15. {
  16. if ($this->isWeekMode()) {
  17. $weeks = explode(',', $this->weeks);
  18. $week_texts = [];
  19. foreach ($weeks as $week) {
  20. $week_texts[] = self::WEEK_TEXTS[$week]??'';
  21. }
  22. return '每' . implode(',', $week_texts);
  23. } else {
  24. return $this->start_time . ' ~ ' . $this->end_time;
  25. }
  26. }
  27. //start_time
  28. public function getStartTimeAttr($value, $data)
  29. {
  30. return $value ? date('Y-m-d', $value) : '';
  31. }
  32. //end_time
  33. public function getEndTimeAttr($value, $data)
  34. {
  35. return $value ? date('Y-m-d', $value) : '';
  36. }
  37. //是否为工作日排号模式
  38. public function isWeekMode()
  39. {
  40. return $this->weeks ? true : false;
  41. }
  42. //生成工作日号源
  43. public static function createWeekTicketAll($daytime)
  44. {
  45. $daytimestamp = strtotime($daytime);
  46. if (strtotime(date('Y-m-d')) > $daytimestamp) {
  47. throw new Exception("该时间已过期,请重新选择时间");
  48. }
  49. $daytimeWeek = date('w', $daytimestamp);
  50. $appoints = (new Appointment())->where('weeks', '<>', '')->where('status', self::STATUS_OPEN)->select();
  51. foreach ($appoints as $appoint) {
  52. $weeks = explode(',', $appoint->weeks);
  53. if (in_array($daytimeWeek, $weeks)) {
  54. //生成号源记录
  55. if (0 == (new AppointmentTicket())->where(['appointment_id' => $appoint->id, 'appointment_daytime' => $daytimestamp])->count()) {
  56. (new AppointmentTicket())->allowField(true)->save([
  57. 'appointment_id' => $appoint->id,
  58. 'appointment_daytime' => $daytimestamp,
  59. 'address_id' => $appoint->address_id,
  60. 'provider_id' => $appoint->provider_id,
  61. 'status' => AppointmentTicket::STATUS_OPEN
  62. ]);
  63. }
  64. }
  65. }
  66. }
  67. //关联专家
  68. public function specialist()
  69. {
  70. return $this->belongsTo('Specialist', 'provider_id');
  71. }
  72. }