quanwei
2025-12-09 ca425b889f3c1b5847ffc26a0229307f7f8ef43e
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
<?php
 
namespace app\api\model\product;
 
use app\api\model\order\OrderProduct;
use app\common\model\product\Comment as CommentModel;
use app\common\exception\BaseException;
 
class Comment extends CommentModel
{
    /**
     * 隐藏字段
     */
    protected $hidden = [
        'status',
        'sort',
        'order_id',
        'product_id',
        'order_product_id',
        'is_delete',
        'update_time'
    ];
 
    /**
     * 关联用户表
     */
    public function users()
    {
        return $this->belongsTo('app\\common\\model\\user\\User')->field(['user_id', 'nickName', 'avatarUrl']);
    }
 
    /**
     * 获取指定商品评价列表
     */
    public function getProductCommentList($product_id, $scoreType = -1, $limit = 15)
    {
        // 筛选条件
        $filter = [
            'product_id' => $product_id,
            'is_delete' => 0,
            'status' => 1,
        ];
        // 评分
        $scoreType > 0 && $filter['score'] = $scoreType;
        return $this->with(['OrderProduct', 'image.file', 'users'])
            ->where($filter)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($limit);
    }
 
    /**
     * 获取指定评分总数
     */
    public function getTotal($product_id)
    {
        return $this->field([
            'count(comment_id) AS `all`',
            'count(score = 10 OR NULL) AS `praise`',
            'count(score = 20 OR NULL) AS `review`',
            'count(score = 30 OR NULL) AS `negative`',
        ])->where([
            'product_id' => $product_id,
            'is_delete' => 0,
            'status' => 1
        ])->find();
    }
 
    /**
     * 验证订单是否允许评价
     */
    public function checkOrderAllowComment($order)
    {
        // 验证订单是否已完成
        if ($order['order_status']['value'] != 30) {
            $this->error = '该订单未完成,无法评价';
            return false;
        }
        // 验证订单是否已评价
        if ($order['is_comment'] == 1) {
            $this->error = '该订单已完成评价';
            return false;
        }
        return true;
    }
 
    /**
     * 根据已完成订单商品 添加评价
     */
    public function addForOrder($order, $productList, $formJsonData)
    {
        // 生成 formData
        $formData = $this->formatFormData($formJsonData);
        // 生成评价数据
        $data = $this->createCommentData($order['user_id'], $order['order_id'], $productList, $formData);
        if (empty($data)) {
            $this->error = '没有输入评价内容';
            return false;
        }
        return $this->transaction(function () use ($order, $productList, $formData, $data) {
            // 记录评价内容
            $result = $this->saveAll($data);
            // 记录评价图片
            $this->saveAllImages($result, $formData);
            // 更新订单评价状态
            $isComment = count($productList) === count($data);
            $this->updateOrderIsComment($order, $isComment, $result);
            return true;
        });
    }
 
    /**
     * 更新订单评价状态
     */
    private function updateOrderIsComment($order, $isComment, $commentList)
    {
        // 更新订单商品
        $orderProductData = [];
        foreach ($commentList as $comment) {
            $orderProductData[] = [
                'data' => [
                    'is_comment' => 1
                ],
                'where' => [
                    'order_product_id' => $comment['order_product_id'],
                ],
            ];
        }
        // 更新订单
        $isComment && $order->save(['is_comment' => 1]);
        return (new OrderProduct)->updateAll($orderProductData);
    }
 
    /**
     * 生成评价数据
     */
    private function createCommentData($user_id, $order_id, $productList, $formData)
    {
        $data = [];
        foreach ($productList as $product) {
            if (!isset($formData[$product['order_product_id']])) {
                throw new BaseException(['msg' => '提交的数据不合法']);
            }
            $item = $formData[$product['order_product_id']];
            !empty($item['content']) && $data[$product['order_product_id']] = [
                'express_score' => $item['express_score'],
                'server_score' => $item['server_score'],
                'describe_score' => $item['describe_score'],
                'content' => $item['content'],
                'is_picture' => !empty($item['image_list']),
                'sort' => 100,
                'status' => 0,
                'user_id' => $user_id,
                'order_id' => $order_id,
                'product_id' => $item['product_id'],
                'order_product_id' => $item['order_product_id'],
                'app_id' => self::$app_id,
                'shop_supplier_id' => $product['orderM']['shop_supplier_id'],
                'score' => $item['score'],
            ];
        }
        return $data;
    }
 
    /**
     * 格式化 formData
     */
    private function formatFormData($formJsonData)
    {
        return array_column(json_decode($formJsonData, true), null, 'order_product_id');
    }
 
    /**
     * 记录评价图片
     */
    private function saveAllImages($commentList, $formData)
    {
        // 生成评价图片数据
        $imageData = [];
        foreach ($commentList as $comment) {
            $item = $formData[$comment['order_product_id']];
            foreach ($item['image_list'] as $imageId) {
                $imageData[] = [
                    'comment_id' => $comment['id'],
                    'image_id' => $imageId['file_id'],
                    'app_id' => self::$app_id
                ];
            }
        }
        $model = new CommentImage;
        return !empty($imageData) && $model->saveAll($imageData);
    }
}