/*
 * 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 javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.action.actionform.Y_SHDJGoodsForm;
import com.yuntong.business.vo.Y_ClientInfoVO;
import com.yuntong.business.vo.Y_FactoryInfoVO;
import com.yuntong.business.vo.Y_FreightStyleVO;
import com.yuntong.business.vo.Y_GoodsInfoVO;
import com.yuntong.business.vo.Y_PaymentStyleVO;
import com.yuntong.business.vo.Y_RegisterInfoVO;
import com.yuntong.business.vo.Y_UserInfoVO;
import com.yuntong.service.Y_IClientManagerService;
import com.yuntong.service.Y_IFactoryManagerService;
import com.yuntong.service.Y_IRegisterManagerService;
import com.yuntong.service.impl.Y_RegisterManagerService;


/** 
 * MyEclipse Struts
 * Creation date: 04-01-2007
 * 
 * XDoclet definition:
 * @struts.action path="/y_SHDJGoods" name="y_SHDJGoodsForm" input="/Y_shgl_3.jsp" scope="request" validate="true"
 */
public class Y_SHDJGoodsAction extends Action {
	private Y_IFactoryManagerService factoryManagerService;
	private Y_IClientManagerService clientManagerService;
	private Y_IRegisterManagerService registerManagerService;
	private Log log = LogFactory.getLog(Y_SHDJGoodsAction.class);
	public Y_IRegisterManagerService getRegisterManagerService() {
		return registerManagerService;
	}
	public void setRegisterManagerService(
			Y_IRegisterManagerService registerManagerService) {
		this.registerManagerService = registerManagerService;
	}
	public Y_IClientManagerService getClientManagerService() {
		return clientManagerService;
	}
	public void setClientManagerService(Y_IClientManagerService clientManagerService) {
		this.clientManagerService = clientManagerService;
	}
	public Y_IFactoryManagerService getFactoryManagerService() {
		return factoryManagerService;
	}
	public void setFactoryManagerService(
			Y_IFactoryManagerService factoryManagerService) {
		this.factoryManagerService = factoryManagerService;
	}
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Y_SHDJGoodsForm gForm = (Y_SHDJGoodsForm) form;
		//得到厂商信息
		HttpSession session=request.getSession();		
		Y_FactoryInfoVO factoryVO=(Y_FactoryInfoVO) session.getAttribute("Y_csdj");
		int factoryID=factoryVO.getFactoryID();
		//如果厂商信息为未注册厂商则添加新信息
		if(factoryID!=9999){			
			factoryVO=factoryManagerService.findFactoryByID(factoryID);			
		}
		//得到客户信息
		Y_ClientInfoVO clientVO=(Y_ClientInfoVO) session.getAttribute("Y_khdj");
		long clientID=clientVO.getClientID();
		
		//如果客户为新客户是则插入新信息		
		if(clientID!=9){			
			clientVO=clientManagerService.findById(clientID);
		}
		//得到货物信息
		Y_GoodsInfoVO gVO=new Y_GoodsInfoVO();
		gVO.setGoodsBulk(Float.parseFloat(gForm.getGoodsBulk()));
		gVO.setBuyStyleID(Integer.parseInt(gForm.getFreightStyle()));
		gVO.setGoodsHeft(Float.parseFloat(gForm.getGoodsHeft()));
		gVO.setGoodsValue(Float.parseFloat(gForm.getGoodsValue()));
		gVO.setGoodsName(gForm.getGoodsName());
		gVO.setGoodsNumberUnit(gForm.getGoodsNumberUnit());
		gVO.setGoodsNumber(Long.parseLong(gForm.getGoodsNumber()));
		log.info("货款计算方式编号"+gForm.getPaymentStyle());
		gVO.setGoodsPaymentModeid(Integer.parseInt(gForm.getPaymentStyle()));
		gVO.setGoodsRemarkInfo(gForm.getGoodsRemark());	
		//当前用户信息
		Y_UserInfoVO uvo= (Y_UserInfoVO) session.getAttribute("userLoginInfo");	
		//插入货票信息
		Y_RegisterInfoVO rvo=new Y_RegisterInfoVO();
		rvo=registerManagerService.addRegisterInfo(factoryVO, clientVO, gVO,uvo);
		
		//保存货票信息
		request.setAttribute("Y_rvo", rvo);
		//保存厂商信息
		request.setAttribute("Y_fvo", factoryVO);
		//保存客户信息 
		request.setAttribute("Y_cvo", clientVO);		
		//根据编号得到结算方式		
		List jlist=(List) session.getAttribute("Y_finfo");
		for (int i = 0; i < jlist.size(); i++) {
			if(((Y_FreightStyleVO)(jlist.get(i))).getFreightID()==gVO.getBuyStyleID()){
				gVO.setBuyStyleName(((Y_FreightStyleVO)(jlist.get(i))).getFreightName());
			}					
		}
		//根据编号得到记费方式
		List plist=(List) session.getAttribute("Y_pinfo");
		for (int i = 0; i < plist.size(); i++) {
			if(((Y_PaymentStyleVO)(plist.get(i))).getPaymentID()==gVO.getGoodsPaymentModeid()){
				gVO.setGoodsPaymentModeName(((Y_PaymentStyleVO)(plist.get(i))).getPaymentName());
			}
		}
		//保存货物信息
		request.setAttribute("Y_gvo", gVO);
		return mapping.findForward("go4");
	}
}