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
Update row 630 in issue
id
Primary key.
INTEGER NOT NULL
repo_id
INTEGER
index
INTEGER
poster_id
INTEGER
original_author
TEXT
original_author_id
INTEGER
name
🔍 代码审查报告:pc-260616 - 需求 盘点单增加时间选择
TEXT
content
## 自动代码审查报告 **分支**: pc-260616 **提交**: `f17760cc43e88f2aa1cd9b4f66da9ae98165719a` **提交人**: chenjunfeng (developer.jeff.c@gmail.com) **时间**: 2026-06-09 14:09:54 --- ## 1. 审查摘要 - **代码质量评分**:6.5 / 10 分 - **总体评价**:业务逻辑链路基本完整,涵盖了盘点单创建、列表查询、详情计算、报表生成与导出等核心场景。但代码中存在**严重的并发安全隐患**(动态修改单例模型表名)、**隐式类型转换导致的逻辑漏洞**、**事务边界划分不当**以及**模型层直接中断执行**等反模式。整体架构偏向传统 CodeIgniter 3 风格,与现代 PHP 规范及高并发场景要求存在一定差距。 - **风险等级**:🔴 高 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `add_merchant_stocktaking` ~L38 | **时间比较逻辑错误**:`$now_business_date['end_business_day_str']` 为字符串格式,直接与数字 `86400*15` 相加会触发 PHP 隐式类型转换(通常转为 `0` 或截断),导致 `15天限制` 校验完全失效。 | 统一转换为时间戳后再进行数值比较,或使用 `DateTime` 对象。 | ```php<br>$nowTs = strtotime($now_business_date['end_business_day_str']);<br>$lastTs = strtotime($lastTimeBusiness['end_business_day_str']);<br>if ($nowTs > $lastTs + 86400 * 15) {<br> throwError("盘点库存截止时间只能是15天内");<br>}<br>``` | | 🔴 严重 | `get_stocktaking_detail` ~L118<br>`get_stocktaking_report` ~L168 | **模型单例状态污染**:CI 模型为单例。通过 `set_table_name()` 动态追加别名(如 `table stocktaking`)会修改全局状态。高并发下极易导致请求 A 的别名被请求 B 误用,引发 SQL 语法错误或数据错乱。 | 放弃修改模型属性,改用 Query Builder 的 `from()` 或 `join()` 指定别名,或实例化临时模型对象。 | ```php<br>// 推荐做法:使用 DB 构造器别名<br>$this->db->from($orginalTableName . ' stocktaking');<br>$amountSum = $this->db->get()->row_array();<br>// 或使用临时实例<br>$tempModel = clone $this->Ahead_merchant_stocktaking_infos_model;<br>$tempModel->set_table_name($orginalTableName . ' stocktaking');<br>``` | | 🔴 严重 | `stocktaking_report_export` ~L208, L213 | **模型层直接 `exit()`**:在 Model 中调用 `exit()` 会直接终止 PHP 进程,导致后续中间件、日志记录、事务回滚或框架生命周期钩子无法执行,且可能引发 `Headers already sent` 错误。 | 改为抛出异常或返回错误数组,由 Controller 层统一处理响应。 | ```php<br>if (empty($exportFields)) {<br> throw new \InvalidArgumentException('导出字段不能为空');<br>}<br>if (!$exportRes['success']) {<br> throw new \RuntimeException($exportRes['msg']);<br>}<br>``` | | 🟠 警告 | `add_merchant_stocktaking` ~L68 | **事务边界不当**:`$this->db->trans_commit()` 执行后,紧接着调用 `add_data()`。若 `add_data` 失败,主事务已提交,导致 `stocktaking` 主表与 `update_data` 关联表数据不一致。 | 将 `add_data` 移入 `try` 块内,在 `trans_commit()` 前执行;或改为异步队列处理。 | ```php<br>// 移入事务块内<br>$this->db->trans_start();<br>try {<br> // ... insert & update_stock ...<br> if (!empty($stocktaking_update_time)) {<br> $this->Ahead_stocktaking_update_data_model->add_data(...);<br> }<br> $this->db->trans_complete(); // 推荐用 trans_complete()<br>} catch (...) { ... }<br>``` | | 🟠 警告 | `search_stocktaking_list` ~L95 | **JSON 解析未做容错**:`json_decode($row['operater_names'], true)` 未校验数据合法性。若数据库存储了非法 JSON 字符串,将返回 `null` 并触发 `implode()` 类型警告。 | 增加空值/类型校验,或使用 `json_decode(..., true) ?: []`。 | ```php<br>$names = json_decode($row['operater_names'], true);<br>$row['admin_name'] = is_array($names) ? implode(',', $names) : $row['admin_name'];<br>``` | | 🟠 警告 | `search_stocktaking_list` ~L78 | **权限过滤潜在越权**:`explode(",", trim($CI->priv_shop_ids, ","))` 当 `priv_shop_ids` 为空字符串时,`explode` 返回 `['']`,传入 `WHERE IN` 可能引发 SQL 异常或意外匹配。 | 增加空值判断,过滤空元素。 | ```php<br>$shopIds = array_filter(explode(",", trim($CI->priv_shop_ids, ",")), 'strlen');<br>if (!empty($shopIds)) {<br> $where['where_in'] = ['_shop_id', $shopIds];<br>}<br>``` | | 🟡 建议 | 全局 | **命名不符合 PSR-12**:类名 `Ahead_merchant_goods_stocktaking_model` 及方法名 `add_merchant_stocktaking` 使用下划线,不符合现代 PHP 驼峰命名规范,影响自动加载与团队协作。 | 逐步重构为 `AheadMerchantGoodsStocktakingModel` 及 `addMerchantStocktaking`。若为历史遗留项目,可配置 IDE 映射或添加 `@deprecated` 注释过渡。 | `class AheadMerchantGoodsStocktakingModel extends Simple_model` | | 🟡 建议 | `get_stocktaking_report` ~L145 | **非标准查询语法强耦合**:大量使用 `'_updated_at >= '`、`'where_in' => [...]` 等自定义键名。强依赖 `Simple_model` 内部解析逻辑,降低代码可移植性,且可能绕过框架默认的 SQL 转义。 | 建议统一使用框架原生 Query Builder 语法,或在 `Simple_model` 中明确文档化并增加参数类型校验。 | 无(需结合框架底层实现调整) | ## 3. 总结与行动建议 ### 🔑 优先修复项(P0) 1. **修复时间比较漏洞**:立即将字符串时间转换为时间戳后再进行 `15天` 阈值校验,否则业务规则形同虚设。 2. **消除模型单例污染**:全面移除 `set_table_name()` 动态修改表名的写法,改用 `$this->db->from('table alias')` 或 `clone` 模型实例,彻底解决高并发下的数据串扰风险。 3. **规范事务与异常处理**:将 `add_data` 纳入事务范围;将 Model 中的 `exit()` 替换为 `throw new Exception()`,确保框架生命周期完整执行。 ### 🛠 后续重构方向 1. **架构解耦**:当前 Model 承担了过多职责(数据查询、业务校验、报表组装、导出逻辑)。建议将 `stocktaking_report_export` 移至 `Service` 层或 `Job` 队列,Model 仅负责数据持久化与基础查询。 2. **查询性能优化**: - 报表查询涉及多表 `LEFT JOIN` 与 `GROUP BY`,建议在 `ahead_merchant_goods_stocktaking` 表建立复合索引:`INDEX idx_merchant_status_time (_merchant_id, _status, _create_time)`。 - `get_stocktaking_report` 中的 `buying_price` 与 `shop_config` 已做批量查询优化,可考虑引入 Redis 缓存门店配置,减少重复 DB 查询。 3. **框架适配说明**:代码结构高度符合 **CodeIgniter 3** 规范。若 `phpci` 为内部定制框架,请重点核对 `Simple_model` 对 `where_in`、`>=` 等自定义语法的 SQL 预处理逻辑,确保已启用参数绑定(Prepared Statements)以防 SQL 注入。 > 💡 **提示**:若需进一步审查 `Simple_model` 底层实现或数据库表结构,可提供相关片段以便进行更精准的索引与查询优化评估。 --- *此 Issue 由代码审查服务自动创建*
TEXT
milestone_id
INTEGER
priority
INTEGER
is_closed
INTEGER
is_pull
INTEGER
num_comments
INTEGER
ref
TEXT
deadline_unix
INTEGER
created_unix
INTEGER
updated_unix
INTEGER
closed_unix
INTEGER
is_locked
INTEGER NOT NULL (default 0
content_version
INTEGER NOT NULL (default 0
time_estimate
INTEGER NOT NULL (default 0
Update
Cancel