153 lines
5.0 KiB
PHP
153 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace app\czg\app\controller;
|
|
|
|
use app\api\validate\CourseCollectValidate;
|
|
use app\common\library\DatabaseRoute;
|
|
use app\common\model\BaseModel;
|
|
use think\facade\Db;
|
|
use app\common\controller\Frontend;
|
|
|
|
class CourseCollectController extends Frontend
|
|
{
|
|
|
|
protected array $noNeedPermission = [];
|
|
|
|
public function initialize(): void
|
|
{
|
|
parent::initialize();
|
|
}
|
|
|
|
private function insertOrder($params)
|
|
{
|
|
$userId = $this->getUserId();
|
|
$model = DatabaseRoute::getDb('course_collect', $userId, true);
|
|
$info = $model->where([
|
|
'user_id' => $userId,
|
|
'classify' => 3,
|
|
'course_id' => $params['courseId'],
|
|
])->order('create_time', 'desc')->limit(1)->find();
|
|
if ($info) {
|
|
// 更新记录
|
|
$model
|
|
->where([
|
|
'course_collect_id' => $info['course_collect_id'],
|
|
'user_id' => $userId,
|
|
])
|
|
->update([
|
|
'update_time' => date('Y-m-d H:i:s'),
|
|
'course_details_id' => $params['courseDetailsId'],
|
|
]);
|
|
} else {
|
|
// 插入新记录
|
|
$model->insert([
|
|
'user_id' => $userId,
|
|
'course_id' => $params['courseId'],
|
|
'course_details_id' => $params['courseDetailsId'],
|
|
'classify' => 3,
|
|
'create_time' => date('Y-m-d H:i:s'),
|
|
'update_time' => date('Y-m-d H:i:s'),
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function upGoodNum($params, $isAdd)
|
|
{
|
|
$courseDetails = $course_d = DatabaseRoute::getDb('course_details', ['course_id' => $params['courseId']]);
|
|
$courseDetails = $courseDetails->where([
|
|
'course_id' => $params['courseId'],
|
|
'course_details_id' => $params['courseDetailsId'],
|
|
])->find();
|
|
|
|
if (!$courseDetails) {
|
|
return; // 数据不存在,不处理
|
|
}
|
|
|
|
$goodNum = isset($courseDetails['good_num']) ? (int)$courseDetails['good_num'] : 0;
|
|
|
|
if ($isAdd) {
|
|
$goodNum += 1; // 点赞
|
|
} else {
|
|
$goodNum -= 1; // 取消点赞
|
|
|
|
}
|
|
|
|
if ($goodNum < 0) {
|
|
return; // 防止出现负数
|
|
}
|
|
Db::connect(DatabaseRoute::getConnection('course_details', ['course_id' => $params['courseId']], true))->name('course_details')->where([
|
|
'course_id' => $params['courseId'],
|
|
'course_details_id' => $params['courseDetailsId'],
|
|
])->update(['good_num' => $goodNum]);
|
|
}
|
|
|
|
public function insertCourseCollect()
|
|
{
|
|
$userId = $this->getUserId();
|
|
$data = $this->request->only(['classify', 'courseDetailsId', 'courseId', 'type']);
|
|
// classify 1收藏 2点赞 3订单
|
|
(new CourseCollectValidate())->scene('insertCourseCollect')->check($data);
|
|
if ($data['classify'] === 3) {
|
|
$this->insertOrder($data);
|
|
} else {
|
|
$where = [
|
|
'user_id' => $userId,
|
|
'classify' => $data['classify'],
|
|
'course_id' => $data['courseId'],
|
|
];
|
|
|
|
if ($data['classify'] === 2) {
|
|
if(empty($data['courseDetailsId'])) {
|
|
$this->error('courseDetailsId不能为空', [], -1);
|
|
}
|
|
$where[] = ['course_details_id', 'eq', $data['courseDetailsId']];
|
|
}
|
|
$model = DatabaseRoute::getDb('course_collect', $this->getUserId());
|
|
$info = $model->where($where)->find();
|
|
if ($data['type'] == 1) {
|
|
if ($info == null) {
|
|
$model = DatabaseRoute::getDb('course_collect', $this->getUserId(), true);
|
|
$model->insert([
|
|
'user_id' => $this->getUserId(),
|
|
'create_time' => getNormalDate(),
|
|
'update_time' => getNormalDate(),
|
|
'course_id' => $data['courseId'],
|
|
'course_details_id' => !empty($data['courseDetailsId'])?$data['courseDetailsId']:null,
|
|
'classify' => $data['classify'],
|
|
]);
|
|
}
|
|
}else{
|
|
if ($info) {
|
|
DatabaseRoute::getDb('course_collect', $this->getUserId(), true)->where([
|
|
'course_collect_id' => $info['course_collect_id']
|
|
])->delete();
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($data['classify'] == 2) {
|
|
$this->upGoodNum($data, $data['type']);
|
|
}
|
|
|
|
$this->success();
|
|
}
|
|
|
|
|
|
// app查询收藏短剧信息
|
|
public function selectByUserId()
|
|
{
|
|
$get = $this->request->get();
|
|
return $this->ApiDataReturn(\app\api\model\Course::selectByUserId($get, $this->auth->user_id));
|
|
|
|
}
|
|
|
|
|
|
// 我的追剧和我的喜欢数量
|
|
public function collectVideoSummary()
|
|
{
|
|
return $this->ApiDataReturn(\app\api\model\Course::collectVideoSummary($this->auth->user_id));
|
|
}
|
|
|
|
|
|
|
|
} |