Appointment.php 6.2 KB

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