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

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ResponseResult;
import com.ruoyi.domain.OrderEntity;
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.location.LinggangKeyWorkLocation;
import com.ruoyi.domain.scheduling.LinggangScheduling;
import com.ruoyi.equipment.domain.Equipment;
import com.ruoyi.equipment.service.IEquipmentService;
import com.ruoyi.service.key.location.LinggangKeyWorkLocationService;
import com.ruoyi.service.scheduling.LinggangSchedulingService;
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;

    @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.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<LinggangKeyWorkLocation> linggangKeyWorkLocations = null;
        List<Equipment> equipments = null;

        if (0 < size) {
            Set<Long> schedulingIds = linggangSchedulings.stream().map(LinggangScheduling::getId).collect(Collectors.toSet());
            linggangKeyWorkLocations = linggangKeyWorkLocationService.listRecent(null, schedulingIds);
            if (CollectionUtils.isNotEmpty(linggangKeyWorkLocations)) {
                Set<String> deviceIds = linggangKeyWorkLocations.stream().map(LinggangKeyWorkLocation::getDevice).collect(Collectors.toSet());
                equipments = equipmentService.listNameAndIDBydeviceIds(deviceIds);
            }
        }
        RemindDriverReportVo reportVo = new RemindDriverReportVo();
        reportVo.setCount(linggangSchedulings.stream().filter(ls -> StringUtils.equalsAnyIgnoreCase(ls.getBcType(), "out")).count());

        if (CollectionUtils.isNotEmpty(linggangKeyWorkLocations)) {
            List<RemindKeyInfoLocalVo> remindKeyInfoLocalVoList = new ArrayList<>();
            for (int i = 0; i < size; i = i + 2) {
                RemindKeyInfoLocalVo localVo = new RemindKeyInfoLocalVo();
                localVo.setNbbm(linggangSchedulings.get(i).getNbbm());
                int index = i;

                Optional<LinggangKeyWorkLocation> optional = linggangKeyWorkLocations.stream().filter(kl -> Objects.equals(kl.getSchedulingId(), linggangSchedulings.get(index).getId())).findFirst();
                if (optional.isPresent()) {
                    localVo.setKeyInfoStatus(optional.get().getType());
                    localVo.setYardName(optional.get().getYardName());

                    if (CollectionUtils.isNotEmpty(equipments)) {
                        Optional<LinggangKeyWorkLocation> finalOptional = optional;
                        Optional<Equipment> equipmentOptional = equipments.stream().filter(e -> Objects.equals(e.getDeviceId(), finalOptional.get().getDevice())).findFirst();
                        equipmentOptional.ifPresent(equipment -> localVo.setEquipmentName(equipment.getName()));
                        if (i == 0) {

                        }
                    }

                    if (i + 1 < size) {
                        optional = linggangKeyWorkLocations.stream().filter(kl -> Objects.equals(kl.getSchedulingId(), linggangSchedulings.get(index).getId())).findFirst();
                        if (optional.isPresent()) {
                            localVo.setReturnYardName(optional.get().getYardName());
                            if (CollectionUtils.isNotEmpty(equipments)) {
                                Optional<LinggangKeyWorkLocation> finalOptional = optional;
                                Optional<Equipment> equipmentOptional = equipments.stream().filter(e -> Objects.equals(e.getDeviceId(), finalOptional.get().getDevice())).findFirst();
                                equipmentOptional.ifPresent(equipment -> localVo.setReturnEquipmentName(equipment.getName()));
                            }
                        }
                    }
                }
                remindKeyInfoLocalVoList.add(localVo);
            }
            Optional<LinggangScheduling> opt = linggangSchedulings.stream().filter(f -> Objects.nonNull(f.getSignInId())).max(Comparator.comparing(LinggangScheduling::getFcsjT));
            if (opt.isPresent()) {
                Optional<LinggangKeyWorkLocation> optional = linggangKeyWorkLocations.stream().filter(kl -> Objects.equals(kl.getSchedulingId(), opt.get().getId())).findFirst();
                if (optional.isPresent()) {
                    Optional<Equipment> equipmentOptional = equipments.stream().filter(e -> Objects.equals(e.getDeviceId(), optional.get().getDevice())).findFirst();
                    equipmentOptional.ifPresent(equipment -> reportVo.setCurrentKeyLocaltion(equipment.getName()));
                }
            } else {
                Optional<LinggangKeyWorkLocation> optional = linggangKeyWorkLocations.stream().filter(kl -> Objects.equals(kl.getSchedulingId(), linggangSchedulings.get(0).getId())).findFirst();
                if (optional.isPresent()) {
                    Optional<Equipment> equipmentOptional = equipments.stream().filter(e -> Objects.equals(e.getDeviceId(), optional.get().getDevice())).findFirst();
                    equipmentOptional.ifPresent(equipment -> reportVo.setCurrentKeyLocaltion(equipment.getName()));
                }
            }

            reportVo.setRemindKeyInfoLocalVoList(remindKeyInfoLocalVoList);
        }

        return ResponseResult.success(reportVo);
    }
}