'json', 'partners' => 'json']; protected $appends = ['is_yh_invested_text']; public function getIsYhInvestedTextAttribute() { $array = [ 0 => '否', 1 => '是', ]; return $array[$this->is_yh_invested] ?? ''; } public function users() { return $this->hasMany(User::class, 'company_id', 'id'); } /** * 地址转经纬度 */ public static function addressTolocation($address) { $map = Config::getValueByKey('map_server'); $map = json_decode($map, true); $url = "https://restapi.amap.com/v3/geocode/geo"; $params = [ 'key' => $map['key'], 'address' => $address, ]; try { $result = httpCurl($url, 'GET', $params); $result = json_decode($result, true); if ($result['status'] == 1) { $location = $result['geocodes'][0]['location']; $location = explode(',', $location); return [ 'lng' => $location[0], 'lat' => $location[1], ]; } return [ 'lng' => null, 'lat' => null, ]; } catch (\Exception $e) { return [ 'lng' => null, 'lat' => null, ]; } } /** * 累计被投企业统计 * @param string|null $start_date 开始日期 * @param string|null $end_date 结束日期 * @param array|null $course_ids 课程ID(仅在自定义时间时生效) * @param bool $retList 是否返回列表 */ public static function yhInvestedTotal($end_date = null, $retList = false) { // 获取这些学员所在的被投企业 $companies = Company::where('is_yh_invested', 1)->get(); // 自定义时间:需要按被投时间筛选 // 筛选出被投时间在范围内的企业 $filteredCompanies = []; foreach ($companies as $company) { $projectUsers = $company->project_users ?? []; $hasValidInvestDate = false; $allInvestDatesNull = true; foreach ($projectUsers as $item) { $investDate = $item['investDate'] ?? null; // 检查是否有有效的被投时间 if ($investDate) { $allInvestDatesNull = false; // 检查被投时间是否在范围内 if ($investDate <= $end_date) { $hasValidInvestDate = true; break; // 只要有一条满足就加入 } } } // 如果有有效的被投时间在范围内,或者所有被投时间都是null,则加入结果 if ($hasValidInvestDate || $allInvestDatesNull) { $filteredCompanies[] = $company; } } $companies = collect($filteredCompanies); // 返回结果 if ($retList) { return $companies->values(); } else { return $companies->count(); } } /** * 被投企业统计 * @param string|null $start_date 开始日期 * @param string|null $end_date 结束日期 * @param array|null $course_ids 课程ID(仅在自定义时间时生效) * @param bool $retList 是否返回列表 */ public static function yhInvested($startDate = null, $endDate = null, $retList = false) { // 获取这些学员所在的被投企业 $companies = Company::where('is_yh_invested', 1)->get(); // 筛选出被投时间在范围内的企业 $filteredCompanies = []; foreach ($companies as $company) { $projectUsers = $company->project_users ?? []; foreach ($projectUsers as $item) { $investDate = $item['investDate'] ?? null; // 检查被投时间是否在范围内 if ($investDate && $investDate >= $startDate && $investDate <= $endDate) { $filteredCompanies[] = $company; break; // 只要有一条满足就加入 } } } $companies = collect($filteredCompanies); // 返回结果 if ($retList) { return $companies->values(); } else { return $companies->count(); } } /** * 被投企业统计(统计或列表)- 按年份范围统计 * @param string|null $start_date 开始日期 * @param string|null $end_date 结束日期 * @param array|null $course_ids 课程ID数组,不传则统计所有课程 * @param bool $retList 是否返回列表,false返回数量,true返回列表(包含学员、课程信息) * @return int|array */ public static function companyInvestedYear($start_date = null, $end_date = null, $retList = false) { // 计算年份范围 $years = []; if ($start_date && $end_date) { // 从开始和结束日期中提取年份范围 $startYear = (int)date('Y', strtotime($start_date)); $endYear = (int)date('Y', strtotime($end_date)); // 生成所有年份的数组 for ($year = $startYear; $year <= $endYear; $year++) { $years[] = $year; } } else { // 如果没有提供日期,使用当前年份 $years[] = (int)date('Y'); } // 获取这些公司中标记为被投的公司 $allInvestedCompanies = Company::where('is_yh_invested', 1)->get(); // 筛选出被投时间在年份范围内的企业 $companies = []; foreach ($allInvestedCompanies as $company) { $projectUsers = $company->project_users ?? []; $hasInvestInYears = false; foreach ($projectUsers as $item) { $investDate = $item['investDate'] ?? null; if ($investDate) { $investYear = (int)date('Y', strtotime($investDate)); if (in_array($investYear, $years)) { $hasInvestInYears = true; break; } } } if ($hasInvestInYears) { $companies[$company->id] = $company; } } $companies = collect($companies); // 返回结果 if ($retList) { return $companies->values(); } else { return $companies->count(); } } }