Appointment.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\controller\base\Permissions;
  4. use app\common\model\Appointment as appointmentModel;
  5. use think\Db;
  6. use time\DateHelper;
  7. class Appointment extends Permissions
  8. {
  9. private function getModel()
  10. {
  11. return new appointmentModel();
  12. }
  13. public function index()
  14. {
  15. $pid = $this->request->param('pid', 0, 'intval');
  16. if ($this->request->isAjax()) {
  17. $post = $this->request->param();
  18. $where = [
  19. 'provider_id' => $pid
  20. ];
  21. if (isset($post['ids']) and !empty($post['ids'])) {
  22. $where['id'] = ['in', $post['ids']];
  23. }
  24. if (isset($post["start_time"]) and !empty($post["start_time"])) {
  25. $timerang = explode(' - ', $post["start_time"]);
  26. $min_time = strtotime($timerang[0]);
  27. $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime($timerang[1]??'');
  28. $where["start_time"] = [['>=', $min_time], ['<=', $max_time]];
  29. }
  30. if (isset($post["create_time"]) and !empty($post["create_time"])) {
  31. $timerang = explode(' - ', $post["create_time"]);
  32. $min_time = strtotime($timerang[0]);
  33. $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime($timerang[1]??'');
  34. $where["create_time"] = [['>=', $min_time], ['<=', $max_time]];
  35. }
  36. $model = $this->getModel();
  37. $count = $model->where($where)->count();
  38. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('id desc')->select();
  39. foreach ($data as $key => $value) {
  40. $value['specialist_name'] = $value->specialist->name;
  41. $data[$key] = $value;
  42. }
  43. return array('code' => 0, 'count' => $count, 'data' => $data);
  44. } else {
  45. return $this->fetch();
  46. }
  47. }
  48. public function publish()
  49. {
  50. $id = $this->request->param('id', 0, 'intval');
  51. $pid = $this->request->param('pid', 0, 'intval');
  52. $this->assign('pid', $pid);
  53. $post = $this->request->post();
  54. $post['provider_id'] = $pid;
  55. if ($this->request->isPost()) {
  56. if (isset($post['start_time']) && !empty($post['start_time'])) {
  57. $timerang = explode(' - ', $post["start_time"]);
  58. $min_time = strtotime(str_replace(['年', '月'], '-', $timerang[0]));
  59. $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime(str_replace(['年', '月'], '-', $timerang[1]??''));
  60. $post['start_time'] = $min_time;
  61. $post['end_time'] = $max_time;
  62. }
  63. if (isset($post['morning_start_time']) && !empty($post['morning_start_time'])) {
  64. $timerang = explode(' - ', $post["morning_start_time"]);
  65. $post['morning_start_time'] = $timerang[0];
  66. $post['morning_end_time'] = $timerang[1];
  67. }
  68. if (isset($post['afternoon_start_time']) && !empty($post['afternoon_start_time'])) {
  69. $timerang = explode(' - ', $post["afternoon_start_time"]);
  70. $post['afternoon_start_time'] = $timerang[0];
  71. $post['afternoon_end_time'] = $timerang[1];
  72. }
  73. if (isset($post['night_start_time']) && !empty($post['night_start_time'])) {
  74. $timerang = explode(' - ', $post["night_start_time"]);
  75. $post['night_start_time'] = $timerang[0];
  76. $post['night_end_time'] = $timerang[1];
  77. }
  78. //验证
  79. $validate = new \think\Validate([
  80. ['provider_id', 'require|number'],
  81. ['start_time', 'number', '排号日期格式不对'],
  82. ['end_time', 'number', '排号日期格式不对'],
  83. ['morning_num|上午放号个数', 'number'],
  84. ['morning_start_time', 'length:8', '上午时间格式不对'],
  85. ['morning_end_time', 'length:8', '上午时间格式不对'],
  86. ['afternoon_num|下午放号个数', 'number'],
  87. ['afternoon_start_time', 'length:8', '下午时间格式不对'],
  88. ['afternoon_end_time', 'length:8', '下午时间格式不对'],
  89. ['night_num|晚上放号个数', 'number'],
  90. ['night_start_time', 'length:8', '晚上时间格式不对'],
  91. ['night_end_time', 'length:8', '晚上时间格式不对'],
  92. ]);
  93. if (!$validate->check($post)) {
  94. $this->error('提交失败:' . $validate->getError());
  95. }
  96. $weeks = $post['weeks']??[];
  97. $weeks = array_keys($weeks);
  98. $weeks = empty($weeks) ? "" : implode(',', $weeks);
  99. }
  100. $model = $this->getModel();
  101. if ($id > 0) {
  102. //修改
  103. $this->error('不允许修改');
  104. } else {
  105. //新增
  106. if ($this->request->isPost()) {
  107. if (empty($weeks)) {
  108. //按时间跨度排号
  109. //判断是否时间跨度重叠
  110. $exits = (new \app\common\model\AppointmentTicket())->where(['provider_id' => $pid, 'appointment_daytime' => [['>=', $post['start_time']], ['<=', $post['end_time']]]])->count();
  111. if ($exits > 0) {
  112. $this->error('该时间跨度内,已存在排号,请重新选择时间');
  113. }
  114. //保存放号设置
  115. if (false == $model->allowField(true)->save($post)) {
  116. $this->error('添加失败');
  117. } else {
  118. //生成号源
  119. $daylist = DateHelper::dateTimeList(date('Y-m-d', $post['start_time']), date('Y-m-d', $post['end_time']), true);
  120. if (count($daylist) == 2 && end($daylist) == current($daylist)) {
  121. array_pop($daylist);
  122. }
  123. $addressId = (new \app\common\model\Specialist())->where('id', $post['provider_id'])->value('address_id');
  124. foreach ($daylist as $datetime) {
  125. $data = [
  126. 'appointment_id' => $model->id,
  127. 'appointment_daytime' => strtotime($datetime),
  128. 'address_id' => $addressId,
  129. 'provider_id' => $post['provider_id'],
  130. 'status' => \app\common\model\AppointmentTicket::STATUS_OPEN
  131. ];
  132. if (false == (new \app\common\model\AppointmentTicket())->allowField(true)->save($data)) {
  133. $this->error('添加失败');
  134. }
  135. }
  136. }
  137. } else {
  138. //按工作日排号,保存放号设置
  139. $post['start_time'] = 0;
  140. $post['end_time'] = 0;
  141. $post['weeks'] = $weeks;
  142. if (false == $model->allowField(true)->save($post)) {
  143. $this->error('添加失败');
  144. }
  145. }
  146. $this->success('添加成功', 'index', ['pid' => $pid]);
  147. } else {
  148. return $this->fetch();
  149. }
  150. }
  151. }
  152. public function delete()
  153. {
  154. if ($this->request->isAjax()) {
  155. $id = $this->request->param('id', 0, 'intval');
  156. if (false == $this->getModel()->where('id', $id)->delete()) {
  157. $this->error('删除失败');
  158. } else {
  159. $this->success('删除成功', 'index');
  160. }
  161. }
  162. }
  163. public function deletes()
  164. {
  165. if ($this->request->isAjax()) {
  166. $post = $this->request->param();
  167. $ids = $post['ids'];
  168. if ($this->getModel()->where('id', 'in', $ids)->delete()) {
  169. $this->success('删除成功');
  170. }
  171. }
  172. }
  173. public function status()
  174. {
  175. if ($this->request->isPost()) {
  176. $post = $this->request->post();
  177. if (false == $this->getModel()->where('id', $post['id'])->update(['status' => $post['status']])) {
  178. $this->error('设置失败');
  179. } else {
  180. $this->success('设置成功', 'index');
  181. }
  182. }
  183. }
  184. }