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

import com.liangjian.dataplatform.member.dao.secondary.AreaDataEsRepository;
import com.liangjian.dataplatform.member.dao.secondary.AreaDataTagEsRepository;
import com.liangjian.dataplatform.member.entity.po.elasticsearch.AreaData;
import com.liangjian.dataplatform.member.entity.po.elasticsearch.AreaDataTag;
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.IAreaDataService;
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;

import static org.elasticsearch.index.query.QueryBuilders.matchPhrasePrefixQuery;

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

    @Autowired
    private AreaDataEsRepository areaDataEsRepository;

    @Autowired
    private AreaDataTagEsRepository areaDataTagEsRepository;

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

        List<BirdGirdFilter> filters = request.getFilters().stream()
                .filter(i -> i.getValue() != null && !i.getValue().isEmpty())
                .collect(Collectors.toList());
        request.setFilters(filters);
        BoolQueryBuilder qb = request.filtersToEsQuery();
        if (filters.size() > 0) {
            qb = QueryBuilders.boolQuery().should(matchPhrasePrefixQuery("ParentAreaName", filters.get(0).getValue()))
                    .should(matchPhrasePrefixQuery("AreaName", filters.get(0).getValue()));
        }
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(qb).withPageable(pageable).build();
        Page<AreaData> paged = areaDataEsRepository.search(searchQuery.getQuery(), pageable);
        BirdGirdTableResponse2<AreaData> response2 = new BirdGirdTableResponse2<>(paged.getContent(), paged.getTotalElements());
        return response2;
    }

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

    @Override
    public boolean deleteAreaDataTag(String id) {
        areaDataTagEsRepository.deleteById(id);
        return true;
    }

    @Override
    public boolean saveAllAreaDataTag(ArrayList<AreaDataTag> areaDataTags) {
        areaDataTagEsRepository.saveAll(areaDataTags);
        return true;
    }
}
