/**
* 导出 excel表格
*
* @param spacecraftId
*/
@Override
public CheckResult exportParamExcel(Long spacecraftId, HttpServletResponse response) {
Map<String, Object> parameterMap = new HashMap<>(5);
parameterMap.put("spacecraftId", spacecraftId);
parameterMap.put("groupIds", -1);
parameterMap.put("paramType", -1);
parameterMap.put("values", -1);
List<SpacecraftTelemetryParam> spacecraftTelemetryParamList = selectByGroupId(parameterMap);
if (spacecraftTelemetryParamList.isEmpty()) {
log.info(MsgPrefixKit.genMgt("没查到该飞行器下的参数信息"));
return CheckResult.fail("没有对应的文件,导出失败");
}
List<SpacecraftTelemetryParamExcel> list = new ArrayList<>(spacecraftTelemetryParamList.size());
//导出 表格数据拼写
for (int i = 0; i < spacecraftTelemetryParamList.size(); i++) {
SpacecraftTelemetryParamExcel spacecraftTelemetryParamExcel = new SpacecraftTelemetryParamExcel();
BeanUtil.copyProperties(spacecraftTelemetryParamList.get(i), spacecraftTelemetryParamExcel);
SpacecraftTelemetryFrame spacecraftTelemetryFrame = telemetryFrameService.selectBySpacecraftId(spacecraftTelemetryParamList.get(i).getSpacecraftId());
spacecraftTelemetryParamExcel.setTelemetryDataName(spacecraftTelemetryFrame.getTelemetryFormat());
list.add(spacecraftTelemetryParamExcel);
}
ExportParams exportParams = new ExportParams();
exportParams.setSheetName("遥测参数");
// 生成workbook 并导出
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, SpacecraftTelemetryParamExcel.class, list);
// 组装excel文件name
Spacecraft spacecraft = spacecraftService.selectById(spacecraftId);
String dateName = DateUtil.format(new Date(), "yyyyMMddHHmmss");
String fileName = spacecraft.getSpacecraftName() + "遥测参数" + dateName;
// 下载excel
outPut(response, fileName, workbook);
//TODO: 目录桌面 后期修改
// File savefile = new File("C:\\Users\\Administrator\\Desktop\\Ceshi.xlsx");
// if (!savefile.exists()) {
// boolean result = savefile.mkdirs();
// System.out.println("目录不存在,创建" + result);
// }
// FileOutputStream fos = null;
// try {
// fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\" + spacecraftId + ".xlsx");
// workbook.write(fos);
// fos.close();
// } catch (Exception e) {
// e.printStackTrace();
// return CheckResult.fail("导出异常失败");
// }
return CheckResult.success("导出成功");
}
/**
* 导入 excel表格
*
* @param spacecraftId
*/
@Transactional(rollbackFor = Exception.class)
@Override
public CheckResult input(MultipartFile file, Long spacecraftId) {
ImportParams importParams = new ImportParams();
// 因为Excel是从第2行开始才有正式数据,所以我们这里从第1行开始读取数据
importParams.setHeadRows(1);
// 需要校验
importParams.setNeedVerify(true);
// 获取模板
ExcelImportResult<SpacecraftTelemetryParamExcel> data = new ExcelImportResult<>();
try {
data = ExcelImportUtil.importExcelMore(file.getInputStream(), SpacecraftTelemetryParamExcel.class, importParams);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
// 获取模板数据(通过校验的数据)
List<SpacecraftTelemetryParamExcel> successList = data.getList();
if (successList.size() == 0) {
// 如果数据量为0直接返回错误信息,不继续执行
log.info(MsgPrefixKit.genMgt("错误信息,不继续执行"));
return CheckResult.fail("错误信息,不继续执行");
}
// 校验失败的数据集合
List<SpacecraftTelemetryParamExcel> failList = data.getFailList();
if (failList.size() > 0) {
// 如果此数据量大于0,直接返回错误信息
log.info(MsgPrefixKit.genMgt("有" + data.getFailList().size() + "条数据不合格,无法导入"));
}
// 循环数据
List<SpacecraftTelemetryParamExcel> spacecraftTelemetryParamExcels = new ArrayList<>(successList);
List<SpacecraftTelemetryParam> spacecraftTelemetryParamList = new ArrayList<>();
for (SpacecraftTelemetryParamExcel spacecraftTelemetryParamExcel : spacecraftTelemetryParamExcels) {
SpacecraftTelemetryParam spacecraftTelemetryParam = new SpacecraftTelemetryParam();
BeanUtil.copyProperties(spacecraftTelemetryParamExcel, spacecraftTelemetryParam);
SpacecraftTelemetryFrame spacecraftTelemetryFrame = telemetryFrameService.selectBySpacecraftId(spacecraftId);
spacecraftTelemetryParam.setTelemetryDataId(spacecraftTelemetryFrame.getTelemetryDataId());
spacecraftTelemetryParam.setSpacecraftId(spacecraftId);
spacecraftTelemetryParamList.add(spacecraftTelemetryParam);
}
Spacecraft spacecraft = spacecraftService.selectById(spacecraftId);
if (spacecraft == null) {
return CheckResult.fail("传入的飞行器id不正确");
}
//TODO: 2 没有修改和删除 查询参数识别码看是否有此数据 ,有修改 没有添加
for (SpacecraftTelemetryParam spacecraftTelemetryParam : spacecraftTelemetryParamList) {
SpacecraftTelemetryParam telemetryParam = selectIdentificationWord(spacecraftTelemetryParam.getParamIdentificationWord(), spacecraftId);
if (telemetryParam == null) {
insert(spacecraftTelemetryParam);
// } else {
// spacecraftTelemetryParam.setSpacecraftParamId(telemetryParam.getSpacecraftParamId());
// updateByPrimaryKey(spacecraftTelemetryParam);
}
}
//TODO: 1 没有修改和删除 查询主键看是否有此数据 , 没有添加
// for (SpacecraftTelemetryParam spacecraftTelemetryParam : spacecraftTelemetryParamList) {
//
// SpacecraftTelemetryParam telemetryParam = selectById(spacecraftTelemetryParam.getSpacecraftParamId());
// if (telemetryParam == null) {
// insert(spacecraftTelemetryParam);
// }
// }
return CheckResult.success(spacecraftTelemetryParamList);
}
EsayPoi导入导出表格
最新推荐文章于 2024-04-26 11:18:43 发布