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

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

/** 
 * MyEclipse Struts
 * Creation date: 04-03-2007
 * 
 * XDoclet definition:
 * @struts.action path="/x_per" name="x_perForm" input="/form/x_per.jsp" parameter="test" scope="request" validate="true"
 * @struts.action-forward name="good" path="/goods.jsp"
 */
public class X_perAction extends DispatchAction {
	/*
	 * 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 select(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		X_perForm x_perForm = (X_perForm) form;// TODO Auto-generated method stub
		
		
		String strId=(String) request.getParameter("id");
		Long id=Long.parseLong(strId);
		
		List list=(List) request.getSession().getAttribute("listper");
		for(int i=0;i<list.size();i++){
			TRegisterInfo info=(TRegisterInfo) list.get(i);
		
			if(info.getRegisterGlideId().equals(id)){
				//更改，即交款
				info.getTRegisterState().setRstateId(3);
				
				service.update(info);
				
				
				List list1;
				
					try {
						list1 = service.findByProperty("TFreightStyle",1);
						for(int j=0;j<list.size();j++){
							TRegisterInfo registerInfo=(TRegisterInfo) list1.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("listper",list1);
					} catch (Y_MyNullResultException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}
		return mapping.findForward("ok");
	}
	
	//查找
	public ActionForward test(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		X_perForm x_perForm = (X_perForm) form;// TODO Auto-generated method stub
		Long id=x_perForm.getPerID();
		String name=x_perForm.getPerName();	
		
		List list=new ArrayList();
		
		if(id==0){
			return mapping.findForward("ok");
		}
		
		
		List list2=(List) request.getSession().getAttribute("listper");
		
		//查找，分条件
		if(id>0 && (!name.equals("")&& name!=null)){
			request.setAttribute("uqiun", "u");//页面'显示所有'的显示标志
			
			for (int i = 0; i < list2.size(); i++) {
				TRegisterInfo info=(TRegisterInfo) list2.get(i);
				if(info.getRegisterGlideId().equals(id) && info.getTGoodsInfo().getGoodsName().equals(name)){
					list.add(info);
				}
			}
		}else if(id>0 && (name.equals("") || name==null)){
			for (int i = 0; i < list2.size(); i++) {
				TRegisterInfo info=(TRegisterInfo) list2.get(i);
				if(info.getRegisterGlideId().equals(id)){
					list.add(info);
				}
			}
		}else if(id==0 && (!name.equals("")&& name!=null)){
			for (int i = 0; i < list2.size(); i++) {
				TRegisterInfo info=(TRegisterInfo) list2.get(i);
				if(info.getTGoodsInfo().getGoodsName().equals(name)){
					list.add(info);
				}
			}
		}
		
		this.fenye(list, request);
		if(list.size()>4){
			request.setAttribute("Selectfenye", "fenye");
		}
		
		request.getSession().setAttribute("listper", list);
		if(list.size()==0){
			request.setAttribute("null", "n");
		}else{
			request.setAttribute("notnull", "not");
		}
		
		return mapping.findForward("ok");
	}
}