package com.tmerclub.cloud.search.builder;

import cn.hutool.core.util.ObjectUtil;
import com.tmerclub.cloud.api.dto.EsPageDTO;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.local.dto.CueProductSearchDTO;
import com.tmerclub.cloud.search.constant.EsConstant;
import com.tmerclub.cloud.search.constant.EsIndexEnum;
import com.tmerclub.cloud.search.util.EsSearchUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 本地生活-球杆商品搜索请求构建
 *
 * @author: frank
 * @create: 2025-04-13
 **/
@Component
public class LocalCueProductSearchRequestBuilder {

    private static final Logger log = LoggerFactory.getLogger(LocalCueProductSearchRequestBuilder.class);

    /**
     * 通过搜索信息分页搜索es数据的信息
     *
     * @param pageDTO             分页数据
     * @param cueProductSearchDTO 商品搜索条件
     * @return 搜索结果
     */
    public SearchResponse pageSearchResult(EsPageDTO pageDTO, CueProductSearchDTO cueProductSearchDTO) {
        //1、准备检索请求
        SearchRequest searchRequest = buildSearchRequest(pageDTO, cueProductSearchDTO);
        return EsSearchUtil.search(searchRequest);
    }

    /**
     * 准备检索请求
     *
     * @param pageDTO 分页参数
     * @param param   搜索参数
     * @return 搜索结果
     */
    private SearchRequest buildSearchRequest(EsPageDTO pageDTO, CueProductSearchDTO param) {

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        Integer lang = I18nMessage.getLang();

        // 构建bool-query
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        // 关键字搜索
        keywordSearch(param, boolQueryBuilder, lang);

        searchSourceBuilder.query(boolQueryBuilder);

        // 分页
        if (Objects.nonNull(pageDTO)) {
            searchSourceBuilder.from((pageDTO.getPageNum() - 1) * pageDTO.getPageSize());
            searchSourceBuilder.size(pageDTO.getPageSize());
        }

        log.info("构建的DSL语句 {}", searchSourceBuilder);

        return new SearchRequest(new String[]{EsIndexEnum.CUE_PRODUCT.value()}, searchSourceBuilder);
    }

    /**
     * 关键字搜索
     */
    private void keywordSearch(CueProductSearchDTO param, BoolQueryBuilder boolQueryBuilder, Integer lang) {
        BoolQueryBuilder keywordShouldQuery = QueryBuilders.boolQuery();
        if (ObjectUtil.isNotEmpty(param.getProductName())) {
            keywordShouldQuery.must(QueryBuilders.wildcardQuery(EsConstant.CUE_PRODUCT_NAME, "*" + param.getProductName() + "*"));
        }
        if(ObjectUtil.isNotEmpty(param.getProductId())){
            keywordShouldQuery.must(QueryBuilders.termQuery(EsConstant.CUE_PRODUCT_ID, param.getProductId()));
        }
        keywordShouldQuery.must(QueryBuilders.termQuery(EsConstant.CUE_BRAND_ID, param.getBrandId()));
        keywordShouldQuery.must(QueryBuilders.termQuery(EsConstant.CUE_TYPE_ID, param.getTypeId()));
        keywordShouldQuery.must(QueryBuilders.termQuery(EsConstant.CUE_SERIES_ID, param.getSeriesId()));

        boolQueryBuilder.must(keywordShouldQuery);
    }
}
