package com.tmerclub.cloud.search.manager;

import com.tmerclub.cloud.api.dto.EsPageDTO;
import com.tmerclub.cloud.api.vo.EsPageVO;
import com.tmerclub.cloud.common.local.dto.CueProductSearchDTO;
import com.tmerclub.cloud.common.local.vo.CueProductSearchVO;
import com.tmerclub.cloud.search.builder.LocalCueProductSearchRequestBuilder;
import com.tmerclub.cloud.search.builder.LocalCueProductSearchResponseBuilder;
import jakarta.annotation.Resource;
import org.elasticsearch.action.search.SearchResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 本地生活-球杆商品搜索
 *
 * @author: frank
 * @create: 2025-04-13
 **/
@Component
public class LocalCueProductSearchManager {
    private static final Logger log = LoggerFactory.getLogger(LocalCueProductSearchManager.class);

    @Resource
    LocalCueProductSearchRequestBuilder localCueProductSearchRequestBuilder;
    @Resource
    LocalCueProductSearchResponseBuilder localCueProductSearchResponseBuilder;

    /**
     * 分页搜索
     *
     * @param pageDTO             分页信息
     * @param cueProductSearchDTO 搜索条件
     * @return EsPageVO<CueProductSearchVO>
     */
    public EsPageVO<CueProductSearchVO> page(EsPageDTO pageDTO, CueProductSearchDTO cueProductSearchDTO) {
        SearchResponse response = localCueProductSearchRequestBuilder.pageSearchResult(pageDTO, cueProductSearchDTO);
        return buildSearchResult(pageDTO, response);
    }


    /**
     * 构建搜索结果数据
     *
     * @param pageDTO  分页信息
     * @param response 搜索结果
     * @return EsPageVO<CueProductSearchVO>
     */
    private EsPageVO<CueProductSearchVO> buildSearchResult(EsPageDTO pageDTO, SearchResponse response) {
        EsPageVO<CueProductSearchVO> esPageVO = new EsPageVO<>();

        // 1、返回的所有查询到的商品
        List<CueProductSearchVO> cueProductSearchVOS = localCueProductSearchResponseBuilder.getProductSearchList(response);
        esPageVO.setList(cueProductSearchVOS);

        // 2、分页信息
        localCueProductSearchResponseBuilder.buildSearchPage(pageDTO, esPageVO, response);
        return esPageVO;
    }
}
