package com.newfiber.modules.inspection.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.newfiber.common.annotation.SysLog; import com.newfiber.common.constant.LWZBaseClazz; import com.newfiber.common.exception.RRException; import com.newfiber.common.utils.PageUtils; import com.newfiber.common.utils.QRCodeGenerator; import com.newfiber.common.utils.R; import com.newfiber.common.utils.poi.ExcelData; import com.newfiber.common.utils.poi.ExcelEntity; import com.newfiber.common.utils.poi.PoiTools; import com.newfiber.modules.inspection.entity.EquImportEntity; import com.newfiber.modules.inspection.entity.EquInfoEntity; import com.newfiber.modules.inspection.entity.ManPumpstation; import com.newfiber.modules.inspection.service.EquInfoService; import com.newfiber.modules.inspection.service.ManPumpstationService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; 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.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; /** * 设备基础信息 * * @author chenshun * @email sunlightcs@gmail.com * @date 2020-09-06 13:49:11 */ @RestController @RequestMapping("equinfo") @Api(tags = "设备管理") public class EquInfoController { @Autowired private EquInfoService equInfoService; @Autowired private LWZBaseClazz lwzBaseClazz; @Resource private ManPumpstationService manPumpstationService; /** * 列表 */ @SysLog @RequestMapping("/list") //@RequiresPermissions("business:equinfo:list") public R list(@RequestBody Map<String, Object> params) { PageUtils page = equInfoService.queryPage(params); return R.ok().put("page", page); } /** * 保存 */ @PostMapping("/save") @ApiOperation("新增设备") //@RequiresPermissions("business:equinfo:save") public R save(@RequestBody EquInfoEntity equInfo) { if(null !=equInfoService.getByEquNo(equInfo.getEquNo())){ throw new RRException("设备编号已存在,请重新输入"); } equInfoService.save(equInfo); return R.ok(); } /** * 修改 */ @ApiOperation("修改设备") @PostMapping("/update") //@RequiresPermissions("business:equinfo:update") public R update(@RequestBody EquInfoEntity equInfo) { EquInfoEntity equInfoEntity = equInfoService.getById(equInfo.getId()); if(StringUtils.isNotBlank(equInfo.getEquNo()) && !equInfo.getEquNo().equals(equInfoEntity.getEquNo())){ if(null !=equInfoService.getByEquNo(equInfo.getEquNo())){ throw new RRException("设备编号已存在,请重新输入"); } } equInfoService.updateById(equInfo); return R.ok(); } /** * 删除 */ @RequestMapping("/delete") //@RequiresPermissions("business:equinfo:delete") public R delete(@RequestBody Integer[] ids) { equInfoService.removeByIds(Arrays.asList(ids)); return R.ok(); } /** * 根据设备编号生成二维码 */ @RequestMapping("/qrimage/{equNo}") //@RequiresPermissions("business:equinfo:qrimage") public ResponseEntity<byte[]> getQRImage(@PathVariable("equNo") String equNo) { byte[] qrcode = null; try { qrcode = QRCodeGenerator.getQRCodeImage(equNo, 350, 350); } catch (WriterException e) { System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.out.println("Could not generate QR Code, IOException :: " + e.getMessage()); } // Set headers final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); return new ResponseEntity<byte[]>(qrcode, headers, HttpStatus.CREATED); } /** * 根据设备编号下载二维码 */ @RequestMapping("/dowimage/{equNo}") //@RequiresPermissions("business:equinfo:qrimage") public void dowimage(@PathVariable("equNo") String equNo, HttpServletRequest request, HttpServletResponse response) { EquInfoEntity equInfoEntity = equInfoService.getOne(new QueryWrapper<EquInfoEntity>().eq("equ_no", equNo)); try { //二维码中包含的信息 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); // 指定编码格式 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定纠错级别(L--7%,M--15%,Q--25%,H--30%) hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 编码内容,编码类型(这里指定为二维码),生成图片宽度,生成图片高度,设置参数 BitMatrix bitMatrix = new MultiFormatWriter().encode(equNo, BarcodeFormat.QR_CODE, 350, 350, hints); // 二维码名称 String fileName = equInfoEntity.getEquName() + ".png"; String userAgent = request.getHeader("User-Agent"); if (userAgent.contains("MSIE") || userAgent.contains("Trident") || userAgent.contains("Edge")) {//IE fileName = java.net.URLEncoder.encode(fileName, "UTF-8"); } else { fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// 谷歌/火狐 } //设置请求头 response.setHeader("Content-Type", "application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); OutputStream outputStream = response.getOutputStream(); MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream); outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 根据设备编号查询设备信息 */ @RequestMapping("/info/{equNo}") //@RequiresPermissions("business:equinfo:info") public R info(@PathVariable("equNo") String equNo) { EquInfoEntity equInfo = equInfoService.getByEquNo(equNo); return R.ok().put("equInfo", equInfo); } /** * 根据设备编号查询设备保养维修记录 */ @RequestMapping("/record/{equNo}") //@RequiresPermissions("business:equinfo:info") public R record(@PathVariable("equNo") String equNo) { Map<String, Object> record = equInfoService.getEquRecord(equNo); return R.ok().put("record", record); } /** * 根据设备编号生成二维码 */ @RequestMapping("app/qrimage") public ResponseEntity<byte[]> getAppQRImage() { byte[] qrcode = null; try { qrcode = QRCodeGenerator.getQRCodeImage(lwzBaseClazz.getAppUrl(), 350, 350); } catch (WriterException e) { System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.out.println("Could not generate QR Code, IOException :: " + e.getMessage()); } // Set headers final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); return new ResponseEntity<byte[]>(qrcode, headers, HttpStatus.CREATED); } @GetMapping("export_excel_template") @ApiOperation("导出设备录入模板") public void exportExcelTemplate(@RequestParam Map<String, Object> params, HttpServletResponse response) { try { ExcelData excelData = new ExcelData(); /** 设置sheet名称 */ List<String> sheets = new ArrayList<>(); sheets.add("设备录入模板"); excelData.setSheetNames(sheets); /** 设置每个sheet的数据 */ ExcelEntity excelEntity = new ExcelEntity(); /** 设置多级表头 */ List<List<String>> titles = new ArrayList<>(); List<String> title = new ArrayList<>( Arrays.asList("泵站", "设备编号", "设备名称", "品牌型号", "采购价格", "安装位置", "采购时间", "保养人员(用户名)", "初次保养日期", "保养间隔(天)", "备注")); titles.add(title); excelEntity.setTitles(titles); List<ExcelEntity> excelEntitys = new ArrayList<>(); excelEntitys.add(excelEntity); /** 组装ExcelData参数类 */ excelData.setExcelEntities(excelEntitys); PoiTools.exportExcel(response, "设备录入模板.xlsx", excelData); } catch (Exception e) { e.printStackTrace(); } } @ApiOperation("导入设备excel数据") @PostMapping("import_equ_excel") @Transactional(rollbackFor = Exception.class) public R importEquExcel(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { throw new RRException("上传失败,请选择文件"); } List<EquImportEntity> equImportEntityList = loadExcel(file); if (CollectionUtils.isNotEmpty(equImportEntityList)) { for (EquImportEntity equImportEntity : equImportEntityList) { // 判断泵站是否存在 ManPumpstation pumpInfoEntity = manPumpstationService.getByName(equImportEntity.getPumpName()); if (null == pumpInfoEntity) { throw new RRException(String.format("泵站{%s}不存在,请先添加泵站", equImportEntity.getPumpName())); } //判断设备是否存在,不存在则保存,存在则更新 EquInfoEntity equInfoEntity = equInfoService.getByEquNo(equImportEntity.getEquNo()); if (null == equInfoEntity) { EquInfoEntity entity = new EquInfoEntity(); BeanUtils.copyProperties(equImportEntity, entity); entity.setPumpNo(pumpInfoEntity.getPumpNo()); equInfoService.save(entity); } else { BeanUtils.copyProperties(equImportEntity, equInfoEntity); equInfoService.updateById(equInfoEntity); } } } return R.ok(); } private List<EquImportEntity> loadExcel(MultipartFile file) { List<EquImportEntity> equImportEntityList = new ArrayList<>(); try { File tempFile; tempFile = File.createTempFile("temp", null); file.transferTo(tempFile); InputStream fileIn = new FileInputStream(tempFile); tempFile.deleteOnExit(); //根据指定的文件输入流导入Excel从而产生Workbook对象 Workbook wb0 = new XSSFWorkbook(fileIn); //获取Excel文档中的第一个表单 Sheet sht0 = wb0.getSheetAt(0); //对Sheet中的每一行进行迭代 for (Row r : sht0) { if (r.getRowNum() < 1) { continue; } EquImportEntity equImportEntity = new EquImportEntity(); equImportEntity.setPumpName(r.getCell(0).getStringCellValue()); equImportEntity.setEquNo(r.getCell(1).getStringCellValue()); equImportEntity.setEquName(r.getCell(2).getStringCellValue()); equImportEntity.setBrandModel(r.getCell(3).getStringCellValue()); equImportEntity.setPrice(r.getCell(4).getNumericCellValue()); equImportEntity.setPosition(r.getCell(5).getStringCellValue()); equImportEntity.setPurchasingTime(r.getCell(6).getDateCellValue()); equImportEntity.setFullname(r.getCell(7).getStringCellValue()); equImportEntity.setMaintainTime(r.getCell(8).getDateCellValue()); equImportEntity.setMaintainCycle(new Double(r.getCell(9).getNumericCellValue()).intValue()); equImportEntity.setRemark(r.getCell(10).getStringCellValue()); if (equImportEntity.checkNull()) { equImportEntityList.add(equImportEntity); } } } catch (IOException e) { e.printStackTrace(); } return equImportEntityList; } }