package com.ifish.action;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ifish.dto.JsonResult;
import com.ifish.dto.PageingDto;
import com.ifish.entity.Comment;
import com.ifish.enums.ResultEnum;
import com.ifish.exception.IfishException;
import com.ifish.service.InformationService;
import com.ifish.validator.PageParam;


@Controller
@ResponseBody
public class InformationAction {
	@Autowired
	private InformationService informationService;
	
	/**
	 * 获取用户评论
	 * @param comment
	 * @return
	 */
	@RequestMapping(value="/information/v3/findCommentsByUeditorId.do",method=RequestMethod.POST)
	public Object findCommentsByUeditorId(Integer ueditorId,Integer firstResult,Integer pageSize,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return informationService.findCommentsByUeditorId(ueditorId, firstResult, pageSize);
	}
	
	/**
	 * 保存评论
	 * @param comment
	 * @return
	 */
	@RequestMapping(value="/information/v3/saveComment.do",method=RequestMethod.POST)
	public Object saveComment(Comment comment,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return informationService.saveComment(comment);
	}
	
	/**
	 * 分页获取资讯
	 * @param pageParam
	 * @param result
	 * @return
	 */
	@RequestMapping(value="/informations",method=RequestMethod.GET)
	public PageingDto<?> getInformationsByPage(@Validated PageParam pageParam,BindingResult result){
		//参数校验
		if(result.hasErrors()){
			throw new IfishException(ResultEnum.error401);
		}
		return informationService.getInformationsByPage(pageParam);
	}
	
	/**
	 * 增加点击数
	 * @param infoId
	 * @return
	 */
	@RequestMapping(value="/informations/{infoId}/additionClickNum",method=RequestMethod.POST)
	public JsonResult<?> addClickNum(@PathVariable("infoId") Integer infoId){
		return informationService.addClickNum(infoId);
	}

}