package com.tarena.oss.web.actions;

import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Set;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.tarena.oss.pricing.pojo.Pricing;
import com.tarena.oss.pricing.pojo.PricingCondDTO;
import com.tarena.oss.pricing.service.PricingMgmtService;
import com.tarena.oss.user.pojo.User;
import com.tarena.oss.user.pojo.UserCondDTO;
import com.tarena.oss.user.service.UserMgmtService;
import com.tarena.oss.userservice.pojo.UserService;
import com.tarena.oss.userservice.pojo.UserServiceCondDTO;
import com.tarena.oss.userservice.service.UserServiceMgmtService;
import com.tarena.oss.util.PageParameter;
import com.tarena.oss.web.forms.UserServiceCondForm;
import com.tarena.oss.web.forms.UserServicesForm;

public class UserServiceAction extends MappingDispatchAction {

	//织入UserServiceMgmtService
	private UserServiceMgmtService service;
	public void setService(UserServiceMgmtService service) {
		this.service = service;
	}
	
	//织入PricingMgmtService
	private PricingMgmtService pservice;
	public void setPservice(PricingMgmtService pservice) {
		this.pservice = pservice;
	}
	
	//织入UserMgmtService
	private UserMgmtService uservice;
	public void setUservice(UserMgmtService uservice) {
		this.uservice = uservice;
	}

	/*
	 * 业务帐号查询
	 */
	public ActionForward listUserServices(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse reponse) throws Exception {
//		获得当前页码
		int currentPage = 0;
		String page = request.getParameter("page");
		if (page==null || page.equals(""))
			currentPage = 1;
		else
			currentPage = Integer.parseInt(page);
		Collection<UserService> us =service.findUserServices(new UserServiceCondDTO(), currentPage);
		request.setAttribute("USERSERVICES", us);
		
//		 计算总页数
		int totalPages = 0;
		int totalRows = service.findUserServicesByCond(new UserServiceCondDTO());
		if (totalRows % PageParameter.CNT_EACH_PAGE == 0)
			totalPages = totalRows/PageParameter.CNT_EACH_PAGE;
		else
			totalPages = totalRows/PageParameter.CNT_EACH_PAGE + 1;
		
		// 获得分页导航中的url
		StringBuffer url = new StringBuffer();
		
		// 获得contextPath
		url.append(request.getContextPath());
		// 获得servletPath
		url.append(request.getServletPath());
		// 获得请求中的参数
		url.append("?");
		Map<String, String[]> params = request.getParameterMap();
		if (params != null && params.size() != 0){
			Set<String> keys = params.keySet();
			for(String key : keys){
				if (key.equals("page")) continue;
				String[] values = params.get(key);
				for(String value : values){
					url.append(key).append("=").append(value).append("&");
				}
			}
		}

		// 获得分页导航的完成字符串
		String pagenav = PageParameter.pageNavigator(currentPage, totalPages, url.substring(0, url.length()-1));
		// 将分页导航信息保存在request中
		request.setAttribute("pagenav", pagenav);
		
		
 		String type=request.getParameter("type");
 		if(type.equals("1")){
 			return mapping.findForward("list");
 		}else if(type.equals("2")){
 			return mapping.findForward("listforupd");
 		}else{
 			return mapping.findForward("listfordel");
 		}
 			
	}
	
	/*
	 * 根据查询条件查询业务帐号
	 */
	public ActionForward listUserServicesByCond(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse reponse) throws Exception {
//		UserServiceCondForm f =(UserServiceCondForm) form;
//		UserServiceCondDTO dto =f.getDto();
//		Collection<UserService>  c =service.findUserServices(dto);
		
		int currentPage =0;
		String page =request.getParameter("page");
		if(page==null || page.equals("")){
			currentPage =1;
		}else{
			currentPage =Integer.parseInt(page);
		}
		
		String username=request.getParameter("username");
		String loginname=request.getParameter("loginname");
		String labip=request.getParameter("labip");
		String status=request.getParameter("status");
		UserServiceCondDTO dto = new UserServiceCondDTO();
		dto.setLabip(labip);
		dto.setStatus(Integer.parseInt(status));
		dto.setLoginname(loginname);
		dto.setUsername(username);
		Collection<UserService>  c=service.findUserServices(dto);
		request.setAttribute("USERSERVICES", c);
		
//		计算总页数
		int totalPages = 0;
		int totalRows = service.findUserServicesByCond(dto);
		if (totalRows % PageParameter.CNT_EACH_PAGE == 0)
			totalPages = totalRows/PageParameter.CNT_EACH_PAGE;
		else
			totalPages = totalRows/PageParameter.CNT_EACH_PAGE + 1;
		
		// 获得分页导航中的url
		StringBuffer url = new StringBuffer();
		
		// 获得contextPath
		url.append(request.getContextPath());
		// 获得servletPath
		url.append(request.getServletPath());
		// 获得请求中的参数
		url.append("?");
		Map<String, String[]> params = request.getParameterMap();
		if (params != null && params.size() != 0){
			Set<String> keys = params.keySet();
			for(String key : keys){
				if (key.equals("page")) continue;
				String[] values = params.get(key);
				for(String value : values){
					url.append(key).append("=").append(value).append("&");
				}
			}
		}

		// 获得分页导航的完成字符串
		String pagenav = PageParameter.pageNavigator(currentPage, totalPages, url.substring(0, url.length()-1));
		// 将分页导航信息保存在request中
		request.setAttribute("pagenav", pagenav);
		
		
		return mapping.findForward("list");
	}

	/*
	 * 增加业务帐号
	 */
	public ActionForward addUserService(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse reponse) throws Exception {
//		System.out.println("111111111");
//		UserServicesForm f= (UserServicesForm) form;
//		System.out.println(f);
//		UserService us = f.getUs();
//		System.out.println("33333333333");
		
		String userid=request.getParameter("userid");
		String priceid=request.getParameter("priceid");
		String loginname=request.getParameter("loginname");
		String loginpassword=request.getParameter("loginpassword");
		String labip=request.getParameter("labip");
		String status=request.getParameter("status");
		
		Date date=new Date();
		UserService us= new UserService();
		us.setLabip(labip);
		us.setStatus(Integer.parseInt(status));
		us.setLoginname(loginname);
		us.setLoginpassword(loginpassword);
		User user = new User();
		user.setId(Integer.parseInt(userid));
		us.setUser(user);
		Pricing pricing = new Pricing();
		pricing.setId(Integer.parseInt(priceid));
		us.setPricing(pricing);
		us.setEnrolldate(date);
		try{
			service.addUserService(us);		
			return mapping.findForward("success");
		}catch(Exception e){
			e.printStackTrace();
			String mesg="添加信息错误";
			request.setAttribute("mesg", mesg);
			return mapping.getInputForward();
		}
		
	}
	
	
	
	/*
	 * 3.05 跳转到增加页面时传UserService和Pricing
	 */
	public ActionForward toAddUs(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		Collection<User> user= uservice.findUsers();
		System.out.println(user);
		Collection<Pricing> pricing = pservice.findPricings();
		
		request.setAttribute("user1", user);
		request.setAttribute("pricings", pricing);
		return mapping.findForward("toaddus");
	}
	
	

	/*
	 * 修改时根据id得到业务帐号的信息
	 */
	public ActionForward oneUserService(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		String str=request.getParameter("usid");
		Integer id =Integer.parseInt(str);
		UserService us=service.findUserService(id);
		Collection<Pricing> pricing = pservice.findPricings();
		
		request.setAttribute("us", us);
		request.setAttribute("pricings", pricing);
		return mapping.findForward("listuserservice");
	}
	
	/*
	 * 修改业务帐号
	 */
	public ActionForward updateUserService(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		String str=request.getParameter("usid");
		Integer id=Integer.parseInt(str);
		UserService us=service.findUserService(id);
		Date enrolldate =us.getEnrolldate();
//		UserServicesForm f =(UserServicesForm) form;
//		f.setEnrolldate(enrolldate);
//		UserService us1=f.getUs();
		
		String loginname=request.getParameter("loginname");
		String loginpassword=request.getParameter("loginpassword");
		String labip=request.getParameter("labip");
		String status=request.getParameter("status");
		
		us.setLoginname(loginname);
		us.setLoginpassword(loginpassword);
		us.setLabip(labip);
		us.setStatus(Integer.parseInt(status));
		
		try{
			us.setId(id);
			us.setEnrolldate(enrolldate);
			service.modifyUserService(us);
			return mapping.findForward("success");
		}catch(Exception e){
			e.printStackTrace();
			String mesg="修改信息错误";
			request.setAttribute("mesg", mesg);
			return mapping.getInputForward();
		}
	}
	
	/*
	 * 删除业务帐号
	 */
	public ActionForward delUserService(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String str = request.getParameter("usid");
		Integer id = Integer.parseInt(str);
		service.removeUserService(id);
		return mapping.findForward("success");		
	}

}
