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.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;

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

    private String field;

    public SxAttributeCompareFilterString(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);
            String value1 = (String) field1.get(t1);

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

            value1 = StringUtils.isEmpty(value1) ? MesPcnExtConstWords.EMPTY : value1;
            value2 = StringUtils.isEmpty(value2) ? MesPcnExtConstWords.EMPTY : value2;

            if (value1.equals(value2)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

}