package cn.novalon.manage.db.dao;

import org.junit.jupiter.api.Test;
import org.springframework.data.relational.core.query.Criteria;

class QueryUtilOrTest {

    @Test
    void testOrCriteriaConstruction() {
        String[] blurrys = {"username", "email"};
        String val = "search";
        
        // 测试当前实现
        Criteria orCriteria = null;
        for (int i = 0; i < blurrys.length; i++) {
            String s = blurrys[i];
            if (i == 0) {
                orCriteria = Criteria.where(s).like("%" + val + "%");
            } else {
                orCriteria = orCriteria.or(s).like("%" + val + "%");
            }
        }
        
        System.out.println("当前实现的Criteria: " + orCriteria);
        System.out.println("Criteria类型: " + orCriteria.getClass().getName());
        
        // 测试链式调用
        Criteria chainedCriteria = Criteria.where("username").like("%" + val + "%")
                .or("email").like("%" + val + "%");
        
        System.out.println("链式调用的Criteria: " + chainedCriteria);
        System.out.println("链式调用类型: " + chainedCriteria.getClass().getName());
        
        // 测试是否相等
        System.out.println("两种实现是否相同: " + orCriteria.equals(chainedCriteria));
        
        // 测试toString
        System.out.println("当前实现toString: " + orCriteria.toString());
        System.out.println("链式调用toString: " + chainedCriteria.toString());
    }
    
    @Test
    void testOrCriteriaWithThreeFields() {
        String[] blurrys = {"username", "email", "phone"};
        String val = "test";
        
        Criteria orCriteria = null;
        for (int i = 0; i < blurrys.length; i++) {
            String s = blurrys[i];
            if (i == 0) {
                orCriteria = Criteria.where(s).like("%" + val + "%");
            } else {
                orCriteria = orCriteria.or(s).like("%" + val + "%");
            }
        }
        
        System.out.println("三个字段的OR条件: " + orCriteria);
        
        // 链式调用
        Criteria chainedCriteria = Criteria.where("username").like("%" + val + "%")
                .or("email").like("%" + val + "%")
                .or("phone").like("%" + val + "%");
        
        System.out.println("三个字段链式调用: " + chainedCriteria);
    }
}
