<?php
|
|
namespace app\common\model\branch;
|
|
use app\common\model\BaseModel;
|
|
/**
|
* 活动评论模型
|
*/
|
class ActivityComment extends BaseModel
|
{
|
protected $name = 'branch_activity_comment';
|
protected $pk = 'comment_id';
|
|
/**
|
* 追加字段
|
* @var string[]
|
*/
|
protected $append = [
|
'create_time_text',
|
];
|
|
/**
|
* 关联用户表
|
*/
|
public function user()
|
{
|
return $this->belongsTo('app\\common\\model\\user\\User', 'user_id', 'user_id')
|
->field(['user_id', 'nickName', 'avatarUrl', 'real_name']);
|
}
|
|
/**
|
* 关联活动表
|
*/
|
public function activity()
|
{
|
return $this->belongsTo('app\\common\\model\\branch\\Activity', 'activity_id', 'activity_id');
|
}
|
|
/**
|
* 关联活动报名表
|
*/
|
public function activityUser()
|
{
|
return $this->belongsTo('app\\common\\model\\branch\\ActivityUser', 'order_id', 'order_id');
|
}
|
|
/**
|
* 关联评论图片表
|
*/
|
public function images()
|
{
|
return $this->hasMany('app\\common\\model\\branch\\ActivityCommentImage', 'comment_id', 'comment_id');
|
}
|
|
/**
|
* 关联图片文件
|
*/
|
|
/**
|
* 创建时间文本
|
*/
|
public function getCreateTimeTextAttr($value, $data)
|
{
|
return isset($data['create_time']) ? date('Y-m-d H:i:s', $data['create_time']) : '';
|
}
|
|
/**
|
* 检查报名记录是否允许评论
|
|
/**
|
* 获取活动评论列表
|
*/
|
public function getActivityCommentList($activity_id, $params = [])
|
{
|
$model = $this->with(['user', 'images.file'])
|
->where('activity_id', '=', $activity_id)
|
->where('is_delete', '=', 0)
|
->where('status', '=', 1);
|
|
// 按评分筛选
|
if (isset($params['score']) && $params['score'] > 0) {
|
$model = $model->where('score', '=', $params['score']);
|
}
|
|
return $model->order(['sort' => 'asc', 'create_time' => 'desc'])
|
->paginate($params);
|
}
|
|
/**
|
* 获取活动评论统计
|
*/
|
public function getCommentStatistics($activity_id)
|
{
|
return $this->field([
|
'count(comment_id) AS `all`',
|
'count(score >= 4 OR NULL) AS `good`',
|
'count(score >= 2 AND score <= 3 OR NULL) AS `medium`',
|
'count(score = 1 OR NULL) AS `bad`',
|
])->where([
|
'activity_id' => $activity_id,
|
'is_delete' => 0,
|
'status' => 1
|
])->find();
|
}
|
|
/**
|
* 获取企业的差评数量
|
* @param int $supplier_id 企业ID
|
* @return int 差评数量
|
*/
|
public function getSupplierNegativeCommentCount($supplier_id,$visit_supplier_name,$exclude_activity_id=0)
|
{
|
$model=$this;
|
if ($exclude_activity_id){
|
$model = $model->where('activity_id', '<>', $exclude_activity_id);
|
}
|
$count = $model->where(function ($query) use ($supplier_id,$visit_supplier_name) {
|
$query->where('supplier_id', '=', $supplier_id)
|
->whereOr('supplier_name', '=', $visit_supplier_name);
|
})->where([
|
'score' => 1, // 1分为差评
|
'is_delete' => 0,
|
'status' => 1
|
])->count();
|
|
return $count;
|
}
|
|
/**
|
* 验证报名记录是否允许评论
|
* @param object $activityUser 报名记录
|
* @return bool
|
*/
|
public function checkAllowComment($activityUser)
|
{
|
// 验证是否已签到
|
if ($activityUser['is_verify'] != 1) {
|
$this->error = '请先签到后再评价';
|
return false;
|
}
|
|
// 验证活动是否已结束
|
$activity = $activityUser['activity'];
|
if ($activity['status_text']['status'] != 2) {
|
$this->error = '活动结束后才能评价';
|
return false;
|
}
|
|
// 验证是否已评价
|
$hasComment = $this->where(['order_id' => $activityUser['order_id']])->find();
|
if ($hasComment) {
|
$this->error = '您已经评价过该活动';
|
return false;
|
}
|
|
return true;
|
}
|
|
/**
|
* 添加评论
|
*/
|
public function addComment($activityUser, $data)
|
{
|
// 验证是否允许评论
|
if (!$this->checkAllowComment($activityUser)) {
|
return false;
|
}
|
|
$commentData = [
|
'activity_id' => $activityUser['activity_id'],
|
'order_id' => $activityUser['order_id'],
|
'user_id' => $activityUser['user_id'],
|
'supplier_id' => $activityUser['activity']['visit_supplier_id'] ?: 0,
|
'supplier_name' => $activityUser['activity']['visit_supplier_name'] ?: '',
|
'content' => $data['content'] ?: '',
|
'score' => $data['score'] ?: 5,
|
'is_picture' => !empty($data['image_list']) ? 1 : 0,
|
'sort' => 100,
|
'status' => 0, // 待审核
|
'app_id' => self::$app_id,
|
];
|
|
return $this->save($commentData);
|
}
|
|
/**
|
* 保存评论图片
|
*/
|
public function saveCommentImages($comment_id, $image_list)
|
{
|
$this->images()->delete();
|
if (empty($image_list)) {
|
return true;
|
}
|
|
$imageData = [];
|
foreach ($image_list as $image) {
|
$imageData[] = [
|
'comment_id' => $comment_id,
|
'image_id' => $image['file_id'],
|
'app_id' => self::$app_id,
|
'create_time' => time(),
|
];
|
}
|
|
$commentImageModel = new ActivityCommentImage();
|
return $commentImageModel->saveAll($imageData);
|
}
|
|
/**
|
* 详情
|
*/
|
public static function detail($comment_id, $with = [])
|
{
|
return (new static())->with($with)->find($comment_id);
|
}
|
}
|