love_php/app/Http/Controllers/Server/H5/AppointmentController.php

364 lines
16 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Http\Controllers\Server\H5;
use App\Jobs\SendMail;
use App\Models\Live\Anchor;
use App\Models\Match\MerchantUser;
use App\Models\MerchantAccount;
use App\Models\Message;
use App\Models\SaasAppointment;
use App\Models\SaasUserAppointment;
use App\Models\Server\SaasAppointmentMember;
use App\Utils\Messenger as Messengers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Carbon;
class AppointmentController extends Controller
{
/**
* 用户预约
* @param Request $request
* @return JsonResponse|string
*/
public function postUserAppointment(Request $request)
{
try {
$type = $request->appointment_type;
$date = $request->appointment_date;
$merchant_id = $request->m_id?:$request->merchant_id;
$owner_m_id = $request->merchant_id;
$start_time = $request->start_time;
$end_time = $request->end_time;
$segment = $request->segment;
$name = $request->name;
$mobile = $request->mobile;
$merchant_user_id = $request->merchant_user_id;
if (config('app.debug')) {
if (empty($type) || empty($date) || empty($merchant_id) || empty($start_time) || empty($end_time) || empty($merchant_user_id) || empty($owner_m_id)) {
return $this->failure('参数不全');
}
}
if ($type == 'merchant') {
$type = MerchantAccount::class;
} else {
return $this->failure('无效的预约类型');
}
//检查日期是否在七天内
$compare_time = CarBon::now()->addDay(15);
$input_date = Carbon::parse($date);
if ($input_date > $compare_time) {
return $this->failure('只能提前7天预约');
}
if (Carbon::parse($end_time) < Carbon::parse($start_time)) {
return $this->failure('结束时间不能小于开始时间');
}
//检查商家是否有开放这段时间
$time = SaasAppointment::where('merchant_id', $merchant_id)->where('appointment_type', $type)
->where('appointment_target_id', $merchant_id)
->whereNotNull('start_time')
->whereNotNull('end_time')
->where('segment', $segment)
->first();
if (!$time) {
return $this->failure('此时间段不在商家开放时间内');
} else {
if (Carbon::parse($time->end_time) < Carbon::parse($end_time)) {
return $this->failure('此时间段不在商家开放时间内');
}
if (Carbon::parse($start_time) < Carbon::parse($time->start_time)) {
return $this->failure('此时间段不在商家开放时间内');
}
}
//检查用户今天有没有预约
$user_list = SaasUserAppointment::where('merchant_id', $merchant_id)->where('appointment_date', $date)
->where('appointment_type', $type)
->where('merchant_user_id', $merchant_user_id)
->first();
if ($user_list) {
return $this->failure($date . '你已经预约过了,每个用户每天只能预约一次');
}
//检查有没有被期它用户预约
$user_list = SaasUserAppointment::where('merchant_id', $merchant_id)->where('appointment_date', $date)
->where('start_time', $start_time)->where('end_time', $end_time)
->first();
if ($user_list) {
return $this->failure('此时间段已被其它用户预约');
}
$appointment = new SaasUserAppointment();
$appointment->appointment_type = $type;
$appointment->name = $name;
$appointment->mobile = $mobile;
$appointment->appointment_id = $merchant_id;
$appointment->appointment_date = $date;
$appointment->merchant_id = $merchant_id;
$appointment->owner_m_id = $owner_m_id;
$appointment->merchant_user_id = $merchant_user_id;
$appointment->start_time = $start_time;
$appointment->end_time = $end_time;
$appointment->save();
$merchant = MerchantAccount::where('id',$merchant_id)->first();
$merchant_user = MerchantUser::where('id',$merchant_user_id)->first();
if($merchant && $merchant->mobile){
$message = '用户'.$appointment->name.' '.$appointment->mobile .'在'.$date.' '.$start_time.'-'.$end_time.'预约了您,请空闲出您的时间';
Message::create([
'phone' => $merchant->mobile,
'message' => $message,
'confirmed' => 1,
'ip' => request() ? request()->ip() : '127.0.0.1',
]);
Messengers::sendSMS($merchant->mobile, $message);
}elseif ($merchant && $merchant->email) {
$message = '用户'.$appointment->name.' '.$appointment->mobile .'在'.$date.' '.$start_time.'-'.$end_time.'预约了您,请空闲出您的时间';
SendMail::dispatch($message, $merchant->email, 'message')->onQueue('security_code');
}
return $this->success('ok');
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 获取可预约时间列表
* @param Request $request
* @return JsonResponse|string
*/
public function getAppointmentTimeList(Request $request)
{
try {
$type = $request->appointment_type;
$date = $request->appointment_date ;
if($date == 'undefined' || $date == null){
$date = date('Y-m-d');
}
$merchant_id = $request->merchant_id;
if ($type == 'merchant') {
$type = MerchantAccount::class;
} else {
return $this->failure('无效的预约类型');
}
$merchant_list = SaasAppointment::where('merchant_id', $merchant_id)->where('appointment_type', $type)
->where('appointment_target_id', $merchant_id)
->where('status', 1)
->whereNotNull('start_time')->whereNotNull('end_time')
->get();
if (!$merchant_list->count()) {
return $this->failure('此商家未设置预约时间');
}
$time = [];
foreach ($merchant_list as $key => $val) {
$temp_time = Carbon::parse($val->start_time);
$emd_time = Carbon::parse($val->end_time);
while ($temp_time < $emd_time) {
$hard_time = $temp_time;
$hard_time_string = $hard_time->format('H:i');
$foot_time = $temp_time->addMinute(30);
$foot_time_string = $foot_time->format('H:i');
if($date == date('Y-m-d')){
$now = Carbon::now()->addMinute(30)->format('H:i');
if(Carbon::parse($now) > $hard_time){
continue;
}
}
$time[] = $hard_time_string . '-' . $foot_time_string;
$temp_time = $foot_time;
$segment[] = $val['segment'];
}
}
$user_list = SaasUserAppointment::where('merchant_id', $merchant_id)->where('appointment_date', $date)
->get();
$list = [];
if ($user_list->count()) {
$id_list = [];
foreach ($user_list as $key => $val) {
$a = $val->start_time . '-' . $val->end_time;
$id = array_search($a, $time);
if ($id !== false) {
$id_list[$key] = $id;
if ($val->merchant_user_id == $request->merchant_user_id) {
$status_list[$id] = 1;
} else {
$status_list[$id] = 2;
}
}
}
foreach ($time as $key => $val) {
if (in_array($key, $id_list)) {
$temp['time'] = $val;
$temp['busy'] = $status_list[$key];
$temp['segment'] = $segment[$key];
$list[] = $temp;
} else {
$temp['time'] = $val;
$temp['busy'] = 0;
$temp['segment'] = $segment[$key];
$list[] = $temp;
}
}
} else {
foreach ($time as $key => $val) {
$temp['time'] = $val;
$temp['busy'] = 0;
$temp['segment'] = $segment[$key];
$list[] = $temp;
}
}
$carbon = Carbon::now();
$data[] = $carbon->format('Y-m-d');
for ($i = 0; $i <= 5; $i++) {
$carbon = $carbon->addDay(1);
$data[] = $carbon->format('Y-m-d');
}
//是否有预约记录
$has_appointment = SaasUserAppointment::where('merchant_user_id', $request->merchant_user_id)
->where('merchant_id', $merchant_id)->where('appointment_type', MerchantAccount::class)
->where('appointment_id', $merchant_id)->exists()?1:0;
return $this->success('ok', compact('list', 'data', 'has_appointment'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
public function getUserAppointmentList(Request $request)
{
$merchant_user_id = $request->merchant_user_id;
$merchant_id = $request->merchant_id;
$list = SaasUserAppointment::where('merchant_user_id', $merchant_user_id)
->where('merchant_id', $merchant_id)->where('appointment_type', MerchantAccount::class)
->where('appointment_id', $merchant_id)
->orderBy('id', 'desc')
->paginate();
return $this->success('ok', $list);
}
/**
* 可预约老师列表
* @param Request $request
* @return JsonResponse|string
*/
public function appointmentMembers(Request $request)
{
try {
$merchant_id = $request->merchant_id;
$merchant = MerchantAccount::with('liveAnchor:m_id,name,pic,designation,introduction')->where('id', $merchant_id)->select('id','created_at')->first();
$merchant->m_id = $merchant->id;
$merchant->type = 'owner_merchant';
unset($merchant->anchorV2);
$members = SaasAppointmentMember::with('liveAnchor:m_id,name,pic,designation,introduction')
->where('owner_m_id', $merchant_id)->where('status', 1)
->orderBy('created_at', 'desc')->selectRaw('*, "merchant" as type')->paginate()->toArray();
$page = $request->input("page");
if ($page == 1 || empty($page)) {
array_unshift($members['data'], $merchant);
}
return $this->success("ok", $members);
}catch (\Exception $e){
$this->getError($e);
return $this->failure();
}
}
/**
* 可预约老师详情-时间表
* @param Request $request
* @param $m_id
* @return JsonResponse|void
*/
public function memberTimeList(Request $request, $m_id)
{
try {
$merchant_user_id = $request->merchant_user_id;
$date = $request->input('appointment_date') ;
$date = $date?:date('Y-m-d');
$anchor = Anchor::where("m_id", $m_id)->selectRaw('m_id,name,pic,designation,introduction')->first();
if (empty($anchor)) throw new \Exception("商户信息丢失");
//是否有预约记录
$has_appointment = SaasUserAppointment::where('merchant_user_id', $merchant_user_id)
->where('merchant_id', $m_id)->where('appointment_type', MerchantAccount::class)
->where('appointment_id', $m_id)->exists()?1:0;
//预约时间表
$merchant_list = SaasAppointment::where('merchant_id', $m_id)->where('appointment_type', MerchantAccount::class)
->where('appointment_target_id', $m_id)
->where('status', 1)
->whereNotNull('start_time')->whereNotNull('end_time')
->get();
if (empty($merchant_list->count())) return $this->failure('此商家未设置预约时间');
$time = [];
foreach ($merchant_list as $val) {
$temp_time = Carbon::parse($val->start_time);
$emd_time = Carbon::parse($val->end_time);
$add_time = ($val->time_type == 'half_hour')?30:60;
while ($temp_time < $emd_time) {
$hard_time = $temp_time;
$hard_time_string = $hard_time->format('H:i');
$foot_time = $temp_time->addMinute($add_time);
$foot_time_string = $foot_time->format('H:i');
if($date == date('Y-m-d')){
$now = Carbon::now()->addMinute($add_time)->format('H:i');
if(Carbon::parse($now) > $hard_time){
continue;
}
}
$time[] = $hard_time_string . '-' . $foot_time_string;
$temp_time = $foot_time;
$segment[] = $val['segment'];
}
}
$user_list = SaasUserAppointment::where('merchant_id', $m_id)->where('appointment_date', $date)
->get();
$list = [];
if ($user_list->count()) {
$id_list = [];
foreach ($user_list as $key => $val) {
$a = $val->start_time . '-' . $val->end_time;
$id = array_search($a, $time);
if ($id !== false) {
$id_list[$key] = $id;
if ($val->merchant_user_id == $request->merchant_user_id) {
$status_list[$id] = 1;
} else {
$status_list[$id] = 2;
}
}
}
foreach ($time as $key => $val) {
if (in_array($key, $id_list)) {
$temp['time'] = $val;
$temp['busy'] = $status_list[$key];
$temp['segment'] = $segment[$key];
$list[] = $temp;
} else {
$temp['time'] = $val;
$temp['busy'] = 0;
$temp['segment'] = $segment[$key];
$list[] = $temp;
}
}
} else {
foreach ($time as $key => $val) {
$temp['time'] = $val;
$temp['busy'] = 0;
$temp['segment'] = $segment[$key];
$list[] = $temp;
}
}
//预约日期表
for ($i = 0; $i <= 14; $i++) {
$data[] = date('Y-m-d', strtotime('+'.$i.' days'));
}
return $this->success('ok', compact('has_appointment', 'data', 'list', 'anchor'));
}catch (\Exception $e){
$this->getError($e);
return $this->failure();
}
}
}