package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.jx;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.ISxAttributeCompareFilterService;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;

public class SxAttributeCompareFilterDouble<T> implements ISxAttributeCompareFilterService<T> {

    private String field;

    public SxAttributeCompareFilterDouble(String field) {
        this.field = field;
    }

    @Override
    public Boolean doFilter(T t1, T t2, Class c1, Class c2) {
        try {
            Field field1 = c1.getDeclaredField(field);
            if (null == field1) {
                return false;
            }
            field1.setAccessible(true);
            Double value1 = (Double) field1.get(t1);

            Field field2 = c2.getDeclaredField(field);
            if (null == field2) {
                return false;
            }
            field2.setAccessible(true);
            Double value2 = (Double) field2.get(t2);

            value1 = StringUtils.isEmpty(value1) ? new Double(0) : value1;
            value2 = StringUtils.isEmpty(value2) ? new Double(0) : value2;

            if (MathOperation.compareTo(value1, value2) == 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

}