/*
 * 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.TRegisterInfo;
import com.yuntong.myexception.Y_MyNullResultException;
import com.yuntong.service.X_IfinancialManageService;

/** 
 * MyEclipse Struts
 * Creation date: 04-06-2007
 * 
 * XDoclet definition:
 * @struts.action parameter="good" validate="true"
 * @struts.action-forward name="ok" path="/Financial/Peraccept.jsp" redirect="true"
 */
public class X_GoodsAction 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 name=(String) request.getSession().getAttribute("name");
		if(name.equals("per")){
			TRegisterInfo info=(TRegisterInfo) request.getSession().getAttribute("good");
			
			//更改,保存
			info.getTRegisterState().setRstateId(3);
		
			service.update(info);
			/**************************/
			
			List list;
			try {
				list = service.findByProperty("TFreightStyle",1);
				for(int i=0;i<list.size();i++){
					TRegisterInfo registerInfo=(TRegisterInfo) list.get(i);
					
					if(!registerInfo.getTRegisterState().getRstateName().equals("收讫未付款") || !registerInfo.getTRegisterState().getRstateName().equals("收讫已付款")){
						
						list.remove(i);
					}
				}
				this.fenye(list, request);
				if(list.size()>4){
					request.setAttribute("fenye", "fenye");
				}
				
				request.getSession().setAttribute("listper1", list);
				request.getSession().setAttribute("listper", list);
			} catch (Y_MyNullResultException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}				
			
			/**************************/
			return mapping.findForward("good");
			
		}else if(name.equals("pick")){
			return mapping.findForward("pick");
		}else if(name.equals("pickinfo")){
			return mapping.findForward("pickinfo");
		}else{
			return null;
		}
		
	}
}