Files
2024-09-27 01:32:49 +08:00

108 lines
2.8 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/4/20
* Time: 10:23
*/
namespace app\api\model;
use app\lib\exception\ParamException;
use think\Db;
use think\facade\Log;
use think\Model;
class UserCollect extends Model
{
protected $table = 'user_collect_school';
/*
* 收藏院校
* @param userID int 用户id
* @param data 收藏内容
* @return | bool
* */
public static function addUserCollectSch($userID, $data)
{
$data['user_id'] = $userID;
$data['add_time'] = time();
$result = self::insertGetId($data);
return $result;
}
/*
* 移除收藏院校
* */
public static function delMyCollectSch($id, $userID)
{
$result = self::where([['id', '=', $id], ['user_id', '=', $userID]])->update(['is_collect' => 2]);
return $result;
}
/*
* 检查是否已收藏该院校
* */
public static function checkMyCollectSch($rec_id, $userID)
{
$result = self::where([['rec_id', '=', $rec_id], ['user_id', '=', $userID],['is_collect','=',1]])->count();
if($result){
throw new ParamException([
'code' => 401,
'msg' => '已收藏过该院校',
'errorCode' => 10001
]);
}
return $result;
}
/*
* 根据用户id查询用户收藏的全部学校的分数段id
* */
public static function getUserCollect($userID)
{
$result = self::field('rec_id')->where([['user_id', '=', $userID], ['is_collect', '=', 1]])->select()->toArray();
if ($result) {
$result = array_column($result, 'rec_id');
}
return $result;
}
/*
* 查询我的收藏院校
* */
public static function getMyCollectSch($data,$userID,$page = 1, $limit = 10)
{
$where [] = empty($data['score'])?
[['user_id','=',$userID],['userPro','=',$data['userPro']],['is_collect','=',1]]:[['user_id','=',$userID],['userPro','=',$data['userPro']],['score','=',$data['score']],['is_collect','=',1]];
//获取配置表年份
$year = config('setsub.year');
//统计收藏学校数量
$count = self::where($where)->count();
//查询收藏数据
$result = self::where($where)
->page($page, $limit)->select();
empty($result) ? $msg = '暂无数据!' : $msg = '查询成功!';
//分页数据
$info = [
'limit' => $limit,
'page_current' => $page,
'page_sum' => ceil($count / $limit),
];
//组合返回
$list = [
'msg' => $msg,
'count' => $count,
'info' => $info,
'data' => $result,
];
return $list;
}
}