AlarmAdasServiceImpl.java 2.4 KB
package com.bsth.service.alarm.impl;

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

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

@Service
public class AlarmAdasServiceImpl implements AlarmAdasService {
    @Override
    public List<AlarmADASVo> list(Map<String, String> map, int page, int size, String order, int direction) {
        List<AlarmADASVo> alarmAdasVoList = new ArrayList<>();
        for (AlarmADASVo alarmADASVo : AlarmCenter.findAllAdas()) {
            try {
                if (filter(alarmADASVo, map)) {
                    alarmAdasVoList.add(alarmADASVo);
                }
            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        return alarmAdasVoList;
    }

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

            AlarmADASVo other = mapper.convertValue(param, AlarmADASVo.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;
                        }
                    }
                }
            }
        } else {
            result = false;
        }

        return result;
    }
}