quanwei
18 hours ago b90b528cb5c6eaebe03fba972fef658a741ce896
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?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);
    }
}