ThreadJobService.java 5.86 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.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.driver.domain.Driver;
import com.ruoyi.driver.mapper.DriverMapper;
import com.ruoyi.job.DriverJob;
import com.ruoyi.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;


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

    @Autowired
    private DriverMapper driverMapper;

    @Value("${api.headImage}")
    private String headImagePre;


    /**
     * 异步上传图片
     *
     * @param url
     * @param base64
     * @throws FileUploadException
     * @throws IOException
     */
    @Async
    public void asyncStartUploadBase64Image(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();
            log.info("文件上传完毕");
        } catch (Exception e) {
            log.error("文件上传异常:{}", e.getMessage());
        } finally {
            try {
                if (!Objects.isNull(outputStream)) {
                    outputStream.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 签到人员与签到设备绑定
     *
     * @param deviceId
     * @param jobCodes
     */
    @Async
    public void asyncInsertSignInContactEquipment(String deviceId, List<String> jobCodes) {
        Integer result = driverMapper.insertDriverFace(deviceId, jobCodes);
        log.info("注册设备与员工关联完毕:{}", result);
    }

    @Async
    public void asyncUploadDriverWithUpdateImageUrl(List<Driver> drivers, String token) {
        // 插入数据
        for (Driver driver : drivers) {
            String imageUlr = driver.getImage();
            // 生成文件路径
            String fileName = driver.getJobCode() + ".png";
            String filePath = RuoYiConfig.getUploadPath() + headImagePre + "/" + fileName;
            try {
                fileName = FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath() + headImagePre, fileName);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            driver.setImage(fileName);
            // 插入数据  如果员工已经存在在不在下载图片
            int result = driverMapper.insertDriver(driver);
            log.debug("插入完毕");
            if (result == 0) {
                continue;
            }
            log.info("图片开始上传");
            // 获取图片请求地址
            String imageUrl = DriverJob.getDownloadImageUrl(token, imageUlr);
            // 获取图片数据
            InputStream is = HttpUtils.doGet(imageUrl, new HashMap<>());
            // 上传图片
            uploadImage(is, filePath);
            log.debug("图片上传完毕");
        }


    }

    private void uploadImage(InputStream is, String filePath) {
        File file = new File(filePath + File.separator);
        log.info("文件路径:{}",file.getPath());
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
        }
        // 保存图片到本地文件
        FileOutputStream fos = null;
        try {

            fos = new FileOutputStream(file);
            byte[] cbuf = new byte[1024];
            int len;
            while ((len = is.read(cbuf)) != -1) {
                fos.write(cbuf, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 检查文件类型
     *
     * @param b
     * @return
     */
    public static String checkImageBase64Format(byte[] b) {
        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;
    }


}