Appointment.php 5.7 KB

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