OrderComment.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\models\common\admin\log\CommonActionLog;
  9. use Yii;
  10. /**
  11. * This is the model class for table "{{%order_comment}}".
  12. *
  13. * @property integer $id
  14. * @property integer $store_id
  15. * @property integer $order_id
  16. * @property integer $order_detail_id
  17. * @property integer $goods_id
  18. * @property integer $user_id
  19. * @property string $score
  20. * @property string $content
  21. * @property string $pic_list
  22. * @property integer $is_hide
  23. * @property integer $is_delete
  24. * @property integer $created_at
  25. * @property integer $updated_at
  26. * @property string $reply_content
  27. * @property integer $is_virtual
  28. * @property string $virtual_user
  29. * @property string $virtual_avatar
  30. */
  31. class OrderComment extends \yii\db\ActiveRecord
  32. {
  33. /**
  34. * 是否删除评论
  35. */
  36. const IS_DELETE_FALSE = 0;
  37. const IS_DELETE_TRUE = 1;
  38. const TAG_ARR = ['性价比高','完美','惊叹','很满意','满意','物流超快'];
  39. /**
  40. * @inheritdoc
  41. */
  42. public static function tableName()
  43. {
  44. return '{{%order_comment}}';
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['store_id', 'order_id', 'order_detail_id', 'goods_id', 'user_id', 'score'], 'required'],
  53. [['store_id', 'mch_id', 'order_id', 'order_detail_id', 'goods_id', 'user_id', 'is_hide', 'is_delete',
  54. 'created_at', 'updated_at', 'is_virtual'], 'integer'],
  55. [['score'], 'number'],
  56. [['pic_list'], 'string'],
  57. [['content'], 'string', 'max' => 1000],
  58. [['reply_content', 'virtual_user', 'virtual_avatar'], 'string', 'max' => 255],
  59. ];
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => 'ID',
  68. 'store_id' => 'Store ID',
  69. 'mch_id' => 'Mch ID',
  70. 'order_id' => 'Order ID',
  71. 'order_detail_id' => 'Order Detail ID',
  72. 'goods_id' => 'Goods ID',
  73. 'user_id' => 'User ID',
  74. 'score' => '评分:1=差评,2=中评,3=好',
  75. 'content' => '评价内容',
  76. 'pic_list' => '图片',
  77. 'is_hide' => '是否隐藏:0=不隐藏,1=隐藏',
  78. 'is_delete' => 'Is Delete',
  79. 'created_at' => 'Addtime',
  80. 'updated_at' => 'update time',
  81. 'reply_content' => 'Reply Content',
  82. 'is_virtual' => 'Is Virtual',
  83. 'virtual_user' => 'Virtual User',
  84. 'virtual_avatar' => 'Virtual Image',
  85. ];
  86. }
  87. public function beforeSave($insert)
  88. {
  89. //查询服务人员
  90. $worker_order_ext = WorkerOrderExt::find()->where(['store_id' => $this->store_id, 'order_id' => $this->order_id])->asArray()->one();
  91. if ($worker_order_ext) {
  92. //查询服务人员订单
  93. $order_ext = WorkerOrderExt::find()->where(['worker_id' => $worker_order_ext['worker_id']])->select('order_id')->column();
  94. //查询订单数量以及评分
  95. $score_arr = self::find()->where(['order_id' => $order_ext, 'is_delete' => 0, 'is_hide' => 0])->select(['score'])->column();
  96. if ($score_arr) {
  97. $num = count($score_arr);
  98. $sum = array_sum($score_arr);
  99. if (intval($this->score) === 0 && intval($this->is_delete) === 0) {
  100. $sum += floatval($this->score);
  101. $num += 1;
  102. }
  103. $star = sprintf('%.1f', ($sum / $num));
  104. if ($star > 0) {
  105. $worker = Worker::findOne($worker_order_ext['worker_id']);
  106. $worker->star = $star;
  107. $worker->save();
  108. }
  109. }
  110. }
  111. return parent::beforeSave($insert);
  112. }
  113. public static function distributeTags($count) {
  114. $allTags = self::TAG_ARR;
  115. shuffle($allTags);
  116. $length = $count > 6 ? 6 : $count;
  117. $randomTags = array_slice($allTags, 0, $length);
  118. shuffle($randomTags); // 打乱顺序
  119. // 2. 将$count随机分配给6个标签
  120. $distribution = self::distributeCount($count, $length);
  121. // 3. 拼接字符串数组
  122. $result = [];
  123. foreach ($randomTags as $index => $tag) {
  124. $result[] = $tag . ' ' . $distribution[$index];
  125. }
  126. return $result;
  127. }
  128. static function distributeCount($total, $parts) {
  129. // 确保参数有效
  130. $total = max(0, $total);
  131. $parts = max(1, $parts);
  132. $distribution = array_fill(0, $parts, 0);
  133. $remaining = $total;
  134. // 如果总数小于部分数,只能分配有限的1
  135. if ($total < $parts) {
  136. for ($i = 0; $i < $total; $i++) {
  137. $distribution[$i] = 1;
  138. }
  139. shuffle($distribution);
  140. return $distribution;
  141. }
  142. // 正常分配流程
  143. for ($i = 0; $i < $parts - 1; $i++) {
  144. // 确保 max 至少为 1
  145. $max = max(1, $remaining - ($parts - $i - 1));
  146. $randomNum = mt_rand(1, $max);
  147. $distribution[$i] = $randomNum;
  148. $remaining -= $randomNum;
  149. }
  150. $distribution[$parts - 1] = $remaining;
  151. return $distribution;
  152. }
  153. }