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

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.TDisrepareInfo;
import com.yuntong.hibernate.database.po.TRegisterInfo;
import com.yuntong.hibernate.database.po.TRoadway;
import com.yuntong.myexception.Y_MyNullResultException;
import com.yuntong.service.X_IfinancialManageService;

/** 
 * MyEclipse Struts
 * Creation date: 04-12-2007
 * 
 * XDoclet definition:
 * @struts.action path="/x_addLose" name="x_addLoseForm" scope="request" validate="true"
 * @struts.action-forward name="ok" path="/lose.jsp"
 */
public class X_addLoseAction 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) {
		
		String name=request.getParameter("ATypeName");		
		String info=request.getParameter("Info");
		float getM=Float.parseFloat(request.getParameter("GetMoney"));		
		float loseM=Float.parseFloat(request.getParameter("LoseMoney"));
		int moto=Integer.parseInt(request.getParameter("Moto"));		
		Long regInfo=Long.parseLong(request.getParameter("RegisterInfo"));
	
		TRegisterInfo registerInfo;		
		try {
			registerInfo = service.findById("registerGlideId", regInfo);
			TRoadway roadway=service.findById(new Integer(moto));
			TDisrepareInfo disrepareInfo=new TDisrepareInfo(registerInfo.getRegisterId(),registerInfo,roadway,info,loseM,getM,name);		
			
			//添加
			service.save(disrepareInfo);
			
			
			
			//使显示能及时的更改
			List list=service.findByAllObject();
			
			this.fenye(list, request);
			if(list.size()>4){
				request.setAttribute("Selectfenye", "fenye");
			}
			request.getSession().setAttribute("listlose", list);
			request.getSession().setAttribute("listlose1", list);
			
			return mapping.findForward("ok");
			
		} catch (Y_MyNullResultException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}