OrderCommentsExtra.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace common\models\order;
  3. use addons\Store\common\models\Store;
  4. use addons\Store\common\models\StoreOrderDetail;
  5. use common\enums\StatusEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use common\models\goods\Goods;
  8. use common\models\user\User;
  9. use PHPUnit\Util\Exception;
  10. use common\models\order\OrderComments;
  11. use Yii;
  12. use yii\helpers\Json;
  13. /**
  14. * This is the model class for table "qimall_order_comments".
  15. *
  16. *
  17. * @property int $id
  18. * @property int $mall_id
  19. * @property int $mch_id
  20. * @property int $store_id
  21. * @property int $order_id
  22. * @property int|null $order_detail_id 订单详情id
  23. * @property int $goods_id 商品ID
  24. * @property int $user_id 会员id
  25. * @property int $score 评分:1=差评,2,3=中评,4,5=好
  26. * @property string $content 评价内容
  27. * @property string $pic_url 评价图片
  28. * @property int $is_top 是否置顶0.否|1.是
  29. * @property string $reply_content 商家回复内容
  30. * @property int $status 状态[-1:删除;0:禁用;1启用]
  31. * @property int $created_at 创建时间
  32. * @property int $updated_at 修改时间
  33. * @property int $is_virtual 是否虚拟用户
  34. * @property string $virtual_user 虚拟用户名
  35. * @property string $virtual_avatar 虚拟头像
  36. * @property int $virtual_time 虚拟评价时间
  37. */
  38. class OrderCommentsExtra extends BaseActiveRecord
  39. {
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function tableName()
  44. {
  45. return '{{%order_comments_extra}}';
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['mall_id', 'mch_id','store_id', 'goods_id', 'created_at', 'updated_at', 'num'], 'integer'],
  54. ];
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getGoods()
  60. {
  61. return $this->hasOne(Goods::class, ['id' => 'goods_id']);
  62. }
  63. /**
  64. * @Author: hua
  65. * @DateTime: 2021/10/8 9:41
  66. * @Copyright: copyright (c) 2021 广东七件事集团
  67. * @param array $params
  68. * @return bool|OrderComments
  69. * @throws \yii\db\Exception
  70. */
  71. public static function setData(array $params)
  72. {
  73. try {
  74. $model = new self();
  75. $model->loadDefaultValues();
  76. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  77. return $model;
  78. } catch (Exception $e) {
  79. self::$static_error = $e->getMessage();
  80. return false;
  81. }
  82. }
  83. /**
  84. *
  85. * @Author: hua
  86. * @DateTime: 2021/10/7 16:41
  87. * @Copyright: copyright (c) 2021 广东七件事集团
  88. * @param $params
  89. * @return bool|OrderComments
  90. * @throws \Exception
  91. */
  92. public static function updateData($params)
  93. {
  94. try {
  95. $res = self::updateAll($params['field'], ['in', 'id', $params['id']]);
  96. if (!$res) throw new Exception(self::getStaticError());
  97. return true;
  98. } catch (Exception $e) {
  99. self::$static_error = $e->getMessage();
  100. return false;
  101. }
  102. }
  103. //用于数据迁移时,将已有评论数据导入到评论拓展表里
  104. public static function updateOldData()
  105. {
  106. $i = 1;
  107. $page_size = 1000;
  108. while ($list = OrderComments::find()
  109. ->select('mall_id,store_id,goods_id,user_id,mch_id,count(*) as num')
  110. ->groupBy('goods_id,mch_id,store_id,user_id')
  111. ->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  112. $data = [];
  113. foreach ($list as $key => $value) {
  114. $data[$value['mall_id'].'_'.$value['store_id'].'_'.$value['mch_id'].'_'.$value['goods_id']] += 1;
  115. }
  116. if (!empty($data)) {
  117. foreach ($data as $k => $val) {
  118. $key_arr = explode('_', $k);
  119. $params = [
  120. 'mall_id' => $key_arr[0],
  121. 'store_id' => $key_arr[1],
  122. 'mch_id' => $key_arr[2],
  123. 'goods_id' => $key_arr[3],
  124. 'num' => $val,
  125. ];
  126. var_dump($params);
  127. $res = self::setData($params);
  128. if (!$res) {
  129. var_dump(self::$static_error);
  130. }
  131. }
  132. }
  133. $i++;
  134. }
  135. }
  136. }