package com.gkhy.safePlatform.doublePrevention.service.impl; import com.gkhy.safePlatform.account.rpc.apimodel.AccountAuthService; import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService; import com.gkhy.safePlatform.account.rpc.apimodel.AccountUserService; import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO; import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.UserInfoRPCRespDTO; import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.UserRPCRespDTO; import com.gkhy.safePlatform.commons.co.ContextCacheUser; import com.gkhy.safePlatform.commons.enums.E; import com.gkhy.safePlatform.commons.exception.AusinessException; import com.gkhy.safePlatform.commons.utils.BeanCopyUtils; import com.gkhy.safePlatform.commons.utils.idService.SnowFlow; import com.gkhy.safePlatform.commons.vo.ResultVO; import com.gkhy.safePlatform.doublePrevention.entity.*; import com.gkhy.safePlatform.doublePrevention.entity.dto.PreventRiskControlTemplateDO; import com.gkhy.safePlatform.doublePrevention.enums.DeviceStatusEnum; import com.gkhy.safePlatform.doublePrevention.enums.StatusEnum; import com.gkhy.safePlatform.doublePrevention.enums.SyncEnum; import com.gkhy.safePlatform.doublePrevention.service.FileHandlerService; import com.gkhy.safePlatform.doublePrevention.service.baseService.*; import com.gkhy.safePlatform.doublePrevention.utils.ExcelProperty; import com.gkhy.safePlatform.doublePrevention.utils.FileOptUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.dubbo.config.annotation.DubboReference; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import java.util.regex.Pattern; import static com.gkhy.safePlatform.doublePrevention.utils.Properties.filePath; @Service public class FileHandlerServiceImpl implements FileHandlerService { @DubboReference(check = false) private AccountAuthService accountAuthService; @DubboReference(check = false) private AccountUserService accountUserService; @DubboReference(check = false) private AccountDepartmentService accountDepartmentService; @Autowired private PreventDangerCheckTaskUnitService preventDangerCheckTaskUnitService; @Autowired private PreventRiskControlMeasureService preventRiskControlMeasureService; @Autowired private PreventDangerCheckWorkService preventDangerCheckWorkService; @Autowired private PreventDangerCheckTaskService preventDangerCheckTaskService; @Autowired private PreventDangerCheckContentService preventDangerCheckContentService; @Autowired private PreventReportConfigService preventReportConfigService; @Autowired private PreventProduceDeviceService preventProduceDeviceService; @Autowired private PreventRiskMapService preventRiskMapService; @Autowired private PreventRiskAnaUnitService preventRiskUnitService; @Autowired private PreventRiskEventService preventRiskEventService; @Autowired private PreventRiskControlTemplateService preventRiskControlTemplateService; private final Logger logger = LoggerFactory.getLogger(this.getClass()); //正整数 String pattern = "^[0-9]*[1-9][0-9]*$"; Pattern pattern2 = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); String[] deviceTitle = {"生产装置", "风险等级", "所属部门", "区域位置"}; String[] riskUnitTitle = {"单元名称", "责任部门", "责任人", "安全风险分析对象编码", "生产装置"}; String[] eventTitle = {"事件名称", "单元名称", "可能造成的后果"}; String[] measureTitle = {"风险事件", "管控措施编号", "管控方式", "管控措施分类1", "管控措施分类2", "管控措施分类3", "管控措施描述", "隐患排查内容"}; /** * 生产装置-导入 */ @Transactional(propagation= Propagation.REQUIRED,timeout = 500) @Override public ResultVO deviceImport(ContextCacheUser currentUser, MultipartFile file) { ResultVO resultVO = new ResultVO<>(); resultVO.setCode("200"); resultVO.setMsg("生产设施设备导入成功"); if (file == null){ throw new AusinessException(E.DATA_PARAM_NULL, "找不到上传文件"); } if (file.getSize() == 0){ throw new AusinessException(E.DATA_PARAM_NULL, "文件内容为空"); } if (!FileOptUtils.isDirExists(filePath)) { throw new AusinessException(E.DATA_PARAM_NULL, "发生错误或不为目录"); } String fileSave = filePath + currentUser.getRealName() + "_" + ".xlsx"; int sum = 0; //获取list List deviceLists = new ArrayList<>(); try { file.transferTo(new File(fileSave)); String name = file.getOriginalFilename(); Boolean isExcel2007 = name.substring(name.lastIndexOf(".") + 1).endsWith("xlsx") ? true : false; InputStream in = new FileInputStream(fileSave); Workbook workbook = null; if (isExcel2007) { workbook = new XSSFWorkbook(in); } else { workbook = new HSSFWorkbook(in); } FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); Sheet sheet = workbook.getSheetAt(0); if (sheet == null) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "excel 第1页不能为空"); } Row row = sheet.getRow(0); // 标题总列数 int colNum = row.getPhysicalNumberOfCells(); String[] title = new String[colNum]; if (colNum != deviceTitle.length) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "上传文件的列的个数错误"); } for (int i = 0; i < colNum; i++) { title[i] = row.getCell(i).getStringCellValue(); if (StringUtils.isNotBlank(title[i])) { title[i] = title[i].trim(); } if (!title[i].equals((deviceTitle[i]))) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "上传文件的第" + (i + 1) + "列的标题为" + title[i] + "不为" + deviceTitle[i]); } } //遍历封装数据 for (int irow = 1; irow <= sheet.getLastRowNum(); irow++) { int realRow = irow + 1; PreventProduceDevice device = new PreventProduceDevice(); Object value = null; row = sheet.getRow(irow); //字段1-生产装置 value = ExcelProperty.getCellValue(row.getCell(0), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + (realRow + 1) + "行,生产装置不能为空.
"); } else { //无重名,设置属性 PreventProduceDevice deviceByName = preventProduceDeviceService.getDeviceByName(value.toString().trim()); if (ObjectUtils.isEmpty(deviceByName)){ device.setProduceDeviceName(value.toString().trim()); }else { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + (realRow + 1) + "行,生产装置名称已被使用,请在生产装置名称后添加编号或所属部门.
"); } } //字段2-风险等级 //String level1=""; value = ExcelProperty.getCellValue(row.getCell(1), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + (realRow + 1) + "行,风险等级不能为空.
"); } /*1-低风险,2-一般风险,3-较大风险,4-重大风险*/ if(value.toString().trim().equals("低风险")){ device.setRiskLevel((byte) 1); } else if(value.toString().trim().equals("一般风险")){ device.setRiskLevel((byte) 2); } else if(value.toString().trim().equals("较大风险")){ device.setRiskLevel((byte) 3); } else if(value.toString().trim().equals("重大风险")){ device.setRiskLevel((byte) 4); } else { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + (realRow + 1) + "行,风险等级不符合规范.
"); } //参数属性3-所属部门 value = ExcelProperty.getCellValue(row.getCell(2), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + (realRow + 1) + "行,所属部门不能为空.
"); } else { //校验部门是否存在 ResultVO depInfo = accountDepartmentService.getDepartmentInfoByDepName(value.toString().trim()); DepInfoRPCRespDTO dep = (DepInfoRPCRespDTO)depInfo.getData(); if (ObjectUtils.isEmpty(dep)){ throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + (realRow + 1) + "行,部门名称有误,或未录入系统.
"); }else { device.setDepName(value.toString().trim()); device.setDepId(dep.getDepId()); } } //参数属性4-区域位置 value = ExcelProperty.getCellValue(row.getCell(3), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + (realRow + 1) + "行的区域位置不能为空.
"); } else { device.setLocation(value.toString().trim()); } //获取填充信息 Date date = new Date(); SnowFlow snowFlow = new SnowFlow();//雪花算法生成器 String uuid = UUID.randomUUID().toString(); //封装参数 device.setId( snowFlow.nextId()); device.setUuid(uuid); device.setStatus(DeviceStatusEnum.DEVICE_USE.getCode()); device.setEnterpriseId((long) 1); device.setEnterpriseUuid("111"); device.setDepUuid(null); device.setCreateByUserName(currentUser.getRealName()); device.setGmtCreate(date); device.setLastEditUserName(currentUser.getRealName()); device.setGmtModitify(date); device.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode()); deviceLists.add(device); } } catch (IOException e) { throw new RuntimeException("读取上传文件发生错误{}"+e); } for (PreventProduceDevice device : deviceLists) { //插入 int result = preventProduceDeviceService.saveDevice(device); sum = sum + result; } resultVO.setCount(sum); return resultVO; } /** * 风险分析单元-导入 */ @Transactional(propagation= Propagation.REQUIRED,timeout = 500) @Override public ResultVO riskUnitImport(ContextCacheUser currentUser, MultipartFile file) { ResultVO resultVO = new ResultVO<>(); resultVO.setCode("200"); resultVO.setMsg(" 风险分析单元数据导入成功"); if (file == null){ throw new AusinessException(E.DATA_PARAM_NULL, "找不到上传文件"); } if (file.getSize() == 0){ throw new AusinessException(E.DATA_PARAM_NULL, "文件内容为空"); } if (!FileOptUtils.isDirExists(filePath)) { throw new AusinessException(E.DATA_PARAM_NULL, "发生错误或不为目录"); } String fileSave = filePath + currentUser.getRealName() + "_" + ".xlsx"; int sum = 0; //获取list List riskUnitLists = new ArrayList<>(); try { file.transferTo(new File(fileSave)); String name = file.getOriginalFilename(); Boolean isExcel2007 = name.substring(name.lastIndexOf(".") + 1).endsWith("xlsx") ? true : false; InputStream in = new FileInputStream(fileSave); Workbook workbook = null; if (isExcel2007) { workbook = new XSSFWorkbook(in); } else { workbook = new HSSFWorkbook(in); } FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); Sheet sheet = workbook.getSheetAt(0); if (sheet == null) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "excel 第1页不能为空"); } Row row = sheet.getRow(0); // 标题总列数 int colNum = row.getPhysicalNumberOfCells(); String[] title = new String[colNum]; if (colNum != riskUnitTitle.length) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "上传文件的列的个数错误"); } for (int i = 0; i < colNum; i++) { title[i] = row.getCell(i).getStringCellValue(); if (StringUtils.isNotBlank(title[i])) { title[i] = title[i].trim(); } if (!title[i].equals((riskUnitTitle[i]))) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "上传文件的第" + (i + 1) + "列的标题为:" + title[i] + ",不为:" + riskUnitTitle[i]); } } //遍历封装数据 for (int irow = 1; irow <= sheet.getLastRowNum(); irow++) { int realRow = irow + 1; PreventRiskAnaUnit riskAnaUnit = new PreventRiskAnaUnit(); Object value = null; Object value1 = null; row = sheet.getRow(irow); //字段1-安全风险分析单元 value = ExcelProperty.getCellValue(row.getCell(0), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,安全风险分析单元名称不能为空.
"); } else { PreventRiskAnaUnit riskUnitByName = preventRiskUnitService.getRiskUnitByName(value.toString().trim()); //无重名,设置属性 if (ObjectUtils.isEmpty(riskUnitByName)){ riskAnaUnit.setRiskUnitName(value.toString().trim()); }else { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,安全风险分析单元名称已被使用,请在安全风险分析单元名称后添加编号或所属部门.
"); } } //字段2-责任部门 value1 = ExcelProperty.getCellValue(row.getCell(1), evaluator); if (null == value1 || StringUtils.isBlank(value1.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,责任部门不能为空.
"); } else { //校验部门是否存在 ResultVO depInfo = accountDepartmentService.getDepartmentInfoByDepName(value1.toString().trim()); DepInfoRPCRespDTO dep = (DepInfoRPCRespDTO)depInfo.getData(); if (ObjectUtils.isEmpty(dep)){ throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,责任部门名称有误,或未录入系统.
"); }else { riskAnaUnit.setLiableDep(value1.toString().trim()); riskAnaUnit.setLiableDepId(dep.getDepId()); riskAnaUnit.setLiableDepUuid(null); } } //字段3-责任人 value = ExcelProperty.getCellValue(row.getCell(2), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行的责任人不能为空.
"); } else { //校验部门是否存在 ResultVO depInfo = accountDepartmentService.getDepartmentInfoByDepName(value1.toString().trim()); DepInfoRPCRespDTO dep = (DepInfoRPCRespDTO)depInfo.getData(); //获取责任人信息 UserInfoRPCRespDTO liablePerson = null; ResultVO> listResultVO = accountUserService.listUserInfoByRealName(value.toString().trim()); List personList = (List)listResultVO.getData(); for (UserInfoRPCRespDTO userInfo : personList) { if (userInfo.getDepId().equals(dep.getDepId())){ liablePerson = BeanCopyUtils.copyBean(userInfo, UserInfoRPCRespDTO.class); } } if (ObjectUtils.isEmpty(liablePerson)){ throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行的责任人名称有误,或未录入系统.
"); } else { riskAnaUnit.setLiablePerson(liablePerson.getRealName()); //获取责任人信息 riskAnaUnit.setLiablePersonId(liablePerson.getUid()); riskAnaUnit.setLiablePersonUuid(liablePerson.getUuid()); } } //参数属性4-安全风险分析对象编码 value = ExcelProperty.getCellValue(row.getCell(3), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,安全风险分析对象编码不能为空.
"); } else { //查询是否有重复编码 PreventRiskAnaUnit riskUnitByCode = preventRiskUnitService.getRiskUnitByCode(value.toString().trim()); if (ObjectUtils.isEmpty(riskUnitByCode)){ riskAnaUnit.setRiskCode(value.toString().trim()); }else { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,安全风险分析对象编码已经被使用.
"); } } //参数属性5-所属生产装置 value = ExcelProperty.getCellValue(row.getCell(4), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,生产装置不能为空.
"); } else { //todo 此处,风险分析单元与设备是否需要同部门 PreventProduceDevice deviceByName = preventProduceDeviceService.getDeviceByName(value.toString().trim()); if (ObjectUtils.isEmpty(deviceByName)){ throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,生产装置不存在或未录入系统.
"); }else { riskAnaUnit.setProduceDeviceId(deviceByName.getId()); riskAnaUnit.setProduceDeviceUuid(deviceByName.getUuid()); } } //获取填充信息 Date date = new Date(); SnowFlow snowFlow = new SnowFlow();//雪花算法生成器 String uuid = UUID.randomUUID().toString(); //封装参数 riskAnaUnit.setId(snowFlow.nextId()); riskAnaUnit.setUuid(uuid); riskAnaUnit.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode()); riskAnaUnit.setEnterpriseId((long) 1); riskAnaUnit.setEnterpriseUuid("111"); riskAnaUnit.setGmtModitify(date); riskAnaUnit.setGmtCreate(date); riskAnaUnit.setCreateByUserName(currentUser.getRealName()); riskAnaUnit.setLastEditUserName(currentUser.getRealName()); //读取上报主配置,设置上报属性 PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG_RISK_ANA_UNIT.getCode()); //设置上报时间为空 riskAnaUnit.setReportTime(null); //设置本条数据上报更新时间 riskAnaUnit.setUpdateReportDataTime(date); //读取上报主配置,进行任务记录上报配置,如果开启上报功能,且设置为自动上报,开启上报相关配置 if (reportConfigById.getReportState() == SyncEnum.REPORT_ON.getCode() && reportConfigById.getReportType() == SyncEnum.REPORT_HAND_EXEC_CONFIG.getCode()){ //设置上报状态为-等待上报 riskAnaUnit.setReportStatus(SyncEnum.SYNC_WAIT_EXEC.getCode()); //设置本条数据上报开关为-开启 riskAnaUnit.setReportSwitch(SyncEnum.REPORT_ON.getCode()); //其他情况默认不开启上报数据,如果是手动上报,可对单条数据进行操作 }else { //设置上报状态为-不上报 riskAnaUnit.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode()); //设置本条数据上报开关为-关闭 riskAnaUnit.setReportSwitch(SyncEnum.REPORT_OFF.getCode()); } //封装数据 riskUnitLists.add(riskAnaUnit); } } catch (IOException e) { throw new RuntimeException("读取上传文件发生错误{}"+e); } //插入 for (PreventRiskAnaUnit riskUnit : riskUnitLists) { int result = preventRiskUnitService.saveRiskAnaUnit(riskUnit); sum = sum + result; } resultVO.setCount(sum); return resultVO; } /** * 安全风险事件-导入 */ @Transactional(propagation= Propagation.REQUIRED,timeout = 500) @Override public ResultVO eventImport(ContextCacheUser currentUser, MultipartFile file) { ResultVO resultVO = new ResultVO<>(); resultVO.setCode("200"); resultVO.setMsg(" 安全风险事件数据导入成功"); if (file == null){ throw new AusinessException(E.DATA_PARAM_NULL, "找不到上传文件"); } if (file.getSize() == 0){ throw new AusinessException(E.DATA_PARAM_NULL, "文件内容为空"); } if (!FileOptUtils.isDirExists(filePath)) { throw new AusinessException(E.DATA_PARAM_NULL, "发生错误或不为目录"); } String fileSave = filePath + currentUser.getRealName() + "_" + ".xlsx"; int sum = 0; //获取list List eventList = new ArrayList<>(); try { file.transferTo(new File(fileSave)); String name = file.getOriginalFilename(); Boolean isExcel2007 = name.substring(name.lastIndexOf(".") + 1).endsWith("xlsx") ? true : false; InputStream in = new FileInputStream(fileSave); Workbook workbook = null; if (isExcel2007) { workbook = new XSSFWorkbook(in); } else { workbook = new HSSFWorkbook(in); } FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); Sheet sheet = workbook.getSheetAt(0); if (sheet == null) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "excel 第1页不能为空"); } Row row = sheet.getRow(0); // 标题总列数 int colNum = row.getPhysicalNumberOfCells(); String[] title = new String[colNum]; if (colNum != eventTitle.length) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "上传文件的列的个数错误"); } for (int i = 0; i < colNum; i++) { title[i] = row.getCell(i).getStringCellValue(); if (StringUtils.isNotBlank(title[i])) { title[i] = title[i].trim(); } if (!title[i].equals((eventTitle[i]))) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "上传文件的第" + (i + 1) + "列的标题为" + title[i] + "不为" + eventTitle[i]); } } //遍历封装数据 for (int irow = 1; irow <= sheet.getLastRowNum(); irow++) { int realRow = irow + 1; PreventRiskEvent event = new PreventRiskEvent(); Object value = null; row = sheet.getRow(irow); //字段1-风险事件名称 value = ExcelProperty.getCellValue(row.getCell(0), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,风险事件名称不能为空.
"); } else { //通过部门名称查部门 PreventRiskEvent riskEventByName = preventRiskEventService.getRiskEventByName(value.toString().trim()); if (ObjectUtils.isEmpty(riskEventByName)){ event.setRiskEventName(value.toString().trim()); }else { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,风险事件名称已经被使用,请为新事件添加编号、部门、设施.
"); } } //字段2-安全风险分析单元 value = ExcelProperty.getCellValue(row.getCell(1), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,安全风险分析单元名称不能为空.
"); } else { PreventRiskAnaUnit riskUnitByName = preventRiskUnitService.getRiskUnitByName(value.toString().trim()); if (ObjectUtils.isEmpty(riskUnitByName)){ throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,安全风险分析单元不存在或未录入系统.
"); }else { event.setRiskUnitId(riskUnitByName.getId()); event.setRiskUnitUuid(riskUnitByName.getUuid()); } } //字段3-可能造成的后果 value = ExcelProperty.getCellValue(row.getCell(2), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { event.setEventResult(null); } else { event.setEventResult(value.toString().trim()); } //获取填充信息 Date date = new Date(); SnowFlow snowFlow = new SnowFlow();//雪花算法生成器 String uuid = UUID.randomUUID().toString(); //封装参数 event.setId(snowFlow.nextId()); event.setUuid(uuid); event.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode()); event.setEnterpriseId((long) 1); event.setEnterpriseUuid("111"); event.setGmtModitify(date); event.setGmtCreate(date); event.setCreateByUserName(currentUser.getRealName()); event.setLastEditUserName(currentUser.getRealName()); //读取上报主配置,设置属性 PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG_RISK_EVENT.getCode()); //设置上报时间为空 event.setReportTime(null); //设置本条数据上报更新时间 event.setUpdateReportDataTime(date); //读取上报主配置,进行任务记录上报配置,如果开启上报功能,且设置为自动上报,开启上报相关配置 if (reportConfigById.getReportState() == SyncEnum.REPORT_ON.getCode() && reportConfigById.getReportType() == SyncEnum.REPORT_HAND_EXEC_CONFIG.getCode()){ //设置上报状态为-等待上报 event.setReportStatus(SyncEnum.SYNC_WAIT_EXEC.getCode()); //设置本条数据上报开关为-开启 event.setReportSwitch(SyncEnum.REPORT_ON.getCode()); //其他情况默认不开启上报数据,如果是手动上报,可对单条数据进行操作 }else { //设置上报状态为-不上报 event.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode()); //设置本条数据上报开关为-关闭 event.setReportSwitch(SyncEnum.REPORT_OFF.getCode()); } eventList.add(event); } } catch (IOException e) { throw new RuntimeException("读取上传文件发生错误{}"+e); } //插入 for (PreventRiskEvent event : eventList) { int result = preventRiskEventService.saveRiskEvent(event); sum = sum + result; } resultVO.setCount(sum); return resultVO; } /** * 管控措施-导入 */ @Transactional(propagation= Propagation.REQUIRED,timeout = 500) @Override public ResultVO measureImport(ContextCacheUser currentUser, MultipartFile file) { ResultVO resultVO = new ResultVO<>(); resultVO.setCode("200"); resultVO.setMsg(" 安全风险管控措施导入成功"); if (file == null){ throw new AusinessException(E.DATA_PARAM_NULL, "找不到上传文件"); } if (file.getSize() == 0){ throw new AusinessException(E.DATA_PARAM_NULL, "文件内容为空"); } if (!FileOptUtils.isDirExists(filePath)) { throw new AusinessException(E.DATA_PARAM_NULL, "发生错误或不为目录"); } String fileSave = filePath + currentUser.getRealName() + "_" + ".xlsx"; int sum = 0; //获取list List measureList = new ArrayList<>(); List checkContentList = new ArrayList<>(); try { file.transferTo(new File(fileSave)); String name = file.getOriginalFilename(); Boolean isExcel2007 = name.substring(name.lastIndexOf(".") + 1).endsWith("xlsx") ? true : false; InputStream in = new FileInputStream(fileSave); Workbook workbook = null; if (isExcel2007) { workbook = new XSSFWorkbook(in); } else { workbook = new HSSFWorkbook(in); } FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); Sheet sheet = workbook.getSheetAt(0); if (sheet == null) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "excel 第1页不能为空"); } Row row = sheet.getRow(0); // 标题总列数 int colNum = row.getPhysicalNumberOfCells(); String[] title = new String[colNum]; if (colNum != measureTitle.length) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "上传文件的列的个数错误"); } for (int i = 0; i < colNum; i++) { title[i] = row.getCell(i).getStringCellValue(); if (StringUtils.isNotBlank(title[i])) { title[i] = title[i].trim(); } if (!title[i].equals((measureTitle[i]))) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "上传文件的第" + (i+1) + "列的标题为" + title[i] + "不为" + measureTitle[i]); } } //遍历封装数据 for (int irow = 1; irow <= sheet.getLastRowNum(); irow++) { int realRow = irow + 1; PreventRiskControlMeasure measure = new PreventRiskControlMeasure(); PreventDangerCheckContent checkContent = new PreventDangerCheckContent(); Object value = null; Object value1 = null; row = sheet.getRow(irow); //字段1-风险事件名称 封装eventId 与 eventUuid value = ExcelProperty.getCellValue(row.getCell(0), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,风险事件名称不能为空.
"); } else { //通过名称查事件 PreventRiskEvent riskEventByName = preventRiskEventService.getRiskEventByName(value.toString().trim()); if (ObjectUtils.isEmpty(riskEventByName)){ throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,风险事件不存在或未录入系统.
"); }else { measure.setRiskEventId(riskEventByName.getId()); measure.setRiskEventUuid(riskEventByName.getUuid()); } } //字段2-管控措施编号 value = ExcelProperty.getCellValue(row.getCell(1), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,管控措施编号不能为空.
"); } else { PreventRiskControlMeasure controlMeasureByCode = preventRiskControlMeasureService.getControlMeasureByCode(value.toString().trim()); if (ObjectUtils.isEmpty(controlMeasureByCode)){ measure.setControlMeasureCode(value.toString().trim()); }else { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,管控措施编号已经被使用.
"); } } //字段3-管控方式 value = ExcelProperty.getCellValue(row.getCell(2), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,管控方式不能为空.
"); } else { if (value.toString().trim().equals("自动化监控")){ measure.setControlType((byte) 1); }else if (value.toString().trim().equals("隐患排查")){ measure.setControlType((byte) 2); }else { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,管控方式不符合要求.
"); } } //字段4、5-管控措施分类1、管控措施分类2 value = ExcelProperty.getCellValue(row.getCell(3), evaluator); value1 = ExcelProperty.getCellValue(row.getCell(4), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,管控措施分类1不能为空.
"); } else if (null == value1 || StringUtils.isBlank(value.toString())){ throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,管控措施分类2不能为空.
"); }else { //todo 需要检查此方法返回值是否正确 PreventRiskControlTemplateDO classify1AndClassify2 = preventRiskControlTemplateService.getByClassify1AndClassify2(value.toString().trim(), value1.toString().trim()); if (ObjectUtils.isEmpty(classify1AndClassify2)){ throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,管控措施分类1与管控措施分类2不符合规范或者不是父子层级.
"); }else { measure.setClassify1(classify1AndClassify2.getClassify1Id()); measure.setClassify2(classify1AndClassify2.getClassify2Id()); } } //字段6-管控措施分类3 value = ExcelProperty.getCellValue(row.getCell(5), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { measure.setClassify3(null); } else { measure.setClassify3(value.toString().trim()); } //字段7-管控措施描述 value = ExcelProperty.getCellValue(row.getCell(6), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { measure.setMeasureDesc(null); } else { measure.setMeasureDesc(value.toString().trim()); } //字段7-隐患排查内容 value = ExcelProperty.getCellValue(row.getCell(7), evaluator); if (null == value || StringUtils.isBlank(value.toString())) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "文件不符合要求,第" + realRow + "行,隐患排查内容不能为空.
"); } else { checkContent.setCheckContent(value.toString().trim()); } //获取填充信息 Date date = new Date(); SnowFlow snowFlow = new SnowFlow();//雪花算法生成器 String uuid = UUID.randomUUID().toString(); long measureId = snowFlow.nextId(); //封装管控措施参数 measure.setId(measureId); measure.setUuid(uuid); measure.setEnterpriseId((long) 1); measure.setEnterpriseUuid("123"); measure.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode()); measure.setGmtModitify(date); measure.setGmtCreate(date); measure.setCreateByUserName(currentUser.getRealName()); measure.setLastEditUserName(currentUser.getRealName()); measure.setCheckTaskUnitId(null); measure.setCheckTaskUnitUuid(null); measure.setCheckWorkId(null); measure.setCheckWorkUuid(null); measure.setCheckTaskId(null); measure.setCheckTaskUuid(null); //封装隐患检查内容的参数 checkContent.setControlMeasureId(measureId); checkContent.setControlMeasureUuid(uuid); PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG_RISK_MEASURE.getCode()); //设置上报时间为空 measure.setReportTime(null); //设置本条数据上报更新时间 measure.setUpdateReportDataTime(date); //读取上报主配置,进行任务记录上报配置,如果开启上报功能,且设置为自动上报,开启上报相关配置 if (reportConfigById.getReportState() == SyncEnum.REPORT_ON.getCode() && reportConfigById.getReportType() == SyncEnum.REPORT_HAND_EXEC_CONFIG.getCode()){ //设置上报状态为-等待上报 measure.setReportStatus(SyncEnum.SYNC_WAIT_EXEC.getCode()); //设置本条数据上报开关为-开启 measure.setReportSwitch(SyncEnum.REPORT_ON.getCode()); //其他情况默认不开启上报数据,如果是手动上报,可对单条数据进行操作 }else { //设置上报状态为-不上报 measure.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode()); //设置本条数据上报开关为-关闭 measure.setReportSwitch(SyncEnum.REPORT_OFF.getCode()); } measureList.add(measure); checkContentList.add(checkContent); } } catch (IOException e) { throw new RuntimeException("读取上传文件发生错误{}"+e); } //插入 for (PreventRiskControlMeasure measure : measureList) { int result = preventRiskControlMeasureService.saveRiskControlMeasure(measure); sum = sum + result; } //保存排查内容 for (PreventDangerCheckContent checkContent : checkContentList) { int result = preventDangerCheckContentService.saveDangerCheckContent(checkContent); } resultVO.setCount(sum); return resultVO; } }