xuhy
2025-09-02 26e10e787cdef67370ac1efc437126a27d02e757
ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/controller/TChargingGunController.java
@@ -14,7 +14,7 @@
import com.ruoyi.chargingPile.service.ISiteService;
import com.ruoyi.chargingPile.service.TChargingGunService;
import com.ruoyi.chargingPile.service.TChargingPileService;
import com.ruoyi.chargingPile.util.QRCodeUtils;
import com.ruoyi.chargingPile.util.*;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
@@ -26,20 +26,24 @@
import com.ruoyi.common.security.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -84,7 +88,6 @@
    /**
     * 查询充电枪列表
     */
    @RequiresPermissions(value = {"/chargingPile", "/chargingGun"}, logical = Logical.OR)
    @ApiOperation(tags = {"后台-充电枪", "管理后台-充电桩信息"},value = "查询充电枪分页列表")
    @PostMapping(value = "/pageList")
    public AjaxResult<PageInfo<TChargingGunVO>> pageList(@RequestBody TChargingGunQuery query) {
@@ -94,7 +97,6 @@
    /**
     * 添加充电枪管理
     */
    @RequiresPermissions(value = {"/chargingPile/add_charging_gun"}, logical = Logical.OR)
    @Log(title = "【充电桩信息】添加充电枪", businessType = BusinessType.INSERT,operatorType = OperatorType.MANAGE)
    @ApiOperation(tags = {"管理后台-充电桩信息"},value = "添加充电枪")
    @PostMapping(value = "/add")
@@ -105,7 +107,6 @@
    /**
     * 修改充电枪
     */
    @RequiresPermissions(value = {"/chargingGun/update"}, logical = Logical.OR)
    @Log(title = "【充电桩信息】修改充电枪", businessType = BusinessType.UPDATE,operatorType = OperatorType.MANAGE)
    @ApiOperation(tags = {"管理后台-充电桩信息"},value = "修改充电枪")
    @PostMapping(value = "/update")
@@ -116,7 +117,6 @@
    /**
     * 查看充电枪详情
     */
    @RequiresPermissions(value = {"/chargingGun/update", "/chargingGun/select"}, logical = Logical.OR)
    @ApiOperation(tags = {"后台-充电枪", "管理后台-充电桩信息"},value = "查看充电枪详情")
    @GetMapping(value = "/getDetailById")
    public AjaxResult<TChargingGun> getDetailById(@RequestParam("id") Integer id) {
@@ -126,7 +126,6 @@
    /**
     * 删除充电枪
     */
    @RequiresPermissions(value = {"/chargingGun/del"}, logical = Logical.OR)
    @Log(title = "【充电桩信息】删除充电枪", businessType = BusinessType.DELETE,operatorType = OperatorType.MANAGE)
    @ApiOperation(tags = {"管理后台-充电桩信息"},value = "删除充电枪")
    @DeleteMapping(value = "/deleteById")
@@ -139,7 +138,6 @@
    /**
     * 批量删除充电枪
     */
    @RequiresPermissions(value = {"/chargingGun/del"}, logical = Logical.OR)
    @Log(title = "【充电桩信息】批量删除充电枪", businessType = BusinessType.DELETE,operatorType = OperatorType.MANAGE)
    @ApiOperation(tags = {"后台-充电枪"},value = "批量删除充电枪")
    @DeleteMapping(value = "/deleteByIds")
@@ -149,60 +147,43 @@
    }
    @RequiresPermissions(value = {"/chargingGun/download_qr_code"}, logical = Logical.OR)
    @ApiOperation(tags = {"管理后台-充电桩信息"},value = "下载二维码")
    @GetMapping(value = "/downloadQRCode/{id}")
    @Log(title = "【充电桩信息】下载二维码", businessType = BusinessType.OTHER,operatorType = OperatorType.MANAGE)
    public void downloadQRCode(@PathVariable Integer id, HttpServletResponse response){
        try {
    public R<String> downloadQRCode(@PathVariable("id") Integer id) throws IOException {
            TChargingGun chargingGun = chargingGunService.getById(id);
            TChargingPile chargingPile = chargingPileService.getById(chargingGun.getChargingPileId());
            String code = chargingPile.getCode() + chargingGun.getCode();
            String fileName = URLEncoder.encode(code, "UTF-8") + ".jpg";
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
            String url = "https://mxcd.zhinenganguan.com?No=" + code;
            String filePath = fileUploadConfig.getLocation() + fileName;
            QRCodeUtils.encode(url, filePath);
            FileInputStream inputStream = new FileInputStream(filePath);
            ServletOutputStream out = response.getOutputStream();
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = inputStream.read(buf, 0, 1024)) != -1) {
                out.write(buf);
            }
            out.flush();
            out.close();
            inputStream.close();
            //清除服务器上的文件
            try {
                // 使用Runtime执行命令
                Process process = Runtime.getRuntime().exec("sudo rm -rf " + filePath);
                // 读取命令的输出
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
                // 等待命令执行完成
                process.waitFor();
                // 关闭流
                reader.close();
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
            MyQrCodeUtil.createCodeToFile("http://qijishenghuiyuan.obs.cn-southwest-2.myhuaweicloud.com/admin/"+code);
            BufferedImage redImage = QRCodeUtil.createImage(code);
            MultipartFile blueFile = convert(redImage,  ".PNG");
            String s = ObsUploadUtil.obsUploadCode( blueFile,code);
            return R.ok(s);
    }
    public static MultipartFile convert(BufferedImage bufferedImage, String fileName) throws IOException {
        // 将 BufferedImage 转换为字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "png", baos);
        byte[] bytes = baos.toByteArray();
        // 创建 ByteArrayResource
        ByteArrayResource resource = new ByteArrayResource(bytes);
        // 创建 MockMultipartFile
        MockMultipartFile multipartFile = new MockMultipartFile(
                "file",
                fileName,
                "image/png",
                resource.getInputStream()
        );
        return multipartFile;
    }
    @Resource
    private TokenService tokenService;
    /**
     * 设备状态统计
     */
    @RequiresPermissions(value = {"/workbench"}, logical = Logical.OR)
    @ApiOperation(tags = {"后台-工作台"},value = "设备状态统计")
    @PutMapping(value = "/gunStatusStatistics")
    public R<GunStatusStatisticsVO> gunStatusStatistics() {