DriverServiceImpl.java 14.1 KB
package com.ruoyi.driver.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.http.HttpUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.global.Result;
import com.ruoyi.common.global.ResultCode;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.equipment.mapper.EquipmentMapper;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.job.DriverJob;
import com.ruoyi.pojo.domain.EquipmentDriverExpand;
import com.ruoyi.pojo.request.DriverSignInRequestVo;
import com.ruoyi.pojo.request.FaceUpdateReqVo;
import com.ruoyi.pojo.response.ResponseScheduling;
import com.ruoyi.service.ThreadJobService;
import com.ruoyi.utils.ConstDateUtil;
import com.ruoyi.utils.ListUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.tomcat.util.buf.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.driver.mapper.DriverMapper;
import com.ruoyi.driver.domain.Driver;
import com.ruoyi.driver.service.IDriverService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.ruoyi.common.ApiProperties.PERSONNEL_API_KEY;
import static com.ruoyi.common.global.ResultCode.CODE_207;
import static com.ruoyi.common.redispre.GlobalRedisPreName.DRIVER_SCHEDULING_PRE;

/**
 * 驾驶员信息Service业务层处理
 *
 * @author 古自健
 * @date 2023-07-04
 */
@Service
public class DriverServiceImpl implements IDriverService {

    private Logger log = LoggerFactory.getLogger(DriverServiceImpl.class);

    @Autowired
    private EquipmentMapper equipmentMapper;
    @Value("${api.url.getSchedulingInfo}")
    private String schedulingInfoUrl;
    @Value("${api.config.password}")
    private String password;
    @Value("${api.config.nonce}")
    private String nonce;
    @Autowired
    private DriverJob driverJob;

    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private ThreadJobService threadJobService;

    @Autowired
    private RedisCache redisCache;

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

    /**
     * 查询驾驶员信息
     *
     * @param id 驾驶员信息主键
     * @return 驾驶员信息
     */
    @Override
    public Driver selectDriverById(Long id) {
        return driverMapper.selectDriverById(id);
    }

    /**
     * 查询驾驶员信息列表
     *
     * @param driver 驾驶员信息
     * @return 驾驶员信息
     */
    @Override
    public List<Driver> selectDriverList(Driver driver) {
        List<Driver> drivers = driverMapper.selectDriverList(driver);
        List<EquipmentDriverExpand> list = equipmentMapper.querySignListByJobCode(drivers);
        for (Driver item : drivers) {
            // 查询对应工号的注册设备号 然后用,拼接展示在前端
            List<String> collect = list.stream().filter(todo -> item.getJobCode().equals(todo.getJobCode())).map(EquipmentDriverExpand::getDeviceId).collect(Collectors.toList());
            item.setSignInEquipment(StringUtils.join(collect));
        }
        return drivers;
    }

    /**
     * 新增驾驶员信息
     *
     * @param driver 驾驶员信息
     * @return 结果
     */
    @Override
    public int insertDriver(Driver driver) {
        return driverMapper.insertDriver(driver);
    }

    /**
     * 修改驾驶员信息
     *
     * @param driver 驾驶员信息
     * @return 结果
     */
    @Override
    public int updateDriver(Driver driver) {
        return driverMapper.updateDriver(driver);
    }

    /**
     * 批量删除驾驶员信息
     *
     * @param ids 需要删除的驾驶员信息主键
     * @return 结果
     */
    @Override
    public int deleteDriverByIds(Long[] ids) {
        return driverMapper.deleteDriverByIds(ids);
    }

    /**
     * 删除驾驶员信息信息
     *
     * @param id 驾驶员信息主键
     * @return 结果
     */
    @Override
    public int deleteDriverById(Long id) {
        return driverMapper.deleteDriverById(id);
    }

    @Override
    public void insertDrivers(List<Driver> driverList) {
        driverMapper.saveDrivers(driverList);
    }

    @Override
    public AjaxResult getDriverSchedulingInfo(String schedulingDate, String jobCode) {
        String key = DRIVER_SCHEDULING_PRE + schedulingDate;
        List<ResponseScheduling> cacheMapValue = redisCache.getCacheMapValue(key, jobCode);
        log.info("获取到排班数据:{}", cacheMapValue);
        // 优先从缓存中读取
        if (cacheMapValue != null && cacheMapValue.size() > 0) {

            return AjaxResult.success(cacheMapValue);
        }
        // 没有再次请求地址获取值
        long timestamp = System.currentTimeMillis();
        // 请求资源地址格式化
        String getSchedulingInfoUrl = null;
        try {
            getSchedulingInfoUrl = String.format(schedulingInfoUrl, "99", schedulingDate, timestamp, nonce, password, driverJob.getSHA1(driverJob.getStringStringMap(String.valueOf(timestamp))));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Map<String, List<ResponseScheduling>> scheduling = driverJob.saveSchedulingToRedis(getSchedulingInfoUrl, schedulingDate);
        // 数据库中读取所有接收到的排班信息
//        List<ResponseScheduling> responseSchedulings = driverMapper.getDriverSchedulingList(schedulingDate, jobCode);
        if (jobCode.equals("700001") || jobCode.equals("700002")) {
            return AjaxResult.success(scheduling.get("724228"));
        }
        return AjaxResult.success(scheduling.get(jobCode));
    }


    @Override
    public AjaxResult getDriverSchedulingAll() {
        return AjaxResult.success(redisCache.getHashKeys(DRIVER_SCHEDULING_PRE + ConstDateUtil.formatDate("yyyyMMdd")));
    }

    @Override
    public AjaxResult checkJobCode(Driver driver) {
        Integer result = driverMapper.jobCodeIsEmpty(driver.getJobCode());
        Map<String, Boolean> resultMap = new HashMap<>();
        if (result > 0) {
            resultMap.put("result", true);
        } else {
            resultMap.put("result", false);
        }
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult uploadImage(MultipartFile file) throws InvalidExtensionException, IOException {
        // 上传并返回新文件名称

        // 上传文件路径
        String baseUrl = RuoYiConfig.getUploadPath() + headImagePre;
        // 校验文件格式
        FileUploadUtils.assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        // 后期可以通过请求头拿到对应的工号
        String fileName = FilenameUtils.getBaseName(file.getOriginalFilename()) + "." + FileUploadUtils.getExtension(file);
        String absPath = FileUploadUtils.getAbsoluteFile(baseUrl, fileName).getAbsolutePath();
        fileName = FileUploadUtils.getPathFileName(baseUrl, fileName);
        file.transferTo(Paths.get(absPath));
        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;
    }

    @Override
    public void downloadHeadImage(String jobCode, HttpServletResponse response) {
        File file = getLocationFile(jobCode);
        ServletOutputStream ops = null;
        try {
            if (file.exists()) {
                byte[] bytes = com.alibaba.excel.util.FileUtils.readFileToByteArray(file);
                ops = response.getOutputStream();
                ops.write(bytes);
                ops.flush();
            }
        } catch (Exception e) {
            throw new RuntimeException("download fail cause:" + e.getMessage());
        } finally {
            try {
                if (ops != null) {
                    ops.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public List<Driver> getDrivers(Driver driver) {
        // TODO 人事比对
        List<Driver> drivers = driverMapper.getDrivers(driver);
        return drivers;
    }


    @Override
    public AjaxResult faceRegistrationFeedback(String deviceId, List<String> jobCodes) {
        threadJobService.asyncInsertSignInContactEquipment(deviceId, jobCodes);
        return AjaxResult.success("注册成功");
    }

    @Override
    public void updateDriverByComputed() {
        Integer count = equipmentMapper.count();
        // 更新人脸认证状态
        driverMapper.updateDriverByComputed(count);
    }

    @Override
    public Result<?> updateFaceByJob(HttpServletRequest request, FaceUpdateReqVo vo) {
        // 获取校验
        String header = request.getHeader("X-TOKEN-AUTHORIZATION");
        if (!PERSONNEL_API_KEY.equals(header)) {
            return Result.ERROR(ResultCode.CODE_401, "X-TOKEN-AUTHORIZATION value error");
        }
        Date date = new Date();
        // 查询当前人员信息库是否存在对应员工数据
        List<String> jobCodes = vo.getFaceDataList().stream().map(FaceUpdateReqVo.FaceData::getJobCode).collect(Collectors.toList());
        Map<String,String> existsSet = driverMapper.queryJobCodesIsExists(jobCodes).stream().collect(Collectors.toMap(item ->item,s->s));
        // 更新失败人员工号列表
        List<String> updateFailJobCodes = new ArrayList<>();
        for (String jobCode : jobCodes) {
            if (Objects.isNull(existsSet.get(jobCode))) {
                updateFailJobCodes.add(jobCode);
            }
        }
        jobCodes = new ArrayList<>(existsSet.values());

        List<Driver> drivers = vo.getFaceDataList().stream().map(item -> {
            Driver driver = new Driver();
            driver.setPosts(item.getPosts());
            driver.setJobCode(item.getJobCode());
            try {
                driver.setImage(getNewImageUrl(item.getJobCode(), item.getImageUrl()));
            } catch (IOException e) {
                log.info(e.getMessage());
                throw new RuntimeException(e);
            }
            driver.setPersonnelName(item.getName());
            driver.setUpdateTime(date);
            return driver;
        }).filter(item -> !Objects.isNull(existsSet.get(item.getJobCode()))).collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(s -> s.getJobCode()))), ArrayList::new));
        List<List<Driver>> lists = ListUtils.splitList(drivers, 400);
        log.info("开始人脸数据更新");
        // 数据更新 更新人脸注册标识 更新基本数据
        for (List<Driver> list : lists) {
            driverMapper.updateSignStatusDriversByJobCodes(list);
        }
        log.info("人脸数据更新完成");
        log.info("未成功更新工号:{}", updateFailJobCodes);
        log.info("开始删除注册表中对应工号的数据");
        driverMapper.deleteDeviceIdAssociatedJobCode(jobCodes);
        log.info("删除注册表中对应工号的数据结束");
        if (updateFailJobCodes.size() != 0)
            return Result.OK(CODE_207, updateFailJobCodes);
        else
            return Result.OK();
    }

    @Override
    public void saveDriverSignInfo(DriverSignInRequestVo vo) {
        Driver driver = new Driver();
        driver.setJobCode(vo.getJobCode());
        driver.setPersonnelName(vo.getName());
        driver.setPosts(vo.getPosts());
        String base64 = vo.getImage();
        String fileName = vo.getJobCode() + ".png";
        String filePath = new File(RuoYiConfig.getUploadPath() + headImagePre + "/" + fileName + File.separator).getAbsolutePath();
        try {
            fileName = FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath() + headImagePre, fileName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        driver.setImage(fileName);
        driver.setUpdateTime(new Date());
        int result = driverMapper.insertDriver(driver);
        if (result == 0) driverMapper.updateSignStatusDriversByJobCodes(new ArrayList<>(Arrays.asList(driver)));
        log.info("开始上传签到图片");
        // 异步上传文件
        threadJobService.asyncStartUploadBase64Image(filePath, base64);
    }

    @Override
    public void updateDrivers(List<Driver> drivers) {
        driverMapper.updateDriverInfoByJobCodes(drivers);
    }

    /**
     * 获取新的imageUrl
     *
     * @param jobCode
     * @param url
     * @return
     */
    private String getNewImageUrl(String jobCode, String url) throws IOException {
        // 生成文件路径
        String fileName = jobCode + ".png";
        String filePath = RuoYiConfig.getUploadPath() + headImagePre + "/" + fileName;
        try {
            fileName = FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath() + headImagePre, fileName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        log.debug("图片开始上传");
        // 获取图片数据
        InputStream is = HttpUtil.createGet(url).execute().bodyStream();
        // 上传图片
        ThreadJobService.uploadImage(is, filePath);
        log.debug("图片上传完毕");
        return fileName;
    }

    private File getLocationFile(String jobCode) {
        String image = driverMapper.getDriverImageByJobCode(jobCode);

        return new File(RuoYiConfig.getProfile() + File.separator + image.replace("/profile", ""));
    }
}