40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package logic
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"bj_power_mes/ent"
|
||
|
|
"bj_power_mes/ent/attachment"
|
||
|
|
)
|
||
|
|
|
||
|
|
// AddAttachment 新增附件记录
|
||
|
|
func (s *Service) AddAttachment(ctx context.Context, bizType, bizId, fileName, filePath 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).Save(ctx)
|
||
|
|
}
|
||
|
|
|
||
|
|
// ListAttachments 按业务类型/ID 查询附件
|
||
|
|
func (s *Service) ListAttachments(ctx context.Context, bizType, bizId string) ([]*ent.Attachment, error) {
|
||
|
|
q := s.ctx.EntClient.Attachment.Query()
|
||
|
|
if bizType != "" {
|
||
|
|
q = q.Where(attachment.BizType(bizType))
|
||
|
|
}
|
||
|
|
if bizId != "" {
|
||
|
|
q = q.Where(attachment.BizId(bizId))
|
||
|
|
}
|
||
|
|
return q.Order(ent.Desc(attachment.FieldCreatedAt), ent.Desc(attachment.FieldID)).All(ctx)
|
||
|
|
}
|
||
|
|
|
||
|
|
// DeleteAttachment 删除附件记录并返回记录(供删除物理文件)
|
||
|
|
func (s *Service) DeleteAttachment(ctx context.Context, id int) (*ent.Attachment, error) {
|
||
|
|
att, err := s.ctx.EntClient.Attachment.Get(ctx, id)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if err := s.ctx.EntClient.Attachment.DeleteOneID(id).Exec(ctx); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return att, nil
|
||
|
|
}
|