RemindDriverKeyLocalController.java 11 KB
package com.ruoyi.controller.dss;

import cn.hutool.core.convert.Convert;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ResponseResult;
import com.ruoyi.domain.OrderEntity;
import com.ruoyi.domain.caiinfo.CarInfo;
import com.ruoyi.domain.dss.key.location.vo.RemindKeyInfoLocalVo;
import com.ruoyi.domain.dss.scheling.dto.RemindDriverReportDTO;
import com.ruoyi.domain.dss.scheling.vo.RemindDriverReportVo;
import com.ruoyi.domain.key.info.KeyInfo;
import com.ruoyi.domain.key.location.LinggangKeyWorkLocation;
import com.ruoyi.domain.scheduling.LinggangScheduling;
import com.ruoyi.equipment.domain.Equipment;
import com.ruoyi.equipment.service.IEquipmentService;
import com.ruoyi.service.carinfo.CarInfoService;
import com.ruoyi.service.key.info.KeyInfoService;
import com.ruoyi.service.key.location.LinggangKeyWorkLocationService;
import com.ruoyi.service.scheduling.LinggangSchedulingService;
import com.ruoyi.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liujun
 * @date 2024年10月09日 15:48
 */
@Slf4j
@RestController
@RequestMapping("/dss")
@Api(tags = "【对接】钥匙提醒功能")
public class RemindDriverKeyLocalController extends BaseController {

    @Autowired
    private LinggangSchedulingService linggangSchedulingService;
    @Autowired
    private LinggangKeyWorkLocationService linggangKeyWorkLocationService;
    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private CarInfoService carInfoService;
    @Autowired
    private KeyInfoService keyInfoService;

    @ApiOperation("司机提醒信息")
    @PostMapping(value = "/remind/driver/key/local/report")
    public ResponseResult<RemindDriverReportVo> remindDriverKeyLocalReport(@Valid @RequestBody RemindDriverReportDTO request, BindingResult bindingResult) {
        // 检查参数绑定结果,如果有错误则返回错误信息
        if (bindingResult.hasErrors()) {
            return ResponseResult.error(bindingResult.getFieldError().getDefaultMessage());
        }

        // 创建并初始化冷柜调度对象
        LinggangScheduling scheduling = new LinggangScheduling();
        scheduling.setJobCode(request.getJobCode());
        scheduling.setScheduleDate(request.getDate());
        scheduling.setStartScheduleDate(request.getDate());
        scheduling.setEndScheduleDate(DateUtils.addDays(request.getDate(), 1));

        // 创建并初始化订单实体对象,用于排序
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrder("ascending");
        orderEntity.setProp("fcsjT");

        // 查询符合条件的冷柜调度列表
        List<LinggangScheduling> linggangSchedulings = linggangSchedulingService.list(scheduling, orderEntity);
        int size = CollectionUtils.size(linggangSchedulings);

        // 初始化钥匙信息和车辆信息列表
        List<KeyInfo> keyInfos = null;
        List<CarInfo> carInfos = null;

        // 如果调度列表不为空,根据调度信息获取车辆信息
        if (0 < size) {
            Set<String> nbbms = linggangSchedulings.stream().map(LinggangScheduling::getNbbm).collect(Collectors.toSet());
            carInfos = carInfoService.list(nbbms);
        }

        // 如果车辆信息不为空,根据车辆信息获取钥匙信息
        if (CollectionUtils.isNotEmpty(carInfos)) {
            Set<String> plateNums = carInfos.stream().map(CarInfo::getPlateNum).collect(Collectors.toSet());
            keyInfos = keyInfoService.listPlateNums(plateNums);
        }

        // 计算车辆和钥匙信息的大小
        int carSize = CollectionUtils.size(carInfos);
        int keyInfoSize = CollectionUtils.size(keyInfos);

        // 创建并初始化提醒司机报告视图对象
        RemindDriverReportVo reportVo = new RemindDriverReportVo();
        List<LinggangScheduling> values = linggangSchedulings.stream().filter(ls -> StringUtils.equalsAnyIgnoreCase(ls.getBcType(), "out")).collect(Collectors.toList());
        size = CollectionUtils.size(values);
        reportVo.setCount(Convert.toLong(size));

        // 如果调度列表不为空,处理每个调度信息,生成提醒信息列表
        if (size > 0) {
            List<RemindKeyInfoLocalVo> remindKeyInfoLocalVoList = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                RemindKeyInfoLocalVo localVo = new RemindKeyInfoLocalVo();
                localVo.setNbbm(values.get(i).getNbbm());

                // 初始化工作位置对象
                LinggangKeyWorkLocation workLocation = new LinggangKeyWorkLocation();
                if (carSize > 0 && keyInfoSize > 0) {
                    int index = i;
                    Optional<CarInfo> optionalCarInfo = carInfos.stream().filter(c -> Objects.equals(c.getNbbm(), values.get(index).getNbbm())).findFirst();
                    if (optionalCarInfo.isPresent()) {
                        Optional<KeyInfo> optional = keyInfos.stream().filter(k -> Objects.equals(k.getPlateNum(), optionalCarInfo.get().getPlateNum())).findFirst();
                        optional.ifPresent(k -> workLocation.setKeyInfoId(k.getId()));
                    }
                }

                // 根据钥匙ID和时间获取工作位置信息
                List<LinggangKeyWorkLocation> keyWorkLocations = null;
                if (Objects.nonNull(workLocation.getKeyInfoId())) {
                    workLocation.setMaxCreateDate(new Date());
                    keyWorkLocations = linggangKeyWorkLocationService.getTenByKeyIdAndTime(workLocation);
                }

                // 处理工作位置信息,确定钥匙状态和相关设备信息
                if (CollectionUtils.isNotEmpty(keyWorkLocations)) {
                    int klSize = CollectionUtils.size(keyWorkLocations);
                    LinggangKeyWorkLocation sourceKL200 = null;
                    LinggangKeyWorkLocation sourceKL1 = null;
                    LinggangKeyWorkLocation sourceKL0 = null;
                    for (int j = 0; j < klSize; j++) {
                        if (Objects.equals(keyWorkLocations.get(j).getType1(), 2) && Objects.isNull(sourceKL200)) {
                            sourceKL200 = keyWorkLocations.get(j);
                            sourceKL200.setIndex(j);
                        } else if (Objects.equals(keyWorkLocations.get(j).getType1(), 1) && Objects.isNull(sourceKL1)) {
                            sourceKL1 = keyWorkLocations.get(j);
                            sourceKL1.setIndex(j);
                        } else if (Objects.equals(keyWorkLocations.get(j).getType1(), 0) && Objects.isNull(sourceKL0)) {
                            sourceKL0 = keyWorkLocations.get(j);
                            sourceKL0.setIndex(j);
                        } else if (Objects.nonNull(sourceKL200) && Objects.nonNull(sourceKL0) && Objects.nonNull(sourceKL1)) {
                            break;
                        }
                    }

                    // 根据工作位置信息设置提醒信息
                    if (Objects.nonNull(sourceKL0) && Objects.nonNull(sourceKL1) && Objects.nonNull(sourceKL0.getIndex()) && Objects.nonNull(sourceKL1.getIndex())) {
                        if (sourceKL0.getIndex() > sourceKL1.getIndex()) {
                            localVo.setKeyInfoStatus(0);
                            Equipment equipment = equipmentService.getOneByDeviceId(sourceKL1.getDevice());
                            if (Objects.nonNull(equipment)) {
                                localVo.setEquipmentName(equipment.getName());
                                localVo.setYardName(sourceKL0.getYardName());
                            }

                        } else {
                            localVo.setKeyInfoStatus(1);
                        }
                    } else if (Objects.nonNull(sourceKL1) && Objects.nonNull(sourceKL1.getIndex())) {
                        localVo.setKeyInfoStatus(0);
                        Equipment equipment = equipmentService.getOneByDeviceId(sourceKL1.getDevice());
                        if (Objects.nonNull(equipment)) {
                            localVo.setEquipmentName(equipment.getName());
                            localVo.setYardName(equipment.getYardName());
                        }

                    } else if (Objects.nonNull(sourceKL0) && Objects.nonNull(sourceKL0.getIndex())) {
                        localVo.setKeyInfoStatus(1);
                    } else if (Objects.nonNull(sourceKL200) && Objects.nonNull(sourceKL200.getIndex())) {
                        localVo.setKeyInfoStatus(16);
                    } else {
                        localVo.setKeyInfoStatus(255);
                    }
                    setReturnInfo(sourceKL200, localVo);
                    remindKeyInfoLocalVoList.add(localVo);
                }
            }

            // 设置报告视图对象的提醒信息列表和当前位置信息
            if(CollectionUtils.isNotEmpty(remindKeyInfoLocalVoList)) {
                reportVo.setRemindKeyInfoLocalVoList(remindKeyInfoLocalVoList);
                reportVo.setCurrentKeyLocaltion(remindKeyInfoLocalVoList.get(0).getEquipmentName());
            }
        }

        // 返回成功响应结果
        return ResponseResult.success(reportVo);
    }

    /**
     * 设置返回信息
     * 此方法用于将设备信息和场地名称设置到本地提醒信息对象中
     *
     * @param sourceKL200      来自KL200的源数据对象,包含设备ID和场地名称等信息
     * @param localVo          本地提醒信息对象,用于存储返回的设备名称和场地名称
     */
    private void setReturnInfo(LinggangKeyWorkLocation sourceKL200, RemindKeyInfoLocalVo localVo) {
        // 检查输入对象是否为空,如果任一对象为空,则不执行后续操作
        if (Objects.isNull(sourceKL200) || Objects.isNull(localVo)) {
            return;
        }

        // 根据设备ID获取设备信息
        Equipment equipment = equipmentService.getOneByDeviceId(sourceKL200.getDevice());

        // 检查是否找到对应的设备信息,如果未找到,则不执行后续操作
        if (Objects.isNull(equipment)) {
            return;
        }

        // 设置本地提醒信息对象的返回设备名称
        localVo.setReturnEquipmentName(equipment.getName());

        // 设置本地提醒信息对象的返回场地名称
        localVo.setReturnYardName(sourceKL200.getYardName());
    }
}