98 lines
4.1 KiB
Java
Raw Normal View History

2022-02-12 08:48:57 +08:00
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.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.servlet.http.HttpServletResponse;
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.findByFileModelNumberAndFileBatchNumberAndFileMachineCodeAndFileProductNumber(fileQo.getFileModelNumber(), fileQo.getFileBatchNumber(), fileQo.getFileMachineCode(), fileQo.getFileProductNumber());
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();
2022-02-28 21:32:34 +08:00
fileEntity1.setLotNo(fileQo.getLotNo());
fileEntity1.setMachineNo(fileQo.getMachineNo());
fileEntity1.setMaterialNo(fileQo.getMaterialNo());
fileEntity1.setModelNo(fileQo.getModelNo());
fileEntity1.setPartNo(fileQo.getPartNo());
2022-02-12 08:48:57 +08:00
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.findByFileModelNumberAndFileBatchNumberAndFileMachineCodeAndFileProductNumber(fileQo.getFileModelNumber(), fileQo.getFileBatchNumber(), fileQo.getFileMachineCode(), fileQo.getFileProductNumber());
if(entity == null){
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时没有该产品的模板,请添加!",locale);
}
String fileUploadPath = entity.getFileUploadPath();
List<Map<String, String>> maps = dataSourceDao.selectDataNameAndData(fileQo.getFileModelNumber(), fileQo.getFileBatchNumber(), fileQo.getFileMachineCode(), fileQo.getFileProductNumber());
if(maps == null || maps.size() == 0){
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时没有该产品的信息!",locale);
}
2022-02-28 21:32:34 +08:00
Map<String,String> map = new HashMap<>();
2022-02-12 08:48:57 +08:00
for(Map<String,String> stringMap : maps){
map.put(stringMap.get("dataname"),stringMap.get("numericalvalue"));
}
String fill = ExportExcel.exportToProveExcel(map,fileUploadPath);
fileDao.updateFileDownloadPath(fileQo.getFileModelNumber(), fileQo.getFileBatchNumber(), fileQo.getFileMachineCode(), fileQo.getFileProductNumber(),fill);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,fill,locale);
}
}