package com.ltgk.common.common;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.GroupPage;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;

import static javafx.scene.input.KeyCode.T;

/**
 * @Author: wan kun
 * @Description: TODO
 * @Date: 2024/9/23 9:22
 * @Version: 1.0
 */
@Log4j2
@Component
public class SolrCommon {
    @Autowired
    private SolrTemplate solrTemplate;

    private static SolrCommon solrCommon;

    @PostConstruct
    public void init() {
        solrCommon = this;
        solrCommon.solrTemplate = this.solrTemplate;
    }

    /**
     * 基础查询
     *
     * @param solrController 文档名
     * @param solrQuery      查询条件
     * @param object         返回内容实体类
     * @return
     */
    public static Page solrSearchAll(String solrController, String solrQuery, Class object) {
        Query queryObject = new SimpleQuery(solrQuery);
        Page query = solrCommon.solrTemplate.query(solrController, queryObject, object);
        return query;
    }

    /**
     * 基础查询
     *
     * @param solrController
     * @param solrQuery
     * @param object
     * @return
     */
    public static Page solrSearchByQuery(String solrController, Query solrQuery, Class object) {
        Page query = solrCommon.solrTemplate.query(solrController, solrQuery, object);
        return query;
    }

    /**
     * 基础查询
     *
     * @param solrController
     * @param solrQuery
     * @param object
     * @return
     */
    public static Page solrSearchByQuery(String solrController, SimpleQuery solrQuery, Class object) {
        Page query = solrCommon.solrTemplate.query(solrController, solrQuery, object);
        return query;
    }

    /**
     * 基础查询
     *
     * @param solrController
     * @param solrQuery
     * @param object
     * @return
     */
    public static GroupPage queryForGroupPage(String solrController, SimpleQuery solrQuery, Class object) {
        GroupPage query = solrCommon.solrTemplate.queryForGroupPage(solrController, solrQuery, object);
        return query;
    }

    /**
     * 创建数据
     *
     * @param solrController 文档名
     * @param T              创建内容
     * @return
     */
    public static Boolean solrCreate(String solrController, Object T) {
        try {
            solrCommon.solrTemplate.saveBean(solrController, T);
            solrCommon.solrTemplate.commit(solrController);
            return true;
        } catch (Exception ex) {
            log.error("SolrCommon:solrCreate -- solr插入数据失败,请求参数{},失败原因{}", JSONObject.toJSONString(T), ex.getMessage());
            return false;
        }
    }

    /**
     * 删除数据
     *
     * @param solrController 文档名
     * @param solrQuery      请求参数
     * @return
     */
    public static Boolean solrDelete(String solrController, String solrQuery) {
        Query queryObject = new SimpleQuery(solrQuery);
        try {
            solrCommon.solrTemplate.delete(solrController, queryObject);
            solrCommon.solrTemplate.commit(solrController);
            return true;
        } catch (Exception ex) {
            log.error("SolrCommon:solrDelete -- solr删除数据失败,请求参数{},失败原因{}", solrQuery, ex.getMessage());
            return false;
        }
    }

    public static Boolean solrCreateByList(String solrController, List<?> object) {
        try {
            solrCommon.solrTemplate.saveBeans(solrController, object);
            solrCommon.solrTemplate.commit(solrController);
            return true;
        } catch (Exception ex) {
            log.error("SolrCommon:solrCreateByList -- solr插入数据失败,请求参数{},失败原因{}", JSONObject.toJSONString(T), ex.getMessage());
            return false;
        }
    }

    public static String getSolrQuerySql(HashMap<String, Object> map) {
        if (ObjectUtils.isEmpty(map))
            return null;
        return "";
    }
}
