Appointment.php 5.7 KB

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