Appointment.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. $exits = (new \app\common\model\AppointmentTicket())->where(['provider_id' => $pid, 'appointment_daytime' => [['>=', $post['start_time']], ['<=', $post['end_time']]]])->count();
  110. if ($exits > 0) {
  111. $this->error('该时间跨度内,已存在排号,请重新选择时间');
  112. }
  113. if (false == $model->allowField(true)->save($post)) {
  114. $this->error('添加失败');
  115. } else {
  116. //生成号源
  117. $daylist = DateHelper::dateTimeList(date('Y-m-d', $post['start_time']), date('Y-m-d', $post['end_time']), true);
  118. if (count($daylist) == 2 && end($daylist) == current($daylist)) {
  119. array_pop($daylist);
  120. }
  121. $addressId = (new \app\common\model\Specialist())->where('id', $post['provider_id'])->value('address_id');
  122. foreach ($daylist as $datetime) {
  123. $post['appointment_id'] = $model->id;
  124. $post['appointment_daytime'] = strtotime($datetime);
  125. $post['address_id'] = $addressId;
  126. if (false == (new \app\common\model\AppointmentTicket())->allowField(true)->save($post)) {
  127. $this->error('添加失败');
  128. }
  129. }
  130. }
  131. } else {
  132. $post['start_time'] = 0;
  133. $post['end_time'] = 0;
  134. $post['weeks'] = $weeks;
  135. if (false == $model->allowField(true)->save($post)) {
  136. $this->error('添加失败');
  137. }
  138. }
  139. $this->success('添加成功', 'index', ['pid' => $pid]);
  140. } else {
  141. return $this->fetch();
  142. }
  143. }
  144. }
  145. public function delete()
  146. {
  147. if ($this->request->isAjax()) {
  148. $id = $this->request->param('id', 0, 'intval');
  149. if (false == $this->getModel()->where('id', $id)->delete()) {
  150. $this->error('删除失败');
  151. } else {
  152. $this->success('删除成功', 'index');
  153. }
  154. }
  155. }
  156. public function deletes()
  157. {
  158. if ($this->request->isAjax()) {
  159. $post = $this->request->param();
  160. $ids = $post['ids'];
  161. if ($this->getModel()->where('id', 'in', $ids)->delete()) {
  162. $this->success('删除成功');
  163. }
  164. }
  165. }
  166. public function status()
  167. {
  168. if ($this->request->isPost()) {
  169. $post = $this->request->post();
  170. if (false == $this->getModel()->where('id', $post['id'])->update(['status' => $post['status']])) {
  171. $this->error('设置失败');
  172. } else {
  173. $this->success('设置成功', 'index');
  174. }
  175. }
  176. }
  177. }