| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- /*
- * @Author: your name
- * @Date: 2021-03-02 09:50:20
- * @LastEditTime: 2021-05-07 16:37:47
- * @LastEditors: Please set LastEditors
- * @Description: In User Settings Edit
- * @FilePath: \admin_php\modules\client\models\v1\CommentListForm.php
- */
- namespace app\modules\client\models\v1;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Goods;
- use app\models\OrderComment;
- use app\models\OrderDetail;
- use app\models\SaasUser;
- use app\models\User;
- use yii\base\Model;
- use yii\data\Pagination;
- use yii\helpers\Json;
- class CommentListForm extends Model
- {
- public $goods_id;
- public $score = -1;
- public $page = 1;
- public $limit = 20;
- public function rules()
- {
- return [
- [['goods_id'], 'required'],
- [['page', 'score'], 'integer'],
- ];
- }
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $query = OrderComment::find()->where(['goods_id' => $this->goods_id, 'is_delete' => 0, 'is_hide' => 0]);
- if ($this->score > 0) {
- if ($this->score == 1) {
- $score = [1, 2];
- } elseif($this->score == 2) {
- $score = [3, 4];
- } else {
- $score = [5];
- }
- $query->andWhere([
- 'score' => $score
- ]);
- }
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
- $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()
- ->select('order_detail_id, is_virtual, virtual_user, virtual_avatar, score, content, pic_list, created_at,
- reply_content, user_id, saas_id, goods_id, user_type, user_name, user_avatar')->all();
- foreach ($list as $i => $item) {
- if (intval($item['user_type']) === OrderComment::USER_TYPE_CUSTOM) {
- $list[$i]['nickname'] = $item['user_name'];
- $list[$i]['avatar_url'] = $item['user_avatar'];
- } else {
- if ($item['saas_id']) {
- $saasUser = SaasUser::findOne($item['saas_id']);
- $list[$i]['nickname'] = $saasUser->name;
- $list[$i]['avatar_url'] = $saasUser->avatar;
- } else {
- $user = User::findOne($item['user_id']);
- $list[$i]['nickname'] = $user->nickname;
- $list[$i]['avatar_url'] = $user->avatar_url;
- }
- }
- $list[$i]['attr'] = Json::decode(OrderDetail::findOne($item['order_detail_id'])->attr);
- if (!$list[$i]['attr']) {
- $goods_info = Goods::findOne($item['goods_id']);
- $attrArray = json_decode($goods_info->attr, true);
- $attrList = $attrArray[0]['attr_list'] ?? null;
- foreach ($attrList as $k=>$attr) {
- $list[$i]['attr'][$k]['attr_name'] = $attr['attr_name'];
- $attr_name_arr = Attr::find()->alias('a')->leftJoin(['ag' => AttrGroup::tableName()], 'a.attr_group_id = ag.id')
- ->where(['a.id' => $attr['attr_id']])->select('a.attr_name, ag.attr_group_name')
- ->asArray()->one();
- $list[$i]['attr'][$k]['attr_group_name'] = $attr_name_arr['attr_group_name'] ?? '';
- }
- }
- $list[$i]['addtime'] = date('Y-m-d', $item['created_at']);
- $list[$i]['pic_list'] = json_decode($item['pic_list']);
- $list[$i]['nickname'] = $this->substr_cut($list[$i]['nickname']);
- if ($item['is_virtual'] == 1) {
- $list[$i]['nickname'] = $this->substr_cut($item['virtual_user']);
- $list[$i]['avatar_url'] = $item['virtual_avatar'];
- }
- unset($list[$i]['virtual_avatar']);
- unset($list[$i]['is_virtual']);
- unset($list[$i]['virtual_user']);
- };
- $data = [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- 'comment_count' => $this->countData(),
- ];
- return [
- 'code' =>0,
- 'data' => $data
- ];
- }
- public function countData()
- {
- if (!$this->validate()) {
- return $this->errorResponse;
- }
- $score_all = OrderComment::find()->alias('oc')
- ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0,])->count();
- $score_3 = OrderComment::find()->alias('oc')
- ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'oc.score' => [5,4]])->count();
- $score_2 = OrderComment::find()->alias('oc')
- ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'oc.score' => [3,2]])->count();
- $score_1 = OrderComment::find()->alias('oc')
- ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'oc.score' => [1]])->count();
- return (object)[
- 'score_all' => $score_all ? $score_all : 0,
- 'score_3' => $score_3 ? $score_3 : 0,
- 'score_2' => $score_2 ? $score_2 : 0,
- 'score_1' => $score_1 ? $score_1 : 0,
- ];
- }
- // 将用户名 做隐藏
- private function substr_cut($user_name)
- {
- $strlen = mb_strlen($user_name, 'utf-8');
- $firstStr = mb_substr($user_name, 0, 1, 'utf-8');
- $lastStr = mb_substr($user_name, -1, 1, 'utf-8');
- return $strlen <= 2 ? $firstStr . '*' : $firstStr . str_repeat("*", 2) . $lastStr;
- }
- }
|