AlarmDsmServiceImpl.java 2.27 KB
package com.bsth.service.alarm.impl;

import com.bsth.data.alarm.AlarmCenter;
import com.bsth.message.entity.AlarmADASVo;
import com.bsth.message.entity.AlarmDSMVo;
import com.bsth.service.alarm.AlarmDsmService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.*;

@Service
public class AlarmDsmServiceImpl implements AlarmDsmService {
    @Override
    public List<AlarmDSMVo> list(Map<String, String> map, int page, int size, String order, int direction) {
        List<AlarmDSMVo> alarmDSMVoList = new ArrayList<>();
        for (AlarmDSMVo alarmDSMVo : AlarmCenter.findAllDsm()) {
            try {
                if (filter(alarmDSMVo, map)) {
                    alarmDSMVoList.add(alarmDSMVo);
                }
            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        return alarmDSMVoList;
    }

    private boolean filter(Object obj, Map<String, String> param) throws NoSuchFieldException, IllegalAccessException {
        boolean result = true;
        ObjectMapper mapper = new ObjectMapper();
        if (param.get("lines") != null) {
            AlarmDSMVo alarmDSMVo = (AlarmDSMVo) obj;
            if (param.get("lines").toString().indexOf(String.format("%s,", alarmDSMVo.getLineCode())) < 0) {
                return false;
            }
        }

        AlarmDSMVo other = mapper.convertValue(param, AlarmDSMVo.class);
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Map.Entry<String, String> entry : param.entrySet()) {
            String key = entry.getKey(), val = entry.getValue();
            if (StringUtils.isEmpty(val)) {
                continue;
            }

            for (Field field : fields) {
                if (field.getName().equals(key)) {
                    field.setAccessible(true);
                    Object fv1 = field.get(obj), fv2 = field.get(other);
                    if (!fv2.equals(fv1)) {
                        return false;
                    }
                }
            }
        }

        return result;
    }
}