SyncData.java 12.9 KB
package com.bsth.data.commonData;

import com.bsth.data.BasicData;
import com.bsth.data.commonData.entity.*;
import com.bsth.entity.*;
import com.bsth.repository.*;
import com.bsth.service.*;
import com.bsth.service.schedule.CarsService;
import com.bsth.util.db.DBUtils_jiaDingBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
@EnableTransactionManagement
public class SyncData  extends Thread{

    Logger logger = LoggerFactory.getLogger(this.getClass());



    @Autowired
    private BusinessService businessService;

    @Autowired
    private BusinessRepository businessRepository;

    @Autowired
    private CarParkRepository carParkRepository;

    @Autowired
    private CarParkService carParkService;

    @Autowired
    private LineService lineService;

    @Autowired
    private LineVersionsService lineVersionsService;

    @Autowired
    private LineInformationRepository informationRepository;

    @Autowired
    private LineInformationService lineInformationService;

    @Autowired
    private CarsService carsService;

    @Autowired
    private CarsRepository carsRepository;

    @Autowired
    private PersonnelRepository personnelRepository;

    @Autowired
    private PersonnelService personnelService;

    @Autowired
    private StationService stationService;

    @Autowired
    private StationRepository stationRepository;

    @Autowired
    private StationRouteRepository stationRouteRepository;

    @Autowired
    private LsStationRouteRepository lsStationRouteRepository;

    @Autowired
    SectionService sectionService;

    @Autowired
    SectionRepository sectionRepository;

    @Autowired
    SectionRouteRepository sectionRouteRepository;

    @Autowired
    LsSectionRouteRepository lsSectionRouteRepository;

    @Autowired
    BasicData.BasicDataLoader basicDataLoader;

    //@Scheduled(cron = "0 1 * * * ?")
    //@Transactional()
    public void SyncData(){
        syncBusiness();
        syncCarPark();
        syncLine();
        syncLineInformation();
        syncCar();
        syncPersonnel();
        syncStation();
        syncStationRoute();
        syncSection();
        syncSectionRoute();
        basicDataLoader.loadAllData();
    }




    public void syncBusiness(){
        try {
            JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
            String sql = "select * from common_bus_company";
            List<BusCompany> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusCompany.class));
            List<Business> businesses=BusCompany.convert(list);
            for (Business business : businesses) {
                if(businessRepository.findByBusinessCode(business.getBusinessCode()).size()>0){
                    businessService.update(business);
                }else {
                    businessService.save(business);
                }
            }
            logger.info(">>>>>>>>>>>>>公司数据同步完成");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>公司数据同步异常",e);
        }
    }


    public void syncCarPark(){
        try {
            JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
            String sql2 = "select *,ST_AsText(b_park_point) b_park_point_wkt,ST_AsText(g_park_point) g_park_point_wkt from common_bus_park";
            List<BusPark> list2 = jdbcTemp.query(sql2, new BeanPropertyRowMapper(BusPark.class));
            List<CarPark> carParks=BusPark.convert(list2);
            for (CarPark carPark : carParks) {
                if(carParkRepository.selectTccInfoByCode(carPark.getParkCode()).size()>0){
                    carParkService.update(carPark);
                }else {
                    carParkService.save(carPark);
                }
            }
            logger.info(">>>>>>>>>>>>>停车场数据同步完成");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>停车场数据同步异常",e);
        }
    }


    public void syncLine(){
        try {
            JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
            String sql = "select * from common_bus_line_base";
            List<BusLineBase> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusLineBase.class));
            List<Line> lines=BusLineBase.convert(list);
            for (Line line : lines) {
                if(line.getId()==null) {
                    line.setId(Integer.valueOf(line.getLineCode()));
                }
                if(line.getId().toString().length()>6) {
                    logger.info(">>>>>>>>>>>>>线路编码不符合条件"+line.getId().toString());
                }
                if(lineService.lineCodeVerification(line.getLineCode()).equals("false")){//线路存在 修改
                    lineService.update(line);
                }else {//新增
                    saveLine(line);
                }
            }
            logger.info(">>>>>>>>>>>>>线路数据同步完成");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>线路数据同步异常",e);
        }
    }

    public void saveLine(Line t){
        // 添加线路版本
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date endDate = simpleDateFormat.parse("2088-08-08 00:00:00");
            LineVersions lineVersions = new LineVersions();
            lineVersions.setName("原始版本");
            lineVersions.setLine(t);
            lineVersions.setLineCode(t.getLineCode());
            lineVersions.setStartDate(new java.sql.Date(new Date().getTime()));
            lineVersions.setEndDate(new java.sql.Date(endDate.getTime()));// 2088-8-8 00:00:00
            lineVersions.setCreateDate(new java.sql.Date(new Date().getTime()));
            lineVersions.setUpdateDate(new java.sql.Date(new Date().getTime()));
            lineVersions.setVersions(1);
            lineVersions.setStatus(1);
            // 先添加线路再添加版本
            lineService.save(t);
            lineVersionsService.save(lineVersions);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return;
        }
    }


    public void syncLineInformation(){
        try {
            JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
            String sql = "select * from common_bus_line_operations";
            List<BusLineOperations> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusLineOperations.class));
            List<LineInformation> lineInformations=BusLineOperations.convert(list);
            for (LineInformation lineInformation : lineInformations) {
                if(informationRepository.findLineInformationByLineCode(String.valueOf(lineInformation.getLine().getId())).size()>0){//线路存在 修改
                    lineInformationService.update(lineInformation);
                }else {//新增
                    lineInformationService.save(lineInformation);
                }
            }
            logger.info(">>>>>>>>>>>>>线路配置数据同步完成");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>线路配置数据同步异常",e);
        }
    }


    public void syncCar(){
        try {
            JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
            String sql = "select * from common_bus_veh";
            List<BusVeh> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusVeh.class));
            List<Cars> cars=BusVeh.convert(list);
            for (Cars car : cars) {
                if(carsRepository.findCarsByCode(car.getInsideCode()).size()>0){//存在
                    carsService.update(car);
                }else {
                    carsService.save(car);
                }
            }
            logger.info(">>>>>>>>>>>>>车辆数据同步完成");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>车辆数据同步异常",e);
        }
    }


    public void syncPersonnel(){
        try {
            JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
            String sql = "select * from common_bus_staff";
            List<BusStaff> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusStaff.class));
            List<Personnel> personnels=BusStaff.convert(list);
            for (Personnel personnel : personnels) {
                if(personnelRepository.findPersonnelByCode(personnel.getJobCodeori()).size()>0){//存在
                    personnelService.update(personnel);
                }else {
                    personnelService.save(personnel);
                }
            }
            logger.info(">>>>>>>>>>>>>人员数据同步完成");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>人员数据同步异常",e);
        }
    }


    public void syncStation(){
        try {
            JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
            String sql = "select *,ST_AsText(center_point) center_point_wkt from common_bus_stop";
            List<BusStop> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusStop.class));
            List<Station> stations=BusStop.convert(list);
            for (Station station : stations) {
                if(stationRepository.findStationByStationCode(station.getStationCode()).size()>0){//存在
                    stationService.update(station);
                }else {
                    stationService.add(station);
                }
            }
            logger.info(">>>>>>>>>>>>>站点数据同步完成");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>站点数据同步异常",e);
        }
    }



    public void syncStationRoute(){
        try {
            JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
            String sql = "select *,ST_AsText(center_point) center_point_wkt,ST_AsText(buffer_polygon) buffer_polygon_wkt from common_bus_stoplevel";
            List<BusStopLevel> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusStopLevel.class));
            List<StationRoute> stationRoutes=BusStopLevel.convert(list);
            List<LsStationRoute> lsStationRoutes=BusStopLevel.convertLS(list);
            if(stationRoutes.size()>0){
                stationRouteRepository.deleteAll();
                lsStationRouteRepository.deleteAll();
            }
            stationRouteRepository.saveAll(stationRoutes);
            lsStationRouteRepository.saveAll(lsStationRoutes);
            logger.info(">>>>>>>>>>>>>站点路由数据同步完成");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>站点路由数据同步异常",e);
        }
    }


    private void syncSection(){
        try {
            JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
            String sql = "select *,ST_AsText(bsection_vector) bsectionVectorWkt from common_road_section";
            List<RoadSection> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(RoadSection.class));
            List<Section> sections=RoadSection.convert(list);
            for (Section section : sections) {
                if(sectionRepository.findSectionByCode(section.getSectionCode()).size()>0){
                    sectionService.modify(section);
                }else {
                    sectionService.add(section);
                }
            }
            logger.info(">>>>>>>>>>>>>路段据同步完成");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>路段数据同步异常",e);
        }
    }


    private void syncSectionRoute(){
        try {
            JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
            String sql = "select * from common_road_sectionlevel where destroy=0";
            List<RoadSectionLevel> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(RoadSectionLevel.class));
            List<SectionRoute> sectionRoutes=RoadSectionLevel.convert(list);
            List<LsSectionRoute> lsSectionRoutes=RoadSectionLevel.convertLS(list);
            if(sectionRoutes.size()>0){
                sectionRouteRepository.deleteAll();
                lsSectionRouteRepository.deleteAll();
            }
            sectionRouteRepository.saveAll(sectionRoutes);
            lsSectionRouteRepository.saveAll(lsSectionRoutes);
            logger.info(">>>>>>>>>>>>>路段路由据同步完成");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>路段路由数据同步异常",e);
        }
    }

}