package com.sercurityControl.proteam.controller; import cn.afterturn.easypoi.cache.manager.IFileLoader; import com.alibaba.fastjson2.JSONObject; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.securityControl.common.core.utils.StringUtils; import com.securityControl.common.core.utils.aes.DateTimeHelper; import com.securityControl.common.core.utils.aes.StringHelper; import com.securityControl.common.core.utils.poi.ExcelUtil; import com.securityControl.common.log.annotation.Log; import com.securityControl.common.log.enums.BusinessType; import com.securityControl.common.log.enums.OperationType; import com.sercurityControl.proteam.domain.vo.BallSwHGVo; import com.sercurityControl.proteam.domain.vo.DeviceVo; import com.sercurityControl.proteam.domain.vo.ReturnCodeEntity; import com.sercurityControl.proteam.domain.vo.SysVideoConfigVo; import com.sercurityControl.proteam.mapper.DeviceMapper; import com.sercurityControl.proteam.service.DeviceService; import com.sercurityControl.proteam.util.BaseController; import com.sercurityControl.proteam.util.ImportExcelUtils; import com.sercurityControl.proteam.util.ResultModel; import io.swagger.annotations.ApiOperation; import org.apache.poi.util.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.regex.Pattern; /** * 设备管理控制层 */ @RestController @RequestMapping("/pot/device/") public class DeviceController extends BaseController { private static final Logger log = LoggerFactory.getLogger(DeviceController.class); @Autowired private DeviceService service; @Autowired private DeviceMapper mapper; @Resource(name = "testTaskExecutor") private ThreadPoolTaskExecutor testTaskExecutor; /** * 分页查询 * * @param deviceVo * @return */ @PostMapping("getDeviceList") @Log(title = "设备管理", menu = "设备管理->设备登记", businessType = BusinessType.QUERY, grade = OperationType.QUERY_BUSINESS, details = "设备管理列表") public Map getDeviceList(DeviceVo deviceVo) { PageHelper.startPage(deviceVo.getPage(), deviceVo.getLimit()); List deviceList = service.getDeviceList(deviceVo); PageInfo pageInfo = new PageInfo(deviceList); System.out.println("输出分页后的数据:" + pageInfo.getList()); Map map = new HashMap(16); map.put("code", 200); map.put("msg", ""); map.put("count", pageInfo.getTotal()); map.put("curr", deviceVo.getPage()); map.put("limit", deviceVo.getLimit()); map.put("data", pageInfo.getList()); return map; } @PostMapping("insertDevice") @Log(title = "设备管理", menu = "设备管理->设备登记", businessType = BusinessType.INSERT, details = "新增设备", grade = OperationType.ADD_BUSINESS) public ReturnCodeEntity insertDevice(DeviceVo deviceVo) { ReturnCodeEntity entity = new ReturnCodeEntity(); try { // String josn = Aes.aesDecrypt(params); // DeviceVo deviceVo = JSON.parseObject(josn, DeviceVo.class);//把json字符串转为实体类 entity = service.insertDevice(deviceVo); } catch (Exception e) { entity.setCode("202"); entity.setMsg("解析异常,请联系管理员"); } return entity; } @PostMapping("updateDevice") @Log(title = "设备管理", menu = "设备管理->设备登记", businessType = BusinessType.UPDATE, details = "修改设备", grade = OperationType.UPDATE_BUSINESS) public ReturnCodeEntity updateDevice( DeviceVo deviceVo ) { ReturnCodeEntity entity = new ReturnCodeEntity(); try { // String josn = Aes.aesDecrypt(params); // DeviceVo deviceVo = JSON.parseObject(josn, DeviceVo.class);//把json字符串转为实体类 entity = service.updateDevice(deviceVo); } catch (Exception e) { entity.setCode("202"); entity.setMsg("解析异常,请联系管理员"); } return entity; } /** * 更新设备状态 * @param deviceVo * @return */ @PostMapping("updateDeviceState") public ReturnCodeEntity updateDeviceState( DeviceVo deviceVo ) { ReturnCodeEntity entity = new ReturnCodeEntity(); try { entity = service.updateDeviceState(deviceVo); } catch (Exception e) { entity.setCode("202"); entity.setMsg("解析异常,请联系管理员"); } return entity; } /** * 删除角色 * * @param * @return */ @PostMapping("deleteDevice") @Log(title = "设备管理", menu = "设备管理->设备登记", businessType = BusinessType.DELETE, details = "删除设备", grade = OperationType.DELETE_BUSINESS) public ReturnCodeEntity deleteDevice(String keyId) { ReturnCodeEntity entity = new ReturnCodeEntity(); try { // String keyId = Aes.aesDecrypt(params); entity = service.deleteDevice(keyId); } catch (Exception e) { entity.setCode("202"); entity.setMsg("解析异常,请联系管理员"); } return entity; } /** * 刷新球机实时状态 * @return */ @PostMapping("refresh") public ReturnCodeEntity refresh( ) { ReturnCodeEntity entity = new ReturnCodeEntity(); try { entity = service.refresh(); } catch (Exception e) { log.error(e.toString(),e); entity.setCode("202"); entity.setMsg("解析异常,请联系管理员"); } return entity; } /** * 球机守望位置接口 * @param puid * @param type 1 设置 2跳转 * @return */ @PostMapping("setSwwzConfig") public ReturnCodeEntity setSwwzConfig(String puid,String type ) { ReturnCodeEntity entity = new ReturnCodeEntity(); try { if("1".equals(type)){ entity = service.setSwwzConfig(puid); }else{ entity = service.gotoSwwzConfig(puid); } } catch (Exception e) { log.error(e.toString(),e); entity.setCode("202"); entity.setMsg("解析异常,请联系管理员"); } return entity; } /** * 获取球机 守望位置初始化数据 * @param vo * @return */ @PostMapping("getSwwzData") public BallSwHGVo getSwwzData(BallSwHGVo vo) { BallSwHGVo entity = new BallSwHGVo(); try { if("1".equals(vo.getType())){ entity = service.getSwwzData(vo); }else{ entity = service.setSwwzData(vo); } } catch (Exception e) { log.error(e.toString(),e); } return entity; } /** * 重置球机状态 为1 * @return */ @PostMapping("refreshDevice") public ReturnCodeEntity refreshDevice( ) { ReturnCodeEntity entity = new ReturnCodeEntity(); try { entity = service.refreshDevice(); } catch (Exception e) { log.error(e.toString(),e); entity.setCode("202"); entity.setMsg("解析异常,请联系管理员"); } return entity; } /** * 查看详情 * * @param * @return */ @PostMapping("getDeviceDetail") @Log(title = "设备管理", menu = "设备管理->设备登记", businessType = BusinessType.QUERY, details = "设备查看详情", grade = OperationType.QUERY_BUSINESS) public DeviceVo getDeviceDetail(String keyId) { DeviceVo deviceVo = null; try { // String keyId = Aes.aesDecrypt(params); deviceVo = service.getDeviceDetail(keyId); return deviceVo; } catch (Exception e) { // log.error(e.toString(),e); } return deviceVo; } /** * 获取视频初始化配置 * @param * @return */ @PostMapping("getVideoConfig") public SysVideoConfigVo getVideoConfig(String keyId) { SysVideoConfigVo configVo = new SysVideoConfigVo(); try { configVo=service.getVideoConfig(); } catch (Exception e) { log.error(e.toString(),e); } return configVo; } @GetMapping("exportDevice") @Log(title = "设备管理", menu = "设备管理->设备登记", businessType = BusinessType.EXPORT, details = "导出设备", grade = OperationType.EXPORT_BUSINESS) public void exportDevice(HttpServletResponse response, DeviceVo deviceVo) { List studentsList = service.getDeviceList(deviceVo); ExcelUtil util = new ExcelUtil(DeviceVo.class); util.exportExcel(response, studentsList, "设备台账"); } @ApiOperation("模板下载") @GetMapping("/downloadExcel") @Log(title = "设备管理", menu = "设备管理->设备登记", businessType = BusinessType.QUERY, details = "模板下载", grade = OperationType.DOWNLOAD_BUSINESS) public void downloadExcel(HttpServletRequest request, HttpServletResponse response) { InputStream inputStream = null; ServletOutputStream servletOutputStream = null; try { String path = "download/" + "device.xlsx"; inputStream = this.getClass().getClassLoader().getResourceAsStream(path); response.setContentType("application/vnd.ms-excel"); response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); response.addHeader("charset", "utf-8"); response.addHeader("Pragma", "no-cache"); String encodeName = URLEncoder.encode("设备台账导入摸板.xlsx", StandardCharsets.UTF_8.toString()); response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName); servletOutputStream = response.getOutputStream(); IOUtils.copy(inputStream, servletOutputStream); response.flushBuffer(); //log.info("文件下载成功!!"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (servletOutputStream != null) { servletOutputStream.close(); } if (inputStream != null) { inputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } } /** * @return void * @author cw chen * @description 设备导入 * @Param file * @Param request * @Param response * @date 2022-12-21 9:45 */ @PostMapping("/importExcel") public void importExcel(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { try { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); MultipartHttpServletRequest multiReq = multipartResolver.resolveMultipart(request); // 违章类型id List lstObj = ImportExcelUtils.readExcel(file, DeviceController.class, null); List list = new ArrayList<>(); List lstError = new ArrayList<>(); // 验证表格中的数据是否重复 List errList = dataIsRepeat(lstObj); if (lstObj != null && lstObj.size() > 0) { list = new ArrayList<>(); lstError = new ArrayList<>(); for (JSONObject obj : lstObj) { String rowRow = obj.getString("rowNo"); if (StringHelper.isEmpty(obj.getString("deviceName"))) { addError(lstError, rowRow, "设备名称", "", "设备名称不能为空"); continue; } if (StringHelper.isEmpty(obj.getString("macId"))) { addError(lstError, rowRow, "设备编码", "", "设备编码不能为空"); continue; } else { DeviceVo deviceVo = new DeviceVo(); deviceVo.setMacId(obj.getString("macId")); int num = mapper.getNumByMacId(deviceVo); if (num > 0) { addError(lstError, rowRow, "设备编码", obj.getString("macId"), "设备编已存在"); continue; } } if (StringHelper.isEmpty(obj.getString("gbCode"))) { addError(lstError, rowRow, "国网编码", "", "国网编码不能为空"); continue; } else { DeviceVo deviceVo = new DeviceVo(); deviceVo.setGbCode(obj.getString("gbCode")); int num = mapper.getNumByGbCode(deviceVo); if (num > 0) { addError(lstError, rowRow, "国网编码", obj.getString("gbCode"), "国网编码已存在"); continue; } } if (StringHelper.isEmpty(obj.getString("puId"))) { addError(lstError, rowRow, "puId", "", "puId不能为空"); continue; } else { DeviceVo deviceVo = new DeviceVo(); deviceVo.setPuId(obj.getString("puId")); int num = mapper.getNumByPuId(deviceVo); if (num > 0) { addError(lstError, rowRow, "puId", obj.getString("puId"), "puId已存在"); continue; } } if (StringHelper.isNotEmpty(obj.getString("ssdw"))) { String id = mapper.getSysDictId("ss_dw", obj.getString("ssdw")); if (StringHelper.isEmpty(id)) { addError(lstError, rowRow, "所示单位", obj.getString("ssdw"), "所属单位不存在"); continue; } } if (StringHelper.isEmpty(obj.getString("deviceType"))) { addError(lstError, rowRow, "设备类型", "", "设备类型不能为空"); continue; } else { String id = mapper.getDeviceTypeByName("device_type", obj.getString("deviceType")); if (StringHelper.isEmpty(id)) { addError(lstError, rowRow, "设备类型", obj.getString("deviceType"), "设备类型不存在"); continue; } } if ("是".equals(obj.getString("isT"))) { if (StringHelper.isEmpty(obj.getString("tName"))) { addError(lstError, rowRow, "统一视频名称", "", "统一视频名称不能为空"); continue; } if (StringHelper.isEmpty(obj.getString("tCode"))) { addError(lstError, rowRow, "统一视频编码", "", "统一视频编码不能为空"); continue; } else { Pattern pattern = Pattern.compile("[0-9]*"); if (!pattern.matcher(obj.getString("tCode")).matches()) { addError(lstError, rowRow, "统一视频编码", obj.getString("tCode"), "统一编码格式不正确"); continue; } else { if (obj.getString("tCode").length() != 18) { addError(lstError, rowRow, "统一视频编码", obj.getString("tCode"), "统一编码格式不正确"); continue; } } DeviceVo deviceVo = new DeviceVo(); deviceVo.settCode(obj.getString("tCode")); int num = mapper.getNumByTcode(deviceVo); if (num > 0) { addError(lstError, rowRow, "统一视频编码", obj.getString("tCode"), "统一编码格式已存在"); continue; } } if (StringHelper.isEmpty(obj.getString("twCode"))) { addError(lstError, rowRow, "统一前端协议", "", "统一前端协议编码空"); continue; } else { DeviceVo deviceVo = new DeviceVo(); deviceVo.setTwCode(obj.getString("twCode")); int num = mapper.getNumByTWcode(deviceVo); if (num > 0) { addError(lstError, rowRow, "统一前端协议", obj.getString("twCode"), "统一前端协议编码已存在"); continue; } } if (StringHelper.isEmpty(obj.getString("tdCode"))) { addError(lstError, rowRow, "统一设备协议编码", "", "统一设备协议编码空"); continue; } else { DeviceVo deviceVo = new DeviceVo(); deviceVo.setTdCode(obj.getString("tdCode")); int num = mapper.getNumByTDcode(deviceVo); if (num > 0) { addError(lstError, rowRow, "统一设备协议", obj.getString("twCode"), "统一设备协议编码已存在"); continue; } } } DeviceVo vo = new DeviceVo(); vo.setDeviceName(obj.getString("deviceName")); vo.setGbCode(obj.getString("gbCode")); vo.setPuId(obj.getString("puId")); vo.setMacId(obj.getString("macId")); if (StringHelper.isNotEmpty(obj.getString("ssdw"))) { String id = mapper.getSysDictId("ss_dw", obj.getString("ssdw")); vo.setSsdw(id); } //设备类型 String id = mapper.getDeviceTypeByName("device_type", obj.getString("deviceType")); vo.setTypeCode(id); vo.setTypeName(obj.getString("deviceType")); if ("是".equals(obj.getString("isT"))) { vo.setIst("1");//是否统一 } else { vo.setIst("0");//是否统一 } vo.setCreateTime(DateTimeHelper.getNowTime()); vo.settCode(obj.getString("tCode"));//统一视频code vo.setTdCode(obj.getString("tdCode"));// vo.setTwCode(obj.getString("twCode")); vo.settName(obj.getString("tName")); vo.setRemark(obj.getString("remarks")); list.add(vo); } } lstError.addAll(errList); ResultModel resultModel = new ResultModel(); if (lstError != null && lstError.size() > 0) { resultModel.setSuccess(false); resultModel.setStatus(200); resultModel.setMsg("导入成功"); resultModel.setData(lstError); } else if (lstError.size() == 0) { service.addImportData(list); resultModel.setSuccess(true); resultModel.setStatus(200); resultModel.setMsg("导入成功"); resultModel.setData(null); } sendJson(response, resultModel); } catch (Exception ex) { ex.printStackTrace(); resultModel = new ResultModel(); resultModel.setSuccess(false); resultModel.setStatus(1000); resultModel.setMsg(ex.getMessage()); sendJson(response, resultModel); } } public void addError(List lstError, String errorRow, String errorLine, String errorData, String errorMsg) { JSONObject error = new JSONObject(); error.put("errorRow", errorRow); error.put("errorLine", errorLine); error.put("errorData", errorData); error.put("errorMsg", errorMsg); lstError.add(error); } /** * @return java.util.List * @author cw chen * @description 验证excel表中重复数据 * @Param lstObj * @date 2022-11-22 15:41 */ public List dataIsRepeat(List lstObj) throws ExecutionException, InterruptedException { List errorList = new ArrayList(); List list1 = new ArrayList(); List list2 = new ArrayList(); List list3 = new ArrayList(); List list4 = new ArrayList(); List list5 = new ArrayList(); List list6 = new ArrayList(); Future> future = testTaskExecutor.submit(() -> { List errList1 = new ArrayList(); for (JSONObject obj : lstObj) { if (StringUtils.isNotBlank(obj.getString("gbCode"))) { list1.add(obj.getString("gbCode")); } else { list1.add(""); } if (StringUtils.isNotBlank(obj.getString("puId"))) { list2.add(obj.getString("puId")); } else { list2.add(""); } if (StringUtils.isNotBlank(obj.getString("macId"))) { list3.add(obj.getString("macId")); } else { list3.add(""); } if (StringUtils.isNotBlank(obj.getString("tCode"))) { list4.add(obj.getString("tCode")); } else { list4.add(""); } if (StringUtils.isNotBlank(obj.getString("twCode"))) { list5.add(obj.getString("twCode")); } else { list5.add(""); } if (StringUtils.isNotBlank(obj.getString("tdCode"))) { list6.add(obj.getString("tdCode")); } else { list6.add(""); } } List same1 = same(list1); same1.forEach(item -> { List errorRow = new ArrayList<>(); String[] rowNos = item.getString("rowNo").split(","); for (int i = 0; i < rowNos.length; i++) { errorRow.add(lstObj.get(Integer.parseInt(rowNos[i]) - 1).getString("rowNo")); } JSONObject error = new JSONObject(); error.put("errorRow", StringUtils.join(errorRow, ",")); error.put("errorLine", "国标编码"); error.put("errorData", item.getString("data")); error.put("errorMsg", "国标编码重复,重复行:" + StringUtils.join(errorRow, ",")); errList1.add(error); }); List same2 = same(list2); same2.forEach(item -> { List errorRow = new ArrayList<>(); String[] rowNos = item.getString("rowNo").split(","); for (int i = 0; i < rowNos.length; i++) { errorRow.add(lstObj.get(Integer.parseInt(rowNos[i]) - 1).getString("rowNo")); } JSONObject error = new JSONObject(); error.put("errorRow", StringUtils.join(errorRow, ",")); error.put("errorLine", "puId"); error.put("errorData", item.getString("data")); error.put("errorMsg", "puId重复,重复行:" + StringUtils.join(errorRow, ",")); errList1.add(error); }); List same3 = same(list3); same3.forEach(item -> { List errorRow = new ArrayList<>(); String[] rowNos = item.getString("rowNo").split(","); for (int i = 0; i < rowNos.length; i++) { errorRow.add(lstObj.get(Integer.parseInt(rowNos[i]) - 1).getString("rowNo")); } JSONObject error = new JSONObject(); error.put("errorRow", StringUtils.join(errorRow, ",")); error.put("errorLine", "设备编码"); error.put("errorData", item.getString("data")); error.put("errorMsg", "设备编码重复,重复行:" + StringUtils.join(errorRow, ",")); errList1.add(error); }); List same4 = same(list4); same4.forEach(item -> { List errorRow = new ArrayList<>(); String[] rowNos = item.getString("rowNo").split(","); for (int i = 0; i < rowNos.length; i++) { errorRow.add(lstObj.get(Integer.parseInt(rowNos[i]) - 1).getString("rowNo")); } JSONObject error = new JSONObject(); error.put("errorRow", StringUtils.join(errorRow, ",")); error.put("errorLine", "统一编码"); error.put("errorData", item.getString("data")); error.put("errorMsg", "统一编码重复,重复行:" + StringUtils.join(errorRow, ",")); errList1.add(error); }); List same5 = same(list5); same5.forEach(item -> { List errorRow = new ArrayList<>(); String[] rowNos = item.getString("rowNo").split(","); for (int i = 0; i < rowNos.length; i++) { errorRow.add(lstObj.get(Integer.parseInt(rowNos[i]) - 1).getString("rowNo")); } JSONObject error = new JSONObject(); error.put("errorRow", StringUtils.join(errorRow, ",")); error.put("errorLine", "统一前端编码"); error.put("errorData", item.getString("data")); error.put("errorMsg", "统一前端编码重复,重复行:" + StringUtils.join(errorRow, ",")); errList1.add(error); }); List same6 = same(list6); same6.forEach(item -> { List errorRow = new ArrayList<>(); String[] rowNos = item.getString("rowNo").split(","); for (int i = 0; i < rowNos.length; i++) { errorRow.add(lstObj.get(Integer.parseInt(rowNos[i]) - 1).getString("rowNo")); } JSONObject error = new JSONObject(); error.put("errorRow", StringUtils.join(errorRow, ",")); error.put("errorLine", "统一设备协议"); error.put("errorData", item.getString("data")); error.put("errorMsg", "统一设备协议重复,重复行:" + StringUtils.join(errorRow, ",")); errList1.add(error); }); return errList1; }); errorList.addAll(future.get()); return errorList; } /** * @return java.util.List * @author cw chen * @description 获取重复数据及下标 * @Param list * @date 2022-11-22 15:42 */ public static List same(List list) { List errList = new ArrayList(); Map map = new HashMap(16); for (int i = 0; i < list.size(); i++) { String key = list.get(i); if (key == null || Objects.equals("", key)) { continue; } String old = map.get(key); if (old != null) { map.put(key, old + "," + (i + 1)); } else { map.put(key, "" + (i + 1)); } } Iterator it = map.keySet().iterator(); while (it.hasNext()) { String key = it.next(); String value = map.get(key); if (value.indexOf(",") != -1) { JSONObject error = new JSONObject(); error.put("data", key); error.put("rowNo", value); System.out.println(key + " 重复,行: " + value); errList.add(error); } } return errList; } }