package com.ssj.aijia.openapi.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion;
import com.ssj.framework.core.security.annotation.IgnoreSecurity;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.kmt.service.KmtExerciseQuestionService;

import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@Controller
@RequestMapping("/api/es")
public class EsDataController {
	
	@Reference(version = "1.0" ,timeout = 60000)
	private KmtExerciseQuestionService kmtExerciseQuestionService;

	/*
	 * python新增的练习册题目回调同步到ES中
	 * 参数：ids  id列表，用;号相隔
	 * 
	 */
	@IgnoreSecurity
    @RequestMapping(value="/save")
	@ResponseBody
	public ResponseEntity aiDataImport(Model model,HttpServletRequest request, HttpServletResponse response,String ids)throws Exception {
		ResponseEntity responseEntity = new ResponseEntity();
        if (StringUtils.isNotEmpty(ids)) {
        	String[] ids_=ids.split(";");
        	KmtExerciseQuestion kmtExerciseQuestion=null;
        	for (String id : ids_) {
        		try {
	        		kmtExerciseQuestion=kmtExerciseQuestionService.getById(id);
	        		if(kmtExerciseQuestion!=null) {
	        			kmtExerciseQuestionService.saveToEs(kmtExerciseQuestion);
	        		}
        		} catch (Exception e) {
        			e.printStackTrace();
				}
			}
			responseEntity.success(ResponseConstant.CODE_999, "成功");
		} else {
            responseEntity.failure(ResponseConstant.CODE_000, "ids不能为空，失败");
        }
        return responseEntity;
	}


	/*
	 * 全量练习册题目同步到ES中
	 */
	@IgnoreSecurity
    @RequestMapping(value="/refreshAll")
	@ResponseBody
	public ResponseEntity refreshAll(Model model,HttpServletRequest request, HttpServletResponse response,String date)throws Exception {
		ResponseEntity responseEntity = new ResponseEntity();
	    kmtExerciseQuestionService.refreshAllToEs(date);
        return responseEntity;
	}
	

}
