Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / CarManagerServiceImpl.java
package com.newfiber.api.pc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.core.commons.PageRequestObject;
import com.newfiber.api.core.commons.PageResultObject;
import com.newfiber.api.core.scheduled.CarLocationTestScheduled;
import com.newfiber.api.pc.dao.AllocMapper;
import com.newfiber.api.pc.dao.CarManagerMapper;
import com.newfiber.api.pc.dto.AllotCarDTO;
import com.newfiber.api.pc.dto.CarSearchDTO;
import com.newfiber.api.pc.model.entity.CarManager;
import com.newfiber.api.pc.model.entity.CarTrack;
import com.newfiber.api.pc.model.entity.LongitudeAndLatitudeTimeReferenceData;
import com.newfiber.api.pc.model.vo.AllotDeatilsVo;
import com.newfiber.api.pc.model.vo.ReturnGisCarLocationDataVo;
import com.newfiber.api.pc.service.CarManagerService;
import com.newfiber.api.pc.service.CarTrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @description: 车辆管理业务层实现类
 * @author: 张鸿志
 * @date: 2021/2/2 10:14
 * @version: v1.0
 */
@Service
public class CarManagerServiceImpl extends ServiceImpl<CarManagerMapper, CarManager> implements CarManagerService {

    @Autowired
    private CarTrackService carTrackService;

    @Autowired
    private AllocMapper allocMapper;



    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /** 当前正在运输的车辆的坐标点集合 */
    public static final String CARXYTOKEN = "car_location:";

    @Override
    public PageResultObject<CarManager> selectListPage(PageRequestObject<CarSearchDTO> pageRequestObject) {
        CarSearchDTO object = pageRequestObject.getObject();
        Integer current = pageRequestObject.getCurrent();
        Integer size = pageRequestObject.getSize();
        EntityWrapper<CarManager> wrapper = new EntityWrapper<>();
        Page<CarManager> page = new Page<>(current,size);
        if(!StringUtils.isEmpty(object.getCarBrand())){
            wrapper.like("car_brand",object.getCarBrand());
        }
        if(!StringUtils.isEmpty(object.getPlateNumber())){
            wrapper.like("plate_number",object.getPlateNumber());
        }
        wrapper.orderDesc(Arrays.asList(new String[]{"car_id"}));
        int count = this.selectCount(wrapper);
        Page<CarManager> carManagerPage = this.selectPage(page, wrapper);
        return new PageResultObject<>(current,size,(long)count, carManagerPage.getRecords());
    }

    @Override
    public List<CarManager> select() {
        EntityWrapper<CarManager> wrapper = new EntityWrapper<>();
        wrapper.eq("is_leisure",0);
        return this.selectList(wrapper);
    }

    @Override
    public void allotCar(AllotCarDTO allotCarDTO) {
        List<CarTrack> carTracks = new ArrayList<>();
        //车辆的初始位置
        for(Integer carId : allotCarDTO.getCarIds()){
            CarTrack carTrack = new CarTrack();
            carTrack.setCarId(carId);
            carTrack.setAllotId(allotCarDTO.getAllotId());
            List<LongitudeAndLatitudeTimeReferenceData> locationDatas = new ArrayList<>();
            locationDatas.add(CarLocationTestScheduled.createInitLocation(null));
            //设置车辆的初始信息
            carTrack.setCarLoations(locationDatas);
            carTracks.add(carTrack);
        }

        //向表中插入数据,但不插入车辆位置数据,车辆位置数据暂时保存在redis中,等调度结束时统一插入到表中,形成历史记录
       // carTrackService.insertBatch(carTracks);
        stringRedisTemplate.opsForValue().set(CARXYTOKEN+ allotCarDTO.getAllotId(), JSON.toJSONString(carTracks));
    }

    @Override
    public void overCarWork(Integer allotId) {
        String key = CARXYTOKEN + allotId;
        if(stringRedisTemplate.hasKey(key)){
            //如果存在就将里面的数据拿出来,更新到表中去
            String json = stringRedisTemplate.opsForValue().get(key);
            List<CarTrack> carTracks = JSONArray.parseArray(json, CarTrack.class);
            //修改车辆的状态为空闲
            List<Integer> collect = carTracks.stream().map(CarTrack::getCarId).collect(Collectors.toList());
            EntityWrapper<CarManager> wrapper = new EntityWrapper<>();
            wrapper.in("car_id",collect);
            List<CarManager> carManagers = this.selectList(wrapper);
            carManagers.forEach(s -> {
                //将车辆的状态改变为空闲状态
                s.setIsLeisure(0);
            });
            this.updateBatchById(carManagers);
            //将位置数据赋给数据库记录的字段
            carTracks.forEach( car -> {
                car.setXyList(JSON.toJSONString(car.getCarLoations()));
            });
            carTrackService.insertBatch(carTracks);
            stringRedisTemplate.delete(key);
        }
    }


    @Override
    public List<ReturnGisCarLocationDataVo> allCarLocations() {
        //从redis中获取数据
        Set<String> keys = stringRedisTemplate.keys(CARXYTOKEN + "*");
        if(keys != null && keys.size() > 0){
            List<ReturnGisCarLocationDataVo> voList = new ArrayList<>();
            //调度编号集合
            List<Integer> allotIds = new ArrayList<>();
            //车辆编号集合
            List<Integer> carIds = new ArrayList<>();
            for(String key :keys){
                String json = stringRedisTemplate.opsForValue().get(key);
                List<CarTrack> carTracks = JSONArray.parseArray(json, CarTrack.class);
                allotIds.add(carTracks.get(0).getAllotId());
                for(CarTrack carTrack : carTracks){
                    ReturnGisCarLocationDataVo vo =new ReturnGisCarLocationDataVo();
                    //同一个key中多个车辆的调度编号是一样的,因为都是属于同一次调度
                    vo.setAllotId(carTracks.get(0).getAllotId());
                    vo.setLocationData(carTrack.getCarLoations());
                    //设置车辆编号
                    vo.setCarId(carTrack.getCarId());
                    //添加车辆编号
                    carIds.add(carTrack.getCarId());
                    voList.add(vo);
                }
            }

            //查询相关数据
            EntityWrapper<CarManager> carWrapper = new EntityWrapper<>();
            carWrapper.in("car_id",carIds);
            //查询出车辆信息
            List<CarManager> carManagers = this.selectList(carWrapper);
            //查询出调度信息
            List<AllotDeatilsVo> allotDeatilsVos = allocMapper.selectAllocDetails(allotIds);
            for(int i = 0; i < voList.size(); i++){
                ReturnGisCarLocationDataVo locationDataVo = voList.get(i);
                //遍历车辆信息
                for(int j = 0; j < carManagers.size(); j++){
                    CarManager carManager = carManagers.get(j);
                    //如果两个车辆编号相等,就将车辆的信息填入返回的对象中去
                    if(locationDataVo.getCarId().equals(carManager.getCarId())){
                        locationDataVo.setPlateNumber(carManager.getPlateNumber());
                        break;
                    }
                }

                //遍历调度信息
                for(int k = 0; k < allotDeatilsVos.size(); k++){
                    AllotDeatilsVo allotDeatilsVo = allotDeatilsVos.get(k);
                    //如果两个调度编号相等
                    if(locationDataVo.getAllotId().equals(allotDeatilsVo.getAllotId())){
                        locationDataVo.setAllotName(allotDeatilsVo.getAllotName());
                        locationDataVo.setAllotSiteName(allotDeatilsVo.getAllotSiteName());
                        locationDataVo.setAssignSiteName(allotDeatilsVo.getAssignSiteName());
                        locationDataVo.setAllotUserNo(allotDeatilsVo.getAllotUserNo());
                        locationDataVo.setAssignUserNo(allotDeatilsVo.getAssignUserNo());
                        break;
                    }
                }
            }
            //返回需要展示的数据
            return voList;

        }
        return null;
    }
}