sqlite-web 0.7.2
gitea.db
issue
Create
Query
access
access_token
action
action_artifact
action_run
action_run_index
action_run_job
action_runner
action_runner_token
action_schedule
action_schedule_spec
action_task
action_task_output
action_task_step
action_tasks_version
action_variable
app_state
attachment
auth_token
badge
branch
collaboration
comment
commit_status
commit_status_index
commit_status_summary
commit_sync_log
commit_sync_status
dbfs_data
dbfs_meta
deploy_key
email_address
email_hash
external_login_user
follow
gpg_key
gpg_key_import
hook_task
issue
issue_assignees
issue_content_history
issue_dependency
issue_index
issue_label
issue_pin
issue_user
issue_watch
label
language_stat
lfs_lock
lfs_meta_object
login_source
milestone
mirror
notice
notification
oauth2_application
oauth2_authorization_code
oauth2_grant
org_user
package
package_blob
package_blob_upload
package_cleanup_rule
package_file
package_property
package_version
project
project_board
project_issue
protected_branch
protected_tag
public_key
pull_auto_merge
pull_request
push_mirror
reaction
release
renamed_branch
repo_archiver
repo_hidden_file
repo_indexer_status
repo_license
repo_redirect
repo_topic
repo_transfer
repo_unit
repository
review
review_state
secret
session
sqlite_sequence
star
stopwatch
system_setting
task
team
team_invite
team_repo
team_unit
team_user
topic
tracked_time
two_factor
upload
user
user_badge
user_blocking
user_open_id
user_redirect
user_setting
version
watch
webauthn_credential
webhook
Toggle helper tables
Structure
Content
Query
Insert
Drop
Import
Export
Delete row 414 from issue
id
414
repo_id
18
index
175
poster_id
1
original_author
original_author_id
0
name
🔍 代码审查报告:pc-260616 - 需求 盘点单增加时间选择
content
## 自动代码审查报告 **分支**: pc-260616 **提交**: `a57e688854
## 自动代码审查报告 **分支**: pc-260616 **提交**: `a57e688854cbfa6b195d2062ecce3f213b84c6c7` **提交人**: chenjunfeng (developer.jeff.c@gmail.com) **时间**: 2026-05-29 18:04:04 --- ## 1. 审查摘要 - **代码质量评分**:5.5 / 10 分 - **总体评价**:代码实现了较为复杂的商品流水与库存管理业务,但带有明显的“历史遗留”特征。方法冗长、职责混杂,存在多处 SQL 语法错误、拼写笔误及严重的 SQL 注入隐患。大量使用原生 SQL 拼接与循环内查询,导致性能瓶颈与维护成本极高。需进行架构级重构与规范化改造。 - **风险等级**:🔴 高(存在 SQL 注入、逻辑断裂、内存溢出风险) ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `Ahead_merchant_goods_stock_model.php`<br>~350行 | **SQL 注入风险**:`update_confiscate_deposit` 中直接拼接 `INSERT ... ON DUPLICATE KEY UPDATE` 语句,未使用参数绑定或框架转义,若 `$v` 数据含恶意字符将导致注入。 | 废弃原生拼接,改用框架 Query Builder 的 `insert_batch()` 或 `query()` 配合 `?` 占位符。 | `$this->db->query("INSERT INTO ... VALUES (?, ?) ON DUPLICATE KEY UPDATE ...", [$val1, $val2]);` | | 🔴 严重 | `Ahead_merchant_goods_stock_model.php`<br>~230行 | **SQL 注入风险**:`update_stock` 中 `$up = "_remain_total=_remain_total+" . $data['quantity'];` 直接拼接数值,未做严格类型校验,存在注入与精度丢失风险。 | 使用框架提供的 `set()` 方法或严格类型转换 `(float)$data['quantity']`,并交由 Query Builder 处理。 | `$this->db->set('_remain_total', '_remain_total + ' . (float)$qty, FALSE)->where(...)->update(...);` | | 🔴 严重 | `Ahead_goods_log_model.php`<br>~135行 | **SQL 语法错误/拼写**:`take_deposit` 查询字段中 `FROM_UNIXTIME(\`take\`._creare_time` 存在拼写错误 `_creare_time`,将导致查询失败。 | 修正字段名为 `_create_time`。 | `FROM_UNIXTIME(\`take\`._create_time, "%Y-%m-%d %H:%i") as time` | | 🔴 严重 | `Ahead_goods_log_model.php`<br>~155行 | **SQL 语法错误**:`confiscate` 字段定义中出现双逗号 `a._status as status,,deposit._end_time`,执行时将抛出 SQL 异常。 | 删除多余的逗号。 | `a._status as status, deposit._end_time as end_time` | | 🟠 警告 | `Ahead_goods_log_model.php`<br>~280-320行 | **N+1 查询性能瓶颈**:在 `foreach ($res as &$v)` 循环中频繁调用 `get_goods_remark()`,数据量大时将导致数据库连接耗尽与响应超时。 | 提取所有 `relation_id`,使用 `WHERE IN` 批量查询备注,再在内存中映射匹配。 | `$ids = array_column($res, 'relation_id'); $remarks = $model->get_batch_remarks($ids);` | | 🟠 警告 | `Ahead_merchant_goods_stock_model.php`<br>~115-120行 | **脆弱逻辑/解析风险**:`search_stock_list` 通过 `strpos` 截取 `$this->db->last_query()` 生成子查询。若 SQL 结构变化或包含 `FROM/ORDER` 字符串字面量,将直接崩溃。 | 放弃字符串截取,使用框架 Query Builder 的 `select()` 与 `get_compiled_select()` 安全构建子查询。 | `$subQuery = $this->db->select('goods._id')->get_compiled_select();` | | 🟠 警告 | `Ahead_merchant_goods_stock_model.php`<br>~100行 | **冗余转义/潜在乱码**:使用 `addslashes()` 处理搜索参数。CI/PHPCI 的 Query Builder 已自动转义,`addslashes` 会导致双重转义或破坏 UTF-8 字符。 | 移除 `addslashes()`,直接传入参数,交由底层驱动处理。 | `$where['like'] = ['goods._goods_name', $params['merchant_goods_name']];` | | 🟠 警告 | `Ahead_merchant_goods_stock_model.php`<br>~450行 | **内存溢出风险**:`import_stocktaking_goods` 使用 `read_excel(3, 1000, true)` 一次性加载全表数据。若 Excel 超万行将触发 OOM。 | 改用 `PhpSpreadsheet` 的迭代器模式或分块读取(Chunk Read),或限制单次导入行数。 | `$reader->setReadFilter(new ChunkReadFilter($startRow, $chunkSize));` | | 🟡 建议 | 两文件顶部 | **反模式:全局 `$CI` 实例化**:`$CI = &get_instance();` 写在类外部,破坏封装性且可能在 CLI 或单元测试中报错。 | 移至类构造函数中,或使用 `$this->load->` 按需加载。 | `public function __construct() { parent::__construct(); $this->CI =& get_instance(); }` | | 🟡 建议 | `Ahead_goods_log_model.php`<br>多处 | **魔法数字与硬编码**:大量使用 `1, 2, 3, 5, 10` 等状态码,可读性差且易出错。 | 提取为类常量或枚举(PHP 8.1+),如 `const TYPE_SALES = 1;`。 | `const TYPE_SALES = 1; if ($type === self::TYPE_SALES) { ... }` | | 🟡 建议 | 两文件 | **违反单一职责原则 (SRP)**:`get_list` 与 `get_export_data` 超 300 行,混合了 SQL 构建、数据查询、业务计算、格式化与导出逻辑。 | 拆分为 `QueryBuilder`、`DataProcessor`、`Formatter` 独立方法或 Service 层。 | 见下方重构建议 | ## 3. 总结与行动建议 ### 🚨 优先修复项(P0) 1. **修复 SQL 语法与拼写错误**:立即修正 `_creare_time` 拼写、`status,,` 双逗号及 `count_falg` 变量名错误,避免线上查询直接报错。 2. **消除 SQL 注入隐患**:全面替换 `update_confiscate_deposit` 与 `update_stock` 中的原生 SQL 拼接,强制使用框架 Query Builder 或参数化查询。 3. **移除 `addslashes` 与脆弱 SQL 解析**:依赖框架自动转义机制;使用 `get_compiled_select()` 替代 `strpos` 截取,提升稳定性。 ### 🛠 后续重构与优化方向 1. **架构分层与职责拆分**: - 当前 Model 承担了 `数据访问` + `业务计算` + `视图格式化` 三重职责。建议引入 **Service 层** 处理 `get_goods_remark` 批量映射、价格计算、状态转换等逻辑。 - 将 `$fields_arr`、`$export_arr` 等配置数组抽离至独立的 `Config` 类或 `YAML/JSON` 文件中,降低 Model 体积。 2. **性能优化策略**: - **批量查询替代循环查询**:所有 `foreach` 内调用 Model 的场景(如获取备注、关联信息)必须改为 `WHERE IN` 批量拉取,并在 PHP 内存中通过 `array_column` 建立索引映射。 - **数据库索引检查**:确保 `_merchant_id`, `_shop_id`, `_create_time`, `_merchant_goods_id` 等高频查询字段已建立联合索引。 3. **代码规范与现代化**: - 统一数组语法为 `[]`,添加 PHPDoc 类型声明(如 `@param int $merchantId`)。 - 若环境支持 PHP 8.1+,强烈建议使用 `enum` 管理 `$type_arr`、`$status_arr` 等业务状态。 - 框架适配说明:代码结构高度契合 CodeIgniter 3 规范。若 `phpci` 为内部定制框架,请确认 `Simple_model` 的 `up()`、`select()` 方法是否已内置防注入机制;若未内置,需立即升级底层驱动。 > 💡 **局限性说明**:`Ahead_goods_log_model.php` 文件末尾被截断,部分 `switch` 分支逻辑与 `get_where` 实现未完整展示。上述审查基于已提供代码片段,若实际业务逻辑包含动态表名切换或复杂事务,建议补充完整代码后二次审查。 --- *此 Issue 由代码审查服务自动创建*
...
milestone_id
0
priority
0
is_closed
0
is_pull
0
num_comments
0
ref
deadline_unix
0
created_unix
1780049044
updated_unix
1780049044
closed_unix
0
is_locked
0
content_version
0
time_estimate
0
Delete
Cancel