'int', 'platform_id' =>'int', 'language_id' =>'int', 'site_id' =>'int', 'avatar' =>'string', 'name' =>'string', 'nickname' =>'string', 'comment' =>'string', 'url' =>'string', 'sort' =>'int', 'create_time' =>'int', 'update_time' =>'int', ]; protected $disuse = ['name']; /** * 反馈附件关联 * @return \think\model\relation\HasMany */ public function CommentImage() { return $this->hasMany(CommentImage::class); } /** * 获取列表 * @param array $data 查询条件 * @param mixed $simple 模糊搜索条件 * @param mixed $focus 精准搜索条件 * @param null $sort * @param null $field * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getAllList(array $data,$simple = null,$focus = null,$sort = null,$field = null):array { $fields = array_keys($this->schema); $collection = $this->field($field)->where(function ($query) use ($data,$fields,$simple,$focus) { if (in_array('platform_id',$fields)) { $query->where('platform_id',$data['platform_id']); } //模糊查询条件 if (!empty($simple)) { foreach ($simple as $key => $item) { if (in_array($key,$fields)) { $query->where($key,'like',"%".$item."%"); } } } //精准查询 if (!empty($focus)) { foreach ($focus as $key => $item) { if (in_array($key,$fields)) { $query->where($key,$item); } } } }); $newCommentImage = []; if (isset($data['page']) && isset($data['size'])) { $count = $collection->count() ?: 0; $page = $collection->limit((int)$data['size'])->page((int)$data['page'])->order($sort)->with('CommentImage')->select()->toArray(); if (!empty($page)) { foreach ($page as $key => $value) { $resData[$key]['language_id'] = $value['site_id']; if (isset($value['CommentImage']) && !empty($value['CommentImage'])) { foreach ($value['CommentImage'] as $key1 => $value1) { $newCommentImage[$key1]['name'] = basename($value1['image']); // $newCommentImage[$key1]['url'] = Env::get('QINIU.cdn') . $value1['image']; $newCommentImage[$key1]['url'] = $value1['image']; } $page[$key]['CommentImage'] = $newCommentImage; } } } return ['page'=>$page,'count'=>$count]; } else { $resData = $collection->order($sort)->with('CommentImage')->select()->toArray(); if (!empty($resData)) { if ($resData['CommentImage']) { foreach ($resData as $key => $value) { $resData[$key]['language_id'] = $value['site_id']; foreach ($value['CommentImage'] as $key1 => $value1) { $newCommentImage[$key1]['name'] = basename($value1['image']); // $newCommentImage[$key1]['url'] = Env::get('QINIU.cdn') . $value1['image']; $newCommentImage[$key1]['url'] = $value1['image']; } $resData[$key]['CommentImage'] = $newCommentImage; } } } return $resData; } } /** * 新增数据 * @param $data * @return bool */ public function addData($data):bool { $this->startTrans(); try { $data['avatar'] = doImageURLtoRelativePath($data['avatar']); $comment = $this->create($data); //处理数据结构 if (!empty($data['CommentImage'])) { foreach ($data['CommentImage'] as &$value) { $value['comment_id'] = $comment->id; $value['image'] = doImageURLtoRelativePath($value['url']); } //模型关联新增 $comment->CommentImage()->saveAll($data['CommentImage']); } $this->commit(); return true; } catch (\Exception $e) { $this->rollback(); return false; } } /** * 获取详情 * @param array $data * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getInfo(array $data):array { $comment = $this->find($data['id']); $comment['CommentImage'] = $this->CommentImage()->where('comment_id',$data['id'])->select(); if ($comment['CommentImage']) { $newImage = []; foreach ($comment['CommentImage'] as $key1 => $value1) { $newImage[$key1]['name'] = basename($value1['image']); $newImage[$key1]['url'] = $value1['image']; } $comment['CommentImage'] = $newImage; } return $comment->toArray(); } /** * 编辑数据 * @param array $data * @return bool */ public function editData(array $data):bool { $this->startTrans(); try { $data['comment_image'] = $data['CommentImage']; unset($data['CommentImage']); $comment = $this->find($data['id']); $data['avatar'] = doImageURLtoRelativePath($data['avatar']); $result1 = $comment->save($data); //先将原先的全部删除 if (!empty($data['comment_image'])) { $result2 = $comment->CommentImage->delete(); if ($result1 && $result2) { //处理数据结构 $newCommentImage = []; foreach ($data['comment_image'] as $key => $value) { $newCommentImage[] = [ 'comment_id' => $data['id'], 'image' => doImageURLtoRelativePath($value['url']), ]; } $data['comment_image'] = $newCommentImage; //新增 $comment->CommentImage()->saveAll($data['comment_image']); } } $this->commit(); return true; } catch (\Exception $e) { $this->rollback(); return false; } } /** * 删除数据 * @param array $data * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function delData(array $data):bool { $comment = $this->find($data['id']); $result1 = $comment->delete(); //先将原先的全部删除 $result2 = $comment->CommentImage->delete();; return $result1 && $result2; } }