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

import cn.com.capcloud.api.action.service.ApiService;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataLuceneIndexFieldAction extends DefaultAction {
    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private ApiService apiService;
    @Resource
    private KHCRedisCache khcRedisCache;
    @Resource
    private LuceneIndexManager luceneIndexManager;
    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;
    }

    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.apiService.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;
    }
}
