feat: 大屏新增虚拟工位滚动栏与多订单轮播,附件表增加删除原因及彻底清除留痕字段
This commit is contained in:
@@ -241,19 +241,24 @@ func uploadAttachmentHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
|
||||
// attachmentViewRow 附件返回视图:附带可直接预览/下载的 URL(按 id 提供,避免中文/多级路径问题)。
|
||||
type attachmentViewRow struct {
|
||||
ID int64 `json:"id"`
|
||||
BizType string `json:"bizType"`
|
||||
BizID string `json:"bizId"`
|
||||
FileType string `json:"fileType"`
|
||||
FileName string `json:"fileName"`
|
||||
FileSize int64 `json:"fileSize"`
|
||||
FileExt string `json:"fileExt"`
|
||||
MimeType string `json:"mimeType"`
|
||||
UploadedBy string `json:"uploadedBy"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
Archived bool `json:"archived"`
|
||||
Deleted bool `json:"deleted"`
|
||||
URL string `json:"url"`
|
||||
ID int64 `json:"id"`
|
||||
BizType string `json:"bizType"`
|
||||
BizID string `json:"bizId"`
|
||||
FileType string `json:"fileType"`
|
||||
FileName string `json:"fileName"`
|
||||
FileSize int64 `json:"fileSize"`
|
||||
FileExt string `json:"fileExt"`
|
||||
MimeType string `json:"mimeType"`
|
||||
UploadedBy string `json:"uploadedBy"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
Archived bool `json:"archived"`
|
||||
Deleted bool `json:"deleted"`
|
||||
DeletedAt int64 `json:"deletedAt"`
|
||||
DeletedBy string `json:"deletedBy"`
|
||||
DeletedReason string `json:"deletedReason"`
|
||||
PurgedAt int64 `json:"purgedAt"`
|
||||
PurgedBy string `json:"purgedBy"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
func attachmentView(ctx *svc.ServiceContext, rec *ent.Attachment) *attachmentViewRow {
|
||||
@@ -261,6 +266,8 @@ func attachmentView(ctx *svc.ServiceContext, rec *ent.Attachment) *attachmentVie
|
||||
ID: int64(rec.ID), BizType: rec.BizType, BizID: rec.BizID, FileType: rec.FileType,
|
||||
FileName: rec.FileName, FileSize: rec.FileSize, FileExt: rec.FileExt, MimeType: rec.MimeType,
|
||||
UploadedBy: rec.UploadedBy, CreatedAt: rec.CreatedAt, Archived: rec.Archived, Deleted: rec.Deleted,
|
||||
DeletedAt: rec.DeletedAt, DeletedBy: rec.DeletedBy, DeletedReason: rec.DeletedReason,
|
||||
PurgedAt: rec.PurgedAt, PurgedBy: rec.PurgedBy,
|
||||
// 公开预览 URL 用磁盘 UUID 文件名(不可枚举),杜绝顺序 ID 越权遍历(安全审计 #8)
|
||||
URL: "/uploads/" + filepath.Base(rec.FilePath),
|
||||
}
|
||||
@@ -327,7 +334,8 @@ func listAllAttachmentsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
q = q.Where(attachment.CreatedAtLTE(t.Add(24 * time.Hour).Unix()))
|
||||
}
|
||||
}
|
||||
if r.URL.Query().Get("includeDeleted") != "true" {
|
||||
includeDeleted := r.URL.Query().Get("includeDeleted") == "true"
|
||||
if !includeDeleted {
|
||||
q = q.Where(attachment.DeletedEQ(false))
|
||||
}
|
||||
total, err := q.Count(ctx0())
|
||||
@@ -335,6 +343,36 @@ func listAllAttachmentsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
// 视角死角提示:未勾选含已删除时,告知当前筛选下有多少已删除附件被隐藏
|
||||
hiddenDeleted := 0
|
||||
if !includeDeleted {
|
||||
hq := ctx.EntClient.Attachment.Query().Where(attachment.DeletedEQ(true))
|
||||
if v := strings.TrimSpace(r.URL.Query().Get("fileType")); v != "" {
|
||||
hq = hq.Where(attachment.FileTypeEQ(strings.ToUpper(v)))
|
||||
}
|
||||
if v := strings.TrimSpace(r.URL.Query().Get("bizType")); v != "" {
|
||||
hq = hq.Where(attachment.BizTypeEQ(v))
|
||||
}
|
||||
if v := strings.TrimSpace(r.URL.Query().Get("bizId")); v != "" {
|
||||
hq = hq.Where(attachment.BizIDContainsFold(v))
|
||||
}
|
||||
if v := strings.TrimSpace(r.URL.Query().Get("fileName")); v != "" {
|
||||
hq = hq.Where(attachment.FileNameContainsFold(v))
|
||||
}
|
||||
if v := strings.TrimSpace(r.URL.Query().Get("startDate")); v != "" {
|
||||
if t, err := time.ParseInLocation("2006-01-02", v, time.Local); err == nil {
|
||||
hq = hq.Where(attachment.CreatedAtGTE(t.Unix()))
|
||||
}
|
||||
}
|
||||
if v := strings.TrimSpace(r.URL.Query().Get("endDate")); v != "" {
|
||||
if t, err := time.ParseInLocation("2006-01-02", v, time.Local); err == nil {
|
||||
hq = hq.Where(attachment.CreatedAtLTE(t.Add(24 * time.Hour).Unix()))
|
||||
}
|
||||
}
|
||||
if n, e := hq.Count(ctx0()); e == nil {
|
||||
hiddenDeleted = n
|
||||
}
|
||||
}
|
||||
list, err := q.Order(ent.Desc("created_at"), ent.Desc("id")).
|
||||
Offset((page - 1) * pageSize).Limit(pageSize).All(ctx0())
|
||||
if err != nil {
|
||||
@@ -347,54 +385,7 @@ func listAllAttachmentsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
// 磁盘状态一并返回,供页面顶部告警条展示
|
||||
ok(w, map[string]any{"total": total, "list": rows, "page": page, "pageSize": pageSize,
|
||||
"disk": diskStatus(ctx)})
|
||||
}
|
||||
}
|
||||
|
||||
// deleteAttachmentHandler 删除附件(逻辑删除:DB 标记 deleted;物理文件由「已删除附件」抽屉执行物理清理释放空间)
|
||||
// POST /api/attachments/delete body: { id }
|
||||
func deleteAttachmentHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
if err := parseJSON(r, &req); err != nil || req.ID <= 0 {
|
||||
fail(w, http.StatusBadRequest, "参数错误:id 必填")
|
||||
return
|
||||
}
|
||||
rec, err := ctx.EntClient.Attachment.Get(ctx0(), int(req.ID))
|
||||
if err != nil {
|
||||
fail(w, http.StatusNotFound, "附件不存在")
|
||||
return
|
||||
}
|
||||
// 删除保护(需求1):关键业务附件禁止删除,避免质量/工艺记录丢失。
|
||||
// 1) 工艺流程(PROCESS_PDF)、流程卡(PROCESS_CARD):全程禁删(工艺依据,需永久留存)。
|
||||
// 2) 检验照片/报告(INSPECTION_*):未超本地保留年数禁删(质量追溯期内不可删)。
|
||||
switch rec.FileType {
|
||||
case FileTypeProcessPDF, FileTypeProcessCard:
|
||||
fail(w, http.StatusForbidden, "工艺流程 / 流程卡为关键工艺文件,禁止删除")
|
||||
return
|
||||
case FileTypeInspectionPhoto, FileTypeInspectionPDF:
|
||||
years := ctx.Config.Attachment.LocalRetentionYears
|
||||
if years <= 0 {
|
||||
years = 1
|
||||
}
|
||||
cutYear := timeNow().Year() - years
|
||||
if time.Unix(rec.CreatedAt, 0).Year() >= cutYear {
|
||||
fail(w, http.StatusForbidden, fmt.Sprintf("检验记录在质量追溯保留期(%d 年)内,禁止删除", years))
|
||||
return
|
||||
}
|
||||
}
|
||||
if _, err := ctx.EntClient.Attachment.UpdateOneID(int(req.ID)).
|
||||
SetDeleted(true).SetDeletedAt(timeNow().Unix()).
|
||||
SetNillableDeletedBy(strPtr(r.Header.Get("X-Username"))).
|
||||
Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "删除附件失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
ctx.EventLog.Write(ctx0(), "attachment.delete", r.Header.Get("X-Username"), rec.BizType, rec.BizID,
|
||||
"删除附件 "+rec.FileName, nil)
|
||||
ok(w, map[string]any{"id": req.ID})
|
||||
"hiddenDeleted": hiddenDeleted, "disk": diskStatus(ctx)})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -540,16 +531,25 @@ func checkDiskOnce(ctx *svc.ServiceContext) {
|
||||
}
|
||||
}
|
||||
|
||||
// purgeDeletedHandler 物理清理已逻辑删除的附件(需求2):
|
||||
// 扫描 deleted=true 的索引,物理删除对应磁盘文件释放本地空间;
|
||||
// 索引保留(deleted 标记不变)以便追溯。默认 dryRun 预检,返回将清理数量与释放字节。
|
||||
// POST /api/attachments/purge-deleted body: { dryRun }
|
||||
// purgeDeletedHandler 批量物理清理已逻辑删除的附件(数据归档页「磁盘空间回收」批量入口):
|
||||
// 扫描 deleted=true 且未清除的索引,物理删除对应磁盘文件释放本地空间;
|
||||
// 索引保留(purged_at/by 留痕)以便追溯。dryRun 预检返回将清理数量与释放字节;
|
||||
// 执行须实名留痕;保护期/禁删保护的附件跳过(计入 protected)。
|
||||
// POST /api/attachments/purge-deleted body: { dryRun, ackName, ackPhone }
|
||||
func purgeDeletedHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
DryRun bool `json:"dryRun"`
|
||||
DryRun bool `json:"dryRun"`
|
||||
AckName string `json:"ackName"`
|
||||
AckPhone string `json:"ackPhone"`
|
||||
}
|
||||
_ = parseJSON(r, &req)
|
||||
if !req.DryRun {
|
||||
if err := checkAckNamePhone(req.AckName, req.AckPhone); err != nil {
|
||||
fail(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
root := attRoot(ctx)
|
||||
recs, err := ctx.EntClient.Attachment.Query().
|
||||
Where(attachment.DeletedEQ(true)).All(ctx0())
|
||||
@@ -557,8 +557,17 @@ func purgeDeletedHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
removed, freed, missing, failed := 0, int64(0), 0, 0
|
||||
removed, freed, missing, failed, protected := 0, int64(0), 0, 0, 0
|
||||
user := r.Header.Get("X-Username")
|
||||
for _, rec := range recs {
|
||||
if rec.PurgedAt != 0 {
|
||||
missing++ // 已清除过(文件已删),仅计数
|
||||
continue
|
||||
}
|
||||
if blocked := attachmentProtectCheck(ctx, rec.CreatedAt) + attachmentFileTypeProtect(ctx, rec); blocked != "" {
|
||||
protected++
|
||||
continue
|
||||
}
|
||||
abs, okp := safeAbsPath(root, rec.FilePath)
|
||||
if !okp {
|
||||
failed++
|
||||
@@ -567,6 +576,10 @@ func purgeDeletedHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
info, statErr := os.Stat(abs)
|
||||
if statErr != nil {
|
||||
missing++ // 文件已不存在(已清理或丢失),仅计数
|
||||
// 补标记:索引侧注明已清除,避免反复扫描
|
||||
now := timeNow().Unix()
|
||||
_, _ = ctx.EntClient.Attachment.UpdateOneID(rec.ID).
|
||||
SetNillablePurgedAt(&now).SetNillablePurgedBy(strPtr(user)).Save(ctx0())
|
||||
continue
|
||||
}
|
||||
size := info.Size()
|
||||
@@ -579,15 +592,21 @@ func purgeDeletedHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
now := timeNow().Unix()
|
||||
_, _ = ctx.EntClient.Attachment.UpdateOneID(rec.ID).
|
||||
SetNillablePurgedAt(&now).SetNillablePurgedBy(strPtr(user)).Save(ctx0())
|
||||
removed++
|
||||
freed += size
|
||||
}
|
||||
if !req.DryRun {
|
||||
ctx.EventLog.Write(ctx0(), "attachment.purge", r.Header.Get("X-Username"), "attachment", "purge",
|
||||
fmt.Sprintf("物理清理已删附件:删除%d 释放%.2fMB 缺失%d 失败%d", removed, float64(freed)/1024/1024, missing, failed),
|
||||
map[string]any{"removed": removed, "freed": freed, "missing": missing, "failed": failed})
|
||||
ctx.EventLog.Write(ctx0(), "attachment.purge", user, "attachment", "purge",
|
||||
fmt.Sprintf("批量物理清理已删附件:删除%d 释放%.2fMB 缺失%d 失败%d 保护期跳过%d(确认人:%s %s)",
|
||||
removed, float64(freed)/1024/1024, missing, failed, protected,
|
||||
strings.TrimSpace(req.AckName), strings.TrimSpace(req.AckPhone)),
|
||||
map[string]any{"removed": removed, "freed": freed, "missing": missing, "failed": failed, "protected": protected})
|
||||
}
|
||||
ok(w, map[string]any{"removed": removed, "freed": freed, "missing": missing, "failed": failed, "dryRun": req.DryRun})
|
||||
ok(w, map[string]any{"removed": removed, "freed": freed, "missing": missing, "failed": failed,
|
||||
"protected": protected, "dryRun": req.DryRun})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user