package org.ccpit.business.questionsManage;

import java.util.HashMap;
import java.util.Map;

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

import org.ccpit.base.controller.BaseController;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.logManage.LogInfoService;
import org.ccpit.base.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping(value="/business/questionManage")
public class QuestionController extends BaseController {

	@Autowired
	private QuestionService questionService;
	@Autowired
	private LogInfoService logInfoService;

	@RequestMapping(value ="/goinQuestionPage")
	public ModelAndView getPage() {
		ModelAndView mv = new ModelAndView("business/questionPage");
		return mv;
	}
	
	/**
	 * 查询所有问题
	 * @author 
	 * @param request
	 * @param Question
	 * @return
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/queryAllQuestion")
	public Object queryAllQuestion(HttpServletRequest request,Question question) {
		PageRequest pageRequest = getPage(request);
		String title = request.getParameter("title");
		Page<Question> pageInformation = questionService.queryQuestions(pageRequest, title);
//		pageInformation.getRows().forEach(Question -> {
//			QuestionService.createQuestionsIndex(Question);
//		});
		return questionService.convert(pageInformation);
	}
	/**
	 * 
	 * isExistQuestion:(根据标题查询是否存在). <br/>
	 * @author 
	 * @param request
	 * @return
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/isExistQuestion")
	public Object isExistQuestion(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		String title = request.getParameter("title");
		boolean isExist = questionService.queryQuestionByTitle(title);
		result.put("flag", isExist);
		if(isExist){
			result.put("info", "问题信息已存在，请不要重复录入...");
		}
		return result;
	}
	
	/**
	 * 
	 * addOrUpdateQuestion:(新增或修改). <br/>
	 * @author
	 * @param operate
	 * @param request
	 * @param response
	 * @param Question
	 * @return
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/addOrUpdateQuestion",produces = "application/json;charset=UTF-8")
	public Object addOrUpdateQuestion(HttpServletRequest request,HttpServletResponse response, Question question){
		String result = "";
		String operate = request.getParameter("operate");
		String questionId = request.getParameter("questionId");
		
		boolean flag = questionService.addOrUpdateQuestion(question, operate, questionId, (User)getUserSession(request), request);
		if(flag){
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存成功！\" }";
		}else {
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存失败！\" }";
		}
		return result;
	}
	
	/**
	 * 
	 * delPerson:(这里用一句话描述这个方法的作用). <br/>
	 * @author 
	 * @param request
	 * @return
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/deleteQuestions")
	public Object deleteQuestions(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		boolean flag = questionService.deleteQuestionById(request.getParameter("ids"));
		result.put("flag", flag);
		if(flag){
			result.put("info", "数据删除成功！");
		}else {
			result.put("info", "数据删除失败！");
		}
		return result;
	} 
	
	/**
	 * 
	 * queryPersonById:(通过id查询). <br/>
	 * @author 
	 * @param id
	 * @return
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/queryQuestionById/{id}")
	public Question queryQuestionById(@PathVariable long id){
		return questionService.queryQuestionById(id);
	}
	
}
