package com.sckw.fleet.dubbo;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sckw.core.exception.BusinessException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.fleet.api.RemoteFleetService;
import com.sckw.fleet.api.model.dto.RUpdateDriverScoreDto;
import com.sckw.fleet.api.model.vo.*;
import com.sckw.fleet.dao.KwfDriverMapper;
import com.sckw.fleet.dao.KwfTruckMapper;
import com.sckw.fleet.model.*;
import com.sckw.fleet.model.enums.TruckTypeEnum;
import com.sckw.fleet.repository.*;
import com.sckw.fleet.service.KwfDriverScoreService;
import com.sckw.fleet.service.KwfDriverService;
import com.sckw.system.api.RemoteSystemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zk
 * @desc 车队服务
 * @date 2023/7/18 0018
 */
@DubboService(group = "design", version = "1.0.0")
@Service
@RequiredArgsConstructor
@Slf4j
public class RemoteFleetServiceImpl implements RemoteFleetService {

    @Autowired
    KwfTruckMapper truckDao;
    @Autowired
    KwfDriverMapper driverDao;
    private final KwfFleetRepository fleetRepository;
    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;

    private final KwfTruckRepository kwfTruckRepository;
    private final KwfDriverRepository driverRepository;
    private final KwfFleetDriverRepository kwfFleetDriverRepository;
    private final KwfFleetTruckRepository kwfFleetTruckRepository;
    private final KwfTruckRouteRepository kwfTruckRouteRepository;
    private final KwfTruckAxleNumRepository kwfTruckAxleNumRepository;
    private final KwfFleetRepository kwfFleetRepository;
    private final KwfTruckDispatchCoefficientRepository truckDispatchCoefficientRepository;
    private final KwfDriverConductRulesRepository driverConductRulesRepository;
    private final KwfTruckAxleNumRepository truckAxleNumRepository;
    private final KwfDriverScoreRepository driverScoreRepository;
    private final KwfDriverScoreService driverScoreService;
    private final KwfDriverService driverService;
    private final KwfTruckReportRepository truckReportRepository;
    private final KwfTruckEntRepository truckEntRepository;


    /**
     * @param driverIds 司机档案主键id，多个已逗号隔开
     * @desc 查询司机信息
     * @author zk
     * @date 2023/7/18
     **/
    @Override
    public Map<Long, RDriverVo> findDriver(List<Long> driverIds) {
        /**获取数据**/
        Map<Long, RDriverVo> driverMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
        List<KwfDriver> drivers = driverDao.findDriver(new HashMap<>(Global.NUMERICAL_SIXTEEN) {{
            put("ids", driverIds);
        }});
        if (CollectionUtils.isEmpty(drivers)) {
            return driverMap;
        }

        /**数据处理**/
        for (KwfDriver driver : drivers) {
            RDriverVo driverVo = new RDriverVo();
            driverVo.setId(driver.getId());
            driverVo.setName(driver.getName());
            driverVo.setPhone(driver.getPhone());
            driverVo.setIdcard(driver.getIdcard());
            driverVo.setTotalComplete(driver.getTotalComplete());
            driverVo.setTotalTake(driver.getTotalTake());
            driverVo.setTotalWeight(driver.getTotalWeight());
            driverVo.setBusinessStatus(driver.getBusinessStatus());
            driverMap.put(driverVo.getId(), driverVo);
        }
        return driverMap;
    }

    /**
     * @param driverId 司机档案主键id
     * @desc 查询司机信息
     * @author zk
     * @date 2023/8/3
     **/
    @Override
    public RDriverVo findDriver(Long driverId) {
        RDriverVo driverVo = new RDriverVo();
        KwfDriver driver = driverDao.selectById(driverId);
        BeanUtils.copyProperties(driver, driverVo);
        return driverVo;
    }

    /**
     * @param phone 司机档手机号
     * @desc 查询司机信息
     * @author zk
     * @date 2023/8/10
     **/
    @Override
    public RDriverDetailVo findDriverDetai(String phone) {
        List<KwfDriver> drivers = driverDao.findDriver(new HashMap<>(Global.NUMERICAL_SIXTEEN) {{
            put("phone", phone);
        }});
        if (CollectionUtils.isEmpty(drivers)) {
            return null;
        }
        KwfDriver driver = drivers.get(0);
        RDriverDetailVo driverDetail = new RDriverDetailVo();
        //BeanUtils.copyPropertiesValue(driver, driverDetail);
        BeanUtils.copyProperties(driver, driverDetail);
        return driverDetail;
    }

    /**
     * @param params 司机档案信息
     * @desc 更新司机信息
     * @author zk
     * @date 2023/8/3
     **/
    @Override
    public int updateById(RDriverVo params) {
        KwfDriver driver = new KwfDriver();
        BeanUtils.copyProperties(params, driver);
        return driverDao.updateById(driver);
    }

    /**
     * @param truckNos 车牌号，多个已逗号隔开
     * @desc 查询车辆信息
     * @author zk
     * @date 2023/7/18
     **/
    @Override
    public Map<String, RTruckVo> findTruck(List<String> truckNos) {
        /**获取数据**/
        Map<String, RTruckVo> truckMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
        List<KwfTruck> trucks = truckDao.findTruck(new HashMap<>(Global.NUMERICAL_SIXTEEN){{put("truckNos", truckNos);}});
        if (CollectionUtils.isEmpty(trucks)) {
            return truckMap;
        }

        /**数据处理**/
        for (KwfTruck truck : trucks) {
            RTruckVo truckVo = new RTruckVo();
            truckVo.setId(truck.getId());
            truckVo.setTruckNo(truck.getTruckNo());
            truckVo.setActualWeight(truck.getActualWeight());
            truckVo.setTrailerNo(truck.getTrailerNo());
            truckVo.setTotalComplete(truck.getTotalComplete());
            truckVo.setTotalTake(truck.getTotalTake());
            truckVo.setTotalWeight(truck.getTotalWeight());
            truckVo.setBusinessStatus(truck.getBusinessStatus());
            truckVo.setTruckType(StringUtils.objectStr(truck.getType()));
            truckMap.put(truckVo.getTruckNo(), truckVo);
        }
        return truckMap;
    }

    /**
     * @param truckId 车辆档案主键id
     * @desc 查询车辆信息
     * @author zk
     * @date 2023/8/3
     **/
    @Override
    public RTruckVo findTruck(Long truckId) {
        RTruckVo truckVo = new RTruckVo();
        KwfTruck truck = truckDao.selectById(truckId);
        BeanUtils.copyProperties(truck, truckVo);
        TmsTruckAxleNum carAxisInfo = getCarAxisInfo(truck.getCarAxis());
        truckVo.setCarAxisId(carAxisInfo.getId());
        truckVo.setCarAxis(carAxisInfo.getName());
        truckVo.setLegalLoad(Objects.equals(TruckTypeEnum.TRUCK.getCode(), truck.getType()) ? carAxisInfo.getLegalLoad() : carAxisInfo.getTractorLegalLoad());
        return truckVo;
    }

    /**
     * @param params 车辆档案信息
     * @desc 更新车辆档案
     * @author zk
     * @date 2023/8/3
     **/
    @Override
    public int updateById(RTruckVo params) {
        KwfTruck truck = new KwfTruck();
        BeanUtils.copyProperties(params, truck);
        return truckDao.updateById(truck);
    }

    /**
     * @param entIds            企业id集
     * @param capacityAmountMin 最小合计载重
     * @param capacityAmountMax 最大合计载重
     * @desc 企业运力统计
     * @author zk
     * @date 2023/9/4
     **/
    @Override
    public List<Map<String, Object>> capacityStatistics(List<Long> entIds, Integer capacityAmountMin, Integer capacityAmountMax) {
        return truckDao.capacityStatistics(entIds, capacityAmountMin, capacityAmountMax);
    }

    @Override
    public RTruckVo findDataFirstTruck(Long entId) {
        List<RTruckVo> dTruckVoList = findTruckByEntIds(entId);
        if (CollectionUtils.isEmpty(dTruckVoList)) {
            return new RTruckVo();
        }
        return dTruckVoList.get(0);
    }

    @Override
    public RTruckVo findDriveAndTruckByPhone(String mobile) {
        RTruckVo rTruckVo = new RTruckVo();
        KwfDriver kwfDriver = driverRepository.queryOneByPhone(mobile);
        //获取车队id
        KwfFleetDriver kwfFleetDriver = kwfFleetDriverRepository.queryOneByDriverId(kwfDriver.getId());
        //根据车队id获取车辆id；
        return null;
    }

    @Override
    public void saveOrUpdate(RTruckRouteVo params) {
        //获取车队信息
        List<KwfFleetTruck> kwfFleetTrucks = kwfFleetTruckRepository.queryByTruckId(params.getTruckId());
        if (CollectionUtils.isNotEmpty(kwfFleetTrucks)) {
            params.setFleetId(kwfFleetTrucks.get(0).getFleetId());
        }
        KwfTruckRoute kwfTruckRoute = new KwfTruckRoute();
        kwfTruckRoute.setFleetId(params.getFleetId());
        kwfTruckRoute.setDriverId(params.getDriverId());
        kwfTruckRoute.setEntId(params.getEntId());
        kwfTruckRoute.setStartPoint(params.getStartPoint());
        kwfTruckRoute.setUnloadingPoint(params.getUnloadingPoint());
        kwfTruckRoute.setTruckId(params.getTruckId());
        kwfTruckRoute.setTruckNo(params.getTruckNo());
        kwfTruckRoute.setShortestTime(params.getShortestTime());
        kwfTruckRoute.setPointInfo(params.getPointInfo());

        KwfTruckRoute truckRoute = kwfTruckRouteRepository.findOneByDriverIdAndPoint(params.getDriverId(),params.getStartPoint(),params.getUnloadingPoint());
        if (Objects.isNull(truckRoute)){
            kwfTruckRouteRepository.saveTruckRoute(kwfTruckRoute);
            return;
        }
        KwfTruckRoute updateTruckRoute = new KwfTruckRoute();
        //如果数据库中没有最短时间进行更新
        if (Objects.isNull(truckRoute.getShortestTime())){
            updateTruckRoute(params, updateTruckRoute, truckRoute);
            return;
        }
        //如果传入的时间大于数据库中的时间才进行更新
        if (StringUtils.isNotBlank(truckRoute.getShortestTime())
                && StringUtils.isNotBlank(params.getShortestTime())
                && truckRoute.getShortestTime().compareTo(params.getShortestTime()) > 0 ){
            updateTruckRoute(params, kwfTruckRoute, truckRoute);
        }
    }

    @Override
    public RDriverVo findDriverByPhone(String mobile) {

        KwfDriver kwfDriver = driverRepository.queryOneByPhone(mobile);
        if (Objects.isNull(kwfDriver)){
            throw new BusinessException("司机信息不存在");
        }
        return getRDriverVo(kwfDriver);
    }

    @Override
    public RFleetDriverVo findFleetByDriveId(Long driveId) {
        KwfFleetDriver fleetByDriver = kwfFleetDriverRepository.findFleetByDriveId(driveId);
        if (Objects.isNull(fleetByDriver)){
            throw new BusinessException("司机车队信息不存在");
        }
        RFleetDriverVo rFleetDriverVo = new RFleetDriverVo();
        rFleetDriverVo.setFleetId(fleetByDriver.getFleetId());
        rFleetDriverVo.setDriverId(fleetByDriver.getDriverId());
        rFleetDriverVo.setEntId(fleetByDriver.getEntId());
        return rFleetDriverVo;
    }

    @Override
    public List<RFleetDriverVo> findFleetDriveByEntId(Long entId) {
        List<KwfFleetDriver> fleetByDriver = kwfFleetDriverRepository.findFleetDriveByEntId(entId);
        return fleetByDriver.stream().map(fleet -> {
            RFleetDriverVo rFleetDriverVo = new RFleetDriverVo();
            rFleetDriverVo.setFleetId(fleet.getFleetId());
            rFleetDriverVo.setDriverId(fleet.getDriverId());
            rFleetDriverVo.setEntId(fleet.getEntId());
            return rFleetDriverVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<RFleetVo> findFleetByEntId(Long entId) {
        List<KwfFleet> fleets = fleetRepository.findFleetByEntId(entId);
        return fleets.stream().map(fleet -> {
            RFleetVo rFleetVo = new RFleetVo();
            rFleetVo.setEntId(fleet.getEntId());
            rFleetVo.setCaptainId(fleet.getCaptainId());
            rFleetVo.setName(fleet.getName());
            rFleetVo.setContacts(fleet.getContacts());
            rFleetVo.setPhone(fleet.getPhone());
            rFleetVo.setId(fleet.getId());
            return rFleetVo;
        }).collect(Collectors.toList());
    }

    private static RDriverVo getRDriverVo(KwfDriver kwfDriver) {
        RDriverVo rDriverVo = new RDriverVo();
        rDriverVo.setId(kwfDriver.getId());
        rDriverVo.setName(kwfDriver.getName());
        rDriverVo.setPhone(kwfDriver.getPhone());
        rDriverVo.setIdcard(kwfDriver.getIdcard());
        rDriverVo.setTotalComplete(kwfDriver.getTotalComplete());
        rDriverVo.setTotalTake(kwfDriver.getTotalTake());
        rDriverVo.setTotalWeight(kwfDriver.getTotalWeight());
        rDriverVo.setBusinessStatus(kwfDriver.getBusinessStatus());
        return rDriverVo;
    }

    private void updateTruckRoute(RTruckRouteVo params, KwfTruckRoute updateTruckRoute, KwfTruckRoute truckRoute) {
        updateTruckRoute.setId(truckRoute.getId());
        updateTruckRoute.setStartPoint(params.getStartPoint());
        kwfTruckRouteRepository.updateTruckRoute(updateTruckRoute);
    }

    /**
     *  获取车辆数据
     * @param truck 车辆对象
     * @return 车辆数据
     */
    private RTruckVo getTruckVo(KwfTruck truck) {
        RTruckVo rTruckVo = new RTruckVo();
        rTruckVo.setId(truck.getId());
        rTruckVo.setTruckNo(truck.getTruckNo());
        rTruckVo.setActualWeight(truck.getActualWeight());
        rTruckVo.setGrossWeight(truck.getGrossWeight());
        rTruckVo.setTareWeight(truck.getTareWeight());
        rTruckVo.setTrailerNo(truck.getTrailerNo());
        rTruckVo.setTotalComplete(truck.getTotalComplete());
        rTruckVo.setTotalTake(truck.getTotalTake());
        rTruckVo.setTotalWeight(truck.getTotalWeight());
        rTruckVo.setBusinessStatus(truck.getBusinessStatus());
        rTruckVo.setTruckType(StringUtils.isBlank(truck.getType()) ? "": String.valueOf(truck.getType()));
        rTruckVo.setStatus(truck.getStatus());
        TmsTruckAxleNum carAxisInfo = getCarAxisInfo(truck.getCarAxis());
        rTruckVo.setCarAxisId(carAxisInfo.getId());
        rTruckVo.setCarAxis(carAxisInfo.getName());
        rTruckVo.setLegalLoad(Objects.equals(TruckTypeEnum.TRUCK.getCode(), truck.getType()) ? carAxisInfo.getLegalLoad() : carAxisInfo.getTractorLegalLoad());
        rTruckVo.setEnergyType(truck.getEnergyType());
        return rTruckVo;
    }
    /**
     * 根据车轴id查询轴数信息
     *

     * @return
     */
    private TmsTruckAxleNum getCarAxisInfo(String axleNumId) {
        if (StringUtils.isEmpty(axleNumId)) {
            return new TmsTruckAxleNum();
        }
        //查询车辆关联司机信息
        TmsTruckAxleNum axleNum = truckAxleNumRepository.getOne(Wrappers.<TmsTruckAxleNum>lambdaQuery()
                .eq(TmsTruckAxleNum::getStatus, 1)
                .eq(TmsTruckAxleNum::getId, Long.parseLong(axleNumId)));
        if (axleNum == null) {
            return new TmsTruckAxleNum();
        }
        return axleNum;
    }
    /**
     * 通过车队id查询车队信息
     * @param fleetId
     * @return
     */
    @Override
    public RFleetVo findFleetById(Long fleetId) {
        KwfFleet fleet = fleetRepository.queryByFleetId(fleetId);
        if (Objects.isNull(fleet)){
            throw new BusinessException("车队信息不存在");
        }
        RFleetVo fleetVo = new RFleetVo();
        fleetVo.setId(fleet.getId());
        fleetVo.setRemark(fleet.getRemark());
        fleetVo.setStatus(fleet.getStatus());
        fleetVo.setDelFlag(fleet.getDelFlag());
        fleetVo.setEntId(fleet.getEntId());
        fleetVo.setCaptainId(fleet.getCaptainId());
        fleetVo.setName(fleet.getName());
        fleetVo.setContacts(fleet.getContacts());
        fleetVo.setPhone(fleet.getPhone());

        return fleetVo;
    }

    /**
     * 查询轴数
     * @param axleNumId
     * @return
     */
    @Override
    public TmsTruckAxleNumVO getCarAxisInfo(Integer axleNumId) {
        if (axleNumId == null) {
            return new TmsTruckAxleNumVO();
        }
        //查询车辆关联司机信息
        TmsTruckAxleNum axleNum = kwfTruckAxleNumRepository.getOneById(axleNumId);
        if (axleNum == null) {
            return new TmsTruckAxleNumVO();
        }
        TmsTruckAxleNumVO vo = new TmsTruckAxleNumVO();
        vo.setId(axleNum.getId());
        vo.setName(axleNum.getName());
        vo.setLegalLoad(axleNum.getLegalLoad());
        return vo;
    }

    /**
     * 查询轴数
     * @param axleNumIds
     * @return
     */
    @Override
    public List<TmsTruckAxleNumVO> getCarAxisInfoList(List<Integer> axleNumIds) {
        //查询车辆关联司机信息
        List<TmsTruckAxleNum> axleNumList = kwfTruckAxleNumRepository.list(Wrappers.<TmsTruckAxleNum>lambdaQuery()
                .eq(TmsTruckAxleNum::getStatus, 1)
                .in(TmsTruckAxleNum::getId, axleNumIds));
        if (CollectionUtils.isEmpty(axleNumList)) {
            return new ArrayList<>();
        }
        List<TmsTruckAxleNumVO> voList = new ArrayList<>();
        axleNumList.forEach(axleNum -> {
            TmsTruckAxleNumVO vo = new TmsTruckAxleNumVO();
            vo.setId(axleNum.getId());
            vo.setName(axleNum.getName());
            voList.add(vo);
        });

        return voList;
    }

    @Override
    public List<RTruckVo> findTruckByTruckIds(List<Long> truckIds) {
        List<KwfTruck> trucks = kwfTruckRepository.findTruckByTruckIds(truckIds);
        if (CollectionUtils.isEmpty(trucks)){
            return Collections.emptyList();
        }
        return trucks.stream()
                .map(this::getTruckVo)
                .collect(Collectors.toList());

    }

    @Override
    public List<RFleetVo> findFleetByIds(Set<Long> fleetIds) {
        List<KwfFleet> fleets = fleetRepository.findByfleetIds(fleetIds);
        if (org.apache.commons.collections4.CollectionUtils.isEmpty(fleets)){
            return Collections.emptyList();
        }
        return fleets.stream()
                .map(RemoteFleetServiceImpl::getRFleetVo)
                .collect(Collectors.toList());
    }

    @Override
    public RFleetVo findFleetByTruckId(Long truckId) {
        KwfFleetTruck fleetByTruckId = kwfFleetTruckRepository.findFleetByTruckId(truckId);
        if(Objects.isNull(fleetByTruckId)){
            return new RFleetVo();
        }
        KwfFleet fleet = kwfFleetRepository.queryByFleetId(fleetByTruckId.getFleetId());
        if (Objects.isNull(fleet)){
            return new RFleetVo();
        }
        return getFleetVo(fleet);
    }

    @NotNull
    private static RFleetVo getFleetVo(KwfFleet fleet) {
        RFleetVo rFleetVo = new RFleetVo();
        rFleetVo.setId(fleet.getId());
        rFleetVo.setRemark(fleet.getRemark());
        rFleetVo.setStatus(fleet.getStatus());
        rFleetVo.setCreateBy(fleet.getCreateBy());
        rFleetVo.setCreateTime(fleet.getCreateTime());
        rFleetVo.setUpdateBy(fleet.getUpdateBy());
        rFleetVo.setUpdateTime(fleet.getUpdateTime());
        rFleetVo.setDelFlag(fleet.getDelFlag());
        rFleetVo.setEntId(fleet.getEntId());
        rFleetVo.setCaptainId(fleet.getCaptainId());
        rFleetVo.setName(fleet.getName());
        rFleetVo.setContacts(fleet.getContacts());
        rFleetVo.setPhone(fleet.getPhone());

        return rFleetVo;
    }

    @NotNull
    private static RFleetVo getRFleetVo(KwfFleet fleet) {
        RFleetVo rFleetVo = new RFleetVo();
        rFleetVo.setId(fleet.getId());
        rFleetVo.setRemark(fleet.getRemark());
        rFleetVo.setStatus(fleet.getStatus());
        rFleetVo.setCreateBy(fleet.getCreateBy());
        rFleetVo.setCreateTime(fleet.getCreateTime());
        rFleetVo.setUpdateBy(fleet.getUpdateBy());
        rFleetVo.setUpdateTime(fleet.getUpdateTime());
        rFleetVo.setDelFlag(fleet.getDelFlag());
        rFleetVo.setEntId(fleet.getEntId());
        rFleetVo.setCaptainId(fleet.getCaptainId());
        rFleetVo.setName(fleet.getName());
        rFleetVo.setContacts(fleet.getContacts());
        rFleetVo.setPhone(fleet.getPhone());


        return rFleetVo;
    }

    @Override
    public RTruckVo findTruckByTruckNo(String truckNo) {
        KwfTruck truck = kwfTruckRepository.findTruckByTruckNo(truckNo);
        if (truck == null) {
            return null;
        }
        return getTruckVo(truck);
    }

    @Override
    public  void updateTruckTareAmount(RTruckVo truckNo) {
        KwfTruck truck = kwfTruckRepository.queryByTruckId(truckNo.getId());
        if (truck == null) {
            return;
        }
        truck.setTareWeight(truckNo.getTareWeight());
        kwfTruckRepository.updateById(truck);
    }

    @Override
    public TruckDispatchCoefficientVO findAutoTruckDispatchByEntId(Long entId) {
        KwfTruckDispatchCoefficient result = truckDispatchCoefficientRepository.findAutoTruckDispatchByEntId(entId);
        if (result == null) {
            return null;
        }
        return getTruckDispatchCoefficientVO(result);
    }


    private TruckDispatchCoefficientVO getTruckDispatchCoefficientVO(KwfTruckDispatchCoefficient result) {
        TruckDispatchCoefficientVO rulesVO = new TruckDispatchCoefficientVO();
        rulesVO.setId(result.getId());
        rulesVO.setEntId(result.getEntId());
        rulesVO.setVehicleWorkHours(result.getVehicleWorkHours());
        rulesVO.setVehicleLoadingHours(result.getVehicleLoadingHours());
        rulesVO.setVehicleUnloadingHours(result.getVehicleUnloadingHours());
        rulesVO.setDriverTimeoutLimit(result.getDriverTimeoutLimit());
        rulesVO.setVehicleAvgLoad(result.getVehicleAvgLoad());
        rulesVO.setVehicleAvgSpeed(result.getVehicleAvgSpeed());
        rulesVO.setVehicleMaxTasks(result.getVehicleMaxTasks());
        rulesVO.setDriverOrderLimit(result.getDriverOrderLimit());
        rulesVO.setYardVehicleCapacity(result.getYardVehicleCapacity());
        rulesVO.setMaxRatio(result.getMaxRatio());
        rulesVO.setBufferCoefficient(result.getBufferCoefficient());
        rulesVO.setCreateTime(result.getCreateTime());
        rulesVO.setUpdateTime(result.getUpdateTime());
        rulesVO.setCreateUser(result.getCreateUser());
        rulesVO.setUpdateUser(result.getUpdateUser());
        return rulesVO;
    }

    @Override
    public List<TruckDispatchCoefficientVO> findAutoTruckDispatchByEntIds(List<Long> entIds) {
        List<KwfTruckDispatchCoefficient> autoTruckDispatchByEntIds = truckDispatchCoefficientRepository.findAutoTruckDispatchByEntIds(entIds);
        return autoTruckDispatchByEntIds.stream()
                .map(this::getTruckDispatchCoefficientVO)
                .collect(Collectors.toList());

    }

    @Override
    public DriverConductRulesVO findDriverConductRulesByEntId(Long entId) {
        KwfDriverConductRules result = driverConductRulesRepository.findDriverConductRulesByEntId(entId);
        if (result == null) {
            return null;
        }

        DriverConductRulesVO rulesVO = new DriverConductRulesVO();
        rulesVO.setId(result.getId());
        rulesVO.setEntId(result.getEntId());
        rulesVO.setUnloadSeriousTimeoutScore(result.getUnloadSeriousTimeoutScore());
        rulesVO.setUnloadSeriousTimeoutMultiple(result.getUnloadSeriousTimeoutMultiple());
        rulesVO.setDocumentErrorMissingScore(result.getDocumentErrorMissingScore());
        rulesVO.setNotOnTimeArriveScore(result.getNotOnTimeArriveScore());
        rulesVO.setFakeUnloadScore(result.getFakeUnloadScore());
        rulesVO.setFakeUnloadDistance(result.getFakeUnloadDistance());
        rulesVO.setIllegalCancelOrderScore(result.getIllegalCancelOrderScore());
        rulesVO.setIllegalCancelOrderMinutes(result.getIllegalCancelOrderMinutes());
        rulesVO.setContinuousOnTimeArriveScore(result.getContinuousOnTimeArriveScore());
        rulesVO.setContinuousOnTimeArriveTimes(result.getContinuousOnTimeArriveTimes());
        rulesVO.setContinuousOnTimeUnloadScore(result.getContinuousOnTimeUnloadScore());
        rulesVO.setContinuousOnTimeUnloadTimes(result.getContinuousOnTimeUnloadTimes());
        rulesVO.setContinuousAccurateUnloadScore(result.getContinuousAccurateUnloadScore());
        rulesVO.setContinuousAccurateUnloadTimes(result.getContinuousAccurateUnloadTimes());
        rulesVO.setCreateTime(result.getCreateTime());
        rulesVO.setUpdateTime(result.getUpdateTime());
        rulesVO.setCreateUser(result.getCreateUser());
        rulesVO.setUpdateUser(result.getUpdateUser());

        return rulesVO;
    }

    @Override
    public List<RTruckVo> findTruckByEntIds(Long entId) {
        //先查询车辆企业关联关系
        List<KwfTruckEnt> truckEntByEntIds = truckEntRepository.findTruckEntByEntIds(entId);
        if(CollectionUtils.isEmpty(truckEntByEntIds)){
            return new ArrayList<>();
        }
        List<Long> truckIds = truckEntByEntIds.stream().map(KwfTruckEnt::getTruckId).collect(Collectors.toList());
        //通过车牌id查询车辆
        List<KwfTruck> truckList = kwfTruckRepository.findTruckByTruckIds(truckIds);
        if(CollectionUtils.isEmpty(truckList)){
            return new ArrayList<>();
        }
        return truckList.stream().map(this::getTruckVo).collect(Collectors.toList());
    }

    /**
     * 修改司机评分
     * @param driverScoreDto

     */
    public void updateDriverScoreByEntId(RUpdateDriverScoreDto driverScoreDto) {
        driverScoreService.updateDriverScore(driverScoreDto);
    }

    @Override
    public void driverBindTruck(Long driverId, Long truckId,Long entId) {
        driverService.bindTruck(driverId, truckId,entId);
    }

    @Override
    public void unbindTruck(Long entId, Long driverId) {
        KwfTruckReport truckReport = truckReportRepository.getOne(Wrappers.<KwfTruckReport>lambdaQuery()
                .eq(KwfTruckReport::getEntId, entId)
                .eq(KwfTruckReport::getDriverId, driverId)
                .eq(KwfTruckReport::getDelFlag, Global.NO)
                .eq(KwfTruckReport::getStatus, Global.YES));
        if(truckReport == null) {
            return;
        }
        truckReport.setStatus(Global.NO);
        log.info("解绑车辆请求参数 ：{}", truckReport);
        truckReportRepository.updateById(truckReport);

    }

    @Override
    public RDriverScoreVo findDriverScore(Long supEntId, Long logEntId, Long driverId) {
        //1. 查询司机的评分
        KwfDriverScore driverScore = driverScoreRepository.findDriverScoreByEntIds(supEntId, logEntId, driverId);
        if (driverScore == null) {
            return null;
        }
        RDriverScoreVo driverScoreVo = new RDriverScoreVo();
        driverScoreVo.setProviderEntId(driverScore.getProviderEntId());
        driverScoreVo.setLogisticsEntId(driverScore.getLogisticsEntId());
        driverScoreVo.setDriverId(driverScore.getDriverId());
        driverScoreVo.setScore(driverScore.getScore());
        return driverScoreVo;
    }
}
