| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\store;
- use app\models\Goods;
- use app\models\OrderComment;
- use app\models\OrderDetail;
- use app\models\SaasUser;
- use app\models\User;
- use yii\base\Model;
- class StoreAdminCommentForm extends Model
- {
- //商品名称
- public $goods_name;
- //日期区间
- public $date_range;
- //商品ID
- public $goods_id;
- //评论状态 好中差评
- public $status = 0;
- //评论状态 已回复未回复
- public $type = 0;
- //评论ID
- public $id;
- //评论内容
- public $reply_content;
- public $mch_id;
- public function rules()
- {
- return [
- [['goods_id', 'status', 'type', 'id'], 'integer'],
- [['goods_name', 'reply_content'], 'string'],
- [['mch_id'], 'safe'],
- [['date_range'], 'array']
- ];
- }
- public function commentGoodsList()
- {
- try {
- $whereMch = [];
- if($this->mch_id){
- $whereMch = ['oc.mch_id' => $this->mch_id];
- }
- //商城ID
- $store_id = get_store_id();
- //商品名称
- $goods_name = $this->goods_name;
- //日期区间
- $date_range = $this->date_range;
- //查询数据
- $query = OrderComment::find()->alias('oc')->where(['oc.store_id' => $store_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'g.is_delete' => 0])->andWhere($whereMch)
- ->leftJoin(['g' => Goods::tableName()], 'oc.goods_id = g.id')
- ->select('oc.goods_id, oc.created_at, g.name, g.id, g.cover_pic, g.price, count(*) comment_num')
- ->groupBy("goods_id");
- if (!empty($goods_name)) {
- $query->andWhere(['LIKE', 'g.name', $goods_name]);
- }
- if (!empty($date_range)) {
- //日期转时间戳
- $begin_time = strtotime($date_range['begin_time']);
- //日期转时间戳 末尾时间再加一天时间
- $end_time = strtotime($date_range['end_time']) + 60 * 60 * 24;
- $query->andWhere(['AND', ['>', 'oc.created_at', $begin_time], ['<', 'oc.created_at', $end_time]]);
- }
- //分页
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach ($list as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => [
- 'list' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function commentList()
- {
- try {
- $status = (int)$this->status;
- $goods_id = $this->goods_id;
- $type = (int)$this->type;
- $query = OrderComment::find()->alias('oc')->leftJoin(['od' => OrderDetail::tableName()], 'oc.order_detail_id = od.id')
- ->leftJoin(['u' => User::tableName()], 'oc.user_id = u.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
- ->where(['oc.is_delete' => 0, 'su.is_delete' => 0, 'od.is_delete' => 0, 'oc.goods_id' => $goods_id]);
- if ($status === 1) {
- $query->andWhere(['>', 'oc.score', 3]);
- }
- if ($status === 2) {
- $query->andWhere(['=', 'oc.score', 3]);
- }
- if ($status === 3) {
- $query->andWhere(['<', 'oc.score', 3]);
- }
- if ($type === 1) {
- $query->andWhere(['oc.reply_content' => null]);
- }
- if ($type === 2) {
- $query->andWhere(['is not', 'oc.reply_content', null]);
- }
- $query->select('oc.id, oc.created_at, oc.score, oc.content, oc.pic_list, oc.reply_content, od.attr, su.name, su.avatar');
- //分页
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach ($list as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['pic_list'] = json_decode($item['pic_list'], true);
- }
- $query = OrderComment::find()->where(['goods_id' => $goods_id, 'is_delete' => 0]);
- //好评数量
- $Praise_query = clone $query;
- $Praise = $Praise_query->andWhere(['>', 'score', 3])->count();
- //中评数量
- $Medium_query = clone $query;
- $Medium_evaluation = $Medium_query->andWhere(['=', 'score', 3])->count();
- //差评数量
- $bad_query = clone $query;
- $bad_num = $bad_query->andWhere(['<', 'score', 3])->count();
- if ($status === 1) {
- $query->andWhere(['>', 'score', 3]);
- }
- if ($status === 2) {
- $query->andWhere(['=', 'score', 3]);
- }
- if ($status === 3) {
- $query->andWhere(['<', 'score', 3]);
- }
- //全部数量
- $all_query = clone $query;
- //未回复数量
- $not_reply = $query->andWhere(['reply_content' => null])->count();
- //全部数量
- $all_reply = $all_query->count();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => [
- 'list' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- 'praise' => $Praise <= 99 ? $Praise : '99+',
- 'medium_evaluatio' => $Medium_evaluation <= 99 ? $Medium_evaluation : '99+',
- 'bad_num' => $bad_num <= 99 ? $bad_num : '99+',
- 'not_reply' => $not_reply <= 99 ? $not_reply : '99+',
- 'all_reply' => $all_reply <= 99 ? $all_reply : '99+'
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 评论删除
- */
- public function commentDel()
- {
- try {
- $id = $this->id;
- $comment = OrderComment::find()->where(['id' => $id, 'is_delete' => 0])->one();
- if (!$comment) {
- throw new \Exception("评论未找到");
- }
- $comment->is_delete = 1;
- if (!$comment->save()) {
- throw new \Exception("操作失败");
- }
- return [
- 'code' => 0,
- 'msg' => "操作成功"
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 回复评论
- */
- public function commentReply()
- {
- try {
- $id = $this->id;
- $reply_content = $this->reply_content;
- $comment = OrderComment::find()->where(['id' => $id, 'is_delete' => 0])->one();
- if (!$comment) {
- throw new \Exception("评论不存在");
- }
- if (!$reply_content) {
- throw new \Exception("评论不能为空");
- }
- if ($comment->reply_content) {
- throw new \Exception("已经评论,不可重复回复");
- }
- $comment->reply_content = $reply_content;
- if (!$comment->save()) {
- throw new \Exception("操作失败");
- }
- return [
- 'code' => 0,
- 'msg' => "操作成功"
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|