package com.wechat.controller;

import java.io.IOException;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.wechat.callback.tencentrequests.UserSentTextRequest;
import com.wechat.common.utils.MvcUtil;
import com.wechat.global.base.BaseController;
import com.wechat.model.dbEntity.TdtgFaqInfo;
import com.wechat.model.responseDto.JsonResp;
import com.wechat.service.FaqService;


/**
 * FAQ问答
 * 
 */
@Controller
@RequestMapping("faq")
public class FaqController extends BaseController {

    @Resource
    private FaqService faqService;

    /**
     * 通过用户发送的信息,取得FAQ的问题
     * 
     * @param userSentTextRequest 用户发送的信息
     */
    @RequestMapping(value = "getFaqInfo", method = RequestMethod.POST)
	public void getFaqInfoByMsg(@RequestBody UserSentTextRequest userSentTextRequest, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JsonResp<TdtgFaqInfo> ret = new JsonResp<TdtgFaqInfo>();
		// 通过用户发送的信息，检索FAQ内容
		TdtgFaqInfo faqInfo = faqService.getInfoByKeyWord(userSentTextRequest.getContent());
		ret.setInfo(faqInfo);
		
		String json = JSON.toJSONString(ret, true);
        try {
            MvcUtil.writeJson(request, response, json);
        } catch (IOException e) {
            // TODO Auto-generated catch block
           log.error(e);
        }

	}
    
    /**
     * FAQ信息取得ID查询
     * 
     * @param int ID信息
     */
    @RequestMapping(value = "getFaqInfoById", method = RequestMethod.GET)
    public void getFaqInfoById(@RequestParam int id, HttpServletRequest request, HttpServletResponse response)throws Exception {
        JsonResp<String> ret = new JsonResp<String>();
        TdtgFaqInfo faqInfo = faqService.getInfoById(id);
        if(faqInfo != null)
        {
        	ret.setInfo(faqInfo.getNewsContent());
        }
        
    	String json = JSON.toJSONString(ret, true);
        try {
            MvcUtil.writeJson(request, response, json);
        } catch (IOException e) {
           log.error(e);
        }
    }


}
