1000], [['reply_content', 'virtual_user', 'virtual_avatar'], 'string', 'max' => 255], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'store_id' => 'Store ID', 'mch_id' => 'Mch ID', 'order_id' => 'Order ID', 'order_detail_id' => 'Order Detail ID', 'goods_id' => 'Goods ID', 'user_id' => 'User ID', 'score' => '评分:1=差评,2=中评,3=好', 'content' => '评价内容', 'pic_list' => '图片', 'is_hide' => '是否隐藏:0=不隐藏,1=隐藏', 'is_delete' => 'Is Delete', 'created_at' => 'Addtime', 'updated_at' => 'update time', 'reply_content' => 'Reply Content', 'is_virtual' => 'Is Virtual', 'virtual_user' => 'Virtual User', 'virtual_avatar' => 'Virtual Image', ]; } public function beforeSave($insert) { //查询服务人员 $worker_order_ext = WorkerOrderExt::find()->where(['store_id' => $this->store_id, 'order_id' => $this->order_id])->asArray()->one(); if ($worker_order_ext) { //查询服务人员订单 $order_ext = WorkerOrderExt::find()->where(['worker_id' => $worker_order_ext['worker_id']])->select('order_id')->column(); //查询订单数量以及评分 $score_arr = self::find()->where(['order_id' => $order_ext, 'is_delete' => 0, 'is_hide' => 0])->select(['score'])->column(); if ($score_arr) { $num = count($score_arr); $sum = array_sum($score_arr); if (intval($this->score) === 0 && intval($this->is_delete) === 0) { $sum += floatval($this->score); $num += 1; } $star = sprintf('%.1f', ($sum / $num)); if ($star > 0) { $worker = Worker::findOne($worker_order_ext['worker_id']); $worker->star = $star; $worker->save(); } } } return parent::beforeSave($insert); } public static function distributeTags($count) { $allTags = self::TAG_ARR; shuffle($allTags); $length = $count > 6 ? 6 : $count; $randomTags = array_slice($allTags, 0, $length); shuffle($randomTags); // 打乱顺序 // 2. 将$count随机分配给6个标签 $distribution = self::distributeCount($count, $length); // 3. 拼接字符串数组 $result = []; foreach ($randomTags as $index => $tag) { $result[] = $tag . ' ' . $distribution[$index]; } return $result; } static function distributeCount($total, $parts) { // 确保参数有效 $total = max(0, $total); $parts = max(1, $parts); $distribution = array_fill(0, $parts, 0); $remaining = $total; // 如果总数小于部分数,只能分配有限的1 if ($total < $parts) { for ($i = 0; $i < $total; $i++) { $distribution[$i] = 1; } shuffle($distribution); return $distribution; } // 正常分配流程 for ($i = 0; $i < $parts - 1; $i++) { // 确保 max 至少为 1 $max = max(1, $remaining - ($parts - $i - 1)); $randomNum = mt_rand(1, $max); $distribution[$i] = $randomNum; $remaining -= $randomNum; } $distribution[$parts - 1] = $remaining; return $distribution; } }