package cn.com.capcloud.api.action.myself.system.data;

import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.api.action.service.DataSourceService;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.cache.KHCRedisCache;
import cn.com.capcloud.core.lucene.LuceneIndexManager;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.system.user.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataLuceneIndexFieldAction extends DefaultAction {
    @Autowired
    private BaseService baseService;
    @Autowired
    private UserService userService;
    @Autowired
    private ApiService apiService;
    @Autowired
    private KHCRedisCache khcRedisCache;
    @Autowired
    private LuceneIndexManager luceneIndexManager;
    @Autowired
    private DataSourceService dsService;
    private static DataLuceneIndexFieldAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.baseService=this.baseService;
        action.userService=this.userService;
        action.apiService =this.apiService;
        action.khcRedisCache =this.khcRedisCache;
        action.luceneIndexManager =this.luceneIndexManager;
        action.dsService =this.dsService;
    }

    public Map<String,Object> reBuildIndex(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject queryParamJson=JackJSONObject.fromObject(jsonStr).getJackJSONObject("queryParam");
        String pageParentUUID=String.valueOf(queryParamJson.get("page_parent_uuid"));
        Map<String, Object> dsMap=action.dsService.findDataSourceByUuid(pageParentUUID);
        String tableName=String.valueOf(dsMap.get("ds_table_name"));
        String dsUUID=String.valueOf(dsMap.get("uuid"));
        action.luceneIndexManager.reBuildIndex(tableName,dsUUID);//重新创建索引
        Map<String,Object> map=new HashMap<>();
        map.put("message","重建索引成功");
        return map;
    }
}

