RemindDriverKeyLocalController.java 9.29 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()));
                    }
                }
                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);
    }

    private void setReturnInfo(LinggangKeyWorkLocation sourceKL200, RemindKeyInfoLocalVo localVo) {
        if (Objects.isNull(sourceKL200) || Objects.isNull(localVo)) {
            return;
        }
        Equipment equipment = equipmentService.getOneByDeviceId(sourceKL200.getDevice());
        if (Objects.isNull(equipment)) {
            return;
        }
        localVo.setReturnEquipmentName(equipment.getName());
        localVo.setReturnYardName(sourceKL200.getYardName());
    }
}