package com.stdproject.common;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Set;

/**
 * @Date: 2025/6/5 9:21
 * @Description:
 */
public class QueryWrapperBuilder {

    public static <T> QueryWrapper<T> buildQueryWrapper(PageRequest pageRequest) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();

        // 过滤条件
        if (pageRequest.getFilters() != null && !pageRequest.getFilters().isEmpty()) {
            for (FilterCondition condition : pageRequest.getFilters()) {
                String field = condition.getField();
                String op = condition.getOperator();
                Object val = condition.getValue();

                if (!StringUtils.hasText(field) || ObjectUtil.isEmpty(val)) continue;
                switch (op.toLowerCase()) {
                    case "eq":
                        queryWrapper.eq(field, val);
                        break;
                    case "like":
                        queryWrapper.like(field, val);
                        break;
                    case "left_like":
                        queryWrapper.likeLeft(field, val);
                        break;
                    case "right_like":
                        queryWrapper.likeRight(field, val);
                        break;
                    case "ne":
                        queryWrapper.ne(field, val);
                        break;
                    case "gt":
                        queryWrapper.gt(field, val);
                        break;
                    case "ge":
                        queryWrapper.ge(field, val);
                        break;
                    case "lt":
                        queryWrapper.lt(field, val);
                        break;
                    case "le":
                        queryWrapper.le(field, val);
                        break;
                    case "in":
                        if (val instanceof Collection) {
                            queryWrapper.in(field, (Collection<?>) val);
                        }
                        break;
                    case "notin":
                        if (val instanceof Collection) {
                            queryWrapper.notIn(field, (Collection<?>) val);
                        }
                        break;
                    default:
                        // 忽略不支持的操作符
                        break;
                }
            }
        }

        // 排序字段白名单校验
//        String orderBy = pageRequest.getOrderBy();
//        if (StringUtils.hasText(orderBy) && isAllowedOrderField(orderBy)) {
//            if ("asc".equalsIgnoreCase(pageRequest.getOrderDirection())) {
//                queryWrapper.orderByAsc(orderBy);
//            } else {
//                queryWrapper.orderByDesc(orderBy);
//            }
//        }
        return queryWrapper;
    }

    private static final Set<String> ALLOWED_ORDER_FIELDS = Set.of(
            "username", "nickname", "email", "phone", "lastmodifydate", "age"
    );

    private static boolean isAllowedOrderField(String field) {
        return ALLOWED_ORDER_FIELDS.contains(field);
    }

}
