package org.ccpit.base.emailSendManage;

import java.util.Date;

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;

/**
 * Company Name : 中贸促信息技术有限责任公司	
 * Date:2016年9月13日
 * author:huoyan
 * Copyright (c) 2016, huoyan@ccpit.org All Rights Reserved.
 *
 */
@RestController
@RequestMapping(value="/business/EmailManage")
public class EmailController extends BaseController{
	@Autowired
	private EmailService EmailService;
	@Autowired 
	private LogInfoService logInfoService;
	
	@RequestMapping(value ="/goinEmailPage")
	public ModelAndView getPage() {
		ModelAndView mv = new ModelAndView("business/emailPage");
		return mv;
	}
	
	//1.查询
	@RequestMapping(value="/queryEmail",produces ="application/json;charset=UTF-8")
	public Object queryAllEmail(HttpServletRequest request,EmailInfo Email) {
		PageRequest pageRequest = getPage(request);
		String hql= "from EmailInfo where 1=1";
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String recipients = request.getParameter("recipients");
		if(title!=null && !title.equals("")){
			hql = hql+"and title like '%"+ title +"%' ";
		}
		if(content!=null && !content.equals("")){
			hql = hql+"and content like '%"+ content +"%' ";
		}
		if(recipients!=null && !recipients.equals("")){
			hql = hql+"and recipients like '%"+ recipients +"%' ";
		}
		Page<EmailInfo> pageInformation = EmailService.queryEmailInfo(pageRequest, hql);
		return EmailService.convert(pageInformation);
	}
	
	//2.新增或修改
	@RequestMapping(value="/addOrUpdateEmail",produces ="application/json;charset=UTF-8")
	public Object addOrUpdateEmail(@PathVariable String operate,HttpServletRequest request,HttpServletResponse response, EmailInfo Email){
		String result = "";
		User user = (User) request.getSession().getAttribute("user_in_session");
		boolean flag = false;
		
		Email.setCreateTime(new Date());
		Email.setCreator(user);
		flag = EmailService.add(Email);
		if(flag){ 
			//记录用户操作日志信息
			logInfoService.recordLog(request, "新增邮件");
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存成功！\" }";
		}else {
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存失败！\" }";
		}
		return result;
	}
	
}
