package com.persagy.proxy.object.controller; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.collect.Sets; import com.persagy.dmp.basic.dto.RequestData; import com.persagy.dmp.basic.model.QueryCriteria; import com.persagy.dmp.basic.utils.JsonNodeUtils; import com.persagy.dmp.common.context.AppContext; import com.persagy.dmp.common.exception.BusinessException; import com.persagy.dmp.common.helper.SpringHelper; import com.persagy.dmp.common.model.entity.BaseEntity; import com.persagy.dmp.common.model.response.CommonResult; import com.persagy.dmp.define.client.DigitalDefineFacade; import com.persagy.dmp.define.entity.ObjectInfoDefine; import com.persagy.dmp.define.entity.ObjectTypeDefine; import com.persagy.dmp.digital.client.DigitalObjectFacade; import com.persagy.dmp.digital.entity.ObjectDigital; import com.persagy.dmp.simple.client.DigitalObjectSimpleFacade; import com.persagy.proxy.adm.constant.AdmCommonConstant; import com.persagy.proxy.adm.constant.AdmDictCategoryEnum; import com.persagy.proxy.adm.constant.AdmRelationTypeEnum; import com.persagy.proxy.adm.request.AdmCreateRequest; import com.persagy.proxy.adm.request.AdmQueryCriteria; import com.persagy.proxy.adm.request.AdmResponse; import com.persagy.proxy.adm.service.IAdmRelationService; import com.persagy.proxy.adm.utils.AdmContextUtil; import com.persagy.proxy.adm.utils.AdmExcelUtil; import com.persagy.proxy.adm.utils.ObjectNameUtil; import com.persagy.proxy.common.entity.RelationDTO; import com.persagy.proxy.dictionary.model.AdmDict; import com.persagy.proxy.dictionary.model.AdmObjectType; import com.persagy.proxy.dictionary.service.IAdmDictService; import com.persagy.proxy.object.model.AdmEquipment; import com.persagy.proxy.object.service.IAdmEquipmentService; import com.persagy.proxy.report.service.IRelationReportService; import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.xssf.usermodel.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.*; /** * Adm 楼层 API * @author lvxy * @date 2021/8/16 */ @Slf4j @RestController @RequestMapping("/object/equip") public class AdmEquipmentController { @Autowired private IAdmEquipmentService service; @Autowired private IAdmRelationService relationService; @Autowired private IRelationReportService relationReportService; @Autowired private IAdmDictService dictService; /** * 统计 * * @param request * @return * @throws Exception */ @PostMapping("/count") public AdmResponse count(@RequestBody AdmQueryCriteria request) throws Exception { request.setName(AdmEquipment.OBJ_TYPE); request.setOnlyCount(true); return service.query(request); } /** * 创建 * @param createRequest * @return * @throws Exception */ @PostMapping("/create") public AdmResponse create(@RequestBody AdmCreateRequest createRequest) throws Exception { List vos = CollUtil.newArrayList(createRequest.getContent()); if(CollUtil.isEmpty(vos)){ return AdmResponse.success(); } // 查询所有设备类型 AdmDict dictCond = AdmDict.builder().type(AdmDictCategoryEnum.EQUIPMENT.getValue()).build(); AdmResponse dictResponse = dictService.queryCategory(AdmContextUtil.toDmpContext(), dictCond, AdmObjectType.class); List dicts = (List) dictResponse.getContent(); // 转换为Map,key为编码后4位。用于适配旧类型编码 Map shortDictMap = new HashMap<>(); Map fullDictMap = new HashMap<>(); for(int i = 0,j = CollUtil.size(dicts);i < j;i++) { AdmObjectType dict = dicts.get(i); shortDictMap.put(StrUtil.subSufByLength(dict.getCode(), 4), dict); fullDictMap.put(dict.getCode(), dict); } // 设备属性调整 vos.stream().forEach(equipment -> { // 如果不是Eq开头,则重新生成ID if(StrUtil.startWith(equipment.getId(), "Eq")) { equipment.setId("Eq"+ IdUtil.simpleUUID()); } // 类型编码适配 String classCode = equipment.getClassCode(); // 如果编码为空,或在4位、6位里都不存在,则报错 if(StrUtil.isBlank(classCode) || (shortDictMap.get(classCode) == null && fullDictMap.get(classCode) == null)) { throw new BusinessException(StrUtil.format("没有找到对象类型【{}】!", equipment.getClassCode())); } // 如果少于6位,则需要适配新编码 if(classCode.length() < 6) { AdmObjectType dict = MapUtil.get(shortDictMap, classCode, AdmObjectType.class); equipment.setClassCode(dict.getCode()); } // 如果名称为空,则提供默认名称 if (StrUtil.isEmpty(equipment.getName())){ AdmObjectType dict = MapUtil.get(fullDictMap, equipment.getClassCode(), AdmObjectType.class); String preName = dict == null ? "设备" : dict.getName(); equipment.setName(ObjectNameUtil.objectName(preName + "-")); } }); vos = service.doInsert(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos); return AdmResponse.success(vos); } /** * 根据ID删除 * @param vos * @return * @throws Exception */ @PostMapping("/delete") public AdmResponse delete( @RequestBody List vos) throws Exception { // 组装上下文条件 service.doDelete(AdmContextUtil.toDmpContext(), vos); return AdmResponse.success(); } /** * 更新 * @param createRequest * @return * @throws Exception */ @PostMapping("/update") public AdmResponse update(@RequestBody AdmCreateRequest createRequest) throws Exception { List vos = CollUtil.newArrayList(createRequest.getContent()); //vos = service.doUpdateEuip(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos); //针对设备更新时,所有更新的字段都放到信息点中的情况特殊处理 if(CollUtil.isNotEmpty(vos)){ Map admEquipmentMap = CollUtil.fieldValueMap(vos, BaseEntity.PROP_ID); AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2BD; String graphCode = typeEnum.getGraphCode(); QueryCriteria criteria = new QueryCriteria(); ObjectNode node = JsonNodeFactory.instance.objectNode(); node.put("graphCode", graphCode); admEquipmentMap.keySet().stream().forEach(equipId -> { node.put("relCode", "Eq2Bd"); node.put("objFrom", equipId); criteria.setCriteria(node); relationService.doDelete(AdmContextUtil.toDmpContext(), criteria); node.put("relCode", "Eq2Fl"); node.put("objFrom", equipId); criteria.setCriteria(node); relationService.doDelete(AdmContextUtil.toDmpContext(), criteria); }); List relationDTOList = new ArrayList<>(vos.size()); admEquipmentMap.keySet().stream().forEach(equipId -> { if(StrUtil.isNotEmpty(admEquipmentMap.get(equipId).getBuildingId())){ RelationDTO relationbd = new RelationDTO(null, graphCode, "Eq2Bd", null, equipId, admEquipmentMap.get(equipId).getBuildingId()); relationDTOList.add(relationbd); } if(StrUtil.isNotEmpty(admEquipmentMap.get(equipId).getFloorId())) { RelationDTO relationfl = new RelationDTO(null, graphCode, "Eq2Fl", null, equipId, admEquipmentMap.get(equipId).getFloorId()); relationDTOList.add(relationfl); } }); if(CollUtil.isNotEmpty(relationDTOList)){ relationService.doSave(AdmContextUtil.toDmpContext(), relationDTOList); } //处理设备更新特殊数据 List equipmentList = new ArrayList<>(vos.size()); vos.stream().forEach(equipment -> { if(CollUtil.isNotEmpty(equipment.getInfos())){ AdmEquipment admEquipment = new AdmEquipment(); admEquipment.setId(equipment.getId()); admEquipment.setInfos(equipment.getInfos()); ObjectNode objectNode = JsonNodeUtils.toObjectNode(admEquipment, "infos", null); AdmEquipment admEquipmentNew = JsonNodeUtils.toEntity(objectNode, AdmEquipment.class, "infos"); if(CollUtil.isNotEmpty(equipment.getNullList())){ admEquipmentNew.setNullList(equipment.getNullList()); } if(StrUtil.isNotEmpty(equipment.getFloorId())){ admEquipmentNew.setFloorId(equipment.getFloorId()); } if(StrUtil.isNotEmpty(equipment.getBuildingId())){ admEquipmentNew.setBuildingId(equipment.getBuildingId()); } equipmentList.add(admEquipmentNew); } }); vos = equipmentList; } vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos); return AdmResponse.success(vos); } /** * 查询 * * @param request * @return * @throws Exception */ @PostMapping("/query") public AdmResponse query(@RequestBody AdmQueryCriteria request) throws Exception { // 组装上下文条件 request.setName(AdmEquipment.OBJ_TYPE); if(CollUtil.isEmpty(request.getCascade()) && CollUtil.isNotEmpty(request.getProjection()) && request.getDistinct() && request.getProjection().size() == 1 && request.getProjection().get(0).equals("classCode")) { /** 特殊处理 * distinct: true * pageNumber: 1 * pageSize: 1000 * projection: ["classCode"] * 0: "classCode" */ RequestData requestData = new RequestData(); requestData.setProjection(request.getProjection()); requestData.setObjType(AdmEquipment.OBJ_TYPE); requestData.setPage(request.getPageNumber().longValue()); requestData.setSize(request.getPageSize().longValue()); CommonResult> result = DigitalObjectSimpleFacade.queryObjListByConditions(AdmContextUtil.toDmpContext().getGroupCode(), AdmContextUtil.toDmpContext().getProjectId(), AdmContextUtil.toDmpContext().getAppId(), null, requestData); if (result == null || CollUtil.isEmpty(result.getData())) { return AdmResponse.success(); }else{ List admEquipments = JSONUtil.toList(result.getData().toString(), AdmEquipment.class); service.ensureSpecialValues(AdmContextUtil.toDmpContext(), admEquipments); return AdmResponse.success(admEquipments); } }else{ return service.query(request); } } /** * 设备绑定的动态信息点或静态信息点 * @param admEquipment * @return * @throws Exception */ @GetMapping("/export") public AdmResponse exportInfos(@RequestBody AdmCreateRequest admEquipment) throws Exception { List vos = CollUtil.newArrayList(admEquipment.getContent()); vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos); return AdmResponse.success(vos); } /** * 根据项目id查询所有的设备和部件 * * @param request * @return * @throws Exception */ @GetMapping("/query-equip") public AdmResponse queryEquip(@RequestBody AdmQueryCriteria request) throws Exception { //request.setName("equipment"); if(StrUtil.isNotEmpty(request.getFilters())){ request.setFilters(String.join(";",request.getFilters(),"objType in ['equipment','component']")); }else{ request.setFilters("objType in ['equipment','component']"); } return service.query(request); } /** * 批量创建设备同时创建资产、创建设备和系统的关系 * @param admEquipment * @return * @throws Exception */ @PostMapping("/creat-equip-pro") public AdmResponse createExt(@RequestBody AdmCreateRequest admEquipment) throws Exception { List vos = admEquipment.getContent(); if(CollUtil.isEmpty(vos)){ return AdmResponse.success(); } //创建设备 vos.stream().forEach(equipment -> { if (StrUtil.isEmpty(equipment.getName())){ equipment.setName(ObjectNameUtil.objectName("设备-")); } }); List reVos = new ArrayList<>(vos.size()); List relationDTOS = new ArrayList<>(16); vos.stream().forEach(admEq -> { //创建设备 List vosR = service.doInsert(AdmContextUtil.toDmpContext(), AdmEquipment.class, CollUtil.newArrayList(admEq)); //TODO 创建资产 //添加设备和系统、建筑、楼层的关系 if(CollUtil.isNotEmpty(vosR)){ String equipId = vosR.get(0).getId(); if(StrUtil.isNotEmpty(equipId)){ if(CollUtil.isNotEmpty(admEq.getLinkSystem())){ admEq.getLinkSystem().stream().forEach(admSystem -> { relationDTOS.add(new RelationDTO(null, "MechSubset", "Sy2Eq", null , admSystem.getId(), equipId)); }); } if(StrUtil.isNotEmpty(admEq.getBuildingId())){ relationDTOS.add(new RelationDTO(null, "MechInArch", "Eq2Bd", null , equipId, admEq.getBuildingId())); } if(StrUtil.isNotEmpty(admEq.getFloorId())){ relationDTOS.add(new RelationDTO(null, "MechInArch", "Eq2Fl", null , equipId, admEq.getFloorId())); } } } reVos.addAll(vosR); }); relationService.doSave(AdmContextUtil.toDmpContext(),relationDTOS); return AdmResponse.success(reVos); } /** * 设备上传excel识别静态信息点 * * @param file * @param request * @param response */ @RequestMapping("/import") public AdmResponse importFile(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) { if (file == null) { return AdmResponse.failure("未选择文件"); } String path = AdmCommonConstant.SERVER_ROOT_PATH + AdmExcelUtil.TEMP_UPLOAD_PAHT; int failurNum = 0; int successCount = 0; String failure = null; String primaryKey = null; String classCodeCloumn = null; XSSFWorkbook workbook = null; FileOutputStream fileOutputStream = null; String groupCode = AppContext.getContext().getGroupCode(); String projectId = AppContext.getContext().getProjectId(); try { workbook = new XSSFWorkbook(file.getInputStream()); XSSFSheet sheet = workbook.getSheetAt(0); // 第一个sheet XSSFRow row1 = sheet.getRow(1); if (row1 == null) { AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename()); return AdmExcelUtil.getImportFailResult("数据不存在", "2", 0, ++failurNum); } /** 获取识别码 */ XSSFCell markCell = row1.getCell(0); if (markCell == null) { AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename()); return AdmExcelUtil.getImportFailResult("未填写识别码", "2", 0, ++failurNum); } primaryKey = markCell.getStringCellValue().trim(); // 识别码 boolean containsKey = AdmCommonConstant.codeColMap.containsKey(primaryKey); if (!containsKey) { AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename()); return AdmExcelUtil.getImportFailResult("未找到识别码", "2", 0, ++failurNum); } primaryKey = AdmCommonConstant.codeColMap.get(primaryKey); /** 获取类型 */ XSSFCell cell = row1.getCell(1); if (cell == null) { AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename()); return AdmExcelUtil.getImportFailResult("未填写设备类型", "2", 0, ++failurNum); } classCodeCloumn = cell.getStringCellValue().trim(); if (!"ClassCode".equals(classCodeCloumn)) { AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename()); return AdmExcelUtil.getImportFailResult("缺少设备类型(ClassCode)", "2", 0, ++failurNum); } // 存取所有的信息点 Map infoMap = new HashMap(); short lastCellNum = row1.getLastCellNum(); for (int i = 1; i <= lastCellNum; i++) { XSSFCell xssfCell = row1.getCell(i); if (xssfCell != null && xssfCell.getStringCellValue() != null) { infoMap.put(i, xssfCell.getStringCellValue().trim()); } } // 录入数据获取 int lastRowNum = sheet.getLastRowNum(); boolean isClassCode = "classCode".equals(primaryKey); for (int i = 2; i <= lastRowNum; i++) { XSSFRow xssfRow = sheet.getRow(i); if (xssfRow == null) { continue; } String value = null; String classCode = null; ObjectNode equipment = null; List equipmentList = null; Map tempMap = new HashMap(); lastCellNum = xssfRow.getLastCellNum(); for (int j = 0; j <= lastCellNum; j++) { cell = xssfRow.getCell(j); if (cell == null || StrUtil.isBlank(cell.getRawValue())) { xssfRow.createCell(j); if (j == 0) { this.setColor(workbook, 252, 255, 77, xssfRow.getCell(j)); } else { this.setColor(workbook, 77, 178, 255, xssfRow.getCell(j)); } continue; } if (j == 0) { cell.setCellType(CellType.STRING); value = cell.getStringCellValue().trim(); String codeType = isClassCode ? xssfRow.getCell(0).toString().trim() : xssfRow.getCell(1).toString().trim(); // 获取类型定义 ObjectTypeDefine define = this.getObjectTypeDefine(groupCode, projectId, codeType); if (define == null) { this.setColor(workbook, 56, 94, 15, cell); continue; } classCode = define.getCode(); equipmentList = isClassCode ? this.getEquipmentList(groupCode, projectId, null, null, classCode) : this.getEquipmentList(groupCode, projectId, primaryKey, value, classCode); if (CollectionUtil.isEmpty(equipmentList)) { failurNum++; this.setColor(workbook, 153, 225, 77, cell); continue; } if (isClassCode) { equipment = equipmentList.get(0); } else { if (equipmentList.size() > 1) { failurNum++; this.setColor(workbook, 249, 203, 77, cell); } else { equipment = equipmentList.get(0); } } continue; } // 剩余列数据处理 if (j != 1 && equipment != null && infoMap.containsKey(j)) { // 获取设备类的信息点 List funidList = this.relationReportService.queryFunidList(groupCode, projectId, Sets.newHashSet(classCode), infoMap.get(j), "STATIC"); if (CollectionUtil.isEmpty(funidList)) { failurNum++; this.setColor(workbook, 77, 178, 255, cell); continue; } ObjectInfoDefine define = funidList.get(0); if ("STRING".equalsIgnoreCase(define.getDataType())) { tempMap.put(define.getCode(), cell.getStringCellValue().trim()); } else if ("INTEGER".equalsIgnoreCase(define.getDataType())) { try { if (!CellType.NUMERIC.equals(cell.getCellTypeEnum()) || StrUtil.isBlank(cell.getRawValue())) { failurNum++; this.setColor(workbook, 155, 227, 255, cell); continue; } cell.setCellType(CellType.STRING); tempMap.put(define.getCode(), cell.getStringCellValue().trim()); } catch (Exception e) { failurNum++; this.setColor(workbook, 155, 227, 255, cell); continue; } } else if ("ENUM".equalsIgnoreCase(define.getDataType())) { if (CellType.NUMERIC.equals(cell.getCellTypeEnum()) && StrUtil.isNotBlank(cell.getRawValue())) { cell.setCellType(CellType.STRING); tempMap.put(define.getCode(), cell.getStringCellValue().trim()); } else if (CellType.STRING.equals(cell.getCellTypeEnum()) && StrUtil.isNotBlank(cell.getRawValue())) { tempMap.put(define.getCode(), cell.getStringCellValue().trim()); } else { failurNum++; this.setColor(workbook, 155, 227, 255, cell); continue; } } else if ("DATETIME".equalsIgnoreCase(define.getDataType())) { try { cell.setCellType(CellType.STRING); String trim = cell.getStringCellValue().trim(); tempMap.put(define.getCode(), DateUtil.parseByPatterns(trim, "yyyyMMdd")); } catch (Exception e) { failurNum++; this.setColor(workbook, 155, 227, 255, cell); continue; } } else if ("DOUBLE".equalsIgnoreCase(define.getDataType())) { try { cell.setCellType(CellType.STRING); String trim = cell.getStringCellValue().trim(); tempMap.put(define.getCode(), Double.parseDouble(trim)); } catch (Exception e) { failurNum++; this.setColor(workbook, 155, 227, 255, cell); continue; } } else if ("BOOLEAN".equalsIgnoreCase(define.getDataType())) { cell.setCellType(CellType.STRING); String trim = cell.getStringCellValue() == null ? null : cell.getStringCellValue().trim(); if ("1".equals(trim) || "0".equals(trim)) { tempMap.put(define.getCode(), Double.parseDouble(trim)); } else { failurNum++; this.setColor(workbook, 155, 227, 255, cell); continue; } } else { failurNum++; this.setColor(workbook, 204, 99, 233, cell); continue; } } } if (isClassCode) { if (CollectionUtil.isNotEmpty(equipmentList) && tempMap.size() > 0) { for (ObjectNode equipNode : equipmentList) { int success = this.updateEquipInfo(tempMap, groupCode, projectId, equipNode.get("id").asText()); if (success == 0) { failurNum++; } else { successCount++; } } } } else { if (StrUtil.isNotBlank(value) && equipment != null && tempMap.size() > 0) { int success = this.updateEquipInfo(tempMap, groupCode, projectId, equipment.get("id").asText()); if (success == 0) { failurNum++; } else { successCount++; } } } } // 写入错误报告对比值颜色 this.wirteReport(workbook, sheet); // 返回正确结果 String fileName = AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename()); return AdmExcelUtil.getImportSuccessResult(fileName, successCount, failurNum); } catch (Exception e) { log.error("台账信息导入失败", e); failure = e.getMessage(); } finally { if (fileOutputStream != null) { try { fileOutputStream.flush(); fileOutputStream.close(); } catch (IOException e) {} } if (workbook != null) { try { workbook.close(); } catch (IOException e) {} } } return AdmResponse.failure(failure); } /** * 更新设备信息 * * @param tempMap * @param groupCode * @param projectId * @param equipId * @return */ private int updateEquipInfo(Map tempMap, String groupCode, String projectId, String equipId) { ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class); Set keySet = tempMap.keySet(); ObjectNode updateEquip = objectMapper.createObjectNode(); for (String key : keySet) { Object obj = tempMap.get(key); updateEquip.put("id", equipId); if (obj instanceof Date) { updateEquip.putPOJO(key, obj); } else { updateEquip.put(key, obj.toString()); } } ObjectDigital updateOne = DigitalObjectFacade.updateOne(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, updateEquip); return updateOne == null ? 0 : 1; } /** * 获取类型定义 * * @param code * @return */ private ObjectTypeDefine getObjectTypeDefine(String groupCode, String projectId, String code) { QueryCriteria queryCriteria = new QueryCriteria(); ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class); ObjectNode condition = objectMapper.createObjectNode(); JSONObject arrays = new JSONObject(); arrays.put("$ne", "system"); condition.putPOJO("parentCode", arrays); condition.put("code", code); queryCriteria.setCriteria(condition); List objectType = DigitalDefineFacade.queryObjectType(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, queryCriteria); return CollectionUtil.isEmpty(objectType) ? null : objectType.get(0); } /** * 获取设备 * * @param groupCode * @param projectId * @param primaryKey * @param code * @param classCode * @return */ private List getEquipmentList(String groupCode, String projectId, String primaryKey, String code, String classCode) { QueryCriteria queryCriteria = new QueryCriteria(); ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class); ObjectNode condition = objectMapper.createObjectNode(); if (StrUtil.isAllNotBlank(primaryKey, code)) { condition.put(primaryKey, code); } condition.put("classCode", classCode); queryCriteria.setCriteria(condition); List objectNodes = DigitalObjectFacade.query(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, queryCriteria); return objectNodes; } /** * 设置填充色 * * @param workbook * @param r * @param g * @param b * @param cell */ private void setColor(XSSFWorkbook workbook, int r, int g, int b, XSSFCell cell) { XSSFCellStyle createCellStyle = workbook.createCellStyle(); createCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); createCellStyle.setFillForegroundColor(new XSSFColor(new Color(r, g, b))); cell.setCellStyle(createCellStyle); } /** * 写入错误报告对比值颜色 * * @param workbook * @param sheet */ private void wirteReport(XSSFWorkbook workbook, XSSFSheet sheet) { XSSFRow row = sheet.getRow(1); short column = (short) (row.getLastCellNum() + 2); XSSFRow row4 = sheet.getRow(4); if (row4 == null) { row4 = sheet.createRow(4); } XSSFCell createCell = row4.createCell(column); createCell.setCellValue("导入失败,识别码超出范围( 本地编码,本地名称,数字化交付码 )"); createCell = row4.createCell(column + 1); this.setColor(workbook,233,99,99,createCell); XSSFRow row5 = sheet.getRow(5); if (row5 == null) { row5 = sheet.createRow(5); } createCell = row5.createCell(column); createCell.setCellValue("导入失败,识别码为空"); createCell = row5.createCell(column + 1); this.setColor(workbook,252,147,77,createCell); XSSFRow row6 = sheet.getRow(6); if (row6 == null) { row6 = sheet.createRow(6); } createCell = row6.createCell(column); createCell.setCellValue("导入失败,多个实例"); createCell = row6.createCell(column + 1); this.setColor(workbook,249,203,77,createCell); XSSFRow row7 = sheet.getRow(7); if (row7 == null) { row7 = sheet.createRow(7); } createCell = row7.createCell(column); createCell.setCellValue("导入失败,设备不存在"); createCell = row7.createCell(column + 1); this.setColor(workbook,153,225,77,createCell); XSSFRow row8 = sheet.getRow(8); if (row8 == null) { row8 = sheet.createRow(8); } createCell = row8.createCell(column); createCell.setCellValue("导入失败,识别码值为空"); createCell = row8.createCell(column + 1); this.setColor(workbook,252,255,77,createCell); XSSFRow row9 = sheet.getRow(9); if (row9 == null) { row9 = sheet.createRow(9); } createCell = row9.createCell(column); createCell.setCellValue("导入失败,输入类型与字典定义不一致"); createCell = row9.createCell(column + 1); this.setColor(workbook,155,227,255,createCell); XSSFRow row10 = sheet.getRow(10); if (row10 == null) { row10 = sheet.createRow(10); } createCell = row10.createCell(column); createCell.setCellValue("导入失败,未支持的输入类型"); createCell = row10.createCell(column + 1); this.setColor(workbook,204,99,233,createCell); XSSFRow row11 = sheet.getRow(11); if (row11 == null) { row11 = sheet.createRow(11); } createCell = row11.createCell(column); createCell.setCellValue("导入失败,信息点不存在"); createCell = row11.createCell(column + 1); this.setColor(workbook,77,178,255,createCell); XSSFRow row12 = sheet.getRow(12); if (row12 == null) { row12 = sheet.createRow(12); } createCell = row12.createCell(column); createCell.setCellValue("导入失败,设备类型不在字典定义中"); createCell = row12.createCell(column + 1); this.setColor(workbook,56,94,15,createCell); } }