From 788a8cb211e636ffd0b41bfa3652c35ff35153a7 Mon Sep 17 00:00:00 2001 From: lewis <604446095@qq.com> Date: Mon, 6 Jan 2025 10:17:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=8F=98=E6=9B=B4=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/ChangeLogController.php | 95 +++++++++++++++++ app/admin/lists/ChangeLogLists.php | 66 ++++++++++++ app/admin/logic/ChangeLogLogic.php | 103 +++++++++++++++++++ app/admin/validate/ChangeLogValidate.php | 82 +++++++++++++++ app/common/model/ChangeLog.php | 22 ++++ 5 files changed, 368 insertions(+) create mode 100644 app/admin/controller/ChangeLogController.php create mode 100644 app/admin/lists/ChangeLogLists.php create mode 100644 app/admin/logic/ChangeLogLogic.php create mode 100644 app/admin/validate/ChangeLogValidate.php create mode 100644 app/common/model/ChangeLog.php diff --git a/app/admin/controller/ChangeLogController.php b/app/admin/controller/ChangeLogController.php new file mode 100644 index 000000000..bad919254 --- /dev/null +++ b/app/admin/controller/ChangeLogController.php @@ -0,0 +1,95 @@ +dataLists(new ChangeLogLists()); + } + + + /** + * @notes 添加 + * @return \think\response\Json + * @author admin + * @date 2025/01/06 10:03 + */ + public function add() + { + $params = (new ChangeLogValidate())->post()->goCheck('add'); + $result = ChangeLogLogic::add($params); + if (true === $result) { + return $this->success('添加成功', [], 1, 1); + } + return $this->fail(ChangeLogLogic::getError()); + } + + + /** + * @notes 编辑 + * @return \think\response\Json + * @author admin + * @date 2025/01/06 10:03 + */ + public function edit() + { + $params = (new ChangeLogValidate())->post()->goCheck('edit'); + $result = ChangeLogLogic::edit($params); + if (true === $result) { + return $this->success('编辑成功', [], 1, 1); + } + return $this->fail(ChangeLogLogic::getError()); + } + + + /** + * @notes 删除 + * @return \think\response\Json + * @author admin + * @date 2025/01/06 10:03 + */ + public function delete() + { + $params = (new ChangeLogValidate())->post()->goCheck('delete'); + ChangeLogLogic::delete($params); + return $this->success('删除成功', [], 1, 1); + } + + + /** + * @notes 获取详情 + * @return \think\response\Json + * @author admin + * @date 2025/01/06 10:03 + */ + public function detail() + { + $params = (new ChangeLogValidate())->goCheck('detail'); + $result = ChangeLogLogic::detail($params); + return $this->data($result); + } + + +} \ No newline at end of file diff --git a/app/admin/lists/ChangeLogLists.php b/app/admin/lists/ChangeLogLists.php new file mode 100644 index 000000000..28e0614d6 --- /dev/null +++ b/app/admin/lists/ChangeLogLists.php @@ -0,0 +1,66 @@ + ['model', 'link_id', 'nums', 'pm', 'url'], + '%like%' => ['mark'], + ]; + } + + + /** + * @notes 获取列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author admin + * @date 2025/01/06 10:03 + */ + public function lists(): array + { + return ChangeLog::where($this->searchWhere) + ->field(['id', 'model', 'link_id', 'nums', 'pm', 'url', 'mark']) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select() + ->toArray(); + } + + + /** + * @notes 获取数量 + * @return int + * @author admin + * @date 2025/01/06 10:03 + */ + public function count(): int + { + return ChangeLog::where($this->searchWhere)->count(); + } + +} \ No newline at end of file diff --git a/app/admin/logic/ChangeLogLogic.php b/app/admin/logic/ChangeLogLogic.php new file mode 100644 index 000000000..fac0eee13 --- /dev/null +++ b/app/admin/logic/ChangeLogLogic.php @@ -0,0 +1,103 @@ + $params['model'], + 'link_id' => $params['link_id'], + 'nums' => $params['nums'], + 'pm' => $params['pm'], + 'url' => $params['url'], + 'mark' => $params['mark'], + ]); + + Db::commit(); + return true; + } catch (\Throwable $e) { + Db::rollback(); + throw new BusinessException($e->getMessage()); + } + } + + + /** + * @notes 编辑 + * @param array $params + * @return bool + * @author admin + * @date 2025/01/06 10:03 + */ + public static function edit(array $params): bool + { + Db::startTrans(); + try { + ChangeLog::where('id', $params['id'])->update([ + 'model' => $params['model'], + 'link_id' => $params['link_id'], + 'nums' => $params['nums'], + 'pm' => $params['pm'], + 'url' => $params['url'], + 'mark' => $params['mark'], + ]); + + Db::commit(); + return true; + } catch (\Throwable $e) { + Db::rollback(); + throw new BusinessException($e->getMessage()); + } + } + + + /** + * @notes 删除 + * @param array $params + * @return bool + * @author admin + * @date 2025/01/06 10:03 + */ + public static function delete(array $params): bool + { + return ChangeLog::destroy($params['id']); + } + + + /** + * @notes 获取详情 + * @param $params + * @return array + * @author admin + * @date 2025/01/06 10:03 + */ + public static function detail($params): array + { + return ChangeLog::findOrEmpty($params['id'])->toArray(); + } +} \ No newline at end of file diff --git a/app/admin/validate/ChangeLogValidate.php b/app/admin/validate/ChangeLogValidate.php new file mode 100644 index 000000000..8861efb17 --- /dev/null +++ b/app/admin/validate/ChangeLogValidate.php @@ -0,0 +1,82 @@ + 'require', + ]; + + + /** + * 参数描述 + * @var string[] + */ + protected $field = [ + 'id' => 'id', + ]; + + + /** + * @notes 添加场景 + * @return ChangeLogValidate + * @author admin + * @date 2025/01/06 10:03 + */ + public function sceneAdd() + { + return $this->remove('id', true); + } + + + /** + * @notes 编辑场景 + * @return ChangeLogValidate + * @author admin + * @date 2025/01/06 10:03 + */ + public function sceneEdit() + { + return $this->only(['id']); + } + + + /** + * @notes 删除场景 + * @return ChangeLogValidate + * @author admin + * @date 2025/01/06 10:03 + */ + public function sceneDelete() + { + return $this->only(['id']); + } + + + /** + * @notes 详情场景 + * @return ChangeLogValidate + * @author admin + * @date 2025/01/06 10:03 + */ + public function sceneDetail() + { + return $this->only(['id']); + } + +} \ No newline at end of file diff --git a/app/common/model/ChangeLog.php b/app/common/model/ChangeLog.php new file mode 100644 index 000000000..bc74fb9b9 --- /dev/null +++ b/app/common/model/ChangeLog.php @@ -0,0 +1,22 @@ + Date: Mon, 6 Jan 2025 11:00:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor(model):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=97=A8=E5=BA=97=E3=80=81=E4=BB=93=E5=BA=93=E5=92=8C=E5=BA=93?= =?UTF-8?q?=E5=AD=98=E6=A8=A1=E5=9E=8B=E4=B8=AD=E7=9A=84=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E9=92=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除了 StoreBranchProduct、WarehouseProduct 和 WarehouseProductStorege 模型中的 onBeforeWrite 和 onAfterWrite 钩子方法 - 移除了用于记录更新前后日志的代码 - 简化了模型结构,提高了代码可读性和性能 --- .../StoreBranchProduct.php | 21 ------------------ .../warehouse_product/WarehouseProduct.php | 22 ------------------- .../WarehouseProductStorege.php | 22 ------------------- 3 files changed, 65 deletions(-) diff --git a/app/common/model/store_branch_product/StoreBranchProduct.php b/app/common/model/store_branch_product/StoreBranchProduct.php index 4f782f0f0..f36b7d9d7 100644 --- a/app/common/model/store_branch_product/StoreBranchProduct.php +++ b/app/common/model/store_branch_product/StoreBranchProduct.php @@ -68,25 +68,4 @@ class StoreBranchProduct extends BaseModel { return $this->hasOne(StoreProduct::class,'id','product_id'); } - public static function onBeforeWrite($data) - { - try { - $where = $data->getWhere(); - if($data){ - $find = self::where($where)->field(array_keys($data->toArray()))->find(); - if($find){ - channelLog(array_merge($find->toArray(), $where),'branch_product','更新前'); - } - } - } catch (Throwable $e) { - Log::error('branch_product:' . $e->getMessage()); - } - } - public static function onAfterWrite($data){ - try{ - channelLog($data->toArray(),'branch_product','更新后'); - }catch(Throwable $e){ - Log::error('branch_product:'.$e->getMessage()); - } - } } \ No newline at end of file diff --git a/app/common/model/warehouse_product/WarehouseProduct.php b/app/common/model/warehouse_product/WarehouseProduct.php index 451a42768..54cbcbd3e 100644 --- a/app/common/model/warehouse_product/WarehouseProduct.php +++ b/app/common/model/warehouse_product/WarehouseProduct.php @@ -46,26 +46,4 @@ class WarehouseProduct extends BaseModel 'create_time', 'update_time', ]; - public static function onBeforeWrite($data) - { - try { - $where = $data->getWhere(); - if($data){ - $find = self::where($where)->field(array_keys($data->toArray()))->find(); - if($find){ - channelLog(array_merge($find->toArray(), $where),'warehouse_product','更新前'); - } - } - } catch (Throwable $e) { - Log::error('warehouse_product:' . $e->getMessage()); - } - } - - public static function onAfterWrite($data){ - try{ - channelLog($data->toArray(),'warehouse_product','更新后'); - }catch(Throwable $e){ - Log::error('warehouse_product:'.$e->getMessage()); - } - } } \ No newline at end of file diff --git a/app/common/model/warehouse_product_storege/WarehouseProductStorege.php b/app/common/model/warehouse_product_storege/WarehouseProductStorege.php index 4dfe1a65f..927b771e3 100644 --- a/app/common/model/warehouse_product_storege/WarehouseProductStorege.php +++ b/app/common/model/warehouse_product_storege/WarehouseProductStorege.php @@ -23,26 +23,4 @@ class WarehouseProductStorege extends BaseModel 'total_price', 'update_time', ]; - public static function onBeforeWrite($data) - { - try { - $where = $data->getWhere(); - if($data){ - $find = self::where($where)->field(array_keys($data->toArray()))->find(); - if($find){ - channelLog(array_merge($find->toArray(), $where), 'warehouse_product_storege', '更新前'); - } - } - } catch (Throwable $e) { - Log::error('warehouse_product_storege:' . $e->getMessage()); - } - } - public static function onAfterWrite($data) - { - try { - channelLog($data->toArray(), 'warehouse_product_storege', '更新后'); - } catch (Throwable $e) { - Log::error('warehouse_product_storege:' . $e->getMessage()); - } - } }