Appointment.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. $daytimestamp = strtotime($daytime . ' +1 year');
  45. // throw new Exception("该时间已过期,请重新选择时间");
  46. }
  47. $daytimeWeek = date('w', $daytimestamp);
  48. $appoints = (new Appointment())->where('weeks', '<>', '')->select();
  49. foreach ($appoints as $appoint) {
  50. $weeks = explode(',', $appoint->weeks);
  51. if (in_array($daytimeWeek, $weeks)) {
  52. //生成号源记录
  53. if (0 == (new AppointmentTicket())->where(['appointment_id' => $appoint->id, 'appointment_daytime' => $daytimestamp])->count()) {
  54. if (empty($appoint->specialist)) {
  55. continue;
  56. }
  57. (new AppointmentTicket())->allowField(true)->save([
  58. 'appointment_id' => $appoint->id,
  59. 'appointment_daytime' => $daytimestamp,
  60. 'address_id' => $appoint->specialist->address_id,
  61. 'provider_id' => $appoint->provider_id,
  62. 'status' => AppointmentTicket::STATUS_OPEN,
  63. ]);
  64. }
  65. }
  66. }
  67. }
  68. //关联专家
  69. public function specialist()
  70. {
  71. return $this->belongsTo('Specialist', 'provider_id');
  72. }
  73. }