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() ]; } } }