diff --git a/app/ExceptionHandler.php b/app/ExceptionHandler.php index 65315340c..41f37353c 100644 --- a/app/ExceptionHandler.php +++ b/app/ExceptionHandler.php @@ -1,8 +1,7 @@ $isDebug ? $exception->getCode() : 500, + 'code' => $isDebug ? $exception->getCode() : 0, 'msg' => $isDebug ? $exception->getMessage() : '服务器内部错误', ]; if ($isDebug) { diff --git a/app/admin/lists/store_branch_product/StoreBranchProductLists.php b/app/admin/lists/store_branch_product/StoreBranchProductLists.php index 919201ba4..01e91f168 100644 --- a/app/admin/lists/store_branch_product/StoreBranchProductLists.php +++ b/app/admin/lists/store_branch_product/StoreBranchProductLists.php @@ -4,6 +4,8 @@ namespace app\admin\lists\store_branch_product; use app\admin\lists\BaseAdminDataLists; +use app\common\model\store_category\StoreCategory; +use app\common\model\store_product_unit\StoreProductUnit; use app\common\model\system_store\SystemStore; use app\common\lists\ListsSearchInterface; use app\common\model\store_branch_product\StoreBranchProduct; @@ -43,9 +45,18 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI public function lists(): array { return StoreBranchProduct::where($this->searchWhere) + ->field(['id', 'image', 'store_name', 'cate_id', 'price', 'sales', 'stock', 'unit', 'cost']) + ->when(!empty($this->adminInfo['store_id']), function ($query) { + $query->where('store_id', $this->adminInfo['store_id']); + }) ->limit($this->limitOffset, $this->limitLength) - ->order(['id' => 'desc']) + ->order(['sort' => 'desc', 'id' => 'desc']) ->select() + ->each(function ($item) { + $item['unit_name'] = StoreProductUnit::where('id', $item['unit'])->value('name'); + $item['cate_name'] = StoreCategory::where('id', $item['cate_id'])->value('name'); + return $item; + }) ->toArray(); } @@ -58,7 +69,10 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI */ public function count(): int { - return SystemStore::where($this->searchWhere)->count(); + return StoreBranchProduct::where($this->searchWhere) + ->when(!empty($this->adminInfo['store_id']), function ($query) { + $query->where('store_id', $this->adminInfo['store_id']); + })->count(); } -} \ No newline at end of file +} diff --git a/app/store/controller/store_product/StoreProductController.php b/app/store/controller/store_product/StoreProductController.php index 5d0a2cc7e..3aac278b8 100644 --- a/app/store/controller/store_product/StoreProductController.php +++ b/app/store/controller/store_product/StoreProductController.php @@ -3,8 +3,9 @@ namespace app\store\controller\store_product; +use app\admin\lists\store_branch_product\StoreBranchProductLists; use app\store\controller\BaseAdminController; -use app\store\lists\store_product\StoreProductLists; +use app\store\logic\store_branch_product\StoreBranchProductLogic; use app\store\logic\store_product\StoreProductLogic; use app\store\validate\store_product\StoreProductValidate; use hg\apidoc\annotation as ApiDoc; @@ -15,29 +16,52 @@ use hg\apidoc\annotation as ApiDoc; * Class StoreProductController * @package app\store\controller\store_product */ -#[ApiDoc\NotParse()] +#[ApiDoc\title('商品列表')] class StoreProductController extends BaseAdminController { - - /** - * @notes 获取商品列表列表 - * @return \think\response\Json - * @author likeadmin - * @date 2024/05/31 10:53 - */ + #[ + ApiDoc\Title('商品列表'), + ApiDoc\url('/store/store_product/storeProduct/lists'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array", children: [ + ['name' => 'id', 'desc' => 'ID', 'type' => 'int'], + ['name' => 'image', 'desc' => '图片', 'type' => 'string'], + ['name' => 'store_name', 'desc' => '商品名称', 'type' => 'string'], + ['name' => 'price', 'desc' => '零售价', 'type' => 'float'], + ['name' => 'cost', 'desc' => '成本价', 'type' => 'float'], + ['name' => 'sales', 'desc' => '销量', 'type' => 'int'], + ['name' => 'stock', 'desc' => '库存', 'type' => 'int'], + ['name' => 'unit_name', 'desc' => '单位', 'type' => 'string'], + ['name' => 'cate_name', 'desc' => '分类', 'type' => 'string'], + ['name' => 'status', 'desc' => '状态:1上架,0下架', 'type' => 'string'], + ]), + ] public function lists() { - return $this->dataLists(new StoreProductLists()); + return $this->dataLists(new StoreBranchProductLists()); } - - /** - * @notes 添加商品列表 - * @return \think\response\Json - * @author likeadmin - * @date 2024/05/31 10:53 - */ + #[ + ApiDoc\Title('添加商品'), + ApiDoc\url('/store/store_product/storeProduct/add'), + ApiDoc\Method('POST'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array", children: [ + ['name' => 'id', 'desc' => 'ID', 'type' => 'int'], + ['name' => 'image', 'desc' => '图片', 'type' => 'string'], + ['name' => 'store_name', 'desc' => '商品名称', 'type' => 'string'], + ['name' => 'price', 'desc' => '零售价', 'type' => 'float'], + ['name' => 'cost', 'desc' => '成本价', 'type' => 'float'], + ['name' => 'sales', 'desc' => '销量', 'type' => 'int'], + ['name' => 'stock', 'desc' => '库存', 'type' => 'int'], + ['name' => 'unit_name', 'desc' => '单位', 'type' => 'string'], + ['name' => 'cate_name', 'desc' => '分类', 'type' => 'string'], + ]), + ] public function add() { $params = (new StoreProductValidate())->post()->goCheck('add'); @@ -48,13 +72,24 @@ class StoreProductController extends BaseAdminController return $this->fail(StoreProductLogic::getError()); } - - /** - * @notes 编辑商品列表 - * @return \think\response\Json - * @author likeadmin - * @date 2024/05/31 10:53 - */ + #[ + ApiDoc\Title('编辑商品'), + ApiDoc\url('/store/store_product/storeProduct/edit'), + ApiDoc\Method('POST'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array", children: [ + ['name' => 'id', 'desc' => 'ID', 'type' => 'int'], + ['name' => 'image', 'desc' => '图片', 'type' => 'string'], + ['name' => 'store_name', 'desc' => '商品名称', 'type' => 'string'], + ['name' => 'price', 'desc' => '零售价', 'type' => 'float'], + ['name' => 'cost', 'desc' => '成本价', 'type' => 'float'], + ['name' => 'sales', 'desc' => '销量', 'type' => 'int'], + ['name' => 'stock', 'desc' => '库存', 'type' => 'int'], + ['name' => 'unit_name', 'desc' => '单位', 'type' => 'string'], + ['name' => 'cate_name', 'desc' => '分类', 'type' => 'string'], + ]), + ] public function edit() { $params = (new StoreProductValidate())->post()->goCheck('edit'); @@ -65,13 +100,24 @@ class StoreProductController extends BaseAdminController return $this->fail(StoreProductLogic::getError()); } - - /** - * @notes 删除商品列表 - * @return \think\response\Json - * @author likeadmin - * @date 2024/05/31 10:53 - */ + #[ + ApiDoc\Title('删除商品'), + ApiDoc\url('/store/store_product/storeProduct/delete'), + ApiDoc\Method('POST'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array", children: [ + ['name' => 'id', 'desc' => 'ID', 'type' => 'int'], + ['name' => 'image', 'desc' => '图片', 'type' => 'string'], + ['name' => 'store_name', 'desc' => '商品名称', 'type' => 'string'], + ['name' => 'price', 'desc' => '零售价', 'type' => 'float'], + ['name' => 'cost', 'desc' => '成本价', 'type' => 'float'], + ['name' => 'sales', 'desc' => '销量', 'type' => 'int'], + ['name' => 'stock', 'desc' => '库存', 'type' => 'int'], + ['name' => 'unit_name', 'desc' => '单位', 'type' => 'string'], + ['name' => 'cate_name', 'desc' => '分类', 'type' => 'string'], + ]), + ] public function delete() { $params = (new StoreProductValidate())->post()->goCheck('delete'); @@ -79,13 +125,24 @@ class StoreProductController extends BaseAdminController return $this->success('删除成功', [], 1, 1); } - - /** - * @notes 获取商品列表详情 - * @return \think\response\Json - * @author likeadmin - * @date 2024/05/31 10:53 - */ + #[ + ApiDoc\Title('商品详情'), + ApiDoc\url('/store/store_product/storeProduct/detail'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array", children: [ + ['name' => 'id', 'desc' => 'ID', 'type' => 'int'], + ['name' => 'image', 'desc' => '图片', 'type' => 'string'], + ['name' => 'store_name', 'desc' => '商品名称', 'type' => 'string'], + ['name' => 'price', 'desc' => '零售价', 'type' => 'float'], + ['name' => 'cost', 'desc' => '成本价', 'type' => 'float'], + ['name' => 'sales', 'desc' => '销量', 'type' => 'int'], + ['name' => 'stock', 'desc' => '库存', 'type' => 'int'], + ['name' => 'unit_name', 'desc' => '单位', 'type' => 'string'], + ['name' => 'cate_name', 'desc' => '分类', 'type' => 'string'], + ]), + ] public function detail() { $params = (new StoreProductValidate())->goCheck('detail'); @@ -93,5 +150,37 @@ class StoreProductController extends BaseAdminController return $this->data($result); } + #[ + ApiDoc\Title('商品上下架'), + ApiDoc\url('/store/store_product/storeProduct/status'), + ApiDoc\Method('POST'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array"), + ] + public function status() + { + $params = (new StoreProductValidate())->post()->goCheck('detail'); + StoreBranchProductLogic::status($params); + return $this->data([]); + } + + #[ + ApiDoc\Title('商品库存增减'), + ApiDoc\url('/store/store_product/storeProduct/stock'), + ApiDoc\Method('POST'), + ApiDoc\Param(name: "id", type: "int", require: true, desc: "id"), + ApiDoc\Param(name: "type", type: "int", require: true, desc: "类型:1增加,2减少"), + ApiDoc\Param(name: "number", type: "int", require: true, desc: "数量"), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array"), + ] + public function stock() + { + $params = (new StoreProductValidate())->post()->goCheck('stock'); + StoreBranchProductLogic::stock($params); + return $this->data([]); + } } diff --git a/app/store/lists/store_product/StoreProductLists.php b/app/store/lists/store_product/StoreProductLists.php index 252a93561..e9491d41d 100644 --- a/app/store/lists/store_product/StoreProductLists.php +++ b/app/store/lists/store_product/StoreProductLists.php @@ -3,6 +3,7 @@ namespace app\store\lists\store_product; +use app\common\model\store_branch_product\StoreBranchProduct; use app\store\lists\BaseAdminDataLists; use app\common\model\store_product\StoreProduct; use app\common\lists\ListsSearchInterface; @@ -27,7 +28,7 @@ class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterfa public function setSearch(): array { return [ - '=' => ['store_name', 'cate_id'], + '=' => ['store_name', 'cate_id', 'store_id'], ]; } @@ -43,7 +44,8 @@ class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterfa */ public function lists(): array { - return StoreProduct::where($this->searchWhere) + return StoreBranchProduct::where($this->searchWhere) + ->where('store_id', $this->adminInfo['store_id']) ->field(['id', 'image', 'store_name', 'cate_id', 'price', 'sales', 'stock', 'is_show', 'unit', 'cost','rose','purchase']) ->limit($this->limitOffset, $this->limitLength) ->order(['id' => 'desc']) @@ -64,6 +66,8 @@ class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterfa */ public function count(): int { - return StoreProduct::where($this->searchWhere)->count(); + return StoreProduct::where($this->searchWhere) + ->where('store_id', $this->adminInfo['store_id']) + ->count(); } } diff --git a/app/store/logic/store_branch_product/StoreBranchProductLogic.php b/app/store/logic/store_branch_product/StoreBranchProductLogic.php new file mode 100644 index 000000000..8e7f63c4c --- /dev/null +++ b/app/store/logic/store_branch_product/StoreBranchProductLogic.php @@ -0,0 +1,203 @@ + $params['store_name'], + 'image' => $params['image'], + 'bar_code' => $params['bar_code'] ?? '', + 'cate_id' => $params['cate_id'], + 'unit' => $params['unit'], + 'stock' => $params['stock'], + 'cost' => $params['cost'], + 'purchase' => $params['purchase'], + 'rose' => $params['rose'], + ]; + $rose_price = bcmul($params['cost'], $params['rose'], 2); + $data['price'] = bcadd($params['cost'], $rose_price, 2); + $res = StoreProduct::create($data); + StoreProductAttrValue::create([ + "bar_code" => $params["bar_code"] ?? '', + "image" => $params["image"] ?? '', + "cost" => $params['cost'], + "purchase" => $params['purchase'], + "unit" => $params["unit"], + "price" => $data['price'], + "stock" => $params['stock'], + "product_id" => $res['id'], + "unique" => setUnique($res['id'], '', 0), + 'sales' => 0, + ]); + StoreCategory::where('id', $params['cate_id'])->inc('three')->update(); + + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + + /** + * @notes 编辑商品列表 + * @param array $params + * @return bool + * @author likeadmin + * @date 2024/05/31 10:53 + */ + public static function edit(array $params): bool + { + Db::startTrans(); + $StoreProduct = StoreProduct::where('id', $params['id'])->find(); + try { + if ($StoreProduct['cate_id'] != $params['cate_id']) { + StoreCategory::where('id', $params['cate_id'])->inc('three')->update(); + StoreCategory::where('id', $StoreProduct['cate_id'])->dec('three')->update(); + } + StoreProduct::where('id', $params['id'])->update([ + 'store_name' => $params['store_name'] + ]); + + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + + /** + * @notes 删除商品列表 + * @param array $params + * @return bool + * @author likeadmin + * @date 2024/05/31 10:53 + */ + public static function delete(array $params): bool + { + return StoreProduct::destroy($params['id']); + } + + + /** + * @notes 获取商品列表详情 + * @param $params + * @return array + * @author likeadmin + * @date 2024/05/31 10:53 + */ + public static function detail($params): array + { + return StoreProduct::findOrEmpty($params['id'])->toArray(); + } + + /** + * 更新商品分类 + */ + public static function updateGoodsclass($id, $type = 0) + { + $pid = StoreCategory::where('id', $id)->value('pid'); + if ($pid) { + $goodsclass = StoreCategory::where('id', $pid)->field('pid,children')->find(); + if ($goodsclass) { + if (count($goodsclass['children']) >= 1) { + if (!in_array($id, $goodsclass['children'])) { + $arr = $goodsclass['children']; + array_push($arr, $id); + StoreCategory::where('id', $pid)->update(['children' => $arr]); + if ($goodsclass['pid'] != 0 && $type == 0) { + self::updateGoodsclass($pid, 1); + } + } + } + } + } + } + + /** + * @notes 商品上下架 + * @param array $params + * @return bool + */ + public static function status(array $params): bool + { + $StoreProduct = StoreBranchProduct::where('id', $params['id'])->find(); + self::checkAuth($StoreProduct); + Db::startTrans(); + try { + StoreBranchProduct::where('id', $params['id'])->update([ + 'status' => $StoreProduct['status'] == 1 ? 0 : 1 + ]); + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + throw new BusinessException($e->getMessage()); + } + } + + /** + * @notes 商品库存 + * @param array $params + * @return bool + */ + public static function stock(array $params): bool + { + $StoreProduct = StoreBranchProduct::where('id', $params['id'])->find(); + self::checkAuth($StoreProduct); + Db::startTrans(); + try { + $stock = $params['type'] == 1 ? $StoreProduct['stock'] + $params['number'] : $StoreProduct['stock'] - $params['number']; + $stock = max($stock, 0); + StoreBranchProduct::where('id', $params['id'])->update(['stock' => $stock]); + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + throw new BusinessException($e->getMessage()); + } + } + + public static function checkAuth($product) + { + if (request()->adminInfo['store_id'] != $product['store_id']) { + throw new BusinessException('没有权限操作'); + } + } + +} diff --git a/app/store/validate/store_product/StoreProductValidate.php b/app/store/validate/store_product/StoreProductValidate.php index 67dff6945..b01017d74 100644 --- a/app/store/validate/store_product/StoreProductValidate.php +++ b/app/store/validate/store_product/StoreProductValidate.php @@ -81,4 +81,15 @@ class StoreProductValidate extends BaseValidate return $this->only(['id']); } + /** + * @notes 详情场景 + * @return StoreProductValidate + * @author likeadmin + * @date 2024/05/31 10:53 + */ + public function sceneStock() + { + return $this->only(['id', 'type', 'number']); + } + }