package com.zm.order.tool.server.handler.refund;

import com.zm.order.tool.server.builder.SearchRefundRequestBuilder;
import com.zm.order.tool.server.enums.BizTypeEnum;
import com.zm.order.tool.server.model.Refund;
import com.zm.order.tool.server.model.refund.BaseIndexRefund;
import com.zm.order.tool.server.model.refund.CateringIndexRefund;
import com.zm.order.tool.server.query.SearchRefundQuery;
import com.zm.order.tool.server.utils.JacksonUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 餐饮退款单搜索处理器
 *
 * <p>
 *
 * </p>
 *
 * @author nianchang
 * @date 2024/02/21 5:48 下午
 */
@Component
public class RefundCateringBusHandler implements RefundBusHandler {

    @Override
    public Integer bizType() {
        return BizTypeEnum.CATERING.getType();
    }

    @Override
    public BaseIndexRefund buildIndex(Refund refund) {
        // 由ES网关订单kafka实现
        return null;
    }

    @Override
    public List<BaseIndexRefund> parseIndex(SearchHit[] searchHits) {
        List<BaseIndexRefund> list = new ArrayList<>(searchHits.length);
        for (SearchHit searchHit : searchHits) {
            CateringIndexRefund each = JacksonUtils.getObjectFromJsonString(
                    searchHit.getSourceAsString(), CateringIndexRefund.class);
            list.add(each);
        }
        return list;
    }

    @Override
    public SearchSourceBuilder buildSearchSourceBuilder(SearchRefundQuery query) {
        SearchSourceBuilder result = SearchRefundRequestBuilder.buildOrderSearchBuilder(query);
        BoolQueryBuilder boolQueryBuilder = (BoolQueryBuilder) result.query();
        return result;
    }
}
