ThreadJobService.java 4.31 KB
package com.ruoyi.service;


import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.file.FileUploadException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.driver.mapper.DriverMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.Objects;


/**
 * 多线程任务
 *
 * @author 20412
 */
@EnableAsync
@Component
@Slf4j
public class ThreadJobService {

    @Autowired
    private DriverMapper driverMapper;



    /**
     * 异步上传图片
     *
     * @param url
     * @param base64
     * @throws FileUploadException
     * @throws IOException
     */
    @Async
    public void asyncStartUpload(String url, String base64) {
        FileOutputStream outputStream = null;
        base64 = base64.replaceAll(" ", "");
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] photoBase64 = decoder.decodeBuffer(base64);
            for (int i = 0; i < photoBase64.length; ++i) {
                //调整异常数据
                if (photoBase64[i] < 0) {
                    photoBase64[i] += 256;
                }
            }
            outputStream = new FileOutputStream(url);
            outputStream.write(photoBase64);
            outputStream.flush();
        } catch (Exception e) {
            log.error("文件上传异常:{}", e.getMessage());
        } finally {
            try {
                if (!Objects.isNull(outputStream)) {
                    outputStream.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Async
    public void asyncUpdateDriver(String deviceId, List<String> jobCodes){
        // 获取员工信息
        // 加锁
        // 同步锁实现
        Integer result = driverMapper.insertDriverFace(deviceId, jobCodes);
        log.info("注册设备与员工关联完毕:{}",result);
    }

    /**
     * 检查文件类型
     *
     * @param base64ImgData
     * @return
     */
    public static String checkImageBase64Format(String base64ImgData) {
        byte[] b = Base64.getDecoder().decode(base64ImgData);
        String type = "";
        if (0x424D == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
            type = "bmp";
        } else if (0x8950 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
            type = "png";
        } else {
            type = "jpg";
        }
//        else if (0xFFD8 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
//            type = "jpg";
//        }
        return type;
    }

    /**
     * 获取相对路径名
     *
     * @param uploadDir
     * @param fileName
     * @return
     * @throws IOException
     */
    public File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
        File desc = new File(uploadDir + File.separator + fileName);

        if (!desc.exists()) {
            if (!desc.getParentFile().exists()) {
                desc.getParentFile().mkdirs();
            }
        }
        return desc;
    }

    /**
     * 获取扩展文件名
     *
     * @param extendFileName
     * @return
     */
    public String extractFilename(String extendFileName) {
        return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
                FilenameUtils.getBaseName(UUID.randomUUID().toString().replace("-", "")), Seq.getId(Seq.uploadSeqType), extendFileName);
    }

    /**
     * @param uploadDir
     * @param fileName
     * @return
     * @throws IOException
     */
    public String getPathFileName(String uploadDir, String fileName) throws IOException {
        int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
        String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
        return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
    }



}