ServiceDataInterface.java 10.5 KB
package com.bsth.controller.realcontrol;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsth.controller.realcontrol.dto.ChangePersonCar;
import com.bsth.controller.realcontrol.dto.DftzAndDestroy;
import com.bsth.data.BasicData;
import com.bsth.data.directive.DayOfDirectives;
import com.bsth.data.directive.DirectiveCreator;
import com.bsth.data.directive.GatewayHttpUtils;
import com.bsth.data.gpsdata_v2.GpsRealData;
import com.bsth.data.gpsdata_v2.entity.GpsEntity;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.data.schedule.external.TccExternalService;
import com.bsth.entity.directive.D60;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.repository.directive.D60Repository;
import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 对外的营运数据接口
 * Created by panzhao on 2017/3/15.
 */
@RestController
@RequestMapping("/companyService")
public class ServiceDataInterface {

    private final static String SECRE_KEY = "dVPHJkWUt5FhMT7jrM2dLV7QvlHAmZFd42rs1P0usBx8A7HZki";

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

    @Autowired
    DayOfSchedule dayOfSchedule;

    @Autowired
    DayOfDirectives dayOfDirectives;

    @Autowired
    D60Repository d60Repository;

    @Autowired
    GpsRealData gpsRealData;

    @Autowired
    TccExternalService tccExternalService;

    /**
     * 获取车辆 和 当前执行班次对照信息
     *
     * @return
     */
    @RequestMapping("/execSchList")
    public List<Map<String, Object>> execSchList(@RequestParam String secretKey) {
        if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
            return null;

        List<Map<String, Object>> rs = new ArrayList<>();
        Collection<ScheduleRealInfo> list = dayOfSchedule.execPlanMap().values();

        Map<String, Object> map;
        for (ScheduleRealInfo sch : list) {
            if (null == sch)
                continue;
            map = new HashMap<>();
            map.put("clZbh", sch.getClZbh());
            map.put("jGh", sch.getjGh());
            map.put("jName", sch.getjGh());
            map.put("sGh", sch.getsGh());
            map.put("sName", sch.getsGh());
            map.put("lpName", sch.getLpName());
            map.put("xlBm", sch.getXlBm());
            map.put("xlName", sch.getXlName());
            map.put("xlDir", sch.getXlDir());
            map.put("qdzName", sch.getQdzName());
            map.put("zdzName", sch.getZdzName());
            map.put("fcsj", sch.getFcsj());
            map.put("dfsj", sch.getDfsj());
            map.put("zdsj", sch.getZdsj());
            map.put("bcType", sch.getBcType());
            map.put("remarks", sch.getRemark());
            map.put("status", sch.getStatus());

            //放站班次,放到的站点
            map.put("majorStationName", sch.getMajorStationName());
            rs.add(map);
        }
        return rs;
    }

    @RequestMapping("/getCurrentDayPlan")
    public List<ScheduleRealInfo> getCurrentDayPlan(
            @RequestParam String companyId,
            @RequestParam String workId,
            @RequestParam String secretKey) {

        if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
            return null;

        List<ScheduleRealInfo> all = new ArrayList<>(dayOfSchedule.findByGsbm(companyId)), rs = new ArrayList<>();
        for (ScheduleRealInfo sch : all) {
            if (sch.getGsBm() != null
                    && sch.getGsBm().equals(companyId)
                    && sch.getjGh().equals(workId)) {
                rs.add(sch);
            }
        }
        return rs;
    }

    @RequestMapping("/returnCCInfo")
    public List<ScheduleRealInfo> returnCCInfo(@RequestParam String companyId, @RequestParam String secretKey) {
        if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
            return null;


        List<ScheduleRealInfo> all = new ArrayList<>(dayOfSchedule.findAll()), rs = new ArrayList<>();
        for (ScheduleRealInfo sch : all) {

            if (sch.getBcType().equals("out")
                    && sch.getGsBm() != null
                    && sch.getGsBm().equals(companyId)) {
                rs.add(sch);
            }
        }
        return rs;
    }

    @RequestMapping("/returnJCInfo")
    public List<ScheduleRealInfo> returnJCInfo(@RequestParam String companyId, @RequestParam String secretKey) {
        if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
            return null;


        List<ScheduleRealInfo> all = new ArrayList<>(dayOfSchedule.findAll()), rs = new ArrayList<>();
        for (ScheduleRealInfo sch : all) {
            if (sch.getBcType().equals("in")
                    && sch.getGsBm() != null
                    && sch.getGsBm().equals(companyId)) {
                rs.add(sch);
            }
        }
        return rs;
    }

    /**
     * 获取全量的进出场数据, 仅供接口项目调用。 由接口项目负责对外所有场站输出
     *
     * @return
     */
    @RequestMapping("/findCurrInAndOut")
    public List<ScheduleRealInfo> findCurrInAndOut(@RequestParam String secretKey) {
        if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
            return null;


        List<ScheduleRealInfo> all = new ArrayList<>(dayOfSchedule.findAll()), rs = new ArrayList<>();
        for (ScheduleRealInfo sch : all) {
            if (sch.getBcType().equals("in")
                    || sch.getBcType().equals("out")) {
                rs.add(sch);
            }
        }
        return rs;
    }

    /**
     * 向指定的车辆下发消息短语
     *
     * @return
     */
    @RequestMapping(value = "/send60Phrase", method = RequestMethod.POST)
    public int send60Phrase(@RequestBody Map<String, String> map, @RequestParam String secretKey) {
        try {
            String nbbm = map.get("nbbm");
            String txt = map.get("txt");
            String sender = map.get("sender");
            if (txt.length() > 50)
                txt = txt.substring(0, 50);
            if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
                return -500;

            //车辆和设备号对照
            String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
            if (StringUtils.isEmpty(deviceId))
                return -404;

            //检查设备是否在线
            long t = System.currentTimeMillis();
            GpsEntity gps = gpsRealData.get(deviceId);
            if (null == gps || (t - gps.getServerTimestamp()) > 1000 * 60 * 5)
                return -405;

            Short dispatchInstruct = 0;//消息短语
            D60 d60 = new DirectiveCreator().createD60(nbbm, txt, dispatchInstruct, gps.getUpDown(), gps.getState(), gps.getLineId());
            d60.setSender(sender);
            // 发送指令
            int code = GatewayHttpUtils.postJson(JSON.toJSONString(d60));
            d60.setHttpCode(code);

            if (code != 0)
                d60.setErrorText("网关通讯失败, code: " + code);

            dayOfDirectives.put60(d60);
            return d60.getMsgId();
        } catch (Exception e) {
            logger.error("", e);
            return -500;
        }
    }

    /**
     * 根据msg id 查询指令响应情况
     *
     * @param msgIds
     * @return
     */
    @RequestMapping("/findD60Reply")
    public List<Map<String, Object>> findD60Reply(@RequestParam String msgIds) {
        List<Map<String, Object>> rs = new ArrayList<>();
        try {
            Map<String, Object> map = new HashMap();

            List<String> ids = Splitter.on(",").splitToList(msgIds);
            D60 d60;
            for (String id : ids) {
                if (StringUtils.isEmpty(id))
                    continue;

                d60 = dayOfDirectives.get(Integer.parseInt(id));
                if (null == d60)
                    continue;

                map.put("msgId", d60.getMsgId());
                map.put("deviceReplyTime", d60.getReply46Time());
                map.put("jsyReplyTime", d60.getReply47Time());
                rs.add(map);
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        return rs;
    }

    /**
     * 获取路牌下所有班次
     *
     * @param lineCode
     * @param lpName
     * @return
     */
    @RequestMapping("/findByLpName")
    public List<ScheduleRealInfo> findByLpName(@RequestParam String lineCode, @RequestParam String lpName, @RequestParam String secretKey) {
        try {
            if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
                return null;

            List<ScheduleRealInfo> list = dayOfSchedule.getLpScheduleMap().get(lineCode + "_" + lpName);
            return list;
        } catch (Exception e) {
            logger.error("", e);
        }
        return null;
    }

    /**
     * 调整出场班次待发,并烂掉后续班次(调派用)
     *
     * @param dad
     * @return
     */
    @RequestMapping(value = "/dftzAndDestroy", method = RequestMethod.POST)
    public Map<String, Object> dftzAndDestroy(@RequestBody DftzAndDestroy dad, @RequestParam String secretKey) {
        if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
            return null;
        return tccExternalService.dftz(dad);
    }

    /**
     * 换人换车,(调派用)
     * @return
     */
    @RequestMapping(value = "/tccHrhc", method = RequestMethod.POST)
    public Map<String, Object> hrhc(@RequestBody String jsonStr, @RequestParam String secretKey){

        if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
            return null;

        jsonStr = StringEscapeUtils.unescapeHtml4(jsonStr);
        JSONObject rootObj = JSON.parseObject(jsonStr);

        List<ChangePersonCar> cpcs = JSONArray.parseArray(rootObj.getString("cpcs"), ChangePersonCar.class);
        return tccExternalService.hrhc(cpcs, rootObj.getString("tccCode"), rootObj.getString("userId"));
    }
}