package com.kingdee.eas.custom.esign.handler;

import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import com.kingdee.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Pattern;

/**
 * @Description e签宝模板字段列表handler
 * @Date 2025/11/25 14:13
 * @Created by 禾源
 */
public class ESignTemplateFileEntryFieldListHandler extends ListHandler {
    private static final Pattern NUMBER_PATTERN = Pattern.compile("-?\\d+(\\.\\d+)?");


    /**
     * 获取自定义过滤器信息
     *
     * @param request  HTTP请求对象，用于获取请求相关信息
     * @param response HTTP响应对象，用于设置响应相关信息
     * @return FilterInfo 过滤器信息对象
     * @throws SHRWebException 当获取过滤器信息过程中发生错误时抛出异常
     */
    @Override
    protected FilterInfo getCustomFilter(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws SHRWebException {
        // 调用父类方法获取基础过滤器信息
        FilterInfo customFilter = super.getCustomFilter(request, response);
        if (customFilter == null) {
            customFilter = new FilterInfo();
        }

        // 根据parentId参数设置过滤条件
        String parentId = (String) this.getRequestParameter(request, "parentId");
        if (StringUtils.isEmpty(parentId) || NUMBER_PATTERN.matcher(parentId).matches()) {
            // parentId为空或为数字时，添加空值过滤条件
            customFilter.getFilterItems().add(new FilterItemInfo("parent1.id", null));
        } else {
            // parentId存在且不为数字时，添加相等过滤条件
            customFilter.getFilterItems().add(new FilterItemInfo("parent1.id", parentId));
        }
        return customFilter;
    }

}
