KeyBoxController.java 17.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
package com.ruoyi.controller.dss;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.ConstDriverProperties;
import com.ruoyi.common.TipEnum;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ResponseResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.caiinfo.CarInfo;
import com.ruoyi.domain.driver.NewDriver;
import com.ruoyi.domain.dss.key.box.dto.KeyBasicSyncDTO;
import com.ruoyi.domain.dss.key.box.dto.SkipOperationDTO;
import com.ruoyi.domain.dss.key.box.dto.TakeKeyDTO;
import com.ruoyi.domain.dss.key.box.vo.*;
import com.ruoyi.domain.dss.key.location.dto.WorkPlateV2DTO;
import com.ruoyi.domain.dss.key.location.vo.WorkPlateV2Vo;
import com.ruoyi.domain.equipment.linke.log.LingangEquipmentLinkeLog;
import com.ruoyi.domain.key.info.KeyInfo;
import com.ruoyi.domain.key.info.box.dto.KeyBoxQueryDTO;
import com.ruoyi.domain.key.info.box.vo.KeyBoxVo;
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.driver.NewDriverService;
import com.ruoyi.service.dss.KeyBoxVoService;
import com.ruoyi.service.equipment.linke.log.LingangEquipmentLinkeLogService;
import com.ruoyi.service.key.info.KeyInfoService;
import com.ruoyi.service.key.location.LinggangKeyWorkLocationService;
import com.ruoyi.service.scheduling.LinggangSchedulingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
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.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author liujun
 * @date 2024年06月25日 13:04
 */
@RestController
@RequestMapping("/dss")
@Api(tags = "【蓝斯一期】钥匙信息")
public class KeyBoxController extends BaseController {

    @Autowired
    private LingangEquipmentLinkeLogService lingangEquipmentLinkeLogService;

    @Autowired
    private KeyBoxVoService keyBoxVoService;
    @Autowired
    private LinggangKeyWorkLocationService linggangKeyWorkLocationService;
    @Autowired
    private LinggangSchedulingService schedulingService;
    @Autowired
    private KeyInfoService keyInfoService;
    @Autowired
    private CarInfoService carInfoService;
    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private NewDriverService newDriverService;

    @PostMapping(value = "/keybox/findKey")
    @ApiOperation("钥匙信息查询")
    public ResponseResult<KeyBoxVo> listSelect(@Valid @RequestBody KeyBoxQueryDTO request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.error(bindingResult.getFieldError().getDefaultMessage());
        }

        LingangEquipmentLinkeLog linkeLog = saveLog(request);

        return keyBoxVoService.listSelect(request, linkeLog);
    }

    @PostMapping(value = "/Driver/workPlateV2")
    @ApiOperation("司机获取当前工作的车辆钥匙信息")
    public ResponseResult<WorkPlateV2Vo> workPlateV2(@Valid @RequestBody WorkPlateV2DTO dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.error(bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            LinggangScheduling scheduling = queryScheduling(dto);
            if (Objects.isNull(scheduling)) {
                return ResponseResult.error(TipEnum.TIP_3.getCode(), TipEnum.TIP_3.getMsg());
            }
            LinggangKeyWorkLocation workLocation = queryKeyWorkLocation(scheduling.getKeyInfoId(), scheduling.getScheduleDate(), dto);
            if (Objects.isNull(workLocation)) {
                return ResponseResult.error(TipEnum.TIP_4.getCode(), TipEnum.TIP_4.getMsg());
            }

            KeyInfo keyInfo = queryKeyInfo(scheduling.getKeyInfoId());
            if (Objects.isNull(keyInfo)) {
                return ResponseResult.error404();
            }

            CarInfo carInfo = queryCarInfo(scheduling.getNbbm());
            if (Objects.isNull(carInfo)) {
                return ResponseResult.error404();
            }

            WorkPlateV2Vo vo = convertWorkPlateV2Vo(scheduling, workLocation, keyInfo, carInfo);
            return ResponseResult.success(vo);
        } catch (ParseException e) {
            logger.error("司机获取当前工作的车辆钥匙信息异常:[{}]", dto, e);
        }

        return ResponseResult.error();

    }

    @ApiOperation(value = "18.钥匙柜基础信息同步")
    @PostMapping(value = "/Device/BasicSync")
    public ResponseResult<BasicSyncVo> basicSync(@Valid @RequestBody KeyBasicSyncDTO dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.error(bindingResult.getFieldError().getDefaultMessage());
        }

        try {
            List<LinggangKeyWorkLocation> workLocations = queryKeyWorkLocation(dto);
            if (Objects.isNull(workLocations)) {
                logger.info("根据DTO的数据无法查询钥匙存放信息:[{}]", dto);
                return ResponseResult.error404();
            }

            Set<Long> schedulingIds = workLocations.stream().map(LinggangKeyWorkLocation::getSchedulingId).collect(Collectors.toSet());
            List<LinggangScheduling> schedulings = queryScheduling(schedulingIds);
            if (CollectionUtils.isEmpty(schedulings)) {
                logger.info("根据DTO的数据无法查询排班信息:[{}]", dto);
                return ResponseResult.error404();
            }

            Equipment equipment = queryEquipment(dto.getDevice());
            if (Objects.isNull(equipment)) {
                logger.info("根据DTO的数据无法查询设备信息:[{}]", dto);
                return ResponseResult.error404();
            }

            Set<Integer> keyInfoIds = workLocations.stream().map(LinggangKeyWorkLocation::getKeyInfoId).collect(Collectors.toSet());
            List<KeyInfo> keyInfos = queryKeyInfos(keyInfoIds);
            if (CollectionUtils.isEmpty(keyInfos)) {
                logger.info("根据DTO的数据无法查询钥匙信息:[{}]", dto);
                return ResponseResult.error404();
            }

            Set<String> jobCodes = schedulings.stream().map(LinggangScheduling::getJobCode).collect(Collectors.toSet());
            List<NewDriver> drivers = queryDrive(jobCodes);
            if (CollectionUtils.isEmpty(drivers)) {
                logger.info("根据DTO的数据无法查询司机信息:[{}]", dto);
                return ResponseResult.error404();
            }

            Set<String> nbbms = schedulings.stream().map(LinggangScheduling::getNbbm).collect(Collectors.toSet());
            List<CarInfo> carInfos = queryCarInfo(nbbms);
            if (CollectionUtils.isEmpty(carInfos)) {
                logger.info("根据DTO的数据无法查询车辆信息:[{}]", dto);
                return ResponseResult.error404();
            }

            BasicSyncVo vo = convertBasicSyncVo(workLocations, schedulings, keyInfos, drivers, carInfos, equipment);
            return ResponseResult.success(vo);
        } catch (ParseException e) {
            logger.error("钥匙柜基础信息同步异常:[{}]", dto, e);
        }
        return ResponseResult.error();
    }

    @PostMapping(value = "/Driver/SkipOperation")
    @ApiOperation(value = "19.人员跳过钥匙柜操作")
    public ResponseResult<SkipOperationVo> skipOperation(@Valid SkipOperationDTO dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.error(bindingResult.getFieldError().getDefaultMessage());
        }
        Equipment equipment = queryEquipment(dto.getDevice());
        if (Objects.isNull(equipment)) {
            logger.info("根据DTO的数据无法查询设备信息:[{}]", dto);
            return ResponseResult.error404();
        }

        SkipOperationVo vo = convertSkipOperationVo(equipment);
        return ResponseResult.success(vo);
    }

    @PostMapping(value = "Driver/TakeKey")
    @ApiModelProperty(value = "20.人员领取钥匙")
    public ResponseResult<TakeKeyVo> takeKey(@Valid TakeKeyDTO dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.error(bindingResult.getFieldError().getDefaultMessage());
        }
        List<LinggangKeyWorkLocation> locations = convert(dto);
        ResponseResult<Boolean> responseResult = linggangKeyWorkLocationService.updateTakeKey(locations);
        if (Objects.isNull(responseResult)) {
            return ResponseResult.error();
        }
        TakeKeyVo vo = convertTakeKeyVo(dto, responseResult);

        return new ResponseResult<>(responseResult.getCode(), responseResult.getMsg(), vo);
    }

    /***
     * 查询排班
     * @author liujun
     * @date 2024/7/16 17:05
     * @param dto
     * @return com.ruoyi.domain.scheduling.LinggangScheduling
     */
    private LinggangScheduling queryScheduling(WorkPlateV2DTO dto) throws ParseException {
        LinggangScheduling scheduling = new LinggangScheduling();
        scheduling.setJobCode(dto.getStaffCode());

        String dateStr = DateUtils.YYYY_MM_DD.format(dto.getTime());
        scheduling.setScheduleDate(DateUtils.YYYY_MM_DD.parse(dateStr));
        String bcType = Objects.equals(0, dto.getEventType()) ? ConstDriverProperties.BC_TYPE_OUT : Objects.equals(1, dto.getEventType()) ? ConstDriverProperties.BC_TYPE_IN : "other";
        scheduling.setBcType(bcType);

        return schedulingService.getOne(scheduling);
    }

    private List<LinggangScheduling> queryScheduling(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return schedulingService.listByIds(ids);
    }


    /***
     * 根据排班时间和钥匙ID查询钥匙
     * @author liujun
     * @date 2024/7/16 17:05
     * @param keyId
     * @param scheduleDate
     * @param dto
     * @return com.ruoyi.domain.key.location.LinggangKeyWorkLocation
     */
    private LinggangKeyWorkLocation queryKeyWorkLocation(Integer keyId, Date scheduleDate, WorkPlateV2DTO dto) {
        LinggangKeyWorkLocation workLocation = new LinggangKeyWorkLocation();
        workLocation.setKeyInfoId(keyId);
        workLocation.setScheduleDate(scheduleDate);
        return linggangKeyWorkLocationService.getOne(workLocation);
    }


    private List<LinggangKeyWorkLocation> queryKeyWorkLocation(KeyBasicSyncDTO dto) throws ParseException {
        LinggangKeyWorkLocation workLocation = new LinggangKeyWorkLocation();

        String dateStr = DateUtils.YYYY_MM_DD.format(dto.getTime());
        workLocation.setScheduleDate(DateUtils.YYYY_MM_DD.parse(dateStr));
        workLocation.setDevice(dto.getDevice());
        workLocation.setDelFlag(Boolean.FALSE);

        return linggangKeyWorkLocationService.list(workLocation);
    }

    private KeyInfo queryKeyInfo(Integer keyId) {
        return keyInfoService.getById(keyId);
    }

    private List<KeyInfo> queryKeyInfos(Collection<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return keyInfoService.listByIds(ids);
    }

    private CarInfo queryCarInfo(String nbbm) {
        CarInfo carInfo = new CarInfo();
        carInfo.setNbbm(nbbm);

        return carInfoService.getOne(carInfo);
    }

    private Equipment queryEquipment(String deviceId) {
        return equipmentService.getOneByDeviceId(deviceId);
    }

    private List<NewDriver> queryDrive(Collection<String> jobCods) {
        if (CollectionUtils.isEmpty(jobCods)) {
            return Collections.emptyList();
        }
        return newDriverService.list(jobCods);
    }

    private List<CarInfo> queryCarInfo(Collection<String> nbbms) {
        if (CollectionUtils.isEmpty(nbbms)) {
            return Collections.emptyList();
        }
        return carInfoService.list(nbbms);
    }


    /***
     * 保存链接日志
     * @author liujun
     * @date 2024/6/25 15:24
     * @param request
     * @return com.ruoyi.domain.equipment.linke.log.LingangEquipmentLinkeLog
     */
    private LingangEquipmentLinkeLog saveLog(KeyBoxQueryDTO request) {
        LingangEquipmentLinkeLog linkeLog = new LingangEquipmentLinkeLog();
        linkeLog.setDevice(request.getDevice());
        linkeLog.setCreateTime(new Date());
        linkeLog.setPassingReferences(JSON.toJSONString(request));
        linkeLog.setUrl("/dss/keybox/findKey");

        lingangEquipmentLinkeLogService.save(linkeLog);
        return linkeLog;
    }

    private WorkPlateV2Vo convertWorkPlateV2Vo(LinggangScheduling scheduling, LinggangKeyWorkLocation workLocation, KeyInfo keyInfo, CarInfo carInfo) {
        WorkPlateV2Vo vo = new WorkPlateV2Vo();
        vo.setYardId(Convert.toStr(keyInfo.getYardId()));
//        vo.setYardName(keyInfo.getY)
        vo.setDevice(workLocation.getDevice());
        vo.setCabinetNo(workLocation.getCabinetNo());
        vo.setPlateNum(carInfo.getPlateNum());

        return vo;
    }

    private BasicSyncVo convertBasicSyncVo(List<LinggangKeyWorkLocation> workLocations, List<LinggangScheduling> schedulings,
                                           List<KeyInfo> keyInfos, List<NewDriver> drivers, List<CarInfo> carInfos, Equipment equipment) {
        BasicSyncVo vo = new BasicSyncVo();

        vo.setDevice(equipment.getDeviceId());
        vo.setDeviceType(equipment.getPromise());
        vo.setTime(workLocations.get(0).getScheduleDate());
        vo.setYardName(equipment.getSiteName());
//        vo.setKeyboxName()

        List<BasicSyncKeyboxVo> keybox = new ArrayList<>();
        BasicSyncKeyboxVo boxvo = new BasicSyncKeyboxVo();
        List<BasicSyncKeyboxKeyItemVo> keyItemVos = workLocations.stream().map(local -> {
            Optional<KeyInfo> optional = keyInfos.stream().filter(k -> Objects.equals(k.getId(), local.getKeyInfoId())).findFirst();
            String keyCode = optional.isPresent() ? optional.get().getName() : null;
            return new BasicSyncKeyboxKeyItemVo(local.getCabinetNo(), keyCode);

        }).collect(Collectors.toList());
        keybox.add(boxvo);
        boxvo.setKeyItem(keyItemVos);


        List<BasicSyncDriverWorkVo> driverWork = new ArrayList<>();
        BasicSyncDriverWorkVo basicSyncDriverWorkVo = new BasicSyncDriverWorkVo();

        List<BasicSyncDriverWorkDriverPlanVo> driverPlanVos = schedulings.stream().map(sc -> {
            BasicSyncDriverWorkDriverPlanVo driverPlanVo = new BasicSyncDriverWorkDriverPlanVo();
            Optional<NewDriver> optional = drivers.stream().filter(d -> Objects.equals(d.getJobCode(), sc.getJobCode())).findFirst();
            if (optional.isPresent()) {
                driverPlanVo.setDriverCode(optional.get().getIcCardCode());
            }

            driverPlanVo.setStaffCode(sc.getJobCode());

            BasicSyncDriverWorkDriverPlanTimePlateVo planTimePlateVo = new BasicSyncDriverWorkDriverPlanTimePlateVo();
            planTimePlateVo.setKey(sc.getScheduleDate());
            Optional<CarInfo> carInfoOptional = carInfos.stream().filter(c -> Objects.equals(c.getNbbm(), sc.getNbbm())).findFirst();
            if (optional.isPresent()) {
                planTimePlateVo.setValue(carInfoOptional.get().getPlateNum());
            }


            driverPlanVo.setTimePlate(planTimePlateVo);
            return driverPlanVo;
        }).collect(Collectors.toList());

        basicSyncDriverWorkVo.setDriverPlan(driverPlanVos);
        driverWork.add(basicSyncDriverWorkVo);

        vo.setKeybox(keybox);
        vo.setDriverWork(driverWork);

        return vo;
    }

    private SkipOperationVo convertSkipOperationVo(Equipment equipment) {
        SkipOperationVo vo = new SkipOperationVo();
        vo.setDevice(equipment.getDeviceId());
        vo.setTime(new Date());
        vo.setDeviceType(equipment.getPromise());
        vo.setResult(0);

        return vo;
    }

    private List<LinggangKeyWorkLocation> convert(TakeKeyDTO dto) {
        if (CollectionUtils.isEmpty(dto.getKeyItem())) {
            return Collections.emptyList();
        }

        return dto.getKeyItem().stream().map(item -> {
            LinggangKeyWorkLocation location = new LinggangKeyWorkLocation();
            location.setDevice(item.getDevice());
            location.setEventType(item.getState());
            location.setKey(item.getKey());
            location.setScheduleDate(dto.getTime());

            return location;
        }).collect(Collectors.toList());
    }

    private TakeKeyVo convertTakeKeyVo(TakeKeyDTO dto, ResponseResult<Boolean> result) {
        TakeKeyVo vo = new TakeKeyVo();
        vo.setDevice(dto.getDevice());
        vo.setTime(new Date());
        vo.setResult(result.isSuccess() ? 0 : 1);
        return vo;
    }
}