Appointment.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\common\model;
  3. use think\Exception;
  4. use think\Model;
  5. class Appointment extends Model
  6. {
  7. protected $updateTime = false;
  8. const STATUS_OPEN = 1;
  9. const STATUS_CLOSE = 0;
  10. const WEEK_TEXTS = ['', '周一', '周二', '周三', '周四', '周五', '周六', '周日'];
  11. //morning_time_periods
  12. public function getMorningTimePeriodsAttr($value, $data)
  13. {
  14. $json = json_decode($value, true);
  15. if (!is_array($json)) {
  16. return [];
  17. } else {
  18. foreach ($json as $k => $item) {
  19. if (empty($item))
  20. unset($json[$k]);
  21. }
  22. return $json;
  23. }
  24. }
  25. //afternoon_time_periods
  26. public function getAfternoonTimePeriodsAttr($value, $data)
  27. {
  28. $json = json_decode($value, true);
  29. if (!is_array($json)) {
  30. return [];
  31. } else {
  32. foreach ($json as $k => $item) {
  33. if (empty($item))
  34. unset($json[$k]);
  35. }
  36. return $json;
  37. }
  38. }
  39. //生成工作日号源
  40. public static function createWeekTicketAll($daytime)
  41. {
  42. $daytimestamp = strtotime($daytime);
  43. if (strtotime(date('Y-m-d')) > $daytimestamp) {
  44. throw new Exception("该时间已过期,请重新选择时间");
  45. }
  46. $daytimeWeek = date('w', $daytimestamp);
  47. $appoints = (new Appointment())->where('weeks', '<>', '')->select();
  48. foreach ($appoints as $appoint) {
  49. $weeks = explode(',', $appoint->weeks);
  50. if (in_array($daytimeWeek, $weeks)) {
  51. //生成号源记录
  52. if (0 == (new AppointmentTicket())->where(['appointment_id' => $appoint->id, 'appointment_daytime' => $daytimestamp])->count()) {
  53. if (empty($appoint->specialist)) {
  54. continue;
  55. }
  56. (new AppointmentTicket())->allowField(true)->save([
  57. 'appointment_id' => $appoint->id,
  58. 'appointment_daytime' => $daytimestamp,
  59. 'address_id' => $appoint->specialist->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. }