package nc.ui.obm.payroll.ebankdfgz.action;

import static nc.bs.framework.common.NCLocator.getInstance;

import java.awt.Event;
import java.awt.event.ActionEvent;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Vector;

import javax.swing.Action;
import javax.swing.KeyStroke;

import nc.bs.framework.common.NCLocator;
import nc.itf.obm.payroll.IBusilogService;
import nc.itf.obm.payroll.IUpdateStateMaintain;
import nc.pubitf.obm.payroll.IDFGZPay;
import nc.ui.obm.payroll.ebankdfgz.model.DfgzBillModel;
import nc.ui.pubapp.uif2app.query2.model.ModelDataManager;
import nc.ui.uif2.NCAction;
import nc.ui.uif2.ShowStatusBarMsgUtil;
import nc.ui.uif2.UIState;
import nc.ui.uif2.model.AbstractAppModel;
import nc.vo.obm.BillObmStateVO;
import nc.vo.obm.log.ObmLog;
import nc.vo.obm.obmvo.PayStateQueryVO;
import nc.vo.obm.payroll.DfgzBillVO;
import nc.vo.payroll.ml.MLPayroll;
/**
 *  
 * 指令状态下载UI端动作响应.
 *
 * @author  yinyan
 * @version 1.0 2011/1/14
 * @since   NC6.0
 */
public class LogStateDownloadAction extends NCAction {
 
	/**serialVersionUID*/
	private static final long serialVersionUID = 1L;
	/**
	 * UI端数据模型
	 */
	private AbstractAppModel model = null;

	private ModelDataManager dataManager;
	
	public LogStateDownloadAction() {
		super();
		setCode("EbankLogDownloadStateAction");
		setBtnName(MLPayroll.getStr00067()/*"状态下载"*/); 
		putValue(Action.SHORT_DESCRIPTION,getBtnName()+"(Ctrl+J)");
		putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke((int)'J', Event.CTRL_MASK));
	}
	@Override
	public void doAction(ActionEvent e) throws Exception {
		Object[] selectObjs = ((DfgzBillModel)getModel()).getSelectedOperaDatas();
		if (selectObjs == null||selectObjs.length==0) {
			throw new Exception(MLPayroll.getStr00068()/*"下载状态前请先选择记录"*/);
		}
		DfgzBillVO[] selectvos = (DfgzBillVO[]) Array.newInstance(DfgzBillVO.class,selectObjs.length);
	    System.arraycopy(selectObjs, 0, selectvos, 0, selectvos.length);
		Collection<String> yurrefs = new Vector<String>();
		for (DfgzBillVO billvo : selectvos) {
			yurrefs.add(billvo.getPayYurref());
		}
		String userid = getModel().getContext().getPk_loginUser();
		PayStateQueryVO statequery = new PayStateQueryVO();
		statequery.setPayFunc("dfgzcx");
		statequery.setUseid(userid);
		statequery.setYurrefs(yurrefs.toArray(new String[0]));
		try {
//			IDFGZPay logsev = (IDFGZPay) getInstance().lookup(IDFGZPay.class.getName());
//			BillObmStateVO[] downvos = logsev.queryDFGZPaystate(statequery);
			IBusilogService logsev = (IBusilogService) getInstance().lookup(IBusilogService.class.getName());
			BillObmStateVO[] downvos = logsev.queryDFGZPaystate(statequery,selectvos);
			DfgzBillModel dmodel=(DfgzBillModel)this.getModel();
			dmodel.updateUILogDataFromDB(selectvos);
			StringBuffer errMsg = new StringBuffer();
			Exception ex = null;
			if(downvos.length==1&&downvos[0].getInterruptException()!=null){
				throw new Exception(downvos[0].getInterruptException().getMessage());
			}
			IUpdateStateMaintain maintian = NCLocator.getInstance().lookup(IUpdateStateMaintain.class);
			for (BillObmStateVO vo : downvos) {
				ex = vo.getInterruptException();
				if (ex != null) {
					errMsg.append(MLPayroll.getStr00069()/*"单据"*/+vo.getBizData("vbillno")+ ex.getMessage()).append("\n");
				}else {
					String sql = "update ebank_dfgz_h set vdef1 = '已下载' where vbillno = '"+vo.getBizData("vbillno")+"' and logflag = 0 and dr = 0";
					maintian.updateState(sql);
				}
			}
			if (errMsg != null&&errMsg.length()>0) {
				ShowStatusBarMsgUtil.showErrorMsg(MLPayroll.getStr00070()/*"下载完成，部分操作失败"*/, errMsg.toString(),getModel().getContext());
				return;
			}else{
				ShowStatusBarMsgUtil.showStatusBarMsg(MLPayroll.getStr00071()/*"下载完成"*/,getModel().getContext());	
			}
		} catch (Exception ex) {
			ObmLog.error(ex.getMessage(), this.getClass(), "doAction");
			throw ex;
		}
	}
	@Override
	protected boolean isActionEnable() { 
		boolean enabled=false;
		if(model!=null){
		  enabled=model.getUiState()==UIState.NOT_EDIT &&model.getSelectedData()!=null;
		}
		return enabled;
	}
	public AbstractAppModel getModel() {
		return model;
	}
	public void setModel(AbstractAppModel model) {
		this.model = model; 
		if(this.model!=null){
			this.model.addAppEventListener(this);
		}
	}
	public ModelDataManager getDataManager() {
		return dataManager;
	}
	public void setDataManager(ModelDataManager dataManager) {
		this.dataManager = dataManager;
	}
}
