/*
 * 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.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.form.X_pickinfoForm;
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="test" validate="true"
 * @struts.action-forward name="ok" path="/pick-up.jsp"
 * @struts.action-forward name="pick" path="/pick-upInfo.jsp"
 */
public class X_pickinfoAction 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_pickinfoForm x_pickinfoForm = (X_pickinfoForm) form;// TODO Auto-generated method stub
		Long id=x_pickinfoForm.getPerID();
		String name =x_pickinfoForm.getPerName();
		List list=new ArrayList();
		List list2=(List) request.getSession().getAttribute("listpickinfo1");
		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);
				}
			}
		}
		if(list.size()==0){
			request.setAttribute("null", "n");
		}
		request.getSession().setAttribute("listpickinfo", list);
		String have=(String) request.getSession().getAttribute("have");
		
		if(have.equals("have")){
			request.setAttribute("havepick1", have);
		}else{
			request.setAttribute("havepick2", have);
			
		}
		
		this.fenye(list, request);
		if(list.size()>4){
			request.setAttribute("fenye", "fenye");
		}
		
		return mapping.findForward("selcet");
	}	
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		X_pickinfoForm x_pickinfoForm = (X_pickinfoForm) form;// TODO Auto-generated method stub
		
		String strId=(String) request.getParameter("id");
		Long id=Long.parseLong(strId);
		List list=(List) request.getSession().getAttribute("listpickinfo1");
		for(int i=0;i<list.size();i++){
			TRegisterInfo info=(TRegisterInfo) list.get(i);
			
			if(info.getRegisterGlideId().equals(id)){
				//更改，即交款
				info.getTRegisterState().setRstateId(9);
				
				service.update(info);
			}
		}
		
		return mapping.findForward("selcet");
	}
	
	public ActionForward back(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		X_pickinfoForm x_pickinfoForm = (X_pickinfoForm) form;// TODO Auto-generated method stub

		return mapping.findForward("ok");
	}
}