From 8567f2de4aa2ba9c7ab9cb5799004922d8d814f3 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Fri, 14 Feb 2025 18:00:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(inventory=5Fstore):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=BA=93=E5=AD=98=E7=9B=98=E7=82=B9=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 Raw 类以处理复杂的数据库查询 - 使用 Raw 实例解决 nums 和 enter_nums 直接比较的问题 --- .../InventoryWarehouseController.php | 96 ++++++++++++++++ .../InventoryWarehouseLists.php | 79 +++++++++++++ .../inventory_store/InventoryStoreLogic.php | 3 +- .../InventoryWarehouseLogic.php | 108 ++++++++++++++++++ .../InventoryWarehouseValidate.php | 82 +++++++++++++ .../InventoryWarehouse.php | 22 ++++ 6 files changed, 389 insertions(+), 1 deletion(-) create mode 100644 app/admin/controller/inventory_warehouse/InventoryWarehouseController.php create mode 100644 app/admin/lists/inventory_warehouse/InventoryWarehouseLists.php create mode 100644 app/admin/logic/inventory_warehouse/InventoryWarehouseLogic.php create mode 100644 app/admin/validate/inventory_warehouse/InventoryWarehouseValidate.php create mode 100644 app/common/model/inventory_warehouse/InventoryWarehouse.php diff --git a/app/admin/controller/inventory_warehouse/InventoryWarehouseController.php b/app/admin/controller/inventory_warehouse/InventoryWarehouseController.php new file mode 100644 index 000000000..78ee75c02 --- /dev/null +++ b/app/admin/controller/inventory_warehouse/InventoryWarehouseController.php @@ -0,0 +1,96 @@ +dataLists(new InventoryWarehouseLists()); + } + + + /** + * @notes 添加仓库盘存 + * @return \think\response\Json + * @author admin + * @date 2025/02/14 17:24 + */ + public function add() + { + $params = (new InventoryWarehouseValidate())->post()->goCheck('add'); + $result = InventoryWarehouseLogic::add($params); + if (true === $result) { + return $this->success('添加成功', [], 1, 1); + } + return $this->fail(InventoryWarehouseLogic::getError()); + } + + + /** + * @notes 编辑仓库盘存 + * @return \think\response\Json + * @author admin + * @date 2025/02/14 17:24 + */ + public function edit() + { + $params = (new InventoryWarehouseValidate())->post()->goCheck('edit'); + $params['admin_id']=$this->adminId; + $result = InventoryWarehouseLogic::edit($params); + if (true === $result) { + return $this->success('编辑成功', [], 1, 1); + } + return $this->fail(InventoryWarehouseLogic::getError()); + } + + + /** + * @notes 删除仓库盘存 + * @return \think\response\Json + * @author admin + * @date 2025/02/14 17:24 + */ + public function delete() + { + $params = (new InventoryWarehouseValidate())->post()->goCheck('delete'); + InventoryWarehouseLogic::delete($params); + return $this->success('删除成功', [], 1, 1); + } + + /** + * @notes 门店盘存核准 + * @return \think\response\Json + * @author admin + * @date 2025/02/14 11:46 + */ + public function enter_nums() + { + $params = $this->request->post(); + $params['warehouse_id']=1; + $result = InventoryWarehouseLogic::enterNums($params); + if (true === $result) { + return $this->success('核准成功', [], 1, 1); + } + } +} \ No newline at end of file diff --git a/app/admin/lists/inventory_warehouse/InventoryWarehouseLists.php b/app/admin/lists/inventory_warehouse/InventoryWarehouseLists.php new file mode 100644 index 000000000..201626785 --- /dev/null +++ b/app/admin/lists/inventory_warehouse/InventoryWarehouseLists.php @@ -0,0 +1,79 @@ + ['product_id'], + ]; + } + + + /** + * @notes 获取仓库盘存列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author admin + * @date 2025/02/14 17:24 + */ + public function lists(): array + { + return InventoryWarehouse::where($this->searchWhere) + ->field(['id', 'product_id', 'admin_id', 'warehouse_id', 'nums', 'enter_nums', 'status','create_time']) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select()->each(function ($item) { + $item->status_name = match ($item->status) { + 0 => '待盘点', + 1 => '盘点中', + 2 => '盘点完成', + default => '未知', + }; + if($item->admin_id){ + $item->admin_name = Admin::where('id',$item->admin_id)->value('name'); + } + if($item->product_id){ + $item->product_name = StoreProduct::where('id',$item->product_id)->withTrashed()->value('store_name'); + } + }) + ->toArray(); + } + + + /** + * @notes 获取仓库盘存数量 + * @return int + * @author admin + * @date 2025/02/14 17:24 + */ + public function count(): int + { + return InventoryWarehouse::where($this->searchWhere)->count(); + } + +} \ No newline at end of file diff --git a/app/admin/logic/inventory_store/InventoryStoreLogic.php b/app/admin/logic/inventory_store/InventoryStoreLogic.php index 836c0dabf..44cb5062e 100644 --- a/app/admin/logic/inventory_store/InventoryStoreLogic.php +++ b/app/admin/logic/inventory_store/InventoryStoreLogic.php @@ -7,6 +7,7 @@ use app\common\model\inventory_store\InventoryStore; use app\common\logic\BaseLogic; use app\common\model\store_branch_product\StoreBranchProduct; use support\exception\BusinessException; +use think\db\Raw; use think\facade\Db; @@ -76,7 +77,7 @@ class InventoryStoreLogic extends BaseLogic 'status'=>2 ]); - $arr=InventoryStore::where('store_id', $params['store_id'])->where('nums','<>','enter_nums')->whereDay('create_time',$params['create_time'])->select(); + $arr=InventoryStore::where('store_id', $params['store_id'])->where('nums','<>',new Raw('enter_nums'))->whereDay('create_time',$params['create_time'])->select(); foreach ($arr as $k=>$v){ StoreBranchProduct::where('product_id',$v['product_id'])->where('store_id',$v['store_id'])->update([ 'stock'=>$v['enter_nums'] diff --git a/app/admin/logic/inventory_warehouse/InventoryWarehouseLogic.php b/app/admin/logic/inventory_warehouse/InventoryWarehouseLogic.php new file mode 100644 index 000000000..32e82fe61 --- /dev/null +++ b/app/admin/logic/inventory_warehouse/InventoryWarehouseLogic.php @@ -0,0 +1,108 @@ +whereDay('create_time')->find(); + if($find){ + throw new BusinessException('今日数据已生成'); + } + $arr=WarehouseProductStorege::where('warehouse_id',1)->field('product_id,nums,warehouse_id')->select()->toArray(); + (new InventoryWarehouse())->saveAll($arr); + + Db::commit(); + return true; + } catch (\Throwable $e) { + Db::rollback(); + throw new BusinessException($e->getMessage()); + } + } + + + /** + * @notes 编辑仓库盘存 + * @param array $params + * @return bool + * @author admin + * @date 2025/02/14 17:24 + */ + public static function edit(array $params): bool + { + Db::startTrans(); + try { + InventoryWarehouse::where('id', $params['id'])->update([ + 'admin_id'=>$params['admin_id']??0, + 'enter_nums'=>$params['nums'] + ]); + Db::commit(); + return true; + } catch (\Throwable $e) { + Db::rollback(); + throw new BusinessException($e->getMessage()); + } + } + + + /** + * @notes 删除仓库盘存 + * @param array $params + * @return bool + * @author admin + * @date 2025/02/14 17:24 + */ + public static function delete(array $params): bool + { + return InventoryWarehouse::destroy($params['id']); + } + + public static function enterNums(array $params): bool + { + Db::startTrans(); + try { + InventoryWarehouse::where('warehouse_id', $params['warehouse_id'])->whereDay('create_time',$params['create_time'])->update([ + 'status'=>2 + ]); + + $arr=InventoryWarehouse::where('warehouse_id', $params['warehouse_id'])->where('nums','<>',new Raw('enter_nums'))->whereDay('create_time',$params['create_time'])->select(); + foreach ($arr as $k=>$v){ + WarehouseProductStorege::where('product_id',$v['product_id'])->where('warehouse_id',$v['warehouse_id'])->update([ + 'nums'=>$v['enter_nums'] + ]); + } + Db::commit(); + return true; + } catch (\Throwable $e) { + Db::rollback(); + throw new BusinessException($e->getMessage()); + } + } + +} \ No newline at end of file diff --git a/app/admin/validate/inventory_warehouse/InventoryWarehouseValidate.php b/app/admin/validate/inventory_warehouse/InventoryWarehouseValidate.php new file mode 100644 index 000000000..99da9280f --- /dev/null +++ b/app/admin/validate/inventory_warehouse/InventoryWarehouseValidate.php @@ -0,0 +1,82 @@ + 'require', + ]; + + + /** + * 参数描述 + * @var string[] + */ + protected $field = [ + 'id' => 'id', + ]; + + + /** + * @notes 添加场景 + * @return InventoryWarehouseValidate + * @author admin + * @date 2025/02/14 17:24 + */ + public function sceneAdd() + { + return $this->remove('id', true); + } + + + /** + * @notes 编辑场景 + * @return InventoryWarehouseValidate + * @author admin + * @date 2025/02/14 17:24 + */ + public function sceneEdit() + { + return $this->only(['id']); + } + + + /** + * @notes 删除场景 + * @return InventoryWarehouseValidate + * @author admin + * @date 2025/02/14 17:24 + */ + public function sceneDelete() + { + return $this->only(['id']); + } + + + /** + * @notes 详情场景 + * @return InventoryWarehouseValidate + * @author admin + * @date 2025/02/14 17:24 + */ + public function sceneDetail() + { + return $this->only(['id']); + } + +} \ No newline at end of file diff --git a/app/common/model/inventory_warehouse/InventoryWarehouse.php b/app/common/model/inventory_warehouse/InventoryWarehouse.php new file mode 100644 index 000000000..fe0e08847 --- /dev/null +++ b/app/common/model/inventory_warehouse/InventoryWarehouse.php @@ -0,0 +1,22 @@ +