门店评论增删改查

wysf
ztt 9 months ago
parent ed8d5d663d
commit bf5db8ca89
  1. 1
      app/api/service/Goods.php
  2. 10
      app/common/model/store/shop/Comment.php
  3. 98
      app/store/controller/shop/Comment.php
  4. 178
      app/store/model/store/shop/Comment.php

@ -235,6 +235,7 @@ class Goods extends GoodsService
$info['evaluate'] = Comment::with('file')
->field(['nickname,content,image_id'])
->hidden(['file'])
->where('is_delete', '=', 0)
->where('shop_id','=', $info['shop_id'])
->select();
// $info['imageList'] = [

@ -47,4 +47,14 @@ class Comment extends BaseModel
return $this->hasOne(UploadFile::class,'file_id', 'image_id')->bind(['image' => 'preview_url']);
}
/**
* 评论详情
* @param $where
* @return static|array|null
*/
public static function detail($where)
{
return static::get($where);
}
}

@ -0,0 +1,98 @@
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\store\controller\shop;
use think\response\Json;
use app\store\controller\Controller;
use app\store\model\store\shop\Comment as CommentModel;
/**
* 门店评论控制器
* Class Clerk
* @package app\store\controller\shop
*/
class Comment extends Controller
{
/**
* 列表记录
* @return Json
* @throws \think\db\exception\DbException
*/
public function list(): Json
{
// 店员列表
$model = new CommentModel;
$list = $model->getList($this->request->param());
return $this->renderSuccess(compact('list'));
}
/**
* 全部记录
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function all(): Json
{
// 店员列表
$model = new CommentModel;
$list = $model->getAll($this->request->param());
return $this->renderSuccess(compact('list'));
}
/**
* 添加评论
* @return Json
*/
public function add(): Json
{
// 新增记录
$model = new CommentModel;
if ($model->add($this->postForm())) {
return $this->renderSuccess('添加成功');
}
return $this->renderError($model->getError() ?: '添加失败');
}
/**
* 编辑店员
* @param int $commentId
* @return Json
*/
public function edit(int $commentId): Json
{
// 评论详情
$model = CommentModel::detail($commentId);
// 更新记录
if ($model->edit($this->postForm())) {
return $this->renderSuccess('更新成功');
}
return $this->renderError($model->getError() ?: '更新失败');
}
/**
* 删除店员
* @param int $commentId
* @return Json
*/
public function delete(int $commentId): Json
{
// 店员详情
$model = CommentModel::detail($commentId);
if (!$model->setDelete()) {
return $this->renderError($model->getError() ?: '删除失败');
}
return $this->renderSuccess('删除成功');
}
}

@ -0,0 +1,178 @@
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\store\model\store\shop;
use app\common\model\store\shop\Comment as CommenModel;
/**
* 商家门店店员模型
* Class Clerk
* @package app\store\model\store\shop
*/
class Comment extends CommenModel
{
// 表单验证场景: 新增
const FORM_SCENE_ADD = 'add';
// 表单验证场景: 编辑
const FORM_SCENE_EDIT = 'edit';
/**
* 获取列表数据
* @param array $param
* @return \think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getAll(array $param = []): \think\Collection
{
// 检索查询条件
$filter = $this->getFilter($param);
// 查询列表数据
return $this->with(['image'])
->where($filter)
->where('is_delete', '=', 0)
->order(['create_time' => 'desc', $this->getPk()])
->select();
}
/**
* 获取列表数据
* @param array $param
* @return \think\Paginator
* @throws \think\db\exception\DbException
*/
public function getList(array $param = []): \think\Paginator
{
// 检索查询条件
$filter = $this->getFilter($param);
// 查询列表数据
return $this->with(['image'])
->where($filter)
->where('is_delete', '=', 0)
->order(['create_time' => 'desc', $this->getPk()])
->paginate(15);
}
/**
* 检索查询条件
* @param array $param
* @return array
*/
private function getFilter(array $param = []): array
{
// 默认查询参数
$params = $this->setQueryDefaultValue($param, [
'shop_id' => 0, // 门店id
'search' => '', // 搜索关键词: 姓名/手机号
'status' => '', // 门店状态(0禁用 1启用)
]);
// 检索查询条件
$filter = [];
// 门店id
$params['shop_id'] > 0 && $filter[] = ['shop_id', '=', (int)$params['shop_id']];
// 门店状态
is_numeric($params['status']) && $filter[] = ['status', '=', (int)$params['status']];
// 搜索关键词
!empty($params['search']) && $filter[] = ['nickname|content', 'like', "%{$params['search']}%"];
return $filter;
}
/**
* 新增记录
* @param array $data
* @return bool
*/
public function add(array $data): bool
{
// 表单验证
if (!$this->validateForm($data, self::FORM_SCENE_ADD)) {
return false;
}
$data['store_id'] = self::$storeId;
return $this->save($data);
}
/**
* 编辑记录
* @param array $data
* @return bool|false
*/
public function edit(array $data): bool
{
// 表单验证
if (!$this->validateForm($data, self::FORM_SCENE_EDIT, $this)) {
return false;
}
return $this->save($data) !== false;
}
/**
* 软删除
* @return bool|false
*/
public function setDelete(): bool
{
return $this->save(['is_delete' => 1]);
}
/**
* 表单验证
* @param $data
* @param string $scene
* @param mixed $detail
* @return bool
*/
private function validateForm($data, string $scene, $detail = null): bool
{
if ($scene === self::FORM_SCENE_ADD) {
if (empty($data['nickname'])) {
$this->error = '请选择指定的会员';
return false;
}
if (empty($data['content'])) {
$this->error = '请输入评论内容';
return false;
}
if (empty($data['image_id'])) {
$this->error = '请输入图片ID';
return false;
}
}
if ($scene === self::FORM_SCENE_EDIT) {
if (empty($data['image_id'])) {
$this->error = '请输入图片ID';
return false;
}
}
return true;
}
/**
* 验证门店是否已存在该店员
* @param int $userId
* @param int $shopId
* @return bool
*/
private function isExistForShop(int $userId, int $shopId): bool
{
$model = new static;
return (bool)$model->where('user_id', '=', $userId)
->where('shop_id', '=', $shopId)
->where('is_delete', '=', 0)
->value($model->getPk());
}
}
Loading…
Cancel
Save