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.

245 lines
8.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Models;
use Illuminate\Support\Facades\Log;
use OwenIt\Auditing\Models\Audit;
class CourseSign extends SoftDeletesModel
{
protected $casts = ['file_ids' => 'json', 'fee_file_ids' => 'json', 'data' => 'json', 'change_data' => 'json'];
protected $appends = ['files', 'fee_files', 'status_text', 'fee_status_text'];
public static $intToString = [
'status' => [
0 => '待审核',
1 => '审核通过',
2 => '审核不通过',
3 => '备选',
4 => '已取消',
5 => '主动放弃',
6 => '黑名单'
],
'fee_status' => [
0 => '未缴费',
1 => '缴费成功',
2 => '缴费失败',
3 => '待确认'
],
];
public function getStatusTextAttribute($value)
{
return self::$intToString['status'][$this->status] ?? '';
}
public function getFeeStatusTextAttribute($value)
{
return self::$intToString['fee_status'][$this->fee_status] ?? '';
}
public function getFilesAttribute($value)
{
if (empty($this->file_ids))
return [];
return Upload::whereIn('id', $this->file_ids)->get();
}
public function getFeeFilesAttribute($value)
{
if (empty($this->fee_file_ids))
return [];
return Upload::whereIn('id', $this->fee_file_ids)->get();
}
public function course()
{
return $this->hasOne(Course::class, 'id', 'course_id');
}
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
/**
* 第三方日志记录
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function thirdAppointmentLogs()
{
return $this->hasMany(ThirdAppointmentLog::class, 'course_sign_id', 'id');
}
/**
* 指定时间内的报名信息(未去重)
*/
public static function courseSignsTotal($start_date, $end_date, $status = null, $course_ids = null, $area = null, $retList = false)
{
$total = CourseSign::whereDate('created_at', '>=', $start_date)
->whereDate('created_at', '<=', $end_date)
->whereHas('user', function ($query) use ($area) {
if (isset($area)) {
if ($area == '苏州市外') {
$allArea = ParameterDetail::where('parameter_id', 5)->get();
$query->whereNotIn('company_area', $allArea->pluck('value'));
} else {
$query->where('company_area', $area);
}
}
})->where(function ($query) use ($status, $course_ids) {
if (isset($status)) {
$query->where('status', $status);
}
if (isset($course_ids)) {
$query->whereIn('course_id', $course_ids);
}
})->whereNotIn('status', [4, 5]);
if ($retList) {
// 返回列表
return $total->get();
} else {
// 返回统计数据
return $total->count();
}
}
/**
* 指定时间内的报名信息(去重)
*/
public static function courseSignsTotalByUnique($start_date, $end_date, $status = null, $course_ids = null, $area = null, $retList = false)
{
$courseSignByType = CourseSign::whereDate('created_at', '>=', $start_date)
->whereDate('created_at', '<=', $end_date)
->whereHas('user', function ($query) use ($area) {
if ($area) {
if ($area == '苏州市外') {
$allArea = ParameterDetail::where('parameter_id', 5)->get();
$query->whereNotIn('company_area', $allArea->pluck('value'));
} else {
$query->where('company_area', $area);
}
}
})->where(function ($query) use ($status, $course_ids) {
if (isset($status)) {
$query->where('status', $status);
}
if (isset($course_ids)) {
$query->whereIn('course_id', $course_ids);
}
})->whereNotIn('status', [4, 5])
->get();
$user = User::whereIn('id', $courseSignByType->pluck('user_id'))->distinct('mobile');
if ($retList) {
// 列表
return $user->get();
} else {
// 统计数据
return $user->count();
}
}
/**
* 指定时间内的被投企业
*/
public static function yhInvested($start_date, $end_date, $retList = false)
{
$courseSignByType = CourseSign::whereDate('created_at', '>=', $start_date)
->whereDate('created_at', '<=', $end_date)
->whereNotIn('status', [4, 5])
->get();
$list = Company::whereHas('users', function ($query) use ($courseSignByType) {
$query->whereIn('id', $courseSignByType->pluck('user_id'));
})->where('is_yh_invested', 1)->get();
if ($retList) {
// 返回列表
return $list;
} else {
// 返回统计数据
return $list->count();
}
}
/**
* 跟班学员(统计或列表)
* @param string $start_date 开始日期
* @param string $end_date 结束日期
* @param array|null $course_ids 课程ID数组不传则统计所有课程
* @param bool $retList 是否返回列表false返回数量true返回列表
* @return int|\Illuminate\Database\Eloquent\Collection
*/
public static function ganbu($start_date, $end_date, $course_ids = null, $retList = false)
{
$courseSignsForGanbu = self::whereDate('created_at', '>=', $start_date)
->whereDate('created_at', '<=', $end_date)
->where(function ($query) use ($course_ids) {
if ($course_ids) {
$query->whereIn('course_id', $course_ids);
}
})->whereNotIn('status', [4, 5])
->whereHas('user', function ($query) {
$query->where('from', '跟班学员');
})->get();
if ($retList) {
return User::with('company')->whereIn('id', $courseSignsForGanbu->pluck('user_id'))->get();
} else {
return User::whereIn('id', $courseSignsForGanbu->pluck('user_id'))->count();
}
}
/**
* 入学后上市公司数量(在指定时间范围内报名的学员所在公司中,在入学后上市的公司数量)
* @param string $start_date 开始日期
* @param string $end_date 结束日期
* @param array|null $course_ids 课程ID数组不传则统计所有课程
* @param bool $retList 是否返回列表false返回数量true返回列表
* @return int|array
*/
public static function companyMarketAfterEnrollment($start_date, $end_date, $course_ids = null, $retList = false)
{
$courseSignsForStock = self::whereDate('created_at', '>=', $start_date)
->whereDate('created_at', '<=', $end_date)
->where(function ($query) use ($course_ids) {
if ($course_ids) {
$query->whereIn('course_id', $course_ids);
}
})->whereNotIn('status', [4, 5])
->with('user.company')
->get();
$companiesAfterEnrollment = [];
foreach ($courseSignsForStock as $sign) {
if ($sign->user && $sign->user->company && $sign->user->company->company_market == 1) {
$signDate = \Carbon\Carbon::parse($sign->created_at)->format('Y-m-d');
$stockDate = $sign->user->company->stock_date;
if ($stockDate && $stockDate >= $signDate) {
$companyId = $sign->user->company->id;
if (!isset($companiesAfterEnrollment[$companyId])) {
$companiesAfterEnrollment[$companyId] = [
'company' => $sign->user->company,
'first_sign_date' => $signDate,
'users' => [],
];
}
if ($retList) {
$companiesAfterEnrollment[$companyId]['users'][] = $sign->user;
}
}
}
}
if ($retList) {
return $companiesAfterEnrollment;
} else {
return count($companiesAfterEnrollment);
}
}
}