feat: 完成附件存储重构与磁盘监控功能,同步物料档案与入库单规则更新
1. 新增跨平台磁盘空间监控能力,每日7点自动检测附件目录剩余空间,触发阈值告警 2. 重构附件存储方案为年/月/日/文件类型分层结构,统一MES与WMS的附件管理逻辑 3. 对齐物料档案与入库单的质量状态校验规则,仅合格品计入库存与出库 4. 实现入库单作废功能与区域库位的多级父子结构管理 5. 删除物料简称字段,补充规格型号与单位必填项,统一系统数据口径 6. 新增附件中心与磁盘状态查询接口,完善权限控制与操作日志
This commit is contained in:
@@ -2,38 +2,107 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"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) {
|
||||
// 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).Save(ctx)
|
||||
SetFilePath(filePath).SetFileSize(fileSize).SetUploader(uploader).
|
||||
SetFileType(fileType).SetFileExt(fileExt).SetFileMd5(fileMD5).SetMimeType(mimeType).
|
||||
Save(ctx)
|
||||
}
|
||||
|
||||
// ListAttachments 按业务类型/ID 查询附件
|
||||
// ListAttachments 按业务类型/ID 查询附件(排除逻辑删除),供各业务页面的附件面板使用。
|
||||
func (s *Service) ListAttachments(ctx context.Context, bizType, bizId string) ([]*ent.Attachment, error) {
|
||||
q := s.ctx.EntClient.Attachment.Query()
|
||||
q := s.ctx.EntClient.Attachment.Query().Where(attachment.DeletedEQ(false))
|
||||
if bizType != "" {
|
||||
q = q.Where(attachment.BizType(bizType))
|
||||
q = q.Where(attachment.BizTypeEQ(bizType))
|
||||
}
|
||||
if bizId != "" {
|
||||
q = q.Where(attachment.BizId(bizId))
|
||||
q = q.Where(attachment.BizIdEQ(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) {
|
||||
// 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.DeleteOneID(id).Exec(ctx); err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -21,8 +21,9 @@ type ProductTypeReq struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Spec string `json:"spec"`
|
||||
Unit string `json:"unit"`
|
||||
Category string `json:"category"`
|
||||
Remark string `json:"remark"`
|
||||
IsActive bool `json:"isActive"`
|
||||
}
|
||||
|
||||
@@ -31,7 +32,16 @@ func (s *Service) CreateProductType(ctx context.Context, req ProductTypeReq) err
|
||||
if !req.IsActive {
|
||||
active = true
|
||||
}
|
||||
if err := s.ctx.Wms.CreateProductType(ctx, req.Code, req.Name, req.Category, req.Remark, active); err != nil {
|
||||
if strings.TrimSpace(req.Name) == "" {
|
||||
return errors.New("名称为必填项")
|
||||
}
|
||||
if strings.TrimSpace(req.Spec) == "" {
|
||||
return errors.New("规格型号为必填项")
|
||||
}
|
||||
if strings.TrimSpace(req.Unit) == "" {
|
||||
return errors.New("单位为必填项")
|
||||
}
|
||||
if err := s.ctx.Wms.CreateProductType(ctx, req.Code, req.Name, req.Spec, req.Unit, req.Category, active); err != nil {
|
||||
return errors.New("WMS 不可达或拒绝写入(编码主数据以 WMS 为单一来源):" + err.Error())
|
||||
}
|
||||
return nil
|
||||
@@ -41,7 +51,16 @@ func (s *Service) UpdateProductType(ctx context.Context, req ProductTypeReq) err
|
||||
if req.Id == 0 {
|
||||
return errors.New("缺少 id")
|
||||
}
|
||||
if err := s.ctx.Wms.UpdateProductType(ctx, req.Id, req.Name, req.Category, req.Remark, req.IsActive); err != nil {
|
||||
if strings.TrimSpace(req.Name) == "" {
|
||||
return errors.New("名称为必填项")
|
||||
}
|
||||
if strings.TrimSpace(req.Spec) == "" {
|
||||
return errors.New("规格型号为必填项")
|
||||
}
|
||||
if strings.TrimSpace(req.Unit) == "" {
|
||||
return errors.New("单位为必填项")
|
||||
}
|
||||
if err := s.ctx.Wms.UpdateProductType(ctx, req.Id, req.Name, req.Spec, req.Unit, req.Category, req.IsActive); err != nil {
|
||||
return errors.New("WMS 不可达或拒绝写入:" + err.Error())
|
||||
}
|
||||
return nil
|
||||
@@ -143,8 +162,8 @@ func (s *Service) localProductTypes(ctx context.Context, code, name, category st
|
||||
continue
|
||||
}
|
||||
rows = append(rows, wmsclient.ProductTypeRow{
|
||||
ID: p.ID, Code: p.Code, Name: p.Name, Category: p.Category,
|
||||
Remark: p.Remark, IsActive: p.IsActive,
|
||||
ID: p.ID, Code: p.Code, Name: p.Name, Spec: p.Spec, Unit: p.Unit,
|
||||
Category: p.Category, IsActive: p.IsActive,
|
||||
CreatedAt: p.CreatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user