diff --git a/app/admin/lists/DishesCategoryLists.php b/app/admin/lists/DishesCategoryLists.php index 1c83e03..f16dfbe 100644 --- a/app/admin/lists/DishesCategoryLists.php +++ b/app/admin/lists/DishesCategoryLists.php @@ -1,65 +1,78 @@ - ['pid', 'name'], - ]; - } - - - /** - * @notes 获取列表 - * @return array - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function lists(): array - { - return DishesCategory::where($this->searchWhere) - ->field(['id', 'pid', 'name', 'create_time']) - ->limit($this->limitOffset, $this->limitLength) - ->order(['id' => 'desc']) - ->select() - ->toArray(); - } - - - /** - * @notes 获取数量 - * @return int - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function count(): int - { - return DishesCategory::where($this->searchWhere)->count(); - } - -} \ No newline at end of file + ['pid', 'name'], + + ]; + } + + + /** + * @notes 获取列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function lists(): array + { + $list = DishesCategory::where($this->searchWhere) + ->field(['id', 'pid', 'name', 'create_time']) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select() + ->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 获取数量 + * @return int + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function count(): int + { + return DishesCategory::where($this->searchWhere)->count(); + } + +} diff --git a/app/admin/lists/DishesLists.php b/app/admin/lists/DishesLists.php index 140c0b3..52b1e35 100644 --- a/app/admin/lists/DishesLists.php +++ b/app/admin/lists/DishesLists.php @@ -1,65 +1,70 @@ - ['name', 'dishes_category_id'], - ]; - } - - - /** - * @notes 获取列表 - * @return array - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function lists(): array - { - return Dishes::where($this->searchWhere) - ->field(['id', 'name', 'dishes_category_id', 'create_time']) - ->limit($this->limitOffset, $this->limitLength) - ->order(['id' => 'desc']) - ->select() - ->toArray(); - } - - - /** - * @notes 获取数量 - * @return int - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function count(): int - { - return Dishes::where($this->searchWhere)->count(); - } - -} \ No newline at end of file + ['name'], + ]; + } + + + /** + * @notes 获取列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function lists(): array + { + $query = Dishes::where($this->searchWhere); + 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) + ->order(['id' => 'desc']) + ->select() + ->toArray(); + } + + + /** + * @notes 获取数量 + * @return int + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function count(): int + { + return Dishes::where($this->searchWhere)->count(); + } + +} diff --git a/app/admin/lists/DishesProductLists.php b/app/admin/lists/DishesProductLists.php index aed4f44..655bda9 100644 --- a/app/admin/lists/DishesProductLists.php +++ b/app/admin/lists/DishesProductLists.php @@ -1,65 +1,67 @@ - ['dishes_id', 'product_id'], - ]; - } - - - /** - * @notes 获取列表 - * @return array - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function lists(): array - { - return DishesProduct::where($this->searchWhere) - ->field(['id', 'dishes_id', 'product_id', 'unit_id', 'nums', 'create_time']) - ->limit($this->limitOffset, $this->limitLength) - ->order(['id' => 'desc']) - ->select() - ->toArray(); - } - - - /** - * @notes 获取数量 - * @return int - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function count(): int - { - return DishesProduct::where($this->searchWhere)->count(); - } - -} \ No newline at end of file + ['dishes_id', 'product_id'], + + ]; + } + + + /** + * @notes 获取列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function lists(): array + { + return DishesProduct::where($this->searchWhere) + ->with(['unit', 'product', 'dishes']) + ->field(['id', 'dishes_id', 'product_id', 'unit_id', 'nums', 'create_time']) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select() + ->toArray(); + } + + + /** + * @notes 获取数量 + * @return int + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function count(): int + { + return DishesProduct::where($this->searchWhere)->count(); + } + +} diff --git a/app/admin/lists/ProductLists.php b/app/admin/lists/ProductLists.php index e783e38..0f0ba93 100644 --- a/app/admin/lists/ProductLists.php +++ b/app/admin/lists/ProductLists.php @@ -1,65 +1,75 @@ - ['category_id', 'unit_id', 'name', 'product_type'], - ]; - } - - - /** - * @notes 获取列表 - * @return array - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function lists(): array - { - return Product::where($this->searchWhere) - ->field(['id', 'category_id', 'unit_id', 'name', 'image', 'product_type', 'create_time']) - ->limit($this->limitOffset, $this->limitLength) - ->order(['id' => 'desc']) - ->select() - ->toArray(); - } - - - /** - * @notes 获取数量 - * @return int - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function count(): int - { - return Product::where($this->searchWhere)->count(); - } - -} \ No newline at end of file + ['unit_id', 'name', 'product_type'], + + ]; + } + + + /** + * @notes 获取列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function lists(): array + { + $query = Product::where($this->searchWhere); + 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) + ->order(['id' => 'desc']) + ->select() + ->each(function ($item) { + $categories = ProductCategory::whereIn('id', $item['category_ids'])->column('name'); + $item['categories'] = implode('/', $categories); + }) + ->toArray(); + } + + + /** + * @notes 获取数量 + * @return int + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function count(): int + { + return Product::where($this->searchWhere)->count(); + } + +} diff --git a/app/admin/logic/ProductLogic.php b/app/admin/logic/ProductLogic.php index b2d931b..311566b 100644 --- a/app/admin/logic/ProductLogic.php +++ b/app/admin/logic/ProductLogic.php @@ -5,6 +5,7 @@ namespace app\admin\logic; use app\common\model\Product; use app\common\logic\BaseLogic; +use app\common\model\ProductCategory; use think\facade\Db; @@ -28,8 +29,10 @@ class ProductLogic extends BaseLogic { Db::startTrans(); try { + $params['category_ids'] = ProductCategory::getParentUntilRoot($params['category_id']); Product::create([ 'category_id' => $params['category_id'], + 'category_ids' => implode(',', $params['category_ids']), 'unit_id' => $params['unit_id'], 'name' => $params['name'], 'image' => $params['image'], @@ -59,8 +62,10 @@ class ProductLogic extends BaseLogic { Db::startTrans(); try { + $params['category_ids'] = ProductCategory::getParentUntilRoot($params['category_id']); Product::where('id', $params['id'])->update([ 'category_id' => $params['category_id'], + 'category_ids' => implode(',', $params['category_ids']), 'unit_id' => $params['unit_id'], 'name' => $params['name'], 'image' => $params['image'], @@ -78,7 +83,6 @@ class ProductLogic extends BaseLogic } } - /** * @notes 删除 * @param array $params @@ -103,4 +107,4 @@ class ProductLogic extends BaseLogic { return Product::findOrEmpty($params['id'])->toArray(); } -} \ No newline at end of file +} diff --git a/app/admin/validate/DishesProductValidate.php b/app/admin/validate/DishesProductValidate.php index 5e60163..23643ba 100644 --- a/app/admin/validate/DishesProductValidate.php +++ b/app/admin/validate/DishesProductValidate.php @@ -1,82 +1,90 @@ - 'require', - ]; - - - /** - * 参数描述 - * @var string[] - */ - protected $field = [ - 'id' => 'id', - ]; - - - /** - * @notes 添加场景 - * @return DishesProductValidate - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function sceneAdd() - { - return $this->remove('id', true); - } - - - /** - * @notes 编辑场景 - * @return DishesProductValidate - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function sceneEdit() - { - return $this->only(['id']); - } - - - /** - * @notes 删除场景 - * @return DishesProductValidate - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function sceneDelete() - { - return $this->only(['id']); - } - - - /** - * @notes 详情场景 - * @return DishesProductValidate - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function sceneDetail() - { - return $this->only(['id']); - } - -} \ No newline at end of file + 'require', + 'dishes_id' => 'require', + 'product_id' => 'require', + 'unit_id' => 'require', + 'nums' => 'require', + ]; + + + /** + * 参数描述 + * @var string[] + */ + protected $field = [ + 'id' => 'id', + 'dishes_id' => '菜品ID', + 'product_id' => '商品ID', + 'unit_id' => '单位ID', + 'nums' => '数量', + ]; + + + /** + * @notes 添加场景 + * @return DishesProductValidate + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function sceneAdd() + { + return $this->remove('id', true); + } + + + /** + * @notes 编辑场景 + * @return DishesProductValidate + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function sceneEdit() + { + return $this->only(['id', 'dishes_id', 'product_id', 'unit_id', 'nums']); + } + + + /** + * @notes 删除场景 + * @return DishesProductValidate + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function sceneDelete() + { + return $this->only(['id']); + } + + + /** + * @notes 详情场景 + * @return DishesProductValidate + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function sceneDetail() + { + return $this->only(['id']); + } + +} diff --git a/app/common/model/DishesProduct.php b/app/common/model/DishesProduct.php index a3d02de..acc7d15 100644 --- a/app/common/model/DishesProduct.php +++ b/app/common/model/DishesProduct.php @@ -18,5 +18,19 @@ class DishesProduct extends BaseModel protected $name = 'dishes_product'; protected $deleteTime = 'delete_time'; - -} \ No newline at end of file + 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'); + } + +} diff --git a/app/common/model/Product.php b/app/common/model/Product.php index 96cf0a6..5fec5a1 100644 --- a/app/common/model/Product.php +++ b/app/common/model/Product.php @@ -18,5 +18,14 @@ class Product extends BaseModel protected $name = 'product'; protected $deleteTime = 'delete_time'; - -} \ No newline at end of file + 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'); + } + +} diff --git a/app/common/model/ProductCategory.php b/app/common/model/ProductCategory.php index 9233d25..19bc84d 100644 --- a/app/common/model/ProductCategory.php +++ b/app/common/model/ProductCategory.php @@ -18,5 +18,15 @@ class ProductCategory extends BaseModel protected $name = 'product_category'; protected $deleteTime = 'delete_time'; - -} \ No newline at end of file + public static function getParentUntilRoot($id) + { + static $parentIds = []; + $parentIds[] = $id; + $pid = self::where('id', $id)->value('pid'); + if ($pid) { + self::getParentUntilRoot($pid); + } + return $parentIds; + } + +} diff --git a/app/common/service/storage/engine/Server.php b/app/common/service/storage/engine/Server.php index bd19040..5f55169 100755 --- a/app/common/service/storage/engine/Server.php +++ b/app/common/service/storage/engine/Server.php @@ -41,20 +41,20 @@ abstract class Server if (empty($this->file)) { 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); - $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')); - if (!in_array(strtolower($this->file->extension), $limit)) { - throw new Exception('不允许上传' . $this->file->extension . '后缀文件'); + if (!in_array(strtolower($extension), $limit)) { + throw new Exception('不允许上传' . $extension . '后缀文件'); } // 文件信息 $this->fileInfo = [ - 'ext' => $this->file->extension, + 'ext' => $extension, 'size' => $this->file->getSize(), - 'mime' => $this->file->getMime, + 'mime' => $mimeType, 'name' => $this->file->getUploadName(), 'realPath' => $this->file->getRealPath(), ]; diff --git a/app/common/validate/BaseValidate.php b/app/common/validate/BaseValidate.php index 5be1236..8259f2e 100755 --- a/app/common/validate/BaseValidate.php +++ b/app/common/validate/BaseValidate.php @@ -17,6 +17,7 @@ declare(strict_types=1); namespace app\common\validate; use app\common\service\JsonService; +use support\exception\BusinessException; use taoser\Validate; class BaseValidate extends Validate @@ -79,9 +80,9 @@ class BaseValidate extends Validate if (!$result) { $exception = is_array($this->error) ? implode(';', $this->error) : $this->error; - JsonService::throw($exception); + throw new BusinessException($exception); } // 3.成功返回数据 return $params; } -} \ No newline at end of file +} diff --git a/app/functions.php b/app/functions.php index 24394e7..2ff634c 100644 --- a/app/functions.php +++ b/app/functions.php @@ -92,6 +92,7 @@ function create_password(string $plaintext, string $salt) : string * @param string $parent_id_name 数组祖先id名 * @param int $level 此值请勿给参数 * @param int $parent_id 此值请勿给参数 + * @param int $level 等级 * @return array */ function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0, $level = 0)