package com.glxp.api.controller.dev; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.RandomUtil; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.github.pagehelper.PageInfo; import com.glxp.api.annotation.AuthRuleAnnotation; import com.glxp.api.common.enums.ResultEnum; import com.glxp.api.common.res.BaseResponse; import com.glxp.api.common.util.ResultVOUtils; import com.glxp.api.controller.BaseController; import com.glxp.api.entity.auth.AuthAdmin; import com.glxp.api.entity.dev.DeviceChangeLogEntity; import com.glxp.api.entity.dev.DeviceChangeOrderItemEntity; import com.glxp.api.entity.dev.DeviceInfoEntity; import com.glxp.api.entity.system.SysPdfTemplateRelevanceLabelEntity; import com.glxp.api.entity.system.SystemPDFTemplateEntity; import com.glxp.api.exception.JsonException; import com.glxp.api.req.dev.*; import com.glxp.api.res.PageSimpleResponse; import com.glxp.api.res.dev.DeptDeviceDetailResponse; import com.glxp.api.service.dev.DeviceChangeLogService; import com.glxp.api.service.dev.DeviceInfoService; import com.glxp.api.service.system.SystemPDFModuleService; import com.glxp.api.service.system.SystemPDFTemplateService; import com.glxp.api.util.JasperUtils; import com.glxp.api.vo.dev.DeviceChangeLogVo; import com.glxp.api.vo.dev.DeviceInfoVo; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.awt.*; import java.awt.font.LineMetrics; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.*; import java.util.List; @RestController @RequestMapping @RequiredArgsConstructor @Slf4j public class DeviceInfoController extends BaseController { private final DeviceInfoService deviceInfoService; private final DeviceChangeLogService deviceChangeLogService; @Value("${file_path}") private String filePath; /** * 生成一个设备号 * * @return */ @AuthRuleAnnotation("") @GetMapping("/udi/device/code/gen") public BaseResponse genDeviceCode() { return ResultVOUtils.success(deviceInfoService.genDeviceCode()); } /** * 本科室设备 * * @param query * @return */ @AuthRuleAnnotation("") @PostMapping("/udi/device/info/page") public BaseResponse page(@RequestBody DeviceInfoQuery query) { AuthAdmin user = super.getUser(); List list = deviceInfoService.pageVo(query, user.getLocDeptCode()); PageInfo pageInfo = new PageInfo<>(list); PageSimpleResponse page = new PageSimpleResponse(); page.setTotal(pageInfo.getTotal()); page.setList(pageInfo.getList()); return ResultVOUtils.success(page); } /** * 所有科室设备 * * @param query * @return */ @AuthRuleAnnotation("") @PostMapping("/udi/device/info/all/page") public BaseResponse allPage(@RequestBody DeviceInfoQuery query) { AuthAdmin user = super.getUser(); List list = deviceInfoService.allPageVo(query); PageInfo pageInfo = new PageInfo<>(list); PageSimpleResponse page = new PageSimpleResponse(); page.setTotal(pageInfo.getTotal()); page.setList(pageInfo.getList()); return ResultVOUtils.success(page); } /** * 查看当前用户所在科室设备详情 * * @param query * @return */ @AuthRuleAnnotation("") @PostMapping("/udi/device/info/detailByUser/page") public BaseResponse detailByUser(@RequestBody @Valid DeviceInfoDetailQuery query) { AuthAdmin user = super.getUser(); query.setDeptCode(user.getLocDeptCode()); List list = deviceInfoService.detail(query); PageInfo pageInfo = new PageInfo<>(list); PageSimpleResponse page = new PageSimpleResponse(); page.setTotal(pageInfo.getTotal()); page.setList(pageInfo.getList()); return ResultVOUtils.success(page); } /** * 查看当前用户所在科室设备详情 * * @param query * @return */ @AuthRuleAnnotation("") @PostMapping("/udi/device/info/detailByUser/one") public BaseResponse detailByUserOne(@RequestBody DeviceInfoDetailQuery query) { List list = deviceInfoService.detail(query); PageInfo pageInfo = new PageInfo<>(list); PageSimpleResponse page = new PageSimpleResponse(); page.setTotal(pageInfo.getTotal()); page.setList(pageInfo.getList()); return ResultVOUtils.success(page); } /** * 查看当前科室设备详情 * * @param query * @return */ @AuthRuleAnnotation("") @PostMapping("/udi/device/info/detailByDeptCode/page") public BaseResponse detailByDeptCode(@RequestBody @Valid DeviceInfoDetailByDeptCodeQuery query) { AuthAdmin user = super.getUser(); query.setDeptCode(user.getLocDeptCode()); List list = deviceInfoService.detail(query); PageInfo pageInfo = new PageInfo<>(list); PageSimpleResponse page = new PageSimpleResponse(); page.setTotal(pageInfo.getTotal()); page.setList(pageInfo.getList()); return ResultVOUtils.success(page); } /** * 查看所有科室设备--设备码级别 * * @param query * @return */ @AuthRuleAnnotation("") @PostMapping("/udi/device/detailInfo/page") public BaseResponse detailInfoPage(@RequestBody DeviceInfoDetailQuery query) { List list = deviceInfoService.detail(query); PageInfo pageInfo = new PageInfo<>(list); PageSimpleResponse page = new PageSimpleResponse(); page.setTotal(pageInfo.getTotal()); page.setList(pageInfo.getList()); return ResultVOUtils.success(page); } /** * 设备详情 * * @param query * @return */ @AuthRuleAnnotation("") @PostMapping("/udi/device/info/detail/page") public BaseResponse detail(@RequestBody @Valid DeviceInfoDetailQuery query) { // AuthAdmin user = super.getUser(); List list = deviceInfoService.detail(query); PageInfo pageInfo = new PageInfo<>(list); PageSimpleResponse page = new PageSimpleResponse(); page.setTotal(pageInfo.getTotal()); page.setList(pageInfo.getList()); return ResultVOUtils.success(page); } /** * 获取设备变更日志 * * @param deviceCode 设备编码 * @return */ @AuthRuleAnnotation("") @GetMapping("/udi/device/log/{deviceCode}") public BaseResponse deviceLogs(@PathVariable String deviceCode) { List logList = deviceChangeLogService.list(Wrappers.lambdaQuery(DeviceChangeLogEntity.class) .eq(DeviceChangeLogEntity::getDeviceCode, deviceCode) .orderByDesc(DeviceChangeLogEntity::getCreateTime) ); return ResultVOUtils.success(BeanUtil.copyToList(logList, DeviceChangeLogVo.class)); } /** * 生成设备二维码 * * @param deviceCode * @throws WriterException */ @GetMapping("/udi/device/genQR/{deviceCode}") public BaseResponse genQRImage(@PathVariable String deviceCode) throws WriterException { DeviceInfoEntity deviceInfo = deviceInfoService.getById(deviceCode); if (deviceInfo == null) { throw new JsonException("未找到该设备,无法生成"); } String text = deviceInfo.getDeviceCode(); // 二维码大小 int width = 500, height = 500; Map hints = new HashMap<>(); //内容编码格式 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //设置二维码边的空度,非负数 hints.put(EncodeHintType.MARGIN, 1); // 生成二维码图片 QRCodeWriter writer = new QRCodeWriter(); BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hints); int margin = 10; int tempM = margin * 2; int[] rec = bm.getEnclosingRectangle(); //获取二维码图案的属性 int resWidth = rec[2] + tempM; int resHeight = rec[3] + tempM; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix resMatrix.clear(); for (int i = margin; i < resWidth - margin; i++) { //循环,将二维码图案绘制到新的bitMatrix中 for (int j = margin; j < resHeight - margin; j++) { if (bm.get(i - margin + rec[0], j - margin + rec[1])) { resMatrix.set(i, j); } } } bm = resMatrix; int w = bm.getWidth(); int h = bm.getHeight(); BufferedImage qrcodeBuffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); // 开始利用二维码数据创建Bitmap图片 for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { qrcodeBuffImg.setRGB(x, y, bm.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } // 新的图片,二维码下面加上文字 String desc = String.format("设备编码:%s", deviceCode); String desc1 = String.format("设备名称:%s", deviceInfo.getProductName()); String desc2 = String.format("设备规格:%s", deviceInfo.getGgxh()); String[] descArr = {desc, desc1, desc2}; // 字体、字型、字号 Font font = new Font("宋体", Font.BOLD, 26); int textHeight = font.getSize(); int textMargin = 10; BufferedImage outImage = new BufferedImage(qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight() + textHeight + (textMargin * 2), BufferedImage.TYPE_4BYTE_ABGR); Graphics2D outg = outImage.createGraphics(); outg.setFont(font); FontMetrics fontMetrics = outg.getFontMetrics(); int lineCount = 1; int lineWidth = outImage.getWidth() - textMargin * 2; int hasWidth = lineWidth; HashMap hashMap = new HashMap<>(); for (String item : descArr) { for (String s : item.split("")) { int strWidth = fontMetrics.stringWidth(s); hasWidth -= strWidth; if (hasWidth <= 0) { hasWidth = lineWidth; lineCount += 1; hasWidth -= strWidth; } Integer i = hashMap.putIfAbsent(lineCount, 1); if (i != null) { hashMap.put(lineCount, i + 1); } } hasWidth = lineWidth; lineCount += 1; } lineCount -= 1; desc = desc.concat(desc1).concat(desc2); outImage = new BufferedImage(qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight() + lineCount * textHeight + (textMargin * (lineCount + 1)), BufferedImage.TYPE_4BYTE_ABGR); outg = outImage.createGraphics(); outg.setFont(font); for (int x = 0; x < outImage.getWidth(); x++) { for (int y = 0; y < outImage.getHeight(); y++) { outImage.setRGB(x, y, 0xFFFFFFFF); } } // 画二维码到新的面板 outg.drawImage(qrcodeBuffImg, 0, 0, qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight(), null); outg.setColor(Color.BLACK); outg.setBackground(Color.WHITE); int startIndex = 0; for (int i = 0; i < lineCount; i++) { Integer length = hashMap.get(i + 1); String line = desc.substring(startIndex, startIndex + length); if (length != null) { startIndex += length.intValue(); } // outg.drawString(line, (outImage.getWidth() - fontMetrics.stringWidth(line) - textMargin) / 2, qrcodeBuffImg.getHeight() + textMargin + (i * textHeight) + (i + 2) * textMargin); outg.drawString(line, textMargin, qrcodeBuffImg.getHeight() + textMargin + (i * textHeight) + (i + 2) * textMargin); } outg.dispose(); try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(outImage, "png", byteArrayOutputStream); byte[] imageBytes = byteArrayOutputStream.toByteArray(); // 使用Base64编码器将字节数组编码为Base64字符串 String base64String = Base64.getEncoder().encodeToString(imageBytes); return ResultVOUtils.success("data:image/png;base64," + base64String); } catch (IOException e) { log.error("e"); throw new JsonException("系统异常"); } } @Resource private SystemPDFModuleService systemPDFModuleService; @Resource private SystemPDFTemplateService systemPDFTemplateService; /** * 设备标签打印 * * @return */ @AuthRuleAnnotation("") @PostMapping("/udiwms/inv/deptDev/label/print") public BaseResponse printLabel(@RequestBody DeviceInfoDetailQuery query, HttpServletRequest request, HttpServletResponse response) throws Exception { List list = deviceInfoService.detail(query); if (CollUtil.isEmpty(list)) { throw new JsonException("未找到该设备,无法生成"); } DeviceInfoVo obj = list.get(0); SysPdfTemplateRelevanceLabelEntity sysPdfTemplateRelevanceLabelEntity = systemPDFModuleService.selectByLabelId(query.getLabelId() + ""); if (null == sysPdfTemplateRelevanceLabelEntity) { return ResultVOUtils.error(ResultEnum.DATA_NOT, "所属模块错误"); } SystemPDFTemplateEntity systemPDFTemplateEntity = systemPDFTemplateService.selectById(String.valueOf(sysPdfTemplateRelevanceLabelEntity.getTemplateId())); if (null == systemPDFTemplateEntity) { return ResultVOUtils.error(ResultEnum.DATA_NOT, "模板错误"); } //打印单号标签 Map data = new HashMap<>(1); Map supData = new HashMap<>(); supData.put("deviceCode", obj.getDeviceCode() == null ? ' ' : obj.getDeviceCode()); supData.put("deptName", obj.getDeptName() == null ? ' ' : obj.getDeptName()); supData.put("productName", obj.getProductName() == null ? ' ' : obj.getProductName()); supData.put("ggxh", obj.getGgxh() == null ? ' ' : obj.getGgxh()); supData.put("productionDate", obj.getProductionDate() == null ? ' ' : obj.getProductionDate()); supData.put("expireDate", obj.getExpireDate() == null ? ' ' : obj.getExpireDate()); supData.put("batchNo", obj.getBatchNo() == null ? ' ' : obj.getBatchNo()); supData.put("serialNo", obj.getSerialNo() == null ? ' ' : obj.getSerialNo()); supData.put("manufactory", obj.getManufactory() == null ? ' ' : obj.getManufactory()); supData.put("measname", obj.getMeasname() == null ? ' ' : obj.getMeasname()); supData.put("zczbhhzbapzbh", obj.getZczbhhzbapzbh() == null ? ' ' : obj.getZczbhhzbapzbh()); supData.put("supName", obj.getSupName() == null ? ' ' : obj.getSupName()); data.put("data", supData); String param = JSON.toJSONString(data); log.error(param); JasperUtils.jasperReport(request, response, param, filePath + "pdf/template/" + systemPDFTemplateEntity.getPath(), "pdf"); return ResultVOUtils.error(ResultEnum.DATA_NOT, "模板错误"); } /** * 更新明细 * * @return */ @AuthRuleAnnotation("") @PostMapping("/udi/device/info/update") public BaseResponse updateOrderItem(@RequestBody DeviceInfoEntity entity) { entity.setUpdateTime(LocalDateTime.now()); deviceInfoService.updateOrderItem(entity); return ResultVOUtils.successMsg("保存成功!"); } }