AdminUtilsController.java 11.1 KB
package com.bsth.controller.realcontrol;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.bsth.common.ResponseCode;
import com.bsth.data.BasicData;
import com.bsth.data.directive.DayOfDirectives;
import com.bsth.data.directive.DirectivesPstThread;
import com.bsth.data.gpsdata_v2.cache.GeoCacheData;
import com.bsth.data.gpsdata_v2.handlers.overspeed.OverspeedProcess;
import com.bsth.data.gpsdata_v2.thread.GpsDataLoaderThread;
import com.bsth.data.msg_queue.DirectivePushQueue;
import com.bsth.data.msg_queue.WebSocketPushQueue;
import com.bsth.data.pilot80.PilotReport;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.filter.SQLInjectFilter;
import com.bsth.service.SectionService;
import com.bsth.service.schedule.utils.SpringUtils;
import com.bsth.util.MailUtils;
import com.bsth.websocket.handler.SendUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by panzhao on 2017/4/14.
 */
@RestController
@RequestMapping("adminUtils")
public class AdminUtilsController {


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

    @Autowired
    DayOfSchedule dayOfSchedule;

    @Autowired
    GeoCacheData geoCacheData;

    @Autowired
    DayOfDirectives dayOfDirectives;

    @Autowired
    SendUtils sendUtils;

    @Autowired
    PilotReport pilotReport;

    @Autowired
    private MailUtils mailUtils;

    @Autowired
    private BasicData.BasicDataLoader basicDataLoader;

    @Autowired
    private SQLInjectFilter sqlInjectFilter;

    @Autowired
    private SectionService sectionService;

    /**
     * 出现重复班次的车辆
     *
     * @param
     * @RequestMapping(value = "/schRepeat", method = RequestMethod.POST)
     * public void schRepeat(@RequestParam String nbbm){
     * logger.info("前端通知,车辆 " + nbbm + "出现重复班次,开始检测...");
     * List<ScheduleRealInfo> list = dayOfSchedule.findByNbbm(nbbm);
     * logger.info("检测前,车辆班次数量:" + list.size());
     * <p>
     * Map<Long, ScheduleRealInfo> map = new HashMap<>();
     * for(ScheduleRealInfo sch : list){
     * if(map.containsKey(sch.getId())){
     * logger.info("检测到重复ID: " + sch.getId());
     * }
     * map.put(sch.getId(), sch);
     * }
     * <p>
     * logger.info("检测后,车辆班次数量:" + list.size());
     * if(map.values().size() > 0){
     * dayOfSchedule.replaceByNbbm(nbbm, map.values());
     * }
     * }
     */

/*    @RequestMapping(value = "/directivePushQueue")
    public void directivePushQueue(){
        DirectivePushQueue.start();
    }*/
    @RequestMapping(value = "/directiveQueueSize")
    public void directiveQueueSize() {
        DirectivePushQueue.size();
    }

    /*@RequestMapping(value = "/webSocketPushQueue")
    public void webSocketPushQueue(){
        WebSocketPushQueue.start();
    }*/

    @RequestMapping(value = "/webSocketQueueSize")
    public void webSocketQueueSize() {
        WebSocketPushQueue.size();
    }

    @RequestMapping(value = "/setHttpFlag")
    public void setHttpFlag(@RequestParam int flag) {
        if (flag != 0 && flag != -1)
            return;
        GpsDataLoaderThread.setFlag(flag);
    }

    @RequestMapping(value = "/updateCacheBuff")
    public void updateCacheBuff() {
        geoCacheData.loadData();
    }

    @RequestMapping(value = "/reCalcLpSch")
    public void reCalcLpSch() {
        dayOfSchedule._test_reCalcLpSch();
    }

    @RequestMapping(value = "/findSchByLpName")
    public List<ScheduleRealInfo> findSchByLpName(@RequestParam String lpName) {
        return dayOfSchedule.getLpScheduleMap().get(lpName);
    }

    @RequestMapping(value = "/findSchByNbbm")
    public List<ScheduleRealInfo> findSchByNbbm(@RequestParam String nbbm) {
        return dayOfSchedule.findByNbbm(nbbm);
    }

    @RequestMapping(value = "/executeCurr")
    public ScheduleRealInfo executeCurr(@RequestParam String nbbm) {
        return dayOfSchedule.executeCurr(nbbm);
    };

    @RequestMapping(value = "/reCalcExecPlan4Nbbm")
    public void reCalcExecPlan4Nbbm(@RequestParam String nbbm) {
        dayOfSchedule.reCalcExecPlan(nbbm);
    };

    @RequestMapping(value = "/reCalcExecPlan4Line")
    public void reCalcExecPlan4Line(@RequestParam String lineCode) {
        Set<String> nbbms = dayOfSchedule.findCarByLineCode(lineCode);
        for (String nbbm : nbbms) {
            dayOfSchedule.reCalcExecPlan(nbbm);
        }
    };

    @RequestMapping(value = "/removeExecPlan")
    public int removeExecPlan(@RequestParam String nbbm) {
        dayOfSchedule.removeExecPlan(nbbm);
        return 1;
    }

    @RequestMapping(value = "/sch_re_calc_id_maps")
    public int reCalcIdMaps() {
        return dayOfSchedule.reCalcIdMaps();
    }

    @RequestMapping(value = "/sch_size_string")
    public String schSizeString() {
        return dayOfSchedule.sizeString();
    }

    @RequestMapping(value = "/containerSize")
    public Map<String, Integer> containerSize() {
        Map<String, Integer> rs = new HashMap<>();
        rs.put("60_size", dayOfDirectives.all60().size());
        rs.put("80_size", pilotReport.findAll().size());
        rs.put("nbbm_sch_size", dayOfSchedule.findAll().size());
        rs.put("lp_sch_size", dayOfSchedule.findAllByLpContainer().size());
        rs.put("id_sch_size", dayOfSchedule.findAllByIdContainer().size());
        rs.put("pst_sch_size", dayOfSchedule.getPstSize());
        rs.put("speeds_size", OverspeedProcess.size());
        return rs;
    }

    @RequestMapping(value = "/websocketRadioText")
    public int radioText(String t, String lineCode) {
        sendUtils.sendRadioText(t, lineCode);
        return 0;
    }

    @Autowired
    DirectivesPstThread directivesPstThread;

    @RequestMapping(value = "/_sd_60_pst")
    public void sd_60_pst() {
        logger.info("手动入库指令....");
        directivesPstThread.run();
    }

    /**
     * 删除班次,删除内存中未清理掉的非当天的班次
     * @createDate 2019.05.13
     * @author zhangxianzhou
     * @param id 线路id
     * @return 被删除的班次信息
     */
    @RequestMapping("/deleteSchduleBC")
    public Map<String, Object> deleteSchduleBC(Long id) {
        Map<String, Object> rs = new HashMap<>();
        rs.put("status", ResponseCode.ERROR);
        ScheduleRealInfo sch = null;
        try {
            sch = dayOfSchedule.get(id);
            if (sch == null) {
                rs.put("msg", "无效的id号");
                return rs;
            }
            
            //内存中删除
            dayOfSchedule.deleteBC(sch);
            rs.put("delete", sch);
            rs.put("status", ResponseCode.SUCCESS);

        } catch (Exception e) {
            logger.error("", e);
            rs.put("msg", e.getMessage());
            sch.setDeleted(false);
        }

        return rs;
    }
    
    @RequestMapping("/changeLogLevel")
    public Map<String, Object> changeLogLevel(@RequestParam String level) {
        Map<String, Object> rs = new HashMap<>();
        rs.put("errCode", 1);
        LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
        context.getLogger("root").setLevel(Level.toLevel(level));
        rs.put("errCode", 0);
        rs.put("errMsg", "成功");

        return rs;
    }

    @RequestMapping("/exportInstance")
    public String exportInstance(@RequestParam String classType) {
        Map<String, Object> result = new HashMap<>();
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            Class cls = Class.forName(classType);
            Field[] fields = cls.getDeclaredFields();
            for (Field field : fields) {
                if (Modifier.isStatic(field.getModifiers()) && field.getName().indexOf("log") == -1) {
                    field.setAccessible(true);
                    result.put(field.getName(), field.get(cls));
                }
            }
            result.put("_bean", SpringUtils.getBean(Class.forName(classType)));
            return mapper.writeValueAsString(result);
        } catch (ClassNotFoundException | JsonProcessingException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return "error";
    }



    @RequestMapping("/changeEmail")
    public String changeEmail(@RequestParam String email) {
        Map<String, Object> result = new HashMap<>();
        try {
            mailUtils.setEmailSendToAddress(email);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    @RequestMapping("/viewEmail")
    public String viewEmail() {
        Map<String, Object> result = new HashMap<>();
        try {
            return mailUtils.getEmailSendToAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    @RequestMapping("/refreshBasicAll")
    public String refreshBasicAll() {
        Map<String, Object> result = new HashMap<>();
        try {
            basicDataLoader.loadAllData();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    @RequestMapping("/setInjectStr")
    public String setInjectStr(@RequestParam String injectStr) {
        Map<String, Object> result = new HashMap<>();
        try {
            SQLInjectFilter.setInjStr(injectStr);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    @RequestMapping("/setSqlInjectEnabled")
    public String setInjectStr(@RequestParam boolean sqlInjectEnabled) {
        Map<String, Object> result = new HashMap<>();
        try {
            sqlInjectFilter.setSqlInjectEnabled(sqlInjectEnabled);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    @RequestMapping("/reloadSystemParam")
    public String reloadSystemParam() {
        Map<String, Object> result = new HashMap<>();
        try {
            basicDataLoader.loadSystemParam();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    @RequestMapping("/section/translateWgs2Bd")
    public String translateWgs2Bd() {
        Map<String, Object> result = new HashMap<>();
        try {
            sectionService.translateWgs2Bd();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }
}