Appointment.php 5.5 KB

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