You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
10 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Appointment;
use App\Models\AppointmentHolidays;
use App\Models\AppointmentTemplate;
use App\Models\Hospital;
use Illuminate\Http\Request;
use Login;
use Yo;
class AppointmentController extends Controller
{
public function month_map($month)
{
$week = date('w', strtotime($month[0]['date']));
if ($week == 0) $week = 7;
$month_list = [
[],
[],
[],
[],
[],
[],
];
for ($i = 0; $i < $week; $i++) {
$month_list[0][] = [
'date' => '',
'day' => 0,
'create' => false,
];
}
foreach ($month as $item) {
$c = date('d', strtotime($item['date'])) + ($week - 2);
$w = date('w', strtotime($item['date']));
if ($w == 0) $w = 7;
$month_list[floor($c / 7)][$w - 1] = $item;
}
foreach ($month_list as $key => $item) {
$c = count($item);
if ($c != 7) {
for ($i = 0; $i < 7 - $c; $i++) {
$month_list[$key][] = [
'date' => '',
'day' => 0,
'create' => false,
];
}
}
}
return $month_list;
}
public function last_day($date)
{
$date = strtotime($date);
$count = cal_days_in_month(CAL_GREGORIAN, date('m', $date), date('Y', $date));
return date('Y-m', $date) . '-' . $count;
}
public function first_day($date)
{
$date = strtotime($date);
return date('Y-m-01', $date);
}
public function check_create($appointment_template, $date, $list)
{
$weekday = $appointment_template->weekday;
$holidays = $appointment_template->holidays;
$weeks = json_decode($appointment_template->weeks, true);
$week = date('w', strtotime($date));
$week = $week == 0 ? 7 : $week;
switch ($week) {
case 1:
case 2:
case 3:
case 4:
case 5:
if (isset($list[$date]) && $list[$date]['type'] == 1) {
if ($holidays == 2) return false;
}
return in_array((string)$week, $weeks);
case 6:
case 7:
if (isset($list[$date]) && $list[$date]['type'] == 2) {
if ($weekday == 1) return true;
}
return in_array((string)$week, $weeks);
}
}
public function date_map($start_date, $end_date, $appointment_template, $list)
{
$date_map = [];
$first_day = self::first_day($start_date);
$last_day = self::last_day($end_date);
$first_day_time = strtotime($first_day);
$last_day_time = strtotime($last_day);
for ($i = $first_day_time; $i <= $last_day_time; $i += 86400) {
$date = date('Y-m-d', $i);
$key = date('Y_m', $i);
$create = self::check_create($appointment_template, $date, $list);
if (strtotime($start_date) > $i) $create = false;
if (strtotime($end_date) < $i) $create = false;
$item = [
'date' => $date,
'day' => date('d', $i),
'create' => $create,
];
$date_map[$key][] = $item;
}
$map = [];
foreach ($date_map as $key => $item) {
$map[] = [
'year' => explode('_', $key)[0],
'month' => explode('_', $key)[1],
'list' => self::month_map($item),
];
}
return $map;
}
public function create(Request $request)
{
Login::admin([], [13, 15, 27]);
$hospital_id = $request->post('hospital');
if (Login::$info->hospital != 0) {
if ($hospital_id != Login::$info->hospital) {
Yo::error_echo(100000, ['机构/医院']);
}
}
$id = $request->post('id');
$date_arr = $request->post('date_arr');
$appointment_template = AppointmentTemplate::find($id);
if (!$appointment_template) Yo::error_echo(100000, ['计划模板']);
if (count($date_arr) > 300) Yo::error_echo(200022);
$hospital = Hospital::where('id', $hospital_id)->where('del', 2)->first();
if (!$hospital) Yo::error_echo(100000, ['机构/医院']);
$list = [];
$error = [];
foreach ($date_arr as $date) {
$week = date('w', strtotime($date));
$week = $week == 0 ? 7 : $week;
$appointment = new Appointment();
$appointment->date = $date;
$appointment->week = $week;
$appointment->start_time = $appointment_template->start_time;
$appointment->end_time = $appointment_template->end_time;
$appointment->stop_time = $appointment_template->stop_time;
$appointment->max_count = $appointment_template->max_count;
$appointment->used_count = 0;
$appointment->hospital = $hospital->id;
$appointment->status = 1;
$appointment->save();
if (!!$appointment->id) {
$list[] = $appointment;
} else {
$error[] = $appointment;
}
}
return Yo::echo([
'list' => $list,
'error' => $error,
]);
}
public function create_list(Request $request)
{
Login::admin([], [13, 27]);
$id = $request->post('id');
$start_date = $request->post('start_date');
$end_date = $request->post('end_date');
if (strtotime($start_date) > strtotime($end_date)) Yo::error_echo(200020);
$appointment_template = AppointmentTemplate::find($id);
if (!$appointment_template) Yo::error_echo(100000, ['计划模板']);
$appointment_holiday_list = AppointmentHolidays::where('date', '>=', $start_date)
->where('date', '<=', $end_date)
->get();
$list = [];
foreach ($appointment_holiday_list as $item) {
$list[$item->date] = [
'date' => $item->date,
'type' => $item->type,
];
}
$date_map = self::date_map($start_date, $end_date, $appointment_template, $list);
if (count($date_map) > 12) Yo::error_echo(200021);
return Yo::echo([
'list' => $date_map,
]);
}
public function delete(Request $request)
{
Login::admin([], [15, 27]);
$ids = $request->post('ids');
$appointments_to_delete = Appointment::whereIn('id', $ids)
->where('used_count', '>', 0)
->get();
if (count($appointments_to_delete)) {
return Yo::error_echo(200023);
}
//Appointment::whereIn('id', $ids)->where('hospital', Login::$info->hospital)->update(['del' => 1]);
if(Login::$info->id==1){
Appointment::whereIn('id', $ids)->update(['del' => 1]);
}else{
Appointment::whereIn('id', $ids)->where('hospital', Login::$info->hospital)->update(['del' => 1]);
}
return Yo::delete_echo($ids);
}
public function change_status(Request $request)
{
Login::admin([], [15, 27]);
$ids = $request->post('ids');
$status = $request->post('status');
// Appointment::whereIn('id', $ids)->where('hospital', Login::$info->hospital)->update(['status' => $status]);
if(Login::$info->id==1){
Appointment::whereIn('id', $ids)->update(['status' => $status]);
}else{
Appointment::whereIn('id', $ids)->where('hospital', Login::$info->hospital)->update(['status' => $status]);
}
return Yo::update_echo($ids);
}
public function change_count(Request $request)
{
Login::admin([], [15, 27]);
$ids = $request->post('ids');
$count = $request->post('count');
// Appointment::whereIn('id', $ids)->where('hospital', Login::$info->hospital)->update(['max_count' => $count]);
if(Login::$info->id==1){
Appointment::whereIn('id', $ids)->update(['max_count' => $count]);
}else{
Appointment::whereIn('id', $ids)->where('hospital', Login::$info->hospital)->update(['max_count' => $count]);
}
return Yo::update_echo($ids);
}
public function list(Request $request)
{
Login::admin([], [15, 27]);
$hospital = $request->post('hospital');
if (Login::$info->hospital != 0) {
if ($hospital != Login::$info->hospital) {
Yo::error_echo(100000, ['机构/医院']);
}
}
$start_date = $request->post('start_date');
$end_date = $request->post('end_date');
$status = $request->post('status');
$weeks = $request->post('weeks');
$query = Appointment::select('*')
->selectRaw("IFNULL((select type from appointment_holidays where appointment_holidays.date = appointments.date),0) as date_type")
->where('hospital', $hospital);
if ($status != 0) $query->where('status', '>=', $status);
if (!!$start_date) $query->where('date', '>=', $start_date);
if (!!$end_date) $query->where('date', '<=', $end_date);
if (count($weeks) == 0) $weeks = [1, 2, 3, 4, 5, 6, 7];
$query->whereIn('week', $weeks);
$appointments = $query->where('del', 2)->orderBy('date', 'desc')->paginate(20);
return Yo::echo($appointments);
}
//自动选择当日
public function SelectToday(Request $request)
{
Login::user();
$hospital = $request->post('hospital');
$date = date('Y-m-d');
$nowtime= date("H:i:s");
//var_dump($nowtime);
$appointments = Appointment::where('hospital', $hospital)
->where('date', $date)
->where('status', 1)
->where('del', 2)
->where('stop_time','>',$nowtime)
->first();
return Yo::echo([
'time' => $appointments,
]);
}
public function mp_list(Request $request)
{
Login::user();
$month = $request->post('month');
$hospital = $request->post('hospital');
$appointments = Appointment::where('hospital', $hospital)
// ->where('date', 'like', $month . '%')
->where('date', date('Y-m-d'))
->where('status', 1)
->where('del', 2)
->orderBy('date')
->get();
$list = [];
foreach ($appointments as $appointment) {
$date = substr($appointment->date, 8, 2);
if (!isset($list[$date])) {
$list[$date] = [
'date' => $appointment->date,
'info' => 0,
'data' => [
'list' => []
],
];
}
$item_time = strtotime($appointment->date . ' ' . $appointment->stop_time);
$start_show = date('H:i', strtotime($appointment->date . ' ' . $appointment->start_time));
$end_show = date('H:i', strtotime($appointment->date . ' ' . $appointment->end_time));
$appointment->start_show = $start_show;
$appointment->end_show = $end_show;
if ($item_time < time()) {
$appointment->used_count = $appointment->max_count;
} else {
$list[$date]['info'] += max($appointment->max_count - $appointment->used_count, 0);
}
$list[$date]['data']['list'][] = $appointment;
}
$l = [];
foreach ($list as $item) {
$l[] = $item;
}
return Yo::echo([
'list' => $l,
]);
}
}