/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yuntong.action;

import java.util.ArrayList;
import java.util.List;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


import com.yuntong.hibernate.database.po.TAgioInfo;
import com.yuntong.hibernate.database.po.TAgioType;
import com.yuntong.hibernate.database.po.TRegisterInfo;
import com.yuntong.myexception.Y_MyNullResultException;
import com.yuntong.service.X_IfinancialManageService;

/** 
 * MyEclipse Struts
 * Creation date: 04-11-2007
 * 
 * XDoclet definition:
 * @struts.action path="/x_few" name="x_fewForm" scope="request" validate="true"
 * @struts.action-forward name="ok" path="/fewMoney.jsp"
 */
public class X_fewAction extends Action {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	
	private X_IfinancialManageService service;
	
	public X_IfinancialManageService getService() {
		return service;
	}

	public void setService(X_IfinancialManageService service) {
		this.service = service;
	}
	private static final int PAGE_SiZE=4;
	//分页
	public void fenye(List list ,HttpServletRequest request){
//		int totalpage=(list.size()-1)/PAGE_SiZE+1;
//		//currentPage设置当前
//		int currentPage=1; 
//		try
//		{
//		 currentPage=Integer.parseInt(request.getParameter("currentPage"));
//		}catch(NumberFormatException ex)
//		{	
//		  ex.printStackTrace();
//		}
//		if(currentPage<1)
//		{
//			currentPage=1;
//		}
//		if(currentPage>=totalpage)
//		{
//			currentPage=totalpage;
//		}
//		//保存分页信息
//		request.setAttribute("currentPage",currentPage+"");
//		request.setAttribute("totalpage", totalpage+"");
//		request.setAttribute("PageSixze", PAGE_SiZE+"");
//		request.setAttribute("offset", ((currentPage-1)*PAGE_SiZE)+""); //offset为本页第一条记录在集合中的位置
		
	}
	
	//查找
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
		String reg=request.getParameter("RegisterInfo").trim();
		
		String type=request.getParameter("ATypeName");
		
	
		List list=(List) request.getSession().getAttribute("listfew1");
		
		List list1=new ArrayList();
		if((reg!=null && !reg.equals("")) && (type!=null && !type.equals(""))){
		
			Long info=Long.parseLong(reg);
			for (int i = 0; i < list.size(); i++) {
				TAgioInfo agioInfo=(TAgioInfo) list.get(i);
				if(agioInfo.getTRegisterInfo().getRegisterGlideId().equals(info) && agioInfo.getTAgioType().getAtypeName().equals(type)){
					list1.add(agioInfo);
				}
			}
		}else if((reg!=null && !reg.equals("")) && (type==null || type.equals(""))){
		
			Long info=Long.parseLong(reg);
			for (int i = 0; i < list.size(); i++) {
				TAgioInfo agioInfo=(TAgioInfo) list.get(i);
				if(agioInfo.getTRegisterInfo().getRegisterGlideId().equals(info)){
					list1.add(agioInfo);
				}
			}
		}else if((reg==null || reg.equals("")) && (type!=null && !type.equals(""))){
		
			for (int i = 0; i < list.size(); i++) {
				TAgioInfo agioInfo=(TAgioInfo) list.get(i);
				if(agioInfo.getTAgioType().getAtypeName().equals(type)){
					list1.add(agioInfo);
				}
			}
		}

		if(list1.size()==0){
			request.setAttribute("null", "");
		}
		
		this.fenye(list1, request);
		if(list1.size()>4){
			request.setAttribute("Selectfenye", "fenye");
		}

		request.getSession().setAttribute("listfew", list1);
		
		return mapping.findForward("ok");
	}
	
	//添加
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
		Long regInfo=Long.parseLong(request.getParameter("RegisterInfo"));		
		String type=request.getParameter("ATypeName");
		String info= request.getParameter("AgioInfo");
		float mon=Float.parseFloat(request.getParameter("AgioMoney"));
	
		TRegisterInfo registerInfo;
		try {
			registerInfo = service.findById("registerGlideId", regInfo);
			TAgioType agioType=service.findTypeByProperty("atypeName", type);
			
			List list=service.findAllAgio();
			TAgioInfo agioInfo=new TAgioInfo();
			
			agioInfo.setAgioInfo(info);
			agioInfo.setAgioMoney(mon);
			agioInfo.setTAgioType(agioType);
			agioInfo.setTRegisterInfo(registerInfo);
			service.save(agioInfo);
			
			List list1=service.findAllAgio();
			
			this.fenye(list, request);
			
			this.fenye(list1, request);
			if(list1.size()>4){
				request.setAttribute("Selectfenye", "fenye");
			}
			
			request.getSession().setAttribute("listfew1", list1);
			request.getSession().setAttribute("listfew", list1);
		} catch (Y_MyNullResultException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return mapping.findForward("ok");
	}
}