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

193 lines
6.0 KiB

<?php
namespace App\Models;
use App\Jobs\CancelAppointCar;
use App\Jobs\CancelAppointMeet;
use App\Jobs\SendAppointCar;
use App\Notifications\MeetNotify;
use App\Repositories\DoorRepository;
use App\Repositories\MeetRepository;
use Illuminate\Support\Facades\Notification;
class Appointment extends SoftDeletesModel
{
protected $appends = ['status_text', 'h5_url', 'site_detail'];
public static $intToString = [
'status' => [
0 => '待审核',
1 => '通过',
2 => '不通过',
3 => '取消',
4 => '失败'
]
];
public function getStatusTextAttribute($value)
{
return self::$intToString['status'][$this->status] ?? '';
}
public function getSiteDetailAttribute($value)
{
$array = explode(',', $this->attributes['site']);
return AppointmentConfig::whereIn('id', $array)->get();
}
public function getH5UrlAttribute($value)
{
return config('config.url') . '/user/h5-show?code=' . $this->code;
}
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
public function appointmentAccompany()
{
return $this->hasMany(AppointmentAccompany::class, 'appointment_id', 'id');
}
/**
* 第三方日志记录
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function thirdAppointmentLogs()
{
return $this->hasMany(ThirdAppointmentLog::class, 'appointment_id', 'id');
}
/**
* 判断对应地点是否可以预约
* 没有被被占用
*/
public static function checkAppointment($startTime, $endTime, $site, $userId = 0)
{
// 开始时间加15分钟
$startTime = date('Y-m-d H:i:s', strtotime($startTime) - 15 * 60);
$endTime = date('Y-m-d H:i:s', strtotime($endTime) + 15 * 60);
$appointments = Appointment::where('start_time', '<', $endTime)
->where('end_time', '>', $startTime)
->where('site', $site)
->where('status', 1)
->where(function ($query) use ($userId) {
if ($userId) {
$query->where('user_id', $userId);
}
})->orderBy('id', 'asc')
->first();
return $appointments;
}
/**
* 发送第三方取消预约请求
*/
public static function sendCancelAppoin($model)
{
$meetIds = explode(',', $model->meet_id);
$meetIds = array_filter($meetIds);
if ($meetIds) {
foreach ($meetIds as $meetId) {
dispatch((new CancelAppointMeet($model, $meetId)));
}
}
// 取消预约车位
if ($model->plate) {
$plateArray = explode(',', $model->plate);
foreach ($plateArray as $plate) {
dispatch((new CancelAppointCar($model, $plate)));
}
}
return true;
}
/**
* 同步预约门禁
*/
public function appointDoor($appointmentModel, $appointmentConfig)
{
// 所有门禁数据合并
$doors = $appointmentConfig->pluck('door')->filter();
if ($doors->isEmpty()) {
return true;
}
$result = false;
$doorRepository = new DoorRepository();
foreach ($appointmentConfig as $config) {
$door = json_decode($config->door, true);
$result = $doorRepository->generateEmpAuthorSet1($appointmentModel, $door, $out);
}
if ($result) {
// 发通知
if ($appointmentModel->user_id) {
// 小程序用户
$data = ['appointment_id' => $appointmentModel->id];
Notification::send(User::find($appointmentModel->user_id), new MeetNotify($data));
} else {
// 自由预约
// 预约成功发送入场二维码通知
// 短信签名
$smsSign = Config::getValueByKey('sms_sign');
$url = \config('app.url') . '/user/h5-show?code=' . $appointmentModel->code;
$time = date('Y-m-d', strtotime($appointmentModel->start_time)) . '至' . date('Y-m-d', strtotime($appointmentModel->end_time));
$content = "{$smsSign}您已成功预约{$appointmentConfig->pluck('name')->implode('、')},预约时间:{$time},预约二维码获取地址:{$url}";
// 给主预约人发信息
ymSms($appointmentModel->mobile, $content);
}
}
return $result;
}
/**
* 预约会议
*/
public function appointMeet($appointmentModel, $appointmentConfig)
{
// 所有会议室数据合并
$rooms = $appointmentConfig->pluck('room')->filter();
if ($rooms->isEmpty()) {
return true;
}
$result = false;
$meetRepository = new MeetRepository();
foreach ($appointmentConfig as $config) {
if (empty($config->room)) {
continue;
};
// 判断是否有预约成功记录,没有就重新预约
$thirdMeetLog = $appointmentModel->thirdAppointmentLogs()->where('remark', '会议室预约')->where('finally', 1)->first();
if ($thirdMeetLog) {
continue;
}
$result = $meetRepository->appointment($appointmentModel, $config, $out);
}
return $result;
}
/**
* 发送第三方预约请求
*/
public static function sendAppoinCar($model)
{
// 预约车位
if ($model->plate) {
$plateArray = explode(',', $model->plate);
foreach ($plateArray as $plate) {
// 判断是否有有预约成功记录,没有就重新预约
$thirdPlateLog = $model->thirdAppointmentLogs()->where('plate_status', 1)->where('plate', $plate)->first();
if (empty($thirdPlateLog)) {
dispatch((new SendAppointCar($model, $plate)));
}
}
}
return true;
}
}