package com.ruoyi.biemo.business.service;

import com.github.pagehelper.util.StringUtil;
import com.ruoyi.biemo.business.domain.Category;
import com.ruoyi.biemo.business.domain.event.CategoryDeleteEvent;
import com.ruoyi.biemo.business.domain.event.CategorySaveEvent;
import com.ruoyi.biemo.core.page.Page;
import com.ruoyi.biemo.elasticsearch.util.EsService;
import com.ruoyi.biemo.mongodb.utils.MongoHelper;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

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

/**
 * @author makesoft
 * @version 1.0
 * @date 2021/1/14 11:59
 */
@Service
public class CategoryService extends EsService<Category> {

    @Autowired
    private MongoHelper mongoHelper;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    protected Category loadData(SearchSourceBuilder context, SearchHit hit) {
        String id = hit.getId();
        if (Objects.isNull(id) || StringUtil.isEmpty(id)) return null;
        return mongoHelper.findById(id,Category.class);
    }
    @Override
    public List<Category> batchLoadData(SearchSourceBuilder context, SearchHit[] hitArr) {
        List<String> ids = new ArrayList<>();
        for(SearchHit hit:hitArr){
            String id = hit.getId();
            if (Objects.isNull(id) || StringUtil.isEmpty(id)) continue;
            ids.add(id);
        }
        return mongoHelper.findListByIds(ids,Category.class);
    }
    /**
     * 查询分类管理
     *
     * @param id 分类管理主键
     * @return 分类管理
     */
    public Category selectCategoryById(String id)
    {
        if(id==null||id.trim().length()==0){
            return null;
        }
//        List<Category> categories = esLambdaQuery().eq(Category::getId,id).query();
        List<Category> categories = esLambdaQuery().getDoc_InfoBy_Id(Category::getId,id).query();
        return categories!=null&&categories.size()>0?categories.get(0):null;
    }

    /**
     * 查询分类管理列表
     *
     * @param category 分类管理
     * @return 分类管理
     */
    public List<Category> selectCategoryList(Category category)
    {
        return esLambdaQuery().eqAll(category).query();
    }

    /**
     * 查询分类管理分页列表
     *
     * @param category 分类管理
     * @return 分类管理
     */
    public Page<Category> selectCategoryPage(Category category, Page<Category> page){
        return esLambdaQuery().eqAll(category).sort(Category::getOrderNum, SortOrder.ASC).page(page.getPageNum(),page.getPageSize()).queryPage(true);
    }

    /**
     * 新增分类管理
     *
     * @param category 分类管理
     * @return 结果
     */
    public String insertOrUpdateCategory(Category category)
    {
        String id = mongoHelper.insert(category);
        category.setId(id);
        applicationContext.publishEvent(new CategorySaveEvent(this,category));
        return id;
    }


    /**
     * 批量删除分类管理
     *
     * @param ids 需要删除的分类管理主键
     * @return 结果
     */
    public void deleteCategoryByIds(String[] ids)
    {
        List<String> list  = Arrays.stream(ids).collect(Collectors.toList());
        mongoHelper.deleteByIds(list, Category.class);
        applicationContext.publishEvent(new CategoryDeleteEvent(this,list));

    }

    /**
     * 删除分类管理信息
     *
     * @param id 分类管理主键
     * @return 结果
     */
    public void deleteCategoryById(String id)
    {
        mongoHelper.deleteById(id,Category.class);
        applicationContext.publishEvent(new CategoryDeleteEvent(this,id));
    }

    @EventListener(classes = {CategorySaveEvent.class})
    public void saveIndex(ApplicationEvent event) {
        CategorySaveEvent saveEvent = (CategorySaveEvent)event;
        List<Category> list = saveEvent.getList();
        insertBatch(list);
    }

    @EventListener(classes = {CategoryDeleteEvent.class})
    public void delIndex(ApplicationEvent event) {
        CategoryDeleteEvent saveEvent = (CategoryDeleteEvent)event;
        List<String> ids = saveEvent.getIds();
        deleteBatch(ids);
    }
}
