LineConfigServiceImpl.java 8.48 KB
package com.bsth.service.realcontrol.impl;

import com.bsth.common.ResponseCode;
import com.bsth.data.LineConfigData;
import com.bsth.entity.realcontrol.LineConfig;
import com.bsth.repository.realcontrol.LineConfigRepository;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.realcontrol.LineConfigService;
import com.google.common.base.Splitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LineConfigServiceImpl extends BaseServiceImpl<LineConfig, Integer> implements LineConfigService {

    @Autowired
    LineConfigRepository lineConfigRepository;

    @Autowired
    LineConfigData lineConfigData;

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

    @Override
    public Map<String, Object> check(String[] codeArray) {
        Map<String, Object> rs = new HashMap<>();
        List<String> notArr = new ArrayList<>();

        for (String lineCode : codeArray) {
            if (null == lineConfigData.get(lineCode + ""))
                notArr.add(lineCode);
        }

        if (notArr.size() > 0) {
            rs.put("status", 1);
            rs.put("not", notArr);
        } else
            rs.put("status", 0);
        return rs;
    }

    @Override
    public Integer init(String lineCode) throws Exception {
        LineConfig conf = lineConfigData.get(lineCode);

        if (conf == null)
            lineConfigData.init(lineCode);

        return 1;
    }

    @Override
    public Map<String, Object> editStartOptTime(String time, String lineCode) {
        Map<String, Object> rs = new HashMap<>();
        try {
            LineConfig conf = lineConfigData.get(lineCode);
            conf.setStartOpt(time);
            lineConfigData.set(conf);

            rs.put("status", ResponseCode.SUCCESS);
            rs.put("time", time);
        } catch (Exception e) {
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", e.getMessage());
            logger.error("", e);
        }
        return rs;
    }

    @Override
    public Map<String, Object> editOutTimeType(String lineCode, int type, String parkCode, String stationCode) {
        Map<String, Object> rs = new HashMap<>();
        try {
            LineConfig conf = lineConfigData.get(lineCode);

            conf.setOutConfig(type);
            if(type == 2){
                conf.setTwinsParks(parkCode);
                conf.setTwinsStations(stationCode);
            }
            lineConfigData.set(conf);

            rs.put("status", ResponseCode.SUCCESS);
            rs.put("conf", conf);
        } catch (Exception e) {
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", e.getMessage());
            logger.error("", e);
        }
        return rs;
    }

    @Override
    public LineConfig getByLineCode(String lineCode) {
        return lineConfigData.get(lineCode);
    }

    @Override
    public Map<String, Object> enableInParkForSource(String lineCode, int enable) {
        Map<String, Object> rs = new HashMap<>();
        try {
            LineConfig conf = lineConfigData.get(lineCode);

            conf.setInParkForSource(enable == 1);
            lineConfigData.set(conf);

            rs.put("status", ResponseCode.SUCCESS);
            rs.put("enable", enable);
        } catch (Exception e) {
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", e.getMessage());
            logger.error("", e);
        }
        return rs;
    }

    @Override
    public Map<String, Object> bufferTimeDiff(String lineCode, String field, String value) {

        Map<String, Object> rs = new HashMap<>();
        try {
            LineConfig conf = lineConfigData.get(lineCode);
            Field f = conf.getClass().getDeclaredField(field);
            f.setAccessible(true);
            f.setInt(conf, Integer.parseInt(value));

            lineConfigData.set(conf);

            rs.put("status", ResponseCode.SUCCESS);
            rs.put("field", field);
            rs.put("value", value);
        } catch (Exception e) {
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", e.getMessage());
            logger.error("", e);
        }
        return rs;
    }

    @Override
    public Map<String, Object> yjtkSet(Map<String, String> map) {
        String lineCode = map.get("lineCode").toString();
        int enableYjtk = Integer.parseInt(map.get("enableYjtk").toString());

        Map<String, Object> rs = new HashMap<>();
        try {
            LineConfig conf = lineConfigData.get(lineCode);

            if(enableYjtk == 1){
                String yjtkStart = map.containsKey("yjtkStart") ? map.get("yjtkStart").toString() : "00:00";
                String yjtkEnd = map.containsKey("yjtkEnd") ? map.get("yjtkEnd").toString() : "23:59";
                int upStopMinute = Integer.parseInt(map.get("upStopMinute").toString());
                int downStopMinute = Integer.parseInt(map.get("downStopMinute").toString());

                conf.setEnableYjtk(true);
                conf.setYjtkStart(yjtkStart);
                conf.setYjtkEnd(yjtkEnd);
                conf.setUpStopMinute(upStopMinute);
                conf.setDownStopMinute(downStopMinute);
            }
            else
                conf.setEnableYjtk(false);

            lineConfigData.set(conf);

            rs.put("status", ResponseCode.SUCCESS);
            rs.put("conf", conf);
        } catch (Exception e) {
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", e.getMessage());
            logger.error("", e);
        }
        return rs;
    }

    @Override
    public Map<String, Object> parkAndStationSet(Map<String, String> map) {
        String lineCode = map.get("lineCode").toString();
        String twinsPark = map.get("twinsPark").toString();
        String twinsStation = map.get("twinsStation").toString();

        Map<String, Object> rs = new HashMap<>();
        try {
            LineConfig conf = lineConfigData.get(lineCode);
            conf.setTwinsParks(twinsPark);
            conf.setTwinsStations(twinsStation);

            lineConfigData.set(conf);

            rs.put("status", ResponseCode.SUCCESS);
            rs.put("conf", conf);
        } catch (Exception e) {
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", e.getMessage());
            logger.error("", e);
        }
        return rs;
    }

    @Override
    public Map<String, Object> findByIdx(String idx) {
        Map<String, Object> rs = new HashMap();
        try{
            List<LineConfig> list = new ArrayList<>();
            List<String> ids = Splitter.on(",").splitToList(idx);

            for(String id : ids){
                list.add(lineConfigData.get(id));
            }

            rs.put("status", ResponseCode.SUCCESS);
            rs.put("list", list);
        }catch (Exception e){
            rs.put("status", ResponseCode.ERROR);
            logger.error("", e);
        }
        return rs;
    }

    @Override
    public Map<String, Object> setAutoExec(Map<String, String> map) {
        String lineCode = map.get("lineCode").toString();
        boolean autoExec = Boolean.parseBoolean(map.get("autoExec"));
        int autoExecOffset = Integer.parseInt(map.get("autoExecOffset"));

        Map<String, Object> rs = new HashMap<>();
        try {
            LineConfig conf = lineConfigData.get(lineCode);
            conf.setAutoExec(autoExec);
            conf.setAutoExecOffset(autoExecOffset);

            lineConfigData.set(conf);
            rs.put("status", ResponseCode.SUCCESS);
            rs.put("conf", conf);
        } catch (Exception e) {
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", e.getMessage());
            logger.error("", e);
        }
        return rs;
    }

    @Override
    public Map<String, Object> setReadReverse(int status, String lineCode) {
        Map<String, Object> rs = new HashMap<>();
        try {
            LineConfig conf = lineConfigData.get(lineCode);
            conf.setReadReverse(status==1?true:false);

            lineConfigData.set(conf);
            rs.put("status", ResponseCode.SUCCESS);
            rs.put("conf", conf);
        } catch (Exception e) {
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", e.getMessage());
            logger.error("", e);
        }
        return rs;
    }
}