feat: 大屏改为纯流式自适应布局,新增KPI带与产线动态拓扑地图

This commit is contained in:
SunYF
2026-10-28 10:10:17 +08:00
parent 1f10c3091e
commit 4d3c4e2c3a
131 changed files with 18118 additions and 1728 deletions
+3 -164
View File
@@ -351,7 +351,7 @@ func listAllAttachmentsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
}
}
// deleteAttachmentHandler 删除附件(逻辑删除:DB 标记 deleted,物理文件由归档任务统一处理)
// deleteAttachmentHandler 删除附件(逻辑删除:DB 标记 deleted;物理文件由「已删除附件」抽屉执行物理清理释放空间)
// POST /api/attachments/delete body: { id }
func deleteAttachmentHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@@ -492,86 +492,6 @@ func diskStatusHandler(ctx *svc.ServiceContext) http.HandlerFunc {
}
}
// archiveAttachmentsHandler 归档:把早于「本地保留年数」的附件移动到外部备份目录(ArchiveDir)
// 并标记 archived=TRUE(保留索引,可追溯)。ArchiveDir 为空时仅标记不移动。
// POST /api/attachments/archive body: { dryRun }
func archiveAttachmentsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
DryRun bool `json:"dryRun"`
ArchiveBefore int64 `json:"archiveBefore"` // 归档截止月(精确到月):归档 createdAt <= ArchiveBefore 的附件,单位 unix 秒
}
_ = parseJSON(r, &req)
cfg := ctx.Config.Attachment
root := attRoot(ctx)
now := timeNow()
// 6 个月限制:归档截止月最晚只能选到 6 个月前,最近半年数据不可归档
sixAgo := now.AddDate(0, -6, 0)
sixAgoEnd := time.Date(sixAgo.Year(), sixAgo.Month()+1, 0, 23, 59, 59, 0, sixAgo.Location())
if req.ArchiveBefore <= 0 {
fail(w, http.StatusBadRequest, "请提供归档截止月")
return
}
if req.ArchiveBefore > sixAgoEnd.Unix() {
fail(w, http.StatusBadRequest, "归档截止月需早于最近 6 个月,最近半年数据不可归档")
return
}
cutMonth := time.Unix(req.ArchiveBefore, 0)
recs, err := ctx.EntClient.Attachment.Query().
Where(attachment.ArchivedEQ(false), attachment.DeletedEQ(false)).
All(ctx0())
if err != nil {
fail(w, http.StatusInternalServerError, err.Error())
return
}
moved, marked, failed := 0, 0, 0
for _, rec := range recs {
if rec.CreatedAt > req.ArchiveBefore { // 仅归档创建时间早于归档截止月(含当月)的附件
continue
}
if req.DryRun {
marked++
continue
}
if cfg.ArchiveDir != "" {
src, ok1 := safeAbsPath(root, rec.FilePath)
dst, ok2 := safeAbsPath(cfg.ArchiveDir, rec.FilePath)
if ok1 && ok2 {
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err == nil {
if err := os.Rename(src, dst); err == nil {
moved++
} else if err := copyFile(src, dst); err == nil {
_ = os.Remove(src)
moved++
} else {
failed++
continue
}
} else {
failed++
continue
}
} else {
failed++
continue
}
}
if _, err := ctx.EntClient.Attachment.UpdateOneID(rec.ID).
SetArchived(true).SetArchivedAt(timeNow().Unix()).Save(ctx0()); err != nil {
failed++
continue
}
marked++
}
if !req.DryRun {
ctx.EventLog.Write(ctx0(), "attachment.archive", r.Header.Get("X-Username"), "attachment", "",
fmt.Sprintf("附件归档:移动%d 标记%d 失败%d(截止月 %d-%02d)", moved, marked, failed, cutMonth.Year(), cutMonth.Month()),
map[string]any{"moved": moved, "marked": marked, "failed": failed})
}
ok(w, map[string]any{"moved": moved, "marked": marked, "failed": failed,
"archiveBefore": req.ArchiveBefore, "archiveDir": cfg.ArchiveDir, "dryRun": req.DryRun})
}
}
// isInlineExt 判断扩展名是否可在浏览器内联预览(图片 / PDF)。
// 与 MES attachment 包的判定保持一致:只有这类文件才允许 inline,其余一律强制下载,
@@ -584,27 +504,6 @@ func isInlineExt(ext string) bool {
return false
}
// copyFile 兜底复制(跨盘符 Rename 失败时用)。
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
return err
}
out, err := os.Create(dst)
if err != nil {
return err
}
if _, err := io.Copy(out, in); err != nil {
_ = out.Close()
return err
}
return out.Close()
}
// StartDiskMonitor 启动磁盘空间监控:每天 DiskCheckHour(默认7点) 检测一次,
// 剩余空间 <20% 告警、<10% 提示清理(写日志 + 操作日志,页面顶部同步提示)。
func StartDiskMonitor(ctx *svc.ServiceContext) {
@@ -642,8 +541,8 @@ func checkDiskOnce(ctx *svc.ServiceContext) {
}
// purgeDeletedHandler 物理清理已逻辑删除的附件(需求2):
// 扫描 deleted=true 的索引,删除对应磁盘文件(仅当文件未被归档到 ArchiveDir 时),
// 释放本地空间;索引保留(deleted 标记不变)以便追溯。默认 dryRun 预检,返回将清理数量与释放字节。
// 扫描 deleted=true 的索引,物理删除对应磁盘文件释放本地空间;
// 索引保留(deleted 标记不变)以便追溯。默认 dryRun 预检,返回将清理数量与释放字节。
// POST /api/attachments/purge-deleted body: { dryRun }
func purgeDeletedHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@@ -660,11 +559,6 @@ func purgeDeletedHandler(ctx *svc.ServiceContext) http.HandlerFunc {
}
removed, freed, missing, failed := 0, int64(0), 0, 0
for _, rec := range recs {
// 已归档的文件已移至 ArchiveDir,不在本地,跳过
if rec.Archived {
missing++
continue
}
abs, okp := safeAbsPath(root, rec.FilePath)
if !okp {
failed++
@@ -697,58 +591,3 @@ func purgeDeletedHandler(ctx *svc.ServiceContext) http.HandlerFunc {
}
}
// cleanLogsHandler 日志清理(需求2,仅管理员):清理日志目录(Config.Log.Path)下
// 早于 N 天(默认 30)的 *.log 文件。与 logdaily 自动滚动清理互补,提供手动「立即清理」入口。
// 默认 dryRun 预检。POST /api/attachments/clean-logs body: { days, dryRun }
func cleanLogsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Days int `json:"days"`
DryRun bool `json:"dryRun"`
}
_ = parseJSON(r, &req)
days := req.Days
if days <= 0 {
days = 30 // 默认保留 30 天
}
dir := strings.TrimSpace(ctx.Config.Log.Path)
if dir == "" {
dir = "logs"
}
cutoff := timeNow().AddDate(0, 0, -days)
entries, err := os.ReadDir(dir)
if err != nil {
fail(w, http.StatusInternalServerError, "读取日志目录失败: "+err.Error())
return
}
removed, failed := 0, 0
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".log") {
continue
}
info, ierr := e.Info()
if ierr != nil {
continue
}
// 按文件名日期(base-2006-01-02.log)或修改时间判定,早于 cutoff 才清
if !info.ModTime().Before(cutoff) {
continue
}
if req.DryRun {
removed++
continue
}
if err := os.Remove(filepath.Join(dir, e.Name())); err != nil {
failed++
continue
}
removed++
}
if !req.DryRun {
ctx.EventLog.Write(ctx0(), "system.cleanlog", r.Header.Get("X-Username"), "log", dir,
fmt.Sprintf("清理日志:删除%d 个早于%d天的日志文件,失败%d", removed, days, failed),
map[string]any{"removed": removed, "failed": failed, "days": days})
}
ok(w, map[string]any{"removed": removed, "failed": failed, "days": days, "dir": dir, "dryRun": req.DryRun})
}
}