package com.xkrs.service.impl; import com.xkrs.dao.DataDictDao; import com.xkrs.dao.DataSourceDao; import com.xkrs.dao.FileDao; import com.xkrs.encapsulation.PromptMessageEnum; import com.xkrs.model.entity.DataSource; import com.xkrs.model.entity.FileEntity; import com.xkrs.model.qo.FileQo; import com.xkrs.service.FileService; import com.xkrs.util.ExcelUploadUtil; import com.xkrs.util.ExportExcel; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.transaction.Transactional; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject; /** * @Author: XinYi Song * @Date: 2022/1/20 15:24 */ @Service public class FileServiceImpl implements FileService { @Resource private FileDao fileDao; @Resource private DataDictDao dataDictDao; @Resource private DataSourceDao dataSourceDao; /** * 添加模板信息 * * @param fileQo * @return */ @Override public String insertFileExcel(FileQo fileQo, MultipartFile fileExcel) throws IOException { Locale locale = LocaleContextHolder.getLocale(); FileEntity fileEntity = fileDao.selectFile(fileQo.getLotNo(), fileQo.getMachineNo(), fileQo.getMaterialNo(), fileQo.getModelNo(), fileQo.getPartNo()); if (fileEntity != null) { return outputEncapsulationObject(PromptMessageEnum.DATA_EXIT, "已存在该产品的模板!", locale); } if (fileExcel == null) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "模板不能为空!", locale); } String file = ExcelUploadUtil.memoryFile(fileExcel); FileEntity fileEntity1 = new FileEntity(); fileEntity1.setLotNo(fileQo.getLotNo()); fileEntity1.setMachineNo(fileQo.getMachineNo()); fileEntity1.setMaterialNo(fileQo.getMaterialNo()); fileEntity1.setModelNo(fileQo.getModelNo()); fileEntity1.setPartNo(fileQo.getPartNo()); fileEntity1.setFileUploadPath(file); fileDao.save(fileEntity1); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale); } /** * 导出excel * * @param fileQo * @return */ @Transactional(rollbackOn = Exception.class) @Override public String exportExcel(FileQo fileQo) throws IOException, InvalidFormatException { Locale locale = LocaleContextHolder.getLocale(); FileEntity entity = fileDao.selectFile(fileQo.getLotNo(), fileQo.getMachineNo(), fileQo.getMaterialNo(), fileQo.getModelNo(), fileQo.getPartNo()); if (entity == null) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该产品的模板,请添加!", locale); } String fileUploadPath = entity.getFileUploadPath(); List dataSources = dataSourceDao.selectDataSource(fileQo.getLotNo(), fileQo.getMachineNo(), fileQo.getMaterialNo(), fileQo.getModelNo(), fileQo.getPartNo()); if (dataSources == null || dataSources.size() == 0) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该产品的信息!", locale); } Map map = new HashMap<>(); for (DataSource dataSource : dataSources) { map.put(dataSource.getInspectionItemCode(), dataSource.getInspectValue()); } String fill = ExportExcel.exportToProveExcel(map, fileUploadPath); fileDao.updateFileDownloadPath(fileQo.getLotNo(), fileQo.getMachineNo(), fileQo.getMaterialNo(), fileQo.getModelNo(), fileQo.getPartNo(), fill); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, fill, locale); } }