Commit eb46e0a0 authored by wangzhengwen's avatar wangzhengwen

搜索,排行榜,直播课

parent f86f7eca
......@@ -85,7 +85,7 @@ class Course extends BaseController
}
$data = $request->param();
$where = ['status'=>3,'is_sell'=>1,'is_del'=>0];
$where = ['status'=>3,'is_sell'=>1,'is_del'=>0,'is_zb'=>1];
$list = CourseModel::where($where)
->where('title', 'like', '%' . $data['searchKeyWords'] . '%')
->field('id,thumb,title,createtime,description,price,content')
......@@ -113,8 +113,9 @@ class Course extends BaseController
$page = $request->param('page/d', 1);
$pageSize = $request->param('pageSize/d', 10);
$is_zb = $request->param('is_zb',0);
$list = (new CourseModel())->getCourseListSecond($data['category_id'],$data['second_category_id'],$page,$pageSize,$data['searchKeyWords'] ?? null);
$list = (new CourseModel())->getCourseListSecond($data['category_id'],$data['second_category_id'],$page,$pageSize,$data['searchKeyWords'] ?? null,$is_zb);
return $this->returnMsg('success',1,$list);
......
......@@ -40,7 +40,8 @@ class CourseProgress extends BaseController
'id' => $data['course_id'],
'status' => 3,
'is_sell' => 1,
'is_del' => 0
'is_del' => 0,
'is_zb'=>0
])->find();
if (!$course) {
......@@ -123,7 +124,8 @@ class CourseProgress extends BaseController
'id' => $data['course_id'],
'status' => 3,
'is_sell' => 1,
'is_del' => 0
'is_del' => 0,
'is_zb'=>0
])->find();
if (!$course) {
......
<?php
namespace app\api\controller;
use app\BaseController;
use think\facade\Cache;
use think\facade\Db;
class Search extends BaseController
{
public function searchPlus()
{
$keyword = $this->request->param('searchKeyWords', '');
$page = $this->request->param('page/d', 1);
$pageSize = $this->request->param('pageSize/d', 10);
$cacheKey = 'search_plus_' . md5($keyword . '_' . $page . '_' . $pageSize);
$cacheData = Cache::get($cacheKey);
if ($cacheData !== null) {
return $this->returnMsg('success', 1, $cacheData);
}
$currentTime = time();
// 构建UNION ALL查询
$baseQuery = '(SELECT a.id, a.title, a.description, IFNULL(b.fileurl, "") as thumb, a.createtime, c.nickname as username, "kc" AS source
FROM fj_course a
LEFT JOIN fj_system_upload_file b ON a.thumb = b.fileid
LEFT JOIN fj_course_teacher c ON a.teacher_id = c.id
WHERE a.is_sell = 1 AND a.is_del = 0 AND a.status = 3 AND a.is_zb = 0';
if (!empty($keyword)) {
$baseQuery .= ' AND a.title LIKE "%'.addslashes($keyword).'%"';
}
$baseQuery .= ' UNION ALL
SELECT a.id, a.title, a.description, IFNULL(b.fileurl, "") as thumb, a.createtime, "" as username, "xm" AS source
FROM fj_project a
LEFT JOIN fj_system_upload_file b ON a.thumb = b.fileid
WHERE a.sh_status = 2 AND a.status = 1 AND a.put_end_time > '.$currentTime;
if (!empty($keyword)) {
$baseQuery .= ' AND a.title LIKE "%'.addslashes($keyword).'%"';
}
$baseQuery .= ' UNION ALL
SELECT a.id, a.title, a.description, IFNULL(b.fileurl, "") as thumb, a.createtime, "" as username, "zs" AS source
FROM fj_cert a
LEFT JOIN fj_system_upload_file b ON a.thumb_id = b.fileid
WHERE a.is_sell = 1 AND a.is_del = 0';
if (!empty($keyword)) {
$baseQuery .= ' AND a.title LIKE "%'.addslashes($keyword).'%"';
}
$baseQuery .= ' UNION ALL
SELECT a.id, a.title, a.description, IFNULL(b.fileurl, "") as thumb, a.createtime, c.username as username, "xxzl" AS source
FROM fj_study_information a
LEFT JOIN fj_system_upload_file b ON a.thumb = b.fileid
LEFT JOIN fj_user c ON a.user_id = c.id
WHERE a.is_sell = 1 AND a.deletetime IS NULL AND a.status = 2';
if (!empty($keyword)) {
$baseQuery .= ' AND a.title LIKE "%'.addslashes($keyword).'%"';
}
$baseQuery .= ') AS combined';
$query = Db::table($baseQuery)->order('createtime', 'desc');
$list = $query->paginate([
'list_rows' => $pageSize,
'page' => $page,
]);
$paginateData = $list->toArray();
Cache::set($cacheKey, $paginateData, 300);
return $this->returnMsg('success',1,$paginateData);
}
public function searchCourse()
{
$keyword = $this->request->param('searchKeyWords', '');
$page = $this->request->param('page/d', 1);
$pageSize = $this->request->param('pageSize/d', 10);
$cacheKey = 'search_course_' . md5($keyword . '_' . $page . '_' . $pageSize);
$cacheData = Cache::get($cacheKey);
if ($cacheData !== null) {
return $this->returnMsg('success', 1, $cacheData);
}
$query = Db::table('fj_course a')
->leftJoin('fj_system_upload_file b', 'a.thumb = b.fileid')
->leftJoin('fj_course_teacher c', 'a.teacher_id = c.id')
->field('a.id, a.title, a.description, IFNULL(b.fileurl, "") as thumb, a.createtime, c.nickname as username, "kc" AS source')
->where('a.is_sell', 1)
->where('a.is_del', 0)
->where('a.status', 3)
->where('a.is_zb', 0)
->order('a.createtime', 'desc');
if (!empty($keyword)) {
$query->where('a.title', 'like', "%{$keyword}%");
}
$list = $query->paginate([
'list_rows' => $pageSize,
'page' => $page,
]);
$paginateData = $list->toArray();
Cache::set($cacheKey, $paginateData, 300);
return $this->returnMsg('success', 1, $paginateData);
}
public function searchStudy()
{
$keyword = $this->request->param('searchKeyWords', '');
$page = $this->request->param('page/d', 1);
$pageSize = $this->request->param('pageSize/d', 10);
$cacheKey = 'search_study_' . md5($keyword . '_' . $page . '_' . $pageSize);
$cacheData = Cache::get($cacheKey);
if ($cacheData !== null) {
return $this->returnMsg('success', 1, $cacheData);
}
$query = Db::table('fj_study_information a')
->leftJoin('fj_system_upload_file b', 'a.thumb = b.fileid')
->leftJoin('fj_user c', 'a.user_id = c.id')
->field('a.id, a.title, a.description, IFNULL(b.fileurl, "") as thumb, a.createtime, c.username as username, "xxzl" AS source')
->where('a.is_sell', 1)
->whereNull('a.deletetime')
->where('a.status', 2)
->order('a.createtime', 'desc');
if (!empty($keyword)) {
$query->where('a.title', 'like', "%{$keyword}%");
}
$list = $query->paginate([
'list_rows' => $pageSize,
'page' => $page,
]);
$paginateData = $list->toArray();
Cache::set($cacheKey, $paginateData, 300);
return $this->returnMsg('success', 1, $paginateData);
}
public function searchProject()
{
$keyword = $this->request->param('searchKeyWords', '');
$page = $this->request->param('page/d', 1);
$pageSize = $this->request->param('pageSize/d', 10);
$currentTime = time();
$cacheKey = 'search_project_' . md5($keyword . '_' . $page . '_' . $pageSize);
$cacheData = Cache::get($cacheKey);
if ($cacheData !== null) {
return $this->returnMsg('success', 1, $cacheData);
}
$query = Db::table('fj_project a')
->leftJoin('fj_system_upload_file b', 'a.thumb = b.fileid')
->field('a.id, a.title, a.description, IFNULL(b.fileurl, "") as thumb, a.createtime, "" as username, "xm" AS source')
->where('a.sh_status', 2)
->where('a.status', 1)
->where('a.put_end_time', '>', $currentTime)
->order('a.createtime', 'desc');
if (!empty($keyword)) {
$query->where('a.title', 'like', "%{$keyword}%");
}
$list = $query->paginate([
'list_rows' => $pageSize,
'page' => $page,
]);
$paginateData = $list->toArray();
Cache::set($cacheKey, $paginateData, 300);
return $this->returnMsg('success', 1, $paginateData);
}
public function searchCert()
{
$keyword = $this->request->param('searchKeyWords', '');
$page = $this->request->param('page/d', 1);
$pageSize = $this->request->param('pageSize/d', 10);
$cacheKey = 'search_cert_' . md5($keyword . '_' . $page . '_' . $pageSize);
$cacheData = Cache::get($cacheKey);
if ($cacheData !== null) {
return $this->returnMsg('success', 1, $cacheData);
}
$query = Db::table('fj_cert a')
->leftJoin('fj_system_upload_file b', 'a.thumb_id = b.fileid')
->field('a.id, a.title, a.description, IFNULL(b.fileurl, "") as thumb, a.createtime, "" as username, "zs" AS source')
->where('a.is_sell', 1)
->where('a.is_del', 0)
->order('a.createtime', 'desc');
if (!empty($keyword)) {
$query->where('a.title', 'like', "%{$keyword}%");
}
$list = $query->paginate([
'list_rows' => $pageSize,
'page' => $page,
]);
$paginateData = $list->toArray();
Cache::set($cacheKey, $paginateData, 300);
return $this->returnMsg('success', 1, $paginateData);
}
/**
* 清除搜索缓存
*/
public function clearSearchCache()
{
$pattern = 'search_plus_*';
Cache::tag('search_plus')->clear();
return $this->returnMsg('缓存清除成功', 1);
}
}
\ No newline at end of file
......@@ -216,6 +216,17 @@ class User extends BaseController
}
public function rankUserExperience(Request $request)
{
$limit = $request->param('limit/d', 1);
$type = $request->param('type', 'all');
$list = UserService::rankUserExperience($limit,$type);
return $this->returnMsg('success',1,$list);
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@
namespace app\api\controller\project;
use app\api\service\ProjectService;
use app\api\service\UserService;
use app\api\validate\ProjectValidate;
use app\BaseController;
......@@ -164,6 +165,19 @@ class Project extends BaseController
return $this->returnMsg('success', 1, $tags);
}
//获取项目完成率排行
public function rankProjectCompletionList(Request $request)
{
$limit = $request->param('limit/d', 1);
$type = $request->param('type', 'all');
$list = ProjectService::getUserProjectCompletionRanking($limit,$type);
return $this->returnMsg('success',1,$list);
}
}
\ No newline at end of file
......@@ -196,4 +196,62 @@ class ProjectService
trace('项目招募超时处理任务结束', 'project');
return $result;
}
/**
* 获取项目完成率
* @param $limit
* @param $timeRange 时间范围: 'all'-全部, 'month'-本月, 'week'-本周, 'day'-今天, 'three_month'-近三月
* @return mixed
*/
public static function getUserProjectCompletionRanking($limit = 10, $timeRange = 'all')
{
// 查询接单总数和完成数量
$query = Db::name('project_put')
->field('user_id,
COUNT(*) as total_puts,
SUM(CASE WHEN complete_status = 3 THEN 1 ELSE 0 END) as completed_projects,
ROUND((SUM(CASE WHEN complete_status = 3 THEN 1 ELSE 0 END) / COUNT(*)) * 100, 2) as completion_rate')
->where('status', 2) // 只统计接单成功的记录
->group('user_id')
->having('total_puts > 0') // 确保分母不为零
->order('completion_rate', 'DESC')
->limit($limit);
// 按时间范围筛选
switch ($timeRange) {
case 'month':
$query->whereTime('put_time', 'month');
break;
case 'week':
$query->whereTime('put_time', 'week');
break;
case 'day':
$query->whereTime('put_time', 'today');
break;
case 'three_month':
$threeMonthAgo = strtotime('-3 months');
$query->where('put_time', '>=', $threeMonthAgo);
break;
}
$ranking = $query->select()->toArray();
// 获取用户信息
if (!empty($ranking)) {
$userIds = array_column($ranking, 'user_id');
$users = Db::name('user')
->whereIn('id', $userIds)
->where('is_del', 0)
->column('username,realname,headico', 'id');
foreach ($ranking as &$item) {
$item['user_info'] = $users[$item['user_id']] ?? [];
// 确保完成率有值,如果没有则为0
$item['completion_rate'] = $item['completion_rate'] ?? 0;
}
}
return $ranking;
}
}
\ No newline at end of file
......@@ -35,5 +35,37 @@ class UserService
return User::where('id',\request()->userId)->update([$filed=>$value]);
}
public static function rankUserExperience($limit = 10, $timeRange = 'all')
{
// 基础查询
$query = userModel
::field('id, username, realname, headico, experience')
->with(['headico'])
->where('is_del', 0)
->where('experience','>',0)
->order('experience', 'DESC')
->limit($limit);
// 按时间范围筛选(如果需要基于注册时间筛选)
switch ($timeRange) {
case 'month':
$query->whereTime('register_time', 'month');
break;
case 'week':
$query->whereTime('register_time', 'week');
break;
case 'day':
$query->whereTime('register_time', 'today');
break;
case 'three_month':
$threeMonthAgo = strtotime('-3 months');
$query->where('register_time', '>=', $threeMonthAgo);
break;
}
$ranking = $query->select()->toArray();
return $ranking;
}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@
namespace app\model;
use app\api\service\UtilService;
use \app\model\CertCategory;
use app\model\CertCategory;
use app\model\system\SystemUploadFile;
use think\facade\Db;
use think\Model;
......@@ -130,7 +130,8 @@ class Cert extends Model
$query->whereIn('cate_id', array_merge([$first_category_id], $son_ids));
}
// 如果一级分类为0,则不限制分类(查询全部)
if ($searchKeyWords)
{
$query->where('title', 'like', '%' . $searchKeyWords . '%');
......
......@@ -30,7 +30,8 @@ class Course extends Model
'status' => 3,
'is_sell' => 1,
'is_del' => 0,
'is_tj'=>1
'is_tj'=>1,
'is_zb'=>0
]);
if (!empty($category_id)) {
......@@ -61,7 +62,8 @@ class Course extends Model
'status' => 3,
'is_sell' => 1,
'is_del' => 0,
'is_hot' => 1
'is_hot' => 1,
'is_zb'=>0
]);
if (!empty($category_id)) {
......@@ -111,9 +113,9 @@ class Course extends Model
}
// 6.20 两级分类查询
public function getCourseListSecond($first_category_id, $second_category_id, $page, $pageSize, $searchKeyWords = null)
public function getCourseListSecond($first_category_id, $second_category_id, $page, $pageSize, $searchKeyWords = null,$is_zb=0)
{
$where = ['status' => 3, 'is_sell' => 1, 'is_del' => 0];
$where = ['status' => 3, 'is_sell' => 1, 'is_del' => 0,'is_zb'=>$is_zb];
$query = self::where($where);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment