|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use EasyWeChat\Factory;
|
|
|
|
|
|
|
|
|
|
class Visit extends SoftDeletesModel
|
|
|
|
|
{
|
|
|
|
|
// 审核状态常量
|
|
|
|
|
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; // 全天
|
|
|
|
|
|
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
|
|
|
|
|
|
protected $appends = ['type_text', 'audit_status_text', 'file_detail', 'vehicle_images_detail', 'follw_people_total'];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'follw_people' => 'json',
|
|
|
|
|
'cars' => 'json',
|
|
|
|
|
'file' => 'json',
|
|
|
|
|
'person_no' => 'json',
|
|
|
|
|
'car_no' => 'json',
|
|
|
|
|
'vehicle_images' => 'json'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function getFileDetailAttribute()
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->file)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
return Upload::whereIn('id', $this->file)->get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取随访人数
|
|
|
|
|
*/
|
|
|
|
|
public function getFollwPeopleTotalAttribute()
|
|
|
|
|
{
|
|
|
|
|
return $this->follw_people ? count($this->follw_people) : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getVehicleImagesDetailAttribute()
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->vehicle_images)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
return Upload::whereIn('id', $this->vehicle_images)->get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTypeTextAttribute()
|
|
|
|
|
{
|
|
|
|
|
$array = [1 => '普通访客', 2 => '施工访客', 3 => '物流访客'];
|
|
|
|
|
return $array[$this->type] ?? '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAuditStatusTextAttribute()
|
|
|
|
|
{
|
|
|
|
|
$array = ['-1' => '待学习', '0' => '待审核', '1' => '通过', '2' => '驳回', '3' => '已进厂', '4' => '已离厂', '5' => '已取消'];
|
|
|
|
|
return $array[$this->audit_status] ?? '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function visitTime()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(VisitTime::class, 'id', 'visit_time_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function admin()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(Admin::class, 'id', 'admin_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function accompany()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(Admin::class, 'id', 'accompany_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function acceptAdmin()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(Admin::class, 'id', 'accept_admin_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function acceptGoodsAdmin()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(Admin::class, 'id', 'accept_goods_admin_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function acceptAdminSignFile()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(Upload::class, 'id', 'accept_admin_sign');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function visitArea()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(VisitArea::class, 'id', 'visit_area_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function audit()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(VisitAudit::class, 'visit_id', 'id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function logs()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(VisitLog::class, 'visit_id', 'id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function gateLogs()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(GateLog::class, 'code', 'code');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 小程序订阅消息
|
|
|
|
|
public static function subMsg($template_id, $openid, $data, $id)
|
|
|
|
|
{
|
|
|
|
|
$config = [
|
|
|
|
|
'app_id' => \config('app.wechat_appid'),
|
|
|
|
|
'secret' => \config('app.wechat_secret')
|
|
|
|
|
];
|
|
|
|
|
$app = Factory::miniProgram($config);
|
|
|
|
|
$data = [
|
|
|
|
|
'template_id' => $template_id,
|
|
|
|
|
'touser' => $openid,
|
|
|
|
|
'page' => 'pages/visit/detail?id=' . $id,
|
|
|
|
|
'data' => $data
|
|
|
|
|
];
|
|
|
|
|
return $app->subscribe_message->send($data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 首页统计
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|