package com.doumee.service.business.impl; import com.doumee.biz.system.SystemDictDataBiz; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; import com.doumee.core.model.LoginUserInfo; import com.doumee.core.model.PageData; import com.doumee.core.model.PageWrap; import com.doumee.core.utils.Constants; import com.doumee.core.utils.DateUtil; import com.doumee.core.utils.Utils; import com.doumee.dao.business.YwPatrolPointMapper; import com.doumee.dao.business.model.Category; import com.doumee.dao.business.model.YwDevice; import com.doumee.dao.business.model.YwPatrolPoint; import com.doumee.dao.system.MultifileMapper; import com.doumee.dao.system.model.Multifile; import com.doumee.dao.system.model.SystemUser; import com.doumee.service.business.YwPatrolPointService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.qrcode.encoder.ByteMatrix; import com.google.zxing.qrcode.encoder.Encoder; import com.google.zxing.qrcode.encoder.QRCode; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.Charset; import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.*; /** * 运维巡检点信息表Service实现 * @author 江蹄蹄 * @date 2024/11/19 16:07 */ @Service public class YwPatrolPointServiceImpl implements YwPatrolPointService { @Autowired private YwPatrolPointMapper ywPatrolPointMapper; @Autowired private SystemDictDataBiz systemDictDataBiz; @Autowired private MultifileMapper multifileMapper; @Override public Integer create(YwPatrolPoint ywPatrolPoint) { if(Objects.isNull(ywPatrolPoint) // || Objects.isNull(ywPatrolPoint.getCode()) || Objects.isNull(ywPatrolPoint.getName()) ){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } String pre = systemDictDataBiz.queryByCode(Constants.XUNJIAN,Constants.XJ_POINT_PREFIX).getCode(); int length = 8; try { length = Integer.parseInt(systemDictDataBiz.queryByCode(Constants.XUNJIAN,Constants.XJ_POINT_CODE_LENGTH).getCode()); }catch (Exception e){ } long num = ywPatrolPointMapper.selectCount(new QueryWrapper()); ywPatrolPoint.setCode(pre+Constants.formartNumString (length,(num+1))); LoginUserInfo loginUserInfo = ywPatrolPoint.getLoginUserInfo(); ywPatrolPoint.setCreateDate(new Date()); ywPatrolPoint.setCreator(loginUserInfo.getId()); ywPatrolPoint.setIsdeleted(Constants.ZERO); ywPatrolPoint.setStatus(Constants.ZERO); ywPatrolPointMapper.insert(ywPatrolPoint); if(Objects.nonNull(ywPatrolPoint.getFileUrl())){ Multifile multifile = new Multifile(); multifile.setCreator(loginUserInfo.getId()); multifile.setCreateDate(new Date()); multifile.setIsdeleted(Constants.ZERO); multifile.setObjType(Constants.MultiFile.FN_PATROL_POINT_FILE.getKey()); multifile.setObjId(ywPatrolPoint.getId()); multifile.setFileurl(ywPatrolPoint.getFileUrl()); multifileMapper.insert(multifile); } return ywPatrolPoint.getId(); } @Override public void deleteById(Integer id, LoginUserInfo user) { ywPatrolPointMapper.update(new UpdateWrapper().lambda() .set(YwPatrolPoint::getIsdeleted,Constants.ONE) .set(YwPatrolPoint::getEditDate, DateUtil.getCurrDateTime()) .set(YwPatrolPoint::getEditor,user.getId()) .eq(YwPatrolPoint::getId,id) ); } @Override public void delete(YwPatrolPoint ywPatrolPoint) { UpdateWrapper deleteWrapper = new UpdateWrapper<>(ywPatrolPoint); ywPatrolPointMapper.delete(deleteWrapper); } @Override public void exportQrcodes(Integer id, HttpServletResponse response){ try { List fileList = new ArrayList<>(); List bikesList = ywPatrolPointMapper.selectList(new QueryWrapper().lambda() .eq(YwPatrolPoint::getIsdeleted,Constants.ZERO) .eq(id!=null,YwPatrolPoint::getId,id) ); if(bikesList== null || bikesList.size() == 0){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } // 创建临时文件的前缀和后缀 String path = systemDictDataBiz.queryByCode(Constants.WX_PLATFORM,Constants.WX_AUTH_URL).getCode(); String uri = systemDictDataBiz.queryByCode(Constants.XUNJIAN,Constants.XJ_RERIRECT_URI).getCode(); String appId = systemDictDataBiz.queryByCode(Constants.WX_PLATFORM,Constants.WX_PLATFORM_APPID).getCode() ; // 创建临时文件 for(YwPatrolPoint l : bikesList){ if(StringUtils.isNotBlank(l.getCode())){ String redirectUri = uri.replace("${ywid}",l.getCode()); String url = path.replace("${url}",URLEncoder.encode(redirectUri)).replace("${appid}",appId); File file = generateQRCodeImage(url,100,100,l.getCode()+".png"); if(file!=null && file.isFile()){ fileList.add(file); } } } if(fileList == null || fileList.size() == 0){ throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,下载内容为空,操作失败!"); } String fileName = "巡检点二维码导出_"+System.currentTimeMillis()+".zip" ; String encodeFileName =URLEncoder.encode(fileName, Charset.forName("UTF-8").toString())+".zip"; response.setHeader("Content-Disposition","attachment;filename=" + encodeFileName); response.setContentType("application/octet-stream"); response.setHeader("eva-opera-type", "download"); response.setHeader("eva-download-filename", encodeFileName); packFilesToZip(fileList,response.getOutputStream()); } catch (IOException e) { throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR, e); } } public static File generateQRCodeImage(String text, int width, int height, String fileName) { try { // 创建二维码数据矩阵 Map hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码为UTF-8 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 设置纠错等级为H hints.put(EncodeHintType.MARGIN, 0); // 设置白边为0 BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE , width, height, hints); // 保存二维码图片到文件系统 File f = new File("temp/"); if(!f.exists()){ f.mkdirs(); } // bitMatrix = renderResult(bitMatrix,width,height); Path path = FileSystems.getDefault().getPath("temp/"+fileName); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); // 保存为PNG格式的图片 return path.toFile(); }catch (Exception e){ e.printStackTrace(); } return null; } public static void main(String[] args) { File f =generateQRCodeImage("dj少时诵诗书少时诵诗书是撒是撒是撒",100,100,UUID.randomUUID().toString()+".png"); System.out.println(f.getAbsolutePath()); } private static BitMatrix renderResult(BitMatrix input, int width, int height) { if (input == null) { return null; } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); // 依据用户的输入宽高,计算最后的输出宽高 int outputWidth = Math.max(width, inputWidth); int outputHeight = Math.max(height, inputHeight); //计算缩放比例 int multiple = Math.min(outputWidth / inputWidth, outputHeight / inputHeight); BitMatrix output = new BitMatrix(outputWidth, outputHeight); int inputY = 0; // 嵌套循环,将ByteMatrix的内容计算padding后转换成BitMatrix for (int outputY = 0; inputY < inputHeight; outputY += multiple) { int inputX = 0; for (int outputX = 0; inputX < inputWidth; outputX += multiple) { if (input.get(inputX, inputY)) { output.setRegion(outputX, outputY, multiple, multiple); } inputX++; } inputY++; } return output; } public static void packFilesToZip(List files, ServletOutputStream os) throws IOException { try (ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(os)) { for (File file : files) { ZipArchiveEntry entry = new ZipArchiveEntry(file.getName()); zipOutputStream.putArchiveEntry(entry); try (FileInputStream fileInputStream = new FileInputStream(file)) { byte[] buffer = new byte[1024]; int length; while ((length = fileInputStream.read(buffer)) > 0) { zipOutputStream.write(buffer, 0, length); } } zipOutputStream.closeArchiveEntry(); } } } @Override public void deleteByIdInBatch(List ids, LoginUserInfo user) { if (CollectionUtils.isEmpty(ids)) { return; } ywPatrolPointMapper.deleteBatchIds(ids); } @Override public void updateById(YwPatrolPoint ywPatrolPoint) { if(Objects.isNull(ywPatrolPoint) || Objects.isNull(ywPatrolPoint.getId()) || Objects.isNull(ywPatrolPoint.getCode()) || Objects.isNull(ywPatrolPoint.getName()) ){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } if(ywPatrolPointMapper.selectCount(new QueryWrapper().lambda().eq(YwPatrolPoint::getIsdeleted,Constants.ZERO) .eq(YwPatrolPoint::getCode,ywPatrolPoint.getCode()).ne(YwPatrolPoint::getId,ywPatrolPoint.getId()))>Constants.ZERO){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"巡检点编码重复!"); } LoginUserInfo loginUserInfo = ywPatrolPoint.getLoginUserInfo(); ywPatrolPoint.setEditDate(new Date()); ywPatrolPoint.setEditor(loginUserInfo.getId()); ywPatrolPointMapper.updateById(ywPatrolPoint); multifileMapper.delete(new QueryWrapper().lambda() .eq(Multifile::getObjId,ywPatrolPoint.getId()) .eq(Multifile::getObjType,Constants.MultiFile.FN_PATROL_POINT_FILE.getKey()) ); if(Objects.nonNull(ywPatrolPoint.getFileUrl())){ Multifile multifile = new Multifile(); multifile.setCreator(loginUserInfo.getId()); multifile.setCreateDate(new Date()); multifile.setIsdeleted(Constants.ZERO); multifile.setObjType(Constants.MultiFile.FN_PATROL_POINT_FILE.getKey()); multifile.setObjId(ywPatrolPoint.getId()); multifile.setFileurl(ywPatrolPoint.getFileUrl()); multifileMapper.insert(multifile); } } @Override public void updateByIdInBatch(List ywPatrolPoints) { if (CollectionUtils.isEmpty(ywPatrolPoints)) { return; } for (YwPatrolPoint ywPatrolPoint: ywPatrolPoints) { this.updateById(ywPatrolPoint); } } @Override public YwPatrolPoint findById(Integer id) { return ywPatrolPointMapper.selectById(id); } @Override public YwPatrolPoint getDetail(Integer id) { YwPatrolPoint ywPatrolPoint = ywPatrolPointMapper.selectById(id); if(Objects.isNull(ywPatrolPoint)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } Multifile multifile = multifileMapper.selectOne(new QueryWrapper().lambda().eq(Multifile::getObjId,id) .eq(Multifile::getObjType,Constants.MultiFile.FN_PATROL_POINT_FILE.getKey()).orderByDesc(Multifile::getId).last(" limit 1")); if(Objects.nonNull(multifile) && StringUtils.isNotBlank(multifile.getFileurl())){ String path = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode() +systemDictDataBiz.queryByCode(Constants.FTP,Constants.YW_PATROL).getCode(); ywPatrolPoint.setFileFullUrl(path + multifile.getFileurl()); ywPatrolPoint.setFileUrl(multifile.getFileurl()); } return ywPatrolPoint; } @Override public YwPatrolPoint findOne(YwPatrolPoint ywPatrolPoint) { QueryWrapper wrapper = new QueryWrapper<>(ywPatrolPoint); return ywPatrolPointMapper.selectOne(wrapper); } @Override public List findList(YwPatrolPoint ywPatrolPoint) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().eq(YwPatrolPoint::getIsdeleted,Constants.ZERO); if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywPatrolPoint.getIdLists())){ wrapper.lambda().notIn(YwPatrolPoint::getId,ywPatrolPoint.getIdLists()); } return ywPatrolPointMapper.selectList(wrapper); } @Override public PageData findPage(PageWrap pageWrap) { IPage page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); MPJLambdaWrapper queryWrapper = new MPJLambdaWrapper(); Utils.MP.blankToNull(pageWrap.getModel()); YwPatrolPoint model = pageWrap.getModel(); queryWrapper.selectAll(YwPatrolPoint.class) .selectAs(Category::getName,YwPatrolPoint::getAreaName) .selectAs(YwDevice::getName,YwPatrolPoint::getDeviceName) .leftJoin(Category.class,Category::getId,YwPatrolPoint::getAreaId) .leftJoin(YwDevice.class,YwDevice::getId,YwPatrolPoint::getDeviceId) .and(Objects.nonNull(model)&&StringUtils.isNotBlank(model.getName()), i->i.like(YwPatrolPoint::getName,model.getName()).or().like(YwPatrolPoint::getCode,model.getName())) .eq(Objects.nonNull(model.getAreaId()),YwPatrolPoint::getAreaId,model.getAreaId()) .eq(YwPatrolPoint::getIsdeleted,Constants.ZERO) .orderByDesc(YwPatrolPoint::getCreateDate) ; IPage iPage = ywPatrolPointMapper.selectJoinPage(page,YwPatrolPoint.class,queryWrapper); return PageData.from(iPage); } @Override public long count(YwPatrolPoint ywPatrolPoint) { QueryWrapper wrapper = new QueryWrapper<>(ywPatrolPoint); return ywPatrolPointMapper.selectCount(wrapper); } }