| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\models\common\admin\log\CommonActionLog;
- use Yii;
- /**
- * This is the model class for table "{{%order_comment}}".
- *
- * @property integer $id
- * @property integer $store_id
- * @property integer $order_id
- * @property integer $order_detail_id
- * @property integer $goods_id
- * @property integer $user_id
- * @property string $score
- * @property string $content
- * @property string $pic_list
- * @property integer $is_hide
- * @property integer $is_delete
- * @property integer $created_at
- * @property integer $updated_at
- * @property string $reply_content
- * @property integer $is_virtual
- * @property string $virtual_user
- * @property string $virtual_avatar
- */
- class OrderComment extends \yii\db\ActiveRecord
- {
- /**
- * 是否删除评论
- */
- const IS_DELETE_FALSE = 0;
- const IS_DELETE_TRUE = 1;
- const TAG_ARR = ['性价比高','完美','惊叹','很满意','满意','物流超快'];
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%order_comment}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['store_id', 'order_id', 'order_detail_id', 'goods_id', 'user_id', 'score'], 'required'],
- [['store_id', 'mch_id', 'order_id', 'order_detail_id', 'goods_id', 'user_id', 'is_hide', 'is_delete',
- 'created_at', 'updated_at', 'is_virtual'], 'integer'],
- [['score'], 'number'],
- [['pic_list'], 'string'],
- [['content'], 'string', 'max' => 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;
- }
- }
|