| | |
| | | package cn.iocoder.yudao.module.mes.service.pd.document; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.document.vo.MesPdDocumentPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.document.vo.MesPdDocumentSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pd.document.MesPdDocumentDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.pd.document.MesPdDocumentMapper; |
| | | import cn.iocoder.yudao.module.mes.service.pd.project.MesPdProjectService; |
| | | import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi; |
| | | import cn.iocoder.yudao.module.system.api.storage.dto.StorageBlobRespDTO; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.List; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | |
| | | projectService.validateProjectExists(createReqVO.getProjectId()); |
| | | // 插入 |
| | | MesPdDocumentDO document = BeanUtils.toBean(createReqVO, MesPdDocumentDO.class); |
| | | // 补充上传人、上传时间 |
| | | if (document.getUploadUserId() == null) { |
| | | document.setUploadUserId(SecurityFrameworkUtils.getLoginUserId()); |
| | | } |
| | | if (document.getUploadTime() == null) { |
| | | document.setUploadTime(LocalDateTime.now()); |
| | | } |
| | | documentMapper.insert(document); |
| | | storageAttachmentApi.bindAttachments("file", "mes_pd_document", |
| | | document.getId(), createReqVO.getBlobIds()); |
| | | // 未填写文件名时,从上传附件自动补全 |
| | | fillFileNameIfAbsent(document); |
| | | return document.getId(); |
| | | } |
| | | |
| | | @Override |
| | | public void updateDocument(MesPdDocumentSaveReqVO updateReqVO) { |
| | | // 校验存在 |
| | | validateDocumentExists(updateReqVO.getId()); |
| | | MesPdDocumentDO existing = validateDocumentExists(updateReqVO.getId()); |
| | | // 更新 |
| | | MesPdDocumentDO updateObj = BeanUtils.toBean(updateReqVO, MesPdDocumentDO.class); |
| | | // 保留原上传人、上传时间;历史数据为空时补齐 |
| | | if (updateObj.getUploadUserId() == null) { |
| | | updateObj.setUploadUserId(existing.getUploadUserId() != null |
| | | ? existing.getUploadUserId() : SecurityFrameworkUtils.getLoginUserId()); |
| | | } |
| | | if (updateObj.getUploadTime() == null) { |
| | | updateObj.setUploadTime(existing.getUploadTime() != null |
| | | ? existing.getUploadTime() : LocalDateTime.now()); |
| | | } |
| | | documentMapper.updateById(updateObj); |
| | | storageAttachmentApi.updateAttachments("file", "mes_pd_document", updateReqVO.getId(), updateReqVO.getBlobIds()); |
| | | // 未填写文件名时,从上传附件自动补全 |
| | | fillFileNameIfAbsent(updateObj); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return document; |
| | | } |
| | | |
| | | /** |
| | | * 未填写文件名时,从上传附件自动补全文件名 |
| | | * |
| | | * @param document 设计资料 |
| | | */ |
| | | private void fillFileNameIfAbsent(MesPdDocumentDO document) { |
| | | if (StrUtil.isNotEmpty(document.getFileName())) { |
| | | return; |
| | | } |
| | | List<StorageBlobRespDTO> attachments = storageAttachmentApi.listAttachments("mes_pd_document", document.getId()); |
| | | if (CollUtil.isNotEmpty(attachments)) { |
| | | document.setFileName(attachments.get(0).getName()); |
| | | documentMapper.updateById(document); |
| | | } |
| | | } |
| | | } |