|
|
<?php
|
|
|
/**
|
|
|
* Created by PhpStorm.
|
|
|
* User: yhbr
|
|
|
* Date: 2018/9/4
|
|
|
* Time: 9:53
|
|
|
*/
|
|
|
|
|
|
namespace app\activity\model;
|
|
|
use think\Model;
|
|
|
use think\Db;
|
|
|
|
|
|
class ActivityModel extends Model
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* @param null $tId
|
|
|
* @param $nowTime
|
|
|
* @param $userId
|
|
|
* @return false|\PDOStatement|string|\think\Collection
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
* @throws \think\exception\DbException
|
|
|
*/
|
|
|
public function activityList($tId = null, $nowTime, $userId)
|
|
|
{
|
|
|
$where = [];
|
|
|
if ($tId != null) {
|
|
|
$where['t_id'] = ['eq', $tId];
|
|
|
}
|
|
|
$res = Db::name('activity')->field('id,name,thumb')->where($where)->select()->toArray();
|
|
|
//只显示还未到报名日期活动最近的一条行程
|
|
|
foreach ($res as $key => $item) {
|
|
|
$item['is_collect'] = $this->isCollect($item['id'], $userId);
|
|
|
$item['schedule'] = $this->latestSchedule($item['id'], $nowTime);
|
|
|
$res[$key] = $item;
|
|
|
if (empty($item['schedule'])) {
|
|
|
unset($res[$key]);
|
|
|
}
|
|
|
}
|
|
|
return array_values($res);
|
|
|
}
|
|
|
|
|
|
public function activityDetail($activityId)
|
|
|
{
|
|
|
$info = Db::name('activity')->field('banner,name,is_down_payment,down_price,content,notice')->where(['id' => $activityId])->find();
|
|
|
if (!empty($info)) {
|
|
|
$arr = explode(',', $info['banner']);
|
|
|
$banner = [];
|
|
|
for ($i = 0; $i < count($arr); $i++) {
|
|
|
$banner[$i] = $arr[$i];
|
|
|
}
|
|
|
$info['banner'] = $banner;
|
|
|
$info['schedule'] = $this->allSchedule($activityId);
|
|
|
$info['comment_list'] = $this->getCommentListByActivityId($activityId);
|
|
|
return $info;
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private function allSchedule($activityId)
|
|
|
{
|
|
|
$res = Db::name('activity_schedule')->field('start_time,end_time,price')->where(['activity_id' => $activityId])->select();
|
|
|
foreach ($res as $k => $v) {
|
|
|
$v['start_time'] = date('Y-m-d', $v['start_time']);
|
|
|
$v['end_time'] = date('Y-m-d', $v['end_time']);
|
|
|
$res[$k] = $v;
|
|
|
}
|
|
|
return $res;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取应显示的活动批次,距离当前最近的批次
|
|
|
* @param $activityId
|
|
|
* @param $nowTime
|
|
|
* @return array|false|\PDOStatement|string|Model
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
* @throws \think\exception\DbException
|
|
|
*/
|
|
|
private function latestSchedule($activityId, $nowTime)
|
|
|
{
|
|
|
$where['activity_id'] = ['eq', $activityId];
|
|
|
$where['deadline'] = ['gt', $nowTime];
|
|
|
$schedule = Db::name('activity_schedule')->field('start_time,price,real_join_num,addition_join_num,maximum')->where($where)->order('deadline')->select()->toArray();
|
|
|
foreach ($schedule as $k => $v) {
|
|
|
$v['start_time'] = date('Y-m-d', $v['start_time']);
|
|
|
$v['sales_num'] = $v['real_join_num'] + $v['addition_join_num'];
|
|
|
$v['residue_num'] = $v['maximum'] - $v['sales_num'];
|
|
|
unset($v['real_join_num']);
|
|
|
unset($v['addition_join_num']);
|
|
|
unset($v['maximum']);
|
|
|
$schedule[$k] = $v;
|
|
|
}
|
|
|
if (!empty($schedule)) {
|
|
|
return $schedule[0];
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 收藏活动
|
|
|
* @param $activityId
|
|
|
* @param $userId
|
|
|
* @return bool|string
|
|
|
* @throws \think\Exception
|
|
|
* @throws \think\exception\PDOException
|
|
|
*/
|
|
|
public function collect($activityId, $userId)
|
|
|
{
|
|
|
$where = [
|
|
|
'user_id' => $userId,
|
|
|
'activity_id' => $activityId
|
|
|
];
|
|
|
$isCollected = Db::name('collect')->where($where)->value('id');
|
|
|
if ($isCollected) {
|
|
|
if (Db::name('collect')->delete($isCollected)) {
|
|
|
return json_encode(['msg' => '取消收藏', 'status' => true]);
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
} else {
|
|
|
if (Db::name('collect')->insert(['user_id' => $userId, 'activity_id' => $activityId])) {
|
|
|
return json_encode(['msg' => '收藏成功', 'status' => true]);
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断谁否收藏
|
|
|
* @param $activityId
|
|
|
* @param $userId
|
|
|
* @return int|string
|
|
|
*/
|
|
|
private function isCollect($activityId, $userId)
|
|
|
{
|
|
|
$isCollected = Db::name('collect')->where(['activity_id' => $activityId, 'user_id' => $userId])->count();
|
|
|
return $isCollected;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 活动评价列表
|
|
|
* @param $activityId
|
|
|
* @return false|\PDOStatement|string|\think\Collection
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
* @throws \think\exception\DbException
|
|
|
*/
|
|
|
private function getCommentListByActivityId($activityId)
|
|
|
{
|
|
|
$res = Db::name('activity_comment')->alias('c')
|
|
|
->field('c.rank_synthetical,c.comment_time,c.content,u.user_nickname,avatar')
|
|
|
->join('user u', 'c.user_id=u.id')
|
|
|
->where(['c.activity_id' => $activityId])
|
|
|
->select();
|
|
|
foreach ($res as $k => $v) {
|
|
|
$v['comment_time'] = date('Y.m.d', $v['comment_time']);
|
|
|
$res[$k] = $v;
|
|
|
}
|
|
|
return $res;
|
|
|
}
|
|
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|