Appointment.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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())->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. $pagenum = $this->request->param('pagenum', 20, 'intval');
  56. $datalist = (new Address())->where($where)->paginate($pagenum, true);
  57. if (empty($datalist)) {
  58. $this->json_error("没有数据");
  59. }
  60. foreach ($datalist as $key => $item) {
  61. $item['thumb_url'] = geturl($item->thumb, '', true);
  62. $datalist[$key] = $item;
  63. }
  64. $this->json_success("查询成功", $datalist);
  65. }
  66. //获取号源列表
  67. public function getTicketByAddressId()
  68. {
  69. $post = $this->request->param();
  70. $validate = new \think\Validate([
  71. ['address_id', 'require|number'],
  72. ['daytime', 'dateFormat:Y-m-d'],
  73. ]);
  74. if (!$validate->check($post)) {
  75. $this->json_error('提交失败:' . $validate->getError());
  76. }
  77. $address_id = $this->request->param('address_id');
  78. $daytime = $this->request->param('daytime', date('Y-m-d'));
  79. try {
  80. //生成工作日号源
  81. \app\common\model\Appointment::createWeekTicketAll($daytime);
  82. } catch (Exception $e) {
  83. $this->json_error($e->getMessage());
  84. }
  85. $ticketModel = new AppointmentTicket();
  86. $tickets = $ticketModel->where('address_id', $address_id)->where('appointment_daytime', strtotime($daytime))->select();
  87. foreach ($tickets as $k => $item) {
  88. $appoint = $item->appointment;
  89. if (empty($appoint)) {
  90. unset($item);
  91. continue;
  92. }
  93. //专家信息
  94. $item->specialist;
  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. }
  108. /**
  109. * @param $timePeriods array 放号设置的时间段
  110. * @param $ticketPeriods array 已经预约的时间段
  111. * @return array
  112. */
  113. private function setPeriodStatus($timePeriods, $ticketPeriods)
  114. {
  115. $daytime = $this->request->param('daytime', date('Y-m-d'));
  116. $arr = [];
  117. $now = time();
  118. foreach ($timePeriods as $ticket => $period) {
  119. //排除过期时段
  120. $timerang = explode(' - ', $period);
  121. $period_end = $timerang[1];
  122. if ($now > strtotime($daytime . ' ' . $period_end)) {
  123. continue;
  124. }
  125. //标记预约状态
  126. if (in_array($period, $ticketPeriods)) {
  127. // $arr[] = ['period' => $period, 'status' => 1];
  128. continue;//排除已被预约的
  129. } else {
  130. $arr[] = ['period' => $period, 'status' => 0];
  131. }
  132. }
  133. return $arr;
  134. }
  135. }