调试菜品、菜品商品、商品

This commit is contained in:
lewis 2025-07-15 16:53:05 +08:00
parent ea0b6f1f65
commit 31f910e196
12 changed files with 435 additions and 358 deletions

View File

@ -1,65 +1,78 @@
<?php <?php
namespace app\admin\lists; namespace app\admin\lists;
use app\admin\lists\BaseAdminDataLists; use app\admin\lists\BaseAdminDataLists;
use app\common\model\DishesCategory; use app\common\model\DishesCategory;
use app\common\lists\ListsSearchInterface; use app\common\lists\ListsSearchInterface;
/** /**
* DishesCategory列表 * DishesCategory列表
* Class DishesCategoryLists * Class DishesCategoryLists
* @package app\admin\lists * @package app\admin\lists
*/ */
class DishesCategoryLists extends BaseAdminDataLists implements ListsSearchInterface class DishesCategoryLists extends BaseAdminDataLists implements ListsSearchInterface
{ {
/** /**
* @notes 设置搜索条件 * @notes 设置搜索条件
* @return \string[][] * @return \string[][]
* @author likeadmin * @author likeadmin
* @date 2025/07/11 11:41 * @date 2025/07/11 11:41
*/ */
public function setSearch(): array public function setSearch(): array
{ {
return [ return [
'=' => ['pid', 'name'], '=' => ['pid', 'name'],
];
} ];
}
/**
* @notes 获取列表 /**
* @return array * @notes 获取列表
* @throws \think\db\exception\DataNotFoundException * @return array
* @throws \think\db\exception\DbException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\DbException
* @author likeadmin * @throws \think\db\exception\ModelNotFoundException
* @date 2025/07/11 11:41 * @author likeadmin
*/ * @date 2025/07/11 11:41
public function lists(): array */
{ public function lists(): array
return DishesCategory::where($this->searchWhere) {
->field(['id', 'pid', 'name', 'create_time']) $list = DishesCategory::where($this->searchWhere)
->limit($this->limitOffset, $this->limitLength) ->field(['id', 'pid', 'name', 'create_time'])
->order(['id' => 'desc']) ->limit($this->limitOffset, $this->limitLength)
->select() ->order(['id' => 'desc'])
->toArray(); ->select()
} ->toArray();
$list = linear_to_tree($list, 'children');
return $this->setDisable($list);
/** }
* @notes 获取数量
* @return int public function setDisable($list)
* @author likeadmin {
* @date 2025/07/11 11:41 foreach ($list as &$item) {
*/ $item['disabled'] = $item['level'] > 0;
public function count(): int if (isset($item['children'])) {
{ $item['children'] = $this->setDisable($item['children']);
return DishesCategory::where($this->searchWhere)->count(); }
} }
return $list;
} }
/**
* @notes 获取数量
* @return int
* @author likeadmin
* @date 2025/07/11 11:41
*/
public function count(): int
{
return DishesCategory::where($this->searchWhere)->count();
}
}

View File

@ -1,65 +1,70 @@
<?php <?php
namespace app\admin\lists; namespace app\admin\lists;
use app\admin\lists\BaseAdminDataLists; use app\admin\lists\BaseAdminDataLists;
use app\common\model\Dishes; use app\common\model\Dishes;
use app\common\lists\ListsSearchInterface; use app\common\lists\ListsSearchInterface;
use app\common\model\DishesCategory;
/**
* Dishes列表 /**
* Class DishesLists * Dishes列表
* @package app\admin\lists * Class DishesLists
*/ * @package app\admin\lists
class DishesLists extends BaseAdminDataLists implements ListsSearchInterface */
{ class DishesLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件 /**
* @return \string[][] * @notes 设置搜索条件
* @author likeadmin * @return \string[][]
* @date 2025/07/11 11:41 * @author likeadmin
*/ * @date 2025/07/11 11:41
public function setSearch(): array */
{ public function setSearch(): array
return [ {
'=' => ['name', 'dishes_category_id'], return [
]; 'like' => ['name'],
} ];
}
/**
* @notes 获取列表 /**
* @return array * @notes 获取列表
* @throws \think\db\exception\DataNotFoundException * @return array
* @throws \think\db\exception\DbException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\DbException
* @author likeadmin * @throws \think\db\exception\ModelNotFoundException
* @date 2025/07/11 11:41 * @author likeadmin
*/ * @date 2025/07/11 11:41
public function lists(): array */
{ public function lists(): array
return Dishes::where($this->searchWhere) {
->field(['id', 'name', 'dishes_category_id', 'create_time']) $query = Dishes::where($this->searchWhere);
->limit($this->limitOffset, $this->limitLength) if (!empty($this->params['dishes_category_id'])) {
->order(['id' => 'desc']) $dishesCategoryIds = DishesCategory::where('id', $this->params['dishes_category_id'])->whereOr('pid', $this->params['dishes_category_id'])->column('id');
->select() $query->whereIn('dishes_category_id', $dishesCategoryIds);
->toArray(); }
} return $query->field(['id', 'name', 'dishes_category_id', 'create_time'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
/** ->select()
* @notes 获取数量 ->toArray();
* @return int }
* @author likeadmin
* @date 2025/07/11 11:41
*/ /**
public function count(): int * @notes 获取数量
{ * @return int
return Dishes::where($this->searchWhere)->count(); * @author likeadmin
} * @date 2025/07/11 11:41
*/
} public function count(): int
{
return Dishes::where($this->searchWhere)->count();
}
}

View File

@ -1,65 +1,67 @@
<?php <?php
namespace app\admin\lists; namespace app\admin\lists;
use app\admin\lists\BaseAdminDataLists; use app\admin\lists\BaseAdminDataLists;
use app\common\model\DishesProduct; use app\common\model\DishesProduct;
use app\common\lists\ListsSearchInterface; use app\common\lists\ListsSearchInterface;
/** /**
* DishesProduct列表 * DishesProduct列表
* Class DishesProductLists * Class DishesProductLists
* @package app\admin\lists * @package app\admin\lists
*/ */
class DishesProductLists extends BaseAdminDataLists implements ListsSearchInterface class DishesProductLists extends BaseAdminDataLists implements ListsSearchInterface
{ {
/** /**
* @notes 设置搜索条件 * @notes 设置搜索条件
* @return \string[][] * @return \string[][]
* @author likeadmin * @author likeadmin
* @date 2025/07/11 11:41 * @date 2025/07/11 11:41
*/ */
public function setSearch(): array public function setSearch(): array
{ {
return [ return [
'=' => ['dishes_id', 'product_id'], '=' => ['dishes_id', 'product_id'],
];
} ];
}
/**
* @notes 获取列表 /**
* @return array * @notes 获取列表
* @throws \think\db\exception\DataNotFoundException * @return array
* @throws \think\db\exception\DbException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\DbException
* @author likeadmin * @throws \think\db\exception\ModelNotFoundException
* @date 2025/07/11 11:41 * @author likeadmin
*/ * @date 2025/07/11 11:41
public function lists(): array */
{ public function lists(): array
return DishesProduct::where($this->searchWhere) {
->field(['id', 'dishes_id', 'product_id', 'unit_id', 'nums', 'create_time']) return DishesProduct::where($this->searchWhere)
->limit($this->limitOffset, $this->limitLength) ->with(['unit', 'product', 'dishes'])
->order(['id' => 'desc']) ->field(['id', 'dishes_id', 'product_id', 'unit_id', 'nums', 'create_time'])
->select() ->limit($this->limitOffset, $this->limitLength)
->toArray(); ->order(['id' => 'desc'])
} ->select()
->toArray();
}
/**
* @notes 获取数量
* @return int /**
* @author likeadmin * @notes 获取数量
* @date 2025/07/11 11:41 * @return int
*/ * @author likeadmin
public function count(): int * @date 2025/07/11 11:41
{ */
return DishesProduct::where($this->searchWhere)->count(); public function count(): int
} {
return DishesProduct::where($this->searchWhere)->count();
} }
}

View File

@ -1,65 +1,75 @@
<?php <?php
namespace app\admin\lists; namespace app\admin\lists;
use app\admin\lists\BaseAdminDataLists; use app\admin\lists\BaseAdminDataLists;
use app\common\model\Product; use app\common\model\Product;
use app\common\lists\ListsSearchInterface; use app\common\lists\ListsSearchInterface;
use app\common\model\ProductCategory;
/**
* Product列表 /**
* Class ProductLists * Product列表
* @package app\admin\lists * Class ProductLists
*/ * @package app\admin\lists
class ProductLists extends BaseAdminDataLists implements ListsSearchInterface */
{ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件 /**
* @return \string[][] * @notes 设置搜索条件
* @author likeadmin * @return \string[][]
* @date 2025/07/11 11:41 * @author likeadmin
*/ * @date 2025/07/11 11:41
public function setSearch(): array */
{ public function setSearch(): array
return [ {
'=' => ['category_id', 'unit_id', 'name', 'product_type'], return [
]; '=' => ['unit_id', 'name', 'product_type'],
}
];
}
/**
* @notes 获取列表
* @return array /**
* @throws \think\db\exception\DataNotFoundException * @notes 获取列表
* @throws \think\db\exception\DbException * @return array
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\DataNotFoundException
* @author likeadmin * @throws \think\db\exception\DbException
* @date 2025/07/11 11:41 * @throws \think\db\exception\ModelNotFoundException
*/ * @author likeadmin
public function lists(): array * @date 2025/07/11 11:41
{ */
return Product::where($this->searchWhere) public function lists(): array
->field(['id', 'category_id', 'unit_id', 'name', 'image', 'product_type', 'create_time']) {
->limit($this->limitOffset, $this->limitLength) $query = Product::where($this->searchWhere);
->order(['id' => 'desc']) if (!empty($this->params['category_id'])) {
->select() $query->whereFindInSet('category_ids', $this->params['category_id']);
->toArray(); }
} return $query->with(['unit', 'category'])
->field(['id', 'category_id', 'category_ids', 'unit_id', 'name', 'image', 'product_type', 'create_time'])
->limit($this->limitOffset, $this->limitLength)
/** ->order(['id' => 'desc'])
* @notes 获取数量 ->select()
* @return int ->each(function ($item) {
* @author likeadmin $categories = ProductCategory::whereIn('id', $item['category_ids'])->column('name');
* @date 2025/07/11 11:41 $item['categories'] = implode('/', $categories);
*/ })
public function count(): int ->toArray();
{ }
return Product::where($this->searchWhere)->count();
}
/**
} * @notes 获取数量
* @return int
* @author likeadmin
* @date 2025/07/11 11:41
*/
public function count(): int
{
return Product::where($this->searchWhere)->count();
}
}

View File

@ -5,6 +5,7 @@ namespace app\admin\logic;
use app\common\model\Product; use app\common\model\Product;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\ProductCategory;
use think\facade\Db; use think\facade\Db;
@ -28,8 +29,10 @@ class ProductLogic extends BaseLogic
{ {
Db::startTrans(); Db::startTrans();
try { try {
$params['category_ids'] = ProductCategory::getParentUntilRoot($params['category_id']);
Product::create([ Product::create([
'category_id' => $params['category_id'], 'category_id' => $params['category_id'],
'category_ids' => implode(',', $params['category_ids']),
'unit_id' => $params['unit_id'], 'unit_id' => $params['unit_id'],
'name' => $params['name'], 'name' => $params['name'],
'image' => $params['image'], 'image' => $params['image'],
@ -59,8 +62,10 @@ class ProductLogic extends BaseLogic
{ {
Db::startTrans(); Db::startTrans();
try { try {
$params['category_ids'] = ProductCategory::getParentUntilRoot($params['category_id']);
Product::where('id', $params['id'])->update([ Product::where('id', $params['id'])->update([
'category_id' => $params['category_id'], 'category_id' => $params['category_id'],
'category_ids' => implode(',', $params['category_ids']),
'unit_id' => $params['unit_id'], 'unit_id' => $params['unit_id'],
'name' => $params['name'], 'name' => $params['name'],
'image' => $params['image'], 'image' => $params['image'],
@ -78,7 +83,6 @@ class ProductLogic extends BaseLogic
} }
} }
/** /**
* @notes 删除 * @notes 删除
* @param array $params * @param array $params
@ -103,4 +107,4 @@ class ProductLogic extends BaseLogic
{ {
return Product::findOrEmpty($params['id'])->toArray(); return Product::findOrEmpty($params['id'])->toArray();
} }
} }

View File

@ -1,82 +1,90 @@
<?php <?php
namespace app\admin\validate; namespace app\admin\validate;
use app\common\validate\BaseValidate; use app\common\validate\BaseValidate;
/** /**
* DishesProduct验证器 * DishesProduct验证器
* Class DishesProductValidate * Class DishesProductValidate
* @package app\admin\validate * @package app\admin\validate
*/ */
class DishesProductValidate extends BaseValidate class DishesProductValidate extends BaseValidate
{ {
/** /**
* 设置校验规则 * 设置校验规则
* @var string[] * @var string[]
*/ */
protected $rule = [ protected $rule = [
'id' => 'require', 'id' => 'require',
]; 'dishes_id' => 'require',
'product_id' => 'require',
'unit_id' => 'require',
/** 'nums' => 'require',
* 参数描述 ];
* @var string[]
*/
protected $field = [ /**
'id' => 'id', * 参数描述
]; * @var string[]
*/
protected $field = [
/** 'id' => 'id',
* @notes 添加场景 'dishes_id' => '菜品ID',
* @return DishesProductValidate 'product_id' => '商品ID',
* @author likeadmin 'unit_id' => '单位ID',
* @date 2025/07/11 11:41 'nums' => '数量',
*/ ];
public function sceneAdd()
{
return $this->remove('id', true); /**
} * @notes 添加场景
* @return DishesProductValidate
* @author likeadmin
/** * @date 2025/07/11 11:41
* @notes 编辑场景 */
* @return DishesProductValidate public function sceneAdd()
* @author likeadmin {
* @date 2025/07/11 11:41 return $this->remove('id', true);
*/ }
public function sceneEdit()
{
return $this->only(['id']); /**
} * @notes 编辑场景
* @return DishesProductValidate
* @author likeadmin
/** * @date 2025/07/11 11:41
* @notes 删除场景 */
* @return DishesProductValidate public function sceneEdit()
* @author likeadmin {
* @date 2025/07/11 11:41 return $this->only(['id', 'dishes_id', 'product_id', 'unit_id', 'nums']);
*/ }
public function sceneDelete()
{
return $this->only(['id']); /**
} * @notes 删除场景
* @return DishesProductValidate
* @author likeadmin
/** * @date 2025/07/11 11:41
* @notes 详情场景 */
* @return DishesProductValidate public function sceneDelete()
* @author likeadmin {
* @date 2025/07/11 11:41 return $this->only(['id']);
*/ }
public function sceneDetail()
{
return $this->only(['id']); /**
} * @notes 详情场景
* @return DishesProductValidate
} * @author likeadmin
* @date 2025/07/11 11:41
*/
public function sceneDetail()
{
return $this->only(['id']);
}
}

View File

@ -18,5 +18,19 @@ class DishesProduct extends BaseModel
protected $name = 'dishes_product'; protected $name = 'dishes_product';
protected $deleteTime = 'delete_time'; protected $deleteTime = 'delete_time';
public function dishes()
} {
return $this->hasOne(Dishes::class, 'id', 'dishes_id')->field('id,name');
}
public function product()
{
return $this->hasOne(Product::class, 'id', 'product_id')->field('id,name,image');
}
public function unit()
{
return $this->hasOne(ProductUnit::class, 'id', 'unit_id')->field('id,name');
}
}

View File

@ -18,5 +18,14 @@ class Product extends BaseModel
protected $name = 'product'; protected $name = 'product';
protected $deleteTime = 'delete_time'; protected $deleteTime = 'delete_time';
public function unit()
} {
return $this->hasOne(ProductUnit::class, 'id', 'unit_id')->field('id,name,conversion_value');
}
public function category()
{
return $this->hasOne(ProductCategory::class, 'id', 'category_id')->field('id,name,pid');
}
}

View File

@ -18,5 +18,15 @@ class ProductCategory extends BaseModel
protected $name = 'product_category'; protected $name = 'product_category';
protected $deleteTime = 'delete_time'; protected $deleteTime = 'delete_time';
public static function getParentUntilRoot($id)
} {
static $parentIds = [];
$parentIds[] = $id;
$pid = self::where('id', $id)->value('pid');
if ($pid) {
self::getParentUntilRoot($pid);
}
return $parentIds;
}
}

View File

@ -41,20 +41,20 @@ abstract class Server
if (empty($this->file)) { if (empty($this->file)) {
throw new Exception('未找到上传文件的信息'); throw new Exception('未找到上传文件的信息');
} }
$this->file->extension = pathinfo($this->file->getUploadName(), PATHINFO_EXTENSION); $extension = pathinfo($this->file->getUploadName(), PATHINFO_EXTENSION);
$finfo = finfo_open(FILEINFO_MIME_TYPE); $finfo = finfo_open(FILEINFO_MIME_TYPE);
$this->file->getMime = finfo_file($finfo, $this->file->getPathname()); $mimeType = finfo_file($finfo, $this->file->getPathname());
// 校验上传文件后缀 // 校验上传文件后缀
$limit = array_merge(config('project.file_image'), config('project.file_video')); $limit = array_merge(config('project.file_image'), config('project.file_video'));
if (!in_array(strtolower($this->file->extension), $limit)) { if (!in_array(strtolower($extension), $limit)) {
throw new Exception('不允许上传' . $this->file->extension . '后缀文件'); throw new Exception('不允许上传' . $extension . '后缀文件');
} }
// 文件信息 // 文件信息
$this->fileInfo = [ $this->fileInfo = [
'ext' => $this->file->extension, 'ext' => $extension,
'size' => $this->file->getSize(), 'size' => $this->file->getSize(),
'mime' => $this->file->getMime, 'mime' => $mimeType,
'name' => $this->file->getUploadName(), 'name' => $this->file->getUploadName(),
'realPath' => $this->file->getRealPath(), 'realPath' => $this->file->getRealPath(),
]; ];

View File

@ -17,6 +17,7 @@ declare(strict_types=1);
namespace app\common\validate; namespace app\common\validate;
use app\common\service\JsonService; use app\common\service\JsonService;
use support\exception\BusinessException;
use taoser\Validate; use taoser\Validate;
class BaseValidate extends Validate class BaseValidate extends Validate
@ -79,9 +80,9 @@ class BaseValidate extends Validate
if (!$result) { if (!$result) {
$exception = is_array($this->error) ? implode(';', $this->error) : $this->error; $exception = is_array($this->error) ? implode(';', $this->error) : $this->error;
JsonService::throw($exception); throw new BusinessException($exception);
} }
// 3.成功返回数据 // 3.成功返回数据
return $params; return $params;
} }
} }

View File

@ -92,6 +92,7 @@ function create_password(string $plaintext, string $salt) : string
* @param string $parent_id_name 数组祖先id名 * @param string $parent_id_name 数组祖先id名
* @param int $level 此值请勿给参数 * @param int $level 此值请勿给参数
* @param int $parent_id 此值请勿给参数 * @param int $parent_id 此值请勿给参数
* @param int $level 等级
* @return array * @return array
*/ */
function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0, $level = 0) function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0, $level = 0)