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.

184 lines
5.1 KiB

11 months ago
<?php
namespace App\Models;
11 months ago
use EasyWeChat\Factory;
11 months ago
class Visit extends SoftDeletesModel
{
10 months ago
// 审核状态常量
const AUDIT_STATUS_PENDING_STUDY = -1; // 待学习
const AUDIT_STATUS_PENDING = 0; // 待审核
const AUDIT_STATUS_APPROVED = 1; // 通过
const AUDIT_STATUS_REJECTED = 2; // 驳回
const AUDIT_STATUS_ENTERED = 3; // 已进厂
const AUDIT_STATUS_LEFT = 4; // 已离厂
const AUDIT_STATUS_CANCELLED = 5; // 已取消
// 访客类型常量
const TYPE_VISITOR = 1; // 普通访客
const TYPE_VISITOR_CAR = 2; // 访客车辆
const TYPE_LOGISTICS_CAR = 3; // 物流车辆
// 证件类型常量
const CREDENT_ID_CARD = 1; // 身份证
const CREDENT_PASSPORT = 2; // 护照
// 用餐类型常量
const MEAL_TYPE_BREAKFAST = 1; // 早餐
const MEAL_TYPE_LUNCH = 2; // 午餐
const MEAL_TYPE_DINNER = 3; // 晚餐
const MEAL_TYPE_ALL_DAY = 4; // 全天
11 months ago
protected $guarded = ['id'];
9 months ago
protected $appends = ['type_text', 'audit_status_text', 'file_detail', 'vehicle_images_detail', 'follw_people_total'];
11 months ago
protected $casts = [
11 months ago
'follw_people' => 'json',
'cars' => 'json',
'file' => 'json',
'person_no' => 'json',
11 months ago
'car_no' => 'json',
'vehicle_images' => 'json'
11 months ago
];
11 months ago
public function getFileDetailAttribute()
11 months ago
{
11 months ago
if (empty($this->file)) {
return [];
}
return Upload::whereIn('id', $this->file)->get();
11 months ago
}
9 months ago
/**
* 获取随访人数
*/
public function getFollwPeopleTotalAttribute()
{
return $this->follw_people ? count($this->follw_people) : 0;
}
11 months ago
public function getVehicleImagesDetailAttribute()
{
if (empty($this->vehicle_images)) {
return [];
}
return Upload::whereIn('id', $this->vehicle_images)->get();
}
11 months ago
public function getTypeTextAttribute()
11 months ago
{
11 months ago
$array = [1 => '普通访客', 2 => '施工访客', 3 => '物流访客'];
return $array[$this->type] ?? '';
11 months ago
}
11 months ago
public function getAuditStatusTextAttribute()
11 months ago
{
11 months ago
$array = ['-1' => '待学习', '0' => '待审核', '1' => '通过', '2' => '驳回', '3' => '已进厂', '4' => '已离厂', '5' => '已取消'];
return $array[$this->audit_status] ?? '';
11 months ago
}
11 months ago
public function visitTime()
11 months ago
{
11 months ago
return $this->hasOne(VisitTime::class, 'id', 'visit_time_id');
11 months ago
}
11 months ago
public function admin()
{
11 months ago
return $this->hasOne(Admin::class, 'id', 'admin_id');
11 months ago
}
public function accompany()
{
11 months ago
return $this->hasOne(Admin::class, 'id', 'accompany_id');
11 months ago
}
11 months ago
public function acceptAdmin()
11 months ago
{
11 months ago
return $this->hasOne(Admin::class, 'id', 'accept_admin_id');
11 months ago
}
11 months ago
public function acceptGoodsAdmin()
11 months ago
{
11 months ago
return $this->hasOne(Admin::class, 'id', 'accept_goods_admin_id');
11 months ago
}
11 months ago
public function acceptAdminSignFile()
11 months ago
{
11 months ago
return $this->hasOne(Upload::class, 'id', 'accept_admin_sign');
11 months ago
}
11 months ago
public function visitArea()
11 months ago
{
11 months ago
return $this->hasOne(VisitArea::class, 'id', 'visit_area_id');
11 months ago
}
11 months ago
public function audit()
11 months ago
{
11 months ago
return $this->hasMany(VisitAudit::class, 'visit_id', 'id');
11 months ago
}
11 months ago
public function logs()
11 months ago
{
11 months ago
return $this->hasMany(VisitLog::class, 'visit_id', 'id');
11 months ago
}
11 months ago
public function gateLogs()
11 months ago
{
11 months ago
return $this->hasMany(GateLog::class, 'code', 'code');
11 months ago
}
11 months ago
// 小程序订阅消息
11 months ago
public static function subMsg($template_id, $openid, $data, $id)
11 months ago
{
11 months ago
$config = [
'app_id' => \config('app.wechat_appid'),
11 months ago
'secret' => \config('app.wechat_secret')
11 months ago
];
11 months ago
$app = Factory::miniProgram($config);
$data = [
'template_id' => $template_id,
'touser' => $openid,
'page' => 'pages/visit/detail?id=' . $id,
'data' => $data
11 months ago
];
11 months ago
return $app->subscribe_message->send($data);
11 months ago
}
9 months ago
/**
* 首页统计
*/
public static function visitHome($type, $startDate, $endDate, $auditStatus)
{
$visit = self::visitQuery($type, $startDate, $endDate, $auditStatus);
$visitTotal = $visit->count();
$follwPeopleTotal = $visit->sum('follw_people_total');
return $visitTotal + $follwPeopleTotal;
}
/**
* 首页统计
*/
public static function visitQuery($type, $startDate, $endDate, $auditStatus)
{
$visit = Visit::where(function ($query) use ($type, $startDate, $endDate, $auditStatus) {
if ($type) {
$query->where('type', $type);
}
if ($auditStatus) {
$query->whereIn('audit_status', $auditStatus);
}
if ($startDate) {
$query->where('date', '>=', $startDate);
}
if ($endDate) {
$query->where('date', '<=', $endDate);
}
})->get();
return $visit;
}
11 months ago
}