宝体数据调用接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

121 lines
2.7 KiB

<?php
namespace app\common\model;
use think\Db;
/**
* 商品评价模型
* Class Comment
* @package app\common\model
*/
class Comment extends BaseModel
{
protected $name = 'comment';
/**
* 所属订单
* @return \think\model\relation\BelongsTo
*/
public function orderM()
{
return $this->belongsTo('Order');
}
/**
* 订单商品
* @return \think\model\relation\BelongsTo
*/
public function OrderGoods()
{
return $this->belongsTo('OrderGoods');
}
/**
* 关联用户表
* @return \think\model\relation\BelongsTo
*/
public function user()
{
return $this->belongsTo('User');
}
/**
* 关联评价图片表
* @return \think\model\relation\HasMany
*/
public function image()
{
return $this->hasMany('CommentImage')->order(['id' => 'asc']);
}
/**
* 评价详情
* @param $comment_id
* @return Comment|null
* @throws \think\exception\DbException
*/
public static function detail($comment_id)
{
return self::get($comment_id, ['user', 'orderM', 'OrderGoods', 'image.file']);
}
/**
* 更新记录
* @param $data
* @return bool
*/
public function edit($data)
{
// 开启事务
Db::startTrans();
try {
// 删除评价图片
$this->image()->delete();
// 添加评论图片
isset($data['images']) && $this->addCommentImages($data['images']);
// 是否为图片评价
$data['is_picture'] = !$this->image()->select()->isEmpty();
// 更新评论记录
$this->allowField(true)->save($data);
Db::commit();
return true;
} catch (\Exception $e) {
$this->error = $e->getMessage();
Db::rollback();
return false;
}
}
/**
* 添加评论图片
* @param $images
* @return int
*/
private function addCommentImages($images)
{
$data = array_map(function ($image_id) {
return [
'image_id' => $image_id,
'wxapp_id' => self::$wxapp_id
];
}, $images);
return $this->image()->saveAll($data);
}
/**
* 获取评价列表
* @return \think\Paginator
* @throws \think\exception\DbException
*/
public function getList()
{
return $this->with(['user', 'orderM', 'OrderGoods'])
->where('is_delete', '=', 0)
->order(['sort' => 'asc', 'create_time' => 'desc'])
->paginate(15, false, [
'query' => request()->request()
]);
}
}