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. $appoint = $item->appointment;
  90. if (empty($appoint)) {
  91. unset($item);
  92. continue;
  93. }
  94. //专家信息
  95. $item->specialist;
  96. $item->specialist->head_pic = geturl($item->specialist->head_pic, '', true);
  97. //已经预约的时间段
  98. $ticketPeriods = (new AppointmentApplication())->where('appointment_ticket_id', $item->id)->where('finish_time', 0)->column('appointment_time');
  99. //预约的时间段
  100. $item['morning_time_period'] = $this->setPeriodStatus($appoint->morning_time_periods, $ticketPeriods);
  101. $item['afternoon_time_period'] = $this->setPeriodStatus($appoint->afternoon_time_periods, $ticketPeriods);
  102. //剩余预约号码个数
  103. $item['morning_remaining'] = count($item['morning_time_period']);
  104. $item['afternoon_remaining'] = count($item['afternoon_time_period']);
  105. $tickets[$k] = $item;
  106. }
  107. $this->json_success("查询成功", $tickets);
  108. // $this->json_success("查询成功", []);
  109. }
  110. /**
  111. * @param $timePeriods array 放号设置的时间段
  112. * @param $ticketPeriods array 已经预约的时间段
  113. * @return array
  114. */
  115. private function setPeriodStatus($timePeriods, $ticketPeriods)
  116. {
  117. $daytime = $this->request->param('daytime', date('Y-m-d'));
  118. $arr = [];
  119. $now = time();
  120. foreach ($timePeriods as $ticket => $period) {
  121. //排除过期时段
  122. $timerang = explode(' - ', $period);
  123. $period_end = $timerang[1];
  124. if ($now > strtotime($daytime . ' ' . $period_end)) {
  125. continue;
  126. }
  127. //标记预约状态
  128. if (in_array($period, $ticketPeriods)) {
  129. // $arr[] = ['period' => $period, 'status' => 1];
  130. continue;//排除已被预约的
  131. } else {
  132. $arr[] = ['period' => $period, 'status' => 0];
  133. }
  134. }
  135. return $arr;
  136. }
  137. }