123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\index\model;
- use app\index\model\Order as OrderModel;
- use app\common\model\Comment as CommentModel;
- use app\index\model\OrderGoods as OrderGoodsModel;
- use app\index\service\User as UserService;
- use app\common\library\helper;
- use app\common\exception\BaseException;
- /**
- * 商品评价模型
- * Class Comment
- * @package app\api\model
- */
- class Comment extends CommentModel
- {
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'status',
- 'sort',
- 'order_id',
- //'goods_id',
- 'order_goods_id',
- 'is_delete',
- 'update_time'
- ];
- /**
- * 获取指定商品评价列表
- * @param int $goodsId 商品ID
- * @param int|null $scoreType 评分 (10好评 20中评 30差评)
- * @return \think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getCommentList(int $goodsId, int $scoreType = null)
- {
- $filter = $this->getFilter($goodsId, $scoreType);
- return $this->with(['user.avatar', 'orderGoods'])
- ->where($filter)
- //->order(['sort' => 'asc', 'create_time' => 'desc'])
- ->order(['comment_id' => 'desc'])
- ->paginate(15);
- }
- /**
- * 获取指定商品评价列表 (限制数量, 不分页)
- * @param int $goodsId 商品ID
- * @param int $limit 限制的数量
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function listRows(int $goodsId, int $limit = 5)
- {
- $filter = $this->getFilter($goodsId);
- return $this->with(['user.avatar'])
- ->where($filter)
- ->order(['sort' => 'asc', $this->getPk()])
- ->limit($limit)
- ->select();
- }
- /**
- * 获取指定商品评价总数量
- * @param int $goodsId
- * @return int
- */
- public function rowsTotal(int $goodsId)
- {
- $filter = $this->getFilter($goodsId);
- return $this->where($filter)->count();
- }
- /**
- * 获取指定用户评价总数量
- * @param int $userId
- * @return int
- */
- public function rowsUserTotal(int $userId)
- {
- return $this->where(['user_id',$userId])->where('status',1)->where('is_delete',0)->count();
- }
- public function rowsTotalBatch(array $goodsIds)
- {
- $filter = [
- ['status', '=', 1],
- ['is_delete', '=', 0],];
- return $this->field('goods_id, count(comment_id) as cnt')->where($filter)->whereIn('goods_id', $goodsIds)->group('goods_id')->select();
- }
- /**
- * 获取查询条件
- * @param int $goodsId 商品ID
- * @param int|null $scoreType 评分 (10好评 20中评 30差评)
- * @return array[]
- */
- private function getFilter(int $goodsId, int $scoreType = null, int $userId = null)
- {
- // 筛选条件
- $filter = [
- ['goods_id', '=', $goodsId],
- ['status', '=', 1],
- ['is_delete', '=', 0],
- ];
- // 评分
- $scoreType > 0 && $filter[] = ['score', '=', $scoreType];
- return $filter;
- }
- /**
- * 获取指定评分总数
- * @param int $goodsId
- * @return array|null|\think\Model
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getTotal(int $goodsId)
- {
- return $this->field([
- 'count(comment_id) AS `all`',
- 'count(score = 10 OR NULL) AS `praise`',
- 'count(score = 20 OR NULL) AS `review`',
- 'count(score = 30 OR NULL) AS `negative`',
- ])->where([
- 'goods_id' => $goodsId,
- 'is_delete' => 0,
- 'status' => 1
- ])->find();
- }
- public function getTotalAll(array $goodsIds)
- {
- return $this->field('goods_id,sum(score) as score_total')->where([
- 'is_delete' => 0,
- 'status' => 1])->whereIn('goods_id', $goodsIds)->group('goods_id')->select();
- }
- /**
- * 验证订单是否允许评价
- * @param OrderModel $order
- * @return boolean
- */
- public function checkOrderAllowComment(OrderModel $order)
- {
- // 验证订单是否已完成
- if ($order['order_status'] != 30) {
- $this->error = 'Not supported now.';
- return false;
- }
- // 验证订单是否已评价
- if ($order['is_comment'] == 1) {
- $this->error = 'The order has been reviewed';
- return false;
- }
- return true;
- }
- /**
- * 根据已完成订单商品 添加评价
- * @param OrderModel $order
- * @param $goodsList
- * @param array $data
- * @return boolean
- * @throws BaseException
- */
- public function increased(OrderModel $order, $goodsList, array $data)
- {
- // 生成 formData
- $formData = $this->formatFormData($data);
- // 生成评价数据
- $data = $this->createCommentData($order['order_id'], $goodsList, $formData);
- if (empty($data)) {
- $this->error = 'No content.';
- return false;
- }
- return $this->transaction(function () use ($order, $goodsList, $formData, $data) {
- // 记录评价内容
- $result = $this->addAll($data);
- // 记录评价图片`
- //$this->saveAllImages($result, $formData);
- // 更新订单评价状态
- $isComment = count($goodsList) === count($data);
- $this->updateOrderIsComment($order, $isComment, $result);
- return true;
- });
- }
- /**
- * 更新订单评价状态
- * @param OrderModel $order
- * @param $isComment
- * @param $commentList
- * @return array|false
- * @throws \Exception
- */
- private function updateOrderIsComment(OrderModel $order, $isComment, $commentList)
- {
- // 更新订单商品
- $orderGoodsData = [];
- foreach ($commentList as $comment) {
- $orderGoodsData[] = [
- 'where' => [
- 'order_goods_id' => $comment['order_goods_id'],
- ],
- 'data' => [
- 'is_comment' => 1
- ]
- ];
- }
- // 更新订单
- $isComment && $order->save(['is_comment' => 1]);
- return (new OrderGoodsModel)->updateAll($orderGoodsData);
- }
- /**
- * 生成评价数据
- * @param int $orderId
- * @param $goodsList
- * @param array $formData
- * @return array
- * @throws BaseException
- */
- private function createCommentData(int $orderId, $goodsList, array $formData)
- {
- $data = [];
- foreach ($goodsList as $goods) {
- if (!isset($formData[$goods['order_goods_id']])) {
- throwError('提交的数据不合法');
- }
- $commentItem = $formData[$goods['order_goods_id']];
- $commentItem['content'] = trim($commentItem['content']);
- !empty($commentItem['content']) && $data[$goods['order_goods_id']] = [
- 'score' => $commentItem['score'],
- 'content' => $commentItem['content'],
- 'is_picture' => !empty($commentItem['uploaded']),
- 'sort' => 100,
- 'status' => 1,
- 'user_id' => UserService::getCurrentLoginUserId(),
- 'order_id' => $orderId,
- 'goods_id' => $commentItem['goods_id'],
- 'order_goods_id' => $commentItem['order_goods_id'],
- 'store_id' => self::$storeId
- ];
- }
- return $data;
- }
- /**
- * 格式化 formData
- * @param array $data
- * @return array
- */
- private function formatFormData(array $data)
- {
- return helper::arrayColumn2Key($data, 'order_goods_id');
- }
- }
|