package com.iamberry.wechat.handles.task;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.iamberry.wechat.core.entity.order.MessageDto;
import com.iamberry.wechat.core.entity.task.WechatTask;
import com.iamberry.wechat.face.porduct.ProductTypeService;
import com.iamberry.wechat.face.task.WechatTaskService;
import com.iamberry.wechat.utils.TokenUtil;

/**
 * @company	深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @author 	献
 * @tel		18271840547
 * @date	2016年9月20日
 * @explain	微信任务管理的handler
 */
@RequestMapping("/admin/task")
@Controller
public class AdminTaskHandler {

	@Autowired
	private WechatTaskService wechatTaskService;
	
	@Autowired
	private ProductTypeService typeService;
	

	/**
	 * list view
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(
				@RequestParam(value = "pageNO", defaultValue = "1") Integer pageNO,
				@RequestParam(value = "pageSize", required = false) Integer pageSize,
				HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("admin/task/list");
		
		// 分页查找
		List<WechatTask> news = wechatTaskService.getAll(10, pageNO);
		mv.addObject("list", news);
		
		// 是否已经select过pageSize了？
		if (pageSize == null) {
			pageSize = wechatTaskService.getCount(10);
		}
		
		// 分页数据
		mv.addObject("pageNO", pageNO);
		mv.addObject("pageSize", pageSize);
		mv.addObject("url", new StringBuilder(StringUtils.replace( request.getRequestURI(), request.getContextPath(), "")).append("?pageSize=").append(pageSize).append("&pageNO="));
		return mv;
	}

	/**
	 * 进入添加页面
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addUI(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("admin/task/add");
		
		// token
		String token = TokenUtil.getToken();
		request.getSession().setAttribute("addTaskToken", token);
		mv.addObject("addTaskToken", token);
		mv.addObject("types", typeService.getAll());
		return mv;
	}
	
	/**
	 * 执行添加任务
	 * @param task
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(WechatTask task, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		
		// 校验token
		String addTaskToken = request.getParameter("addTaskToken");
		if (StringUtils.isEmpty(addTaskToken)) {
			mv.addObject("msgObj", new MessageDto(false, "对不起，操作失败!", "对不起，您操作失败，请重试!"));
			return mv;
		}
		String sessionToken = (String) request.getSession().getAttribute("addTaskToken");
		if (!StringUtils.equals(addTaskToken, sessionToken)) {
			request.getSession().removeAttribute("addTaskToken");
			mv.addObject("msgObj", new MessageDto(false, "对不起，操作失败!", "对不起，您操作失败，请重试!"));
			return mv;
		}
		request.getSession().removeAttribute("addTaskToken");
		
		// 校验数据
		int count = wechatTaskService.insertTask(task);
		if (count < 1) {
			mv.addObject("msgObj", new MessageDto(false, "对不起，操作失败!", "对不起，您操作失败，请重试!"));
			return mv;
		}
		
		mv.addObject("msgObj", new MessageDto(true, "恭喜您，添加成功!", "恭喜您，添加成功!"));
		return mv;
	}
	
	/**
	 * 往修改页面
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/updateUI/{id}", method = RequestMethod.GET)
	public ModelAndView updateUI(@PathVariable("id") Integer id, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("admin/task/edit");
		
		// token
		String token = TokenUtil.getToken();
		request.getSession().setAttribute("updateTaskToken", token);
		mv.addObject("updateTaskToken", token);
		
		// task
		WechatTask task = wechatTaskService.getOne(id);
		if (task == null) {
			mv.addObject("msgObj", new MessageDto(false, "对不起，操作失败!", "对不起，您操作失败，请重试!"));
			return mv;
		}
		mv.addObject("obj", task);
		
		// types
		mv.addObject("types", typeService.getAll());
		return mv;
	}
	
	/**
	 * 
	 * @param task
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView update(WechatTask task, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		// 校验token
		String addTaskToken = request.getParameter("updateTaskToken");
		if (StringUtils.isEmpty(addTaskToken)) {
			mv.addObject("msgObj", new MessageDto(false, "对不起，操作失败!", "对不起，您操作失败，请重试!"));
			return mv;
		}
		String sessionToken = (String) request.getSession().getAttribute("updateTaskToken");
		if (!StringUtils.equals(addTaskToken, sessionToken)) {
			request.getSession().removeAttribute("updateTaskToken");
			mv.addObject("msgObj", new MessageDto(false, "对不起，操作失败!", "对不起，您操作失败，请重试!"));
			return mv;
		}
		request.getSession().removeAttribute("updateTaskToken");
		// 更新
		task.setTaskType(1);			// 默认
		int count = wechatTaskService.updateTask(task);
		if (count < 1) {
			mv.addObject("msgObj", new MessageDto(false, "对不起，操作失败!", "对不起，您操作失败，请重试!"));
			return mv;
		}
		
		mv.addObject("msgObj", new MessageDto(true, "恭喜您，修改成功!", "恭喜您，修改成功!"));
		return mv;
	}
}
