LineConfigServiceImpl.java 4.33 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 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) {
        Map<String, Object> rs = new HashMap<>();
        try {
            LineConfig conf = lineConfigData.get(lineCode);

            conf.setOutConfig(type);
            //conf.setInConfig(type);
            lineConfigData.set(conf);

            rs.put("status", ResponseCode.SUCCESS);
            rs.put("type", type);
        } 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;
    }
}