package com.liangjian.dataplatform.member.service.impl;

import com.liangjian.dataplatform.member.dao.secondary.ProductDataEsRepository;
import com.liangjian.dataplatform.member.dao.secondary.ProductDataTagEsRepository;
import com.liangjian.dataplatform.member.entity.po.elasticsearch.ProductData;
import com.liangjian.dataplatform.member.entity.po.elasticsearch.ProductDataTag;
import com.liangjian.dataplatform.member.entity.pojo.BirdGirdFilter;
import com.liangjian.dataplatform.member.entity.vo.request.BirdGirdRequest;
import com.liangjian.dataplatform.member.entity.vo.response.BirdGirdTableResponse2;
import com.liangjian.dataplatform.member.service.IProductDataService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author 黄树文
 * @date 2020/3/10 10:25
 */
@Service
public class ProductDataServiceImpl implements IProductDataService {

    @Autowired
    private ProductDataEsRepository productDataEsRepository;

    @Autowired
    private ProductDataTagEsRepository ProductDataTagEsRepository;

    @Override
    public BirdGirdTableResponse2<ProductData> queryByCondition(BirdGirdRequest request) {
        Integer page = request.getPageIndex();
        Integer size = request.getPageSize();
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.ASC, "ProductCode");

        List<BirdGirdFilter> filters = request.getFilters().stream()
                .filter(i -> i.getValue() != null && !i.getValue().isEmpty())
                .collect(Collectors.toList());
        request.setFilters(filters);
        BoolQueryBuilder qb = request.filtersToEsQuery();
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(qb).withPageable(pageable).build();
        Page<ProductData> paged = productDataEsRepository.search(searchQuery.getQuery(), pageable);
        BirdGirdTableResponse2<ProductData> response2 = new BirdGirdTableResponse2<>(paged.getContent(), paged.getTotalElements());
        return response2;
    }

    @Override
    public BirdGirdTableResponse2<ProductDataTag> findProductDataTag(String keyword, Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page, size);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchQuery("ObjectId",keyword));
        Page<ProductDataTag> paged = ProductDataTagEsRepository.search(sourceBuilder.query(), pageable);
        BirdGirdTableResponse2<ProductDataTag> response2 = new BirdGirdTableResponse2<>(paged.getContent(), paged.getTotalElements());
        return response2;
    }

    @Override
    public boolean deleteProductDataTagById(String id) {
        ProductDataTagEsRepository.deleteById(id);
        return true;
    }

    @Override
    public boolean saveAllProductDataTag(ArrayList<ProductDataTag> productDataTags) {
        ProductDataTagEsRepository.saveAll(productDataTags);
        return true;
    }
}
