package logic import ( "context" "time" "bj_power_mes/ent" "bj_power_mes/ent/attachment" ) // AddAttachment 新增附件记录(含统一存储方案字段:文件类型/扩展名/MD5/MIME)。 func (s *Service) AddAttachment(ctx context.Context, bizType, bizId, fileType, fileName, filePath, fileExt, fileMD5, mimeType string, fileSize int, uploader string) (*ent.Attachment, error) { return s.ctx.EntClient.Attachment.Create(). SetBizType(bizType).SetBizId(bizId).SetFileName(fileName). SetFilePath(filePath).SetFileSize(fileSize).SetUploader(uploader). SetFileType(fileType).SetFileExt(fileExt).SetFileMd5(fileMD5).SetMimeType(mimeType). Save(ctx) } // ListAttachments 按业务类型/ID 查询附件(排除逻辑删除),供各业务页面的附件面板使用。 func (s *Service) ListAttachments(ctx context.Context, bizType, bizId string) ([]*ent.Attachment, error) { q := s.ctx.EntClient.Attachment.Query().Where(attachment.DeletedEQ(false)) if bizType != "" { q = q.Where(attachment.BizTypeEQ(bizType)) } if bizId != "" { q = q.Where(attachment.BizIdEQ(bizId)) } return q.Order(ent.Desc(attachment.FieldCreatedAt), ent.Desc(attachment.FieldID)).All(ctx) } // FindByMD5 同业务对象 + 同 MD5 且未删除的既有附件(秒传/去重用)。 func (s *Service) FindByMD5(ctx context.Context, bizType, bizId, fileMD5 string) (*ent.Attachment, error) { return s.ctx.EntClient.Attachment.Query(). Where(attachment.BizTypeEQ(bizType), attachment.BizIdEQ(bizId), attachment.FileMd5EQ(fileMD5), attachment.DeletedEQ(false)). Only(ctx) } // GetAttachment 按 id 取附件。 func (s *Service) GetAttachment(ctx context.Context, id int) (*ent.Attachment, error) { return s.ctx.EntClient.Attachment.Get(ctx, id) } // DeleteAttachment 逻辑删除附件(DB 标记 deleted,物理文件由归档任务统一处理)。 func (s *Service) DeleteAttachment(ctx context.Context, id int, operator string) (*ent.Attachment, error) { att, err := s.ctx.EntClient.Attachment.Get(ctx, id) if err != nil { return nil, err } if _, err := s.ctx.EntClient.Attachment.UpdateOneID(id). SetDeleted(true).SetDeletedBy(operator).Save(ctx); err != nil { return nil, err } return att, nil } // ListAttachmentPage 附件中心:全量分页查询(文件类型/业务类型/业务标识/文件名/时间区间)。 // 默认只统计未删除记录;includeDeleted=true 时含已逻辑删除(便于追溯)。 func (s *Service) ListAttachmentPage(ctx context.Context, fileType, bizType, bizId, fileName string, start, end time.Time, includeDeleted bool, page, pageSize int) (int, []*ent.Attachment, error) { q := s.ctx.EntClient.Attachment.Query() if !includeDeleted { q = q.Where(attachment.DeletedEQ(false)) } if fileType != "" { q = q.Where(attachment.FileTypeEQ(fileType)) } if bizType != "" { q = q.Where(attachment.BizTypeEQ(bizType)) } if bizId != "" { q = q.Where(attachment.BizIdContainsFold(bizId)) } if fileName != "" { q = q.Where(attachment.FileNameContainsFold(fileName)) } if !start.IsZero() { q = q.Where(attachment.CreatedAtGTE(start)) } if !end.IsZero() { q = q.Where(attachment.CreatedAtLTE(end)) } total, err := q.Count(ctx) if err != nil { return 0, nil, err } list, err := q.Order(ent.Desc(attachment.FieldCreatedAt), ent.Desc(attachment.FieldID)). Offset((page - 1) * pageSize).Limit(pageSize).All(ctx) if err != nil { return 0, nil, err } return total, list, nil } // ListArchivableAttachments 列出「未归档且未删除」的附件(归档任务用,年份判断在调用方)。 func (s *Service) ListArchivableAttachments(ctx context.Context) ([]*ent.Attachment, error) { return s.ctx.EntClient.Attachment.Query(). Where(attachment.ArchivedEQ(false), attachment.DeletedEQ(false)). All(ctx) } // MarkArchived 标记附件已归档(物理文件已移到外部备份目录)。 func (s *Service) MarkArchived(ctx context.Context, id int) error { _, err := s.ctx.EntClient.Attachment.UpdateOneID(id). SetArchived(true).SetArchivedAt(time.Now().Unix()).Save(ctx) return err }