KeyBoxController.java 7.18 KB
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.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.service.carinfo.CarInfoService;
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.ApiOperation;
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.Date;
import java.util.Objects;


/**
 * @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;

    @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.error(TipEnum.TIP_404.getCode(), TipEnum.TIP_404.getMsg());
            }

            CarInfo carInfo = queryCarInfo(scheduling.getNbbm());
            if (Objects.isNull(carInfo)) {
                return ResponseResult.error(TipEnum.TIP_404.getCode(), TipEnum.TIP_404.getMsg());
            }

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

        return ResponseResult.error();

    }

    /***
     * 查询排班
     * @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);
    }

    /***
     * 根据排班时间和钥匙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 KeyInfo queryKeyInfo(Integer keyId) {
        return keyInfoService.getById(keyId);
    }

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

        return carInfoService.getOne(carInfo);
    }


    /***
     * 保存链接日志
     * @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;
    }
}