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

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

@ -27,6 +27,7 @@ class DishesCategoryLists extends BaseAdminDataLists implements ListsSearchInter
{ {
return [ return [
'=' => ['pid', 'name'], '=' => ['pid', 'name'],
]; ];
} }
@ -42,14 +43,26 @@ class DishesCategoryLists extends BaseAdminDataLists implements ListsSearchInter
*/ */
public function lists(): array public function lists(): array
{ {
return DishesCategory::where($this->searchWhere) $list = DishesCategory::where($this->searchWhere)
->field(['id', 'pid', 'name', 'create_time']) ->field(['id', 'pid', 'name', 'create_time'])
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc']) ->order(['id' => 'desc'])
->select() ->select()
->toArray(); ->toArray();
$list = linear_to_tree($list, 'children');
return $this->setDisable($list);
} }
public function setDisable($list)
{
foreach ($list as &$item) {
$item['disabled'] = $item['level'] > 0;
if (isset($item['children'])) {
$item['children'] = $this->setDisable($item['children']);
}
}
return $list;
}
/** /**
* @notes 获取数量 * @notes 获取数量

View File

@ -6,6 +6,7 @@ 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;
/** /**
@ -26,7 +27,7 @@ class DishesLists extends BaseAdminDataLists implements ListsSearchInterface
public function setSearch(): array public function setSearch(): array
{ {
return [ return [
'=' => ['name', 'dishes_category_id'], 'like' => ['name'],
]; ];
} }
@ -42,8 +43,12 @@ class DishesLists extends BaseAdminDataLists implements ListsSearchInterface
*/ */
public function lists(): array public function lists(): array
{ {
return Dishes::where($this->searchWhere) $query = Dishes::where($this->searchWhere);
->field(['id', 'name', 'dishes_category_id', 'create_time']) if (!empty($this->params['dishes_category_id'])) {
$dishesCategoryIds = DishesCategory::where('id', $this->params['dishes_category_id'])->whereOr('pid', $this->params['dishes_category_id'])->column('id');
$query->whereIn('dishes_category_id', $dishesCategoryIds);
}
return $query->field(['id', 'name', 'dishes_category_id', 'create_time'])
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc']) ->order(['id' => 'desc'])
->select() ->select()

View File

@ -27,6 +27,7 @@ class DishesProductLists extends BaseAdminDataLists implements ListsSearchInterf
{ {
return [ return [
'=' => ['dishes_id', 'product_id'], '=' => ['dishes_id', 'product_id'],
]; ];
} }
@ -43,6 +44,7 @@ class DishesProductLists extends BaseAdminDataLists implements ListsSearchInterf
public function lists(): array public function lists(): array
{ {
return DishesProduct::where($this->searchWhere) return DishesProduct::where($this->searchWhere)
->with(['unit', 'product', 'dishes'])
->field(['id', 'dishes_id', 'product_id', 'unit_id', 'nums', 'create_time']) ->field(['id', 'dishes_id', 'product_id', 'unit_id', 'nums', 'create_time'])
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc']) ->order(['id' => 'desc'])

View File

@ -6,6 +6,7 @@ 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;
/** /**
@ -26,7 +27,8 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
public function setSearch(): array public function setSearch(): array
{ {
return [ return [
'=' => ['category_id', 'unit_id', 'name', 'product_type'], '=' => ['unit_id', 'name', 'product_type'],
]; ];
} }
@ -42,11 +44,19 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
*/ */
public function lists(): array public function lists(): array
{ {
return Product::where($this->searchWhere) $query = Product::where($this->searchWhere);
->field(['id', 'category_id', 'unit_id', 'name', 'image', 'product_type', 'create_time']) if (!empty($this->params['category_id'])) {
$query->whereFindInSet('category_ids', $this->params['category_id']);
}
return $query->with(['unit', 'category'])
->field(['id', 'category_id', 'category_ids', 'unit_id', 'name', 'image', 'product_type', 'create_time'])
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc']) ->order(['id' => 'desc'])
->select() ->select()
->each(function ($item) {
$categories = ProductCategory::whereIn('id', $item['category_ids'])->column('name');
$item['categories'] = implode('/', $categories);
})
->toArray(); ->toArray();
} }

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

View File

@ -20,6 +20,10 @@ class DishesProductValidate extends BaseValidate
*/ */
protected $rule = [ protected $rule = [
'id' => 'require', 'id' => 'require',
'dishes_id' => 'require',
'product_id' => 'require',
'unit_id' => 'require',
'nums' => 'require',
]; ];
@ -29,6 +33,10 @@ class DishesProductValidate extends BaseValidate
*/ */
protected $field = [ protected $field = [
'id' => 'id', 'id' => 'id',
'dishes_id' => '菜品ID',
'product_id' => '商品ID',
'unit_id' => '单位ID',
'nums' => '数量',
]; ];
@ -52,7 +60,7 @@ class DishesProductValidate extends BaseValidate
*/ */
public function sceneEdit() public function sceneEdit()
{ {
return $this->only(['id']); return $this->only(['id', 'dishes_id', 'product_id', 'unit_id', 'nums']);
} }

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,7 +80,7 @@ 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)