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

import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;

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.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.pojo.response.ResponseScheduling;
import com.ruoyi.utils.ConstDateUtil;
import com.ruoyi.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 static com.ruoyi.redispre.GlobalRedisPreName.DRIVER_SCHEDULING_PRE;

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


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

    @Autowired
    private RedisCache redisCache;

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

    /**
     * 查询驾驶员信息列表
     *
     * @param driver 驾驶员信息
     * @return 驾驶员信息
     */
    @Override
    public List<Driver> selectDriverList(Driver driver) {
        return driverMapper.selectDriverList(driver);
    }

    /**
     * 新增驾驶员信息
     *
     * @param driver 驾驶员信息
     * @return 结果
     */
    @Override
    public int insertDriver(Driver driver) {
        // 判断是否存在相同的工号数据  存在既更新
//        Driver driverById = driverMapper.selectDriverById(driver.getId());
//        if (driverById == null){
//
//        }
        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);
        // 优先从缓存中读取
        if (cacheMapValue != null && cacheMapValue.size() > 0) {
            return AjaxResult.success(cacheMapValue);
        }
        // 数据库中读取所有接收到的排班信息
        List<ResponseScheduling> responseSchedulings = driverMapper.getDriverSchedulingList(schedulingDate, jobCode);
        return AjaxResult.success(responseSchedulings);
    }

    @Override
    public void saveDriverScheduling(List<ResponseScheduling> schedulings) {
        List<List<ResponseScheduling>> lists = ListUtils.splitList(schedulings, 1000);
        for (List<ResponseScheduling> responseSchedulings : lists) {
            driverMapper.saveDriverScheduling(responseSchedulings);
        }
    }

    @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() + "/head/image";
        // 上传并返回新文件名称
        FileUploadUtils.assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        String fileName = file.getOriginalFilename();
        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;
    }
}