Appointment.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\controller\base\Base;
  4. use app\common\model\Address;
  5. use app\common\model\Appointment as appointmentModel;
  6. use app\common\model\AppointmentApplication;
  7. use app\common\model\AppointmentTicket;
  8. use time\DateHelper;
  9. class Appointment extends Base
  10. {
  11. //获取地址列表
  12. public function addressList()
  13. {
  14. $post = $this->request->param();
  15. $validate = new \think\Validate([
  16. ['page', 'number'],
  17. ['pagenum', 'number|<=:1000']
  18. ]);
  19. if (!$validate->check($post)) {
  20. $this->json_error('提交失败:' . $validate->getError());
  21. }
  22. $pagenum = $this->request->param('pagenum', 20, 'intval');
  23. $datalist = (new Address())->paginate($pagenum, true);
  24. if (empty($datalist)) {
  25. $this->json_error("没有数据");
  26. }
  27. foreach ($datalist as $key => $item) {
  28. $item['thumb_url'] = geturl($item->thumb, '', true);
  29. $datalist[$key] = $item;
  30. }
  31. $this->json_success("查询成功", $datalist);
  32. }
  33. //按地址id获取号源信息
  34. public function getTicketByAddressId()
  35. {
  36. $post = $this->request->param();
  37. $validate = new \think\Validate([
  38. ['address_id', 'require|number'],
  39. ['daytime', 'dateFormat:Y-m-d'],
  40. ]);
  41. if (!$validate->check($post)) {
  42. $this->json_error('提交失败:' . $validate->getError());
  43. }
  44. $address_id = $this->request->param('address_id');
  45. $daytime = $this->request->param('daytime', date('Y-m-d'));
  46. //生成工作日号源
  47. \app\common\model\Appointment::createWeekTicketAll($daytime);
  48. $ticketModel = new AppointmentTicket();
  49. $tickets = $ticketModel->where('address_id', $address_id)->where('appointment_daytime', strtotime($daytime))->select();
  50. foreach ($tickets as $k => $item) {
  51. $appoint = $item->appointment;
  52. //专家信息
  53. $item->specialist;
  54. $item->specialist->head_pic = geturl($item->specialist->head_pic, '', true);
  55. //拆分预约时段
  56. $ticketPeriods = (new AppointmentApplication())->where('appointment_ticket_id', $item->id)->column('appointment_time');
  57. $item['morning_time_period'] = $this->setPeriodStatus(DateHelper::splitTimePeriod($appoint->morning_start_time, $appoint->morning_end_time, $appoint->morning_num), $ticketPeriods);
  58. $item['afternoon_time_period'] = $this->setPeriodStatus(DateHelper::splitTimePeriod($appoint->afternoon_start_time, $appoint->afternoon_end_time, $appoint->afternoon_num), $ticketPeriods);
  59. $item['night_time_period'] = $this->setPeriodStatus(DateHelper::splitTimePeriod($appoint->night_start_time, $appoint->night_end_time, $appoint->night_num), $ticketPeriods);
  60. //剩余预约号码
  61. $item['morning_remaining'] = count($item['morning_time_period']);
  62. $item['afternoon_remaining'] = count($item['afternoon_time_period']);
  63. $item['night_remaining'] = count($item['night_time_period']);
  64. $tickets[$k] = $item;
  65. }
  66. $this->json_success("查询成功", $tickets);
  67. }
  68. /**
  69. * @param $timePeriods array 拆分的时间段
  70. * @param $ticketPeriods array 已经预约的时间段
  71. * @return array
  72. */
  73. private function setPeriodStatus($timePeriods, $ticketPeriods)
  74. {
  75. $daytime = $this->request->param('daytime', date('Y-m-d'));
  76. $arr = [];
  77. $now = time();
  78. foreach ($timePeriods as $ticket => $period) {
  79. //排除过期时段
  80. $timerang = explode(' - ', $period);
  81. $period_end = $timerang[1];
  82. if ($now > strtotime($daytime . ' ' . $period_end)) {
  83. continue;
  84. }
  85. //标记预约状态
  86. if (in_array($period, $ticketPeriods)) {
  87. // $arr[] = ['period' => $period, 'status' => 1];
  88. continue;//排除
  89. } else {
  90. $arr[] = ['period' => $period, 'status' => 0];
  91. }
  92. }
  93. return $arr;
  94. }
  95. //按时间获取地址列表
  96. public function addressListByDayTime()
  97. {
  98. $ticketModel = new AppointmentTicket();
  99. $post = $this->request->param();
  100. $validate = new \think\Validate([
  101. ['daytime', 'date|dateFormat:Y-m-d'],
  102. ['page', 'number'],
  103. ['pagenum', 'number|<=:1000']
  104. ]);
  105. if (!$validate->check($post)) {
  106. $this->json_error('提交失败:' . $validate->getError());
  107. }
  108. $daytime = $this->request->param('daytime', date('Y-m-d'));
  109. //生成工作日号源
  110. \app\common\model\Appointment::createWeekTicketAll($daytime);
  111. $addressIds = $ticketModel->where('appointment_daytime', strtotime($daytime))->distinct(true)->column('address_id');
  112. $where = [];
  113. $where['id'] = ['in', $addressIds];
  114. $pagenum = $this->request->param('pagenum', 20, 'intval');
  115. $datalist = (new Address())->where($where)->paginate($pagenum, true);
  116. if (empty($datalist)) {
  117. $this->json_error("没有数据");
  118. }
  119. foreach ($datalist as $key => $item) {
  120. $item['thumb_url'] = geturl($item->thumb, '', true);
  121. $datalist[$key] = $item;
  122. }
  123. $this->json_success("查询成功", $datalist);
  124. }
  125. }