AppService.java 5.71 KB
package com.ruoyi.service;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.driver.service.impl.DriverServiceImpl;
import com.ruoyi.equipment.domain.Equipment;
import com.ruoyi.equipment.mapper.EquipmentMapper;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.pojo.request.HeartPackageVo;
import com.ruoyi.pojo.response.ApplicationResponseVo;
import com.ruoyi.upgrade.domain.VersionUpgrade;
import com.ruoyi.upgrade.mapper.VersionUpgradeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Objects;

/**
 * 应用service
 */
@Service
public class AppService {

    @Value("${api.apk.path}")
    String apkPath;

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private VersionUpgradeMapper versionUpgradeMapper;

    public ApplicationResponseVo checkVersionNum(Integer versionNum, String deviceId) {
        boolean result = DriverServiceImpl.doCheckDevice(deviceId);
        if (result) {
            return getApplicationResponseVoByDeviceId(versionNum);
        } else {
            return getApplicationResponseVo(versionNum);
        }
    }

    private ApplicationResponseVo getApplicationResponseVoByDeviceId(Integer versionNum) {
        // 比对版本  需要更新 判断是否需要强制更新
        VersionUpgrade vu = versionUpgradeMapper.queryLatestVersionNum(1);
        // 无需更新
        if (!Objects.isNull(vu) && vu.getVersionCode().equals(versionNum)) {
            return ApplicationResponseVo.ApplicationResponseVoBuild().appNoUpdate(versionNum);
        }
        // 更新判断
        return ApplicationResponseVo.ApplicationResponseVoBuild().appForceUpdate(vu.getVersionCode(), vu.getApkUrl());
    }

    private ApplicationResponseVo getApplicationResponseVo(Integer versionNum) {
        // 比对版本  需要更新 判断是否需要强制更新
        VersionUpgrade vu = versionUpgradeMapper.queryLatestVersionNum(2);
        // 无需更新
        if (!Objects.isNull(vu) && vu.getVersionCode().equals(versionNum)) {
            return ApplicationResponseVo.ApplicationResponseVoBuild().appNoUpdate(versionNum);
        }
        // 更新判断
        return ApplicationResponseVo.ApplicationResponseVoBuild().appForceUpdate(vu.getVersionCode(), vu.getApkUrl());
    }

    public void downloadApk(String url, HttpServletResponse response) throws IOException {
        // 把文件写入响应体
        String realFileName = url.substring(url.indexOf("_") + 1);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        FileUtils.setAttachmentResponseHeader(response, realFileName);
        FileUtils.writeBytes(RuoYiConfig.getUploadPath() + url, response.getOutputStream());
    }

    public AjaxResult uploadApk(MultipartFile file) throws IOException {
        // 校验文件
        checkFile(file);
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath() + apkPath;
        // 上传并返回新文件名称
        String fileName = "app.apk";
        // 获取相对路径
        String absPath = FileUploadUtils.getAbsoluteFile(filePath, fileName).getAbsolutePath();
        // 上传文件
        file.transferTo(Paths.get(absPath));
        fileName = FileUploadUtils.getPathFileName(filePath, fileName);
        // 返回url
        String url = serverConfig.getUrl() + fileName;
        AjaxResult ajax = AjaxResult.success();
        ajax.put("url", url);
        ajax.put("fileName", fileName);
        ajax.put("newFileName", FileUtils.getName(fileName));
        ajax.put("originalFilename", file.getOriginalFilename());
        return ajax;
    }

    private void checkFile(MultipartFile file) {

        if (StringUtils.isEmpty(file.getOriginalFilename())) {
            throw new RuntimeException("不是apk文件请重新上传");
        }
        if (!file.getOriginalFilename().endsWith(".apk")) {
            throw new RuntimeException("不是apk文件请重新上传");
        }
    }

    public void checkAppHeart(HeartPackageVo vo) {
        Equipment equipment = new Equipment();
        equipment.setLastHeartRes(new Date());
        equipment.setDeviceId(vo.getDeviceId());
        equipment.setRemark("版本号:" + vo.getVersionNum());
        equipmentMapper.updateEquipmentByDeviceId(equipment);
    }

    public AjaxResult uploadApkOther(MultipartFile file)  throws IOException {
        // 校验文件
        checkFile(file);
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath() + apkPath + "/other";
        // 上传并返回新文件名称
        String fileName = "app.apk";
        // 获取相对路径
        String absPath = FileUploadUtils.getAbsoluteFile(filePath, fileName).getAbsolutePath();
        // 上传文件
        file.transferTo(Paths.get(absPath));
        fileName = FileUploadUtils.getPathFileName(filePath, fileName);
        // 返回url
        String url = serverConfig.getUrl() + fileName;
        AjaxResult ajax = AjaxResult.success();
        ajax.put("url", url);
        ajax.put("fileName", fileName);
        ajax.put("newFileName", FileUtils.getName(fileName));
        ajax.put("originalFilename", file.getOriginalFilename());
        return ajax;
    }
}