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

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
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.hibernate.database.po.TAgioInfo;
import com.yuntong.hibernate.database.po.TRegisterInfo;
import com.yuntong.service.X_IfinancialManageService;

/** 
 * MyEclipse Struts
 * Creation date: 04-03-2007
 * 
 * XDoclet definition:
 * @struts.action parameter="test" validate="true"
 * @struts.action-forward name="lose" path="/financial/lose.jsp"
 * @struts.action-forward name="ribose" path="/financial/ribose.jsp"
 * @struts.action-forward name="moto" path="/financial/moto.jsp"
 * @struts.action-forward name="per" path="/financial/Peraccept.jsp"
 * @struts.action-forward name="few" path="/financial/fewMoney.jsp"
 * @struts.action-forward name="pick" path="/financial/pick-up.jsp"
 */
public class X_FinancialAction 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 void  draw(HttpServletRequest request, HttpServletResponse response) throws IOException{
		int width=100,heighe=18;
		//内存中生成的图片
		BufferedImage image=new BufferedImage(width,heighe,BufferedImage.TYPE_INT_BGR);
		
		//开始制作图像
		Graphics g=image.getGraphics();
		g.setColor(Color.red);;
		
		g.fillRect(1, 1, width-1, heighe);
		g.drawRect(0, 0, width-1, heighe);
		g.setColor(new Color(102,102,102));
		
		g.drawLine(0, 0, 1, 1);
		g.dispose();
//		ImageIO.write(image, "JPEG", response.getOutputStream());
		request.setAttribute("image", image);
	}
	
	public ActionForward per(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub	
		try{
			List list=service.findByProperty("TFreightStyle",1);
			for(int i=0;i<list.size();i++){
				TRegisterInfo info=(TRegisterInfo) list.get(i);
				
				if(!info.getTRegisterState().getRstateName().equals("收讫未付款") && !info.getTRegisterState().getRstateName().equals("收讫已付款")){
					
					list.remove(info);
				}
			}
			this.fenye(list, request);
			if(list.size()>4){
				request.setAttribute("fenye", "fenye");
			}
			this.draw(request, response);
			request.getSession().setAttribute("listper1", list);
			request.getSession().setAttribute("listper", list);
			return mapping.findForward("per");
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
		
	}
	
	public ActionForward pick(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try{
			List list=service.findByProperty("TFreightStyle",2);
			for(int i=0;i<list.size();i++){
				TRegisterInfo info=(TRegisterInfo) list.get(i);
				if((!info.getTRegisterState().getRstateName().equals("到货已登记提付")) && (!info.getTRegisterState().getRstateName().equals("提付未付款")) && (!info.getTRegisterState().getRstateName().equals("提付已付款"))){
					list.remove(info);
					i--;
				}
			}
			this.fenye(list, request);
			if(list.size()>4){
				request.getSession().setAttribute("fenye", "fenye");
			}
			request.getSession().setAttribute("listpick", list);
			
			return mapping.findForward("pick");
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
		
	}
	
	public ActionForward lose(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			List list=service.findByAllObject();
			this.fenye(list, request);
			if(list.size()>4){
				request.setAttribute("fenye", "fenye");
			}
			request.getSession().setAttribute("listlose", list);
			request.getSession().setAttribute("listlose1", list);
			return mapping.findForward("lose");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		
	}
	
	public ActionForward ribose(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return mapping.findForward("ribose");
	}
	
	public ActionForward few(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		List typeid=service.findByAllTAgio();
		try{
			List list=service.findAllAgio();
			
			this.fenye(list, request);
			if(list.size()>4){
				request.setAttribute("fenye", "fenye");
			}
			request.getSession().setAttribute("listfew1", list);
			request.getSession().setAttribute("listfew", list);
			request.getSession().setAttribute("typeid",typeid);
			
			return mapping.findForward("few");
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}		
	}	
}