/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:memberManageSys
 * File Name:UserEmailController.java
 * Package Name:ccpit.base.user
 * Date:2015年11月23日上午10:00:30
 * Copyright (c) 2015, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.base.user;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import net.sf.json.JSONArray;

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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * ClassName:UserEmailController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2015年11月23日 上午10:00:30 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping("/admin/user")
public class UserEmailController extends BaseController {

	@Autowired
	private UserEmailService userEmailService;
	@Autowired
	private UserService userService;
	@Autowired
	private LogInfoService logInfoService;
	
	@RequestMapping("/goinUserEmailListPage")
	public Object goinApproveplanListPage() {
		ModelAndView mv = new ModelAndView("admin/user/userEmailPage");
		return mv;
	}
	/**
	 * 
	 * getAllApproveplans:(根据条件查询经营信息并分页显示). <br/>
	 * @author dingwei
	 * @param request
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping("/getAllUserEmails")
    @ResponseBody
    public Object getAllUserEmails(HttpServletRequest request){
        PageRequest pageRequest = getPage(request);
        User user = (User) request.getSession().getAttribute("user_in_session");
        String userId = request.getParameter("");
        String hql = "from UserEmail where userId ='"+ userId;
        Page<UserEmail> pageMemberBase = userEmailService.queryPage(pageRequest, hql);
        return userEmailService.convert(pageMemberBase);
    }
	/**
	 * 
	 * addOrUpdateApproveplan:(新增或修改会籍信息). <br/>
	 * @author dingwei
	 * @param request
	 * @param response
	 * @param userEmail
	 * @since JDK 1.6
	 */
	@RequestMapping("/addOrUpdateUserEmail")
	public void addOrUpdateUserEmail(HttpServletRequest request,HttpServletResponse response, UserEmail userEmail) {
		String result = "";
		User user = (User) request.getSession().getAttribute("user_in_session");
		//判断操作类别  新增|修改
		UserEmail userEmail1 = user.getUserEmail();
		boolean flag = false;
		if(null != userEmail1){ //判断用户发送有效是否存在
			user.setUserEmail(null);
			boolean flag1 = userService.updateUser(user);
			if(null != userEmail.getEmailCount()){
				userEmail1.setEmailCount(userEmail.getEmailCount());
			}
			if(null != userEmail.getEmailPassword()){
				userEmail1.setEmailPassword(userEmail.getEmailPassword());
			}
			if(null != userEmail.getRemark()){
				userEmail1.setRemark(userEmail.getRemark());
			}
			if(null != userEmail.getServerHost()){
				userEmail1.setServerHost(userEmail.getServerHost());
			}
			if(null != userEmail.getServerPort()){
				userEmail1.setServerPort(userEmail.getServerPort());
			}
			if(null != userEmail.getServerProtocol()){
				userEmail1.setServerProtocol(userEmail.getServerProtocol());
			}
			userEmail1.setModifyBy(user);
			userEmail1.setModifyTime(new Date());
			if(flag1){
				boolean flag2 = userEmailService.UpdateUserEmail(userEmail1);
				if(flag2){
					user.setUserEmail(userEmail1);
					flag = userService.updateUser(user);
				}
			}
		}
		if(null == userEmail1){
			userEmail.setCreateTime(new Date());
			userEmail.setCreator(user);
			user.setUserEmail(userEmail);
			boolean flag1 = userEmailService.addUserEmail(userEmail);
			if(flag1){
				flag = userService.updateUser(user);
			}
			if(flag){
				//记录用户操作日志信息
				logInfoService.recordLog(request, "新增邮箱信息信息");
			}
		}else {
			
		}
		if(flag){
			//记录用户操作日志信息
			logInfoService.recordLog(request, "修改邮箱信息");
		}
		if(flag){
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存成功！\" }";
		}else {
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存失败！\" }";
		}
		try { 
			response.setContentType("text/html");
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据id删除
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/deleteUserEmailById")
	@ResponseBody
	public Object deleteUserEmailById(HttpServletRequest request){
		Map result = new HashMap();
		String jsonIds = request.getParameter("ids");
		JSONArray jsonArray = JSONArray.fromObject(jsonIds);
		Object[] ids = jsonArray.toArray();
		boolean flag = false;
		for(Object newsId : ids){
			UserEmail userEmail = userEmailService.queryUserEmailById((Integer)newsId);
			if(null != userEmail){
				userEmail.setWhetherDelete(false);
				flag = userEmailService.UpdateUserEmail(userEmail);
			}
		}
		//记录用户操作日志信息
		logInfoService.recordLog(request, "删除邮箱信息");
		result.put("flag", flag);
		return result;
	}
	/**
	 * 
	 * queryApproveplanById:(根据id查询会籍详细信息). <br/>
	 * @author dingwei
	 * @param request
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping("/queryUserEmailById")
	@ResponseBody
	public Object queryUserEmailById(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user_in_session");
        String userId = user.getId()+"";
        String hql = "from UserEmail where creator_id ='"+ user.getId() +"' and whetherDelete = true";
		List<UserEmail> userEmailList = userEmailService.queryUserEmailAll(hql);
		if(null != userEmailList && userEmailList.size()>0){
			return userEmailList.get(0);
		}
		return null;
	}
}

