123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\common\model;
- /**
- * 商品评价模型
- * Class Comment
- * @package app\common\model
- */
- class Comment extends BaseModel
- {
- // 定义表名
- protected $name = 'comment';
- // 定义主键
- protected $pk = 'comment_id';
- /**
- * 所属订单
- * @return \think\model\relation\BelongsTo
- */
- public function orderData()
- {
- return $this->belongsTo('Order');
- }
- /**
- * 订单商品
- * @return \think\model\relation\BelongsTo
- */
- public function orderGoods()
- {
- return $this->belongsTo('OrderGoods')
- ->field(['order_goods_id', 'goods_id', 'goods_name', 'image_id', 'goods_props', 'order_id']);
- }
- /**
- * 关联用户表
- * @return \think\model\relation\BelongsTo
- */
- public function user()
- {
- return $this->belongsTo('User')->field(['user_id', 'nick_name', 'avatar_id']);
- }
- /**
- * 关联评价图片表
- * @return \think\model\relation\HasMany
- */
- public function images()
- {
- return $this->hasMany('CommentImage')->order(['id']);
- }
- public function getStatusTextAttr($value, $data) {
- $text = str_replace([0,1,2], ['待审核', '已通过', '不通过'], $data['status']);
- return $text;
- }
- /**
- * 详情记录
- * @param int $commentId
- * @param array $with
- * @return static|array|null
- */
- public static function detail(int $commentId, array $with = [])
- {
- return static::get($commentId, $with);
- }
- /**
- * 添加评论图片
- * @param array $images
- * @return array|false
- */
- protected function addCommentImages(array $images)
- {
- $data = array_map(function ($imageId) {
- return [
- 'image_id' => $imageId,
- 'store_id' => self::$storeId
- ];
- }, $images);
- return $this->image()->saveAll($data) !== false;
- }
- /**
- * 累积点赞数
- * @param int $commentId ID
- * @param int $num 递增的数量
- * @return mixed
- */
- public static function setIncByField(int $commentId, $field = '', int $num = 1)
- {
- return (new static)->setInc([['comment_id', '=', $commentId]], $field, $num);
- }
- /**
- * 累积收藏数
- * @param int $commentId ID
- * @param int $num 递减的数量
- * @return mixed
- */
- public static function setDecByField(int $commentId, $field = '', int $num = 1)
- {
- return (new static)->setDec([['comment_id', '=', $commentId]], $field, $num);
- }
- }
|