package com.webclient.beans.udprgather;

import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.server.MXServer;
import psdi.util.MXApplicationException;
import psdi.util.MXException;
import psdi.webclient.system.beans.DataBean;

import java.rmi.RemoteException;
import java.util.Vector;

/**
 * 工厂需求申请选择车间需求申请
 * edit 20230419
 */
public class SelectRrDataBean extends DataBean {

    @Override
    public synchronized int execute() throws MXException, RemoteException {
        int ex = super.execute();
        MboRemote appMbo=app.getAppBean().getMbo();
        String status=appMbo.getString("STATUS");
        if(!"WAPPR".equalsIgnoreCase(status)){
            throw new MXApplicationException("udprgather", "udprgather_wappr", new String[0] );
        }
        DataBean assetDataBean= app.getDataBean("selectpr_Table");//选择车间需求DataBean
        DataBean targetDataBean= app.getDataBean("1682228924278");//工厂需求明细行DataBean
        MboSetRemote udprlineSet=appMbo.getMboSet("PRLINE");
        Vector<MboRemote> vec=assetDataBean.getSelection();
        if (!vec.isEmpty() && appMbo!=null) {
            MboSetRemote prlineSet = MXServer.getMXServer().getMboSet("PRLINE",MXServer.getMXServer().getSystemUserInfo());
            for(int i=0;i<vec.size();i++){
                MboRemote vMbo = (MboRemote)vec.get(i);
//                MboSetRemote prlineSet=vMbo.getMboSet("$PRLINE","PRLINE","PRNUM ='"+vMbo.getString("PRNUM")+"' AND SITEID ='"+vMbo.getString("SITEID")+"'");
                prlineSet.setWhere("PRNUM ='"+vMbo.getString("PRNUM")+"' AND SITEID ='"+vMbo.getString("SITEID")+"'");
                prlineSet.reset();
                for(int j=0;j<prlineSet.count();j++){
                    MboRemote prlineMbo=prlineSet.getMbo(j);
                    MboRemote targetMbo= udprlineSet.add();
                    //设置关联字段值 prnum=:prnum and siteid=:siteid
                    MboRemote ownerMbo=targetMbo.getOwner();
                    targetMbo.setValue("prnum",ownerMbo.getString("prnum"),2L);
                    targetMbo.setValue("siteid",ownerMbo.getString("siteid"),2L);
                    targetMbo.setValue("LINETYPE", prlineMbo.getString("LINETYPE"));
                    if("ITEM".equalsIgnoreCase(prlineMbo.getString("LINETYPE"))){
                        targetMbo.setValue("ITEMNUM", prlineMbo.getString("ITEMNUM"));
                    }
                    targetMbo.setValue("DESCRIPTION", prlineMbo.getString("DESCRIPTION"));
                    targetMbo.setValue("ORDERQTY", prlineMbo.getDouble("APPRQUANTITY"));
                    targetMbo.setValue("APPRQUANTITY", prlineMbo.getDouble("APPRQUANTITY"));
                    targetMbo.setValue("REMARKS", prlineMbo.getString("REMARKS"));
                    targetMbo.setValue("ORDERUNIT", prlineMbo.getString("ORDERUNIT"));
                    targetMbo.setValue("CONVERSION", prlineMbo.getDouble("CONVERSION"));
                    targetMbo.setValue("UDPRNUM", prlineMbo.getString("PRNUM"),2L);
                    targetMbo.setValue("UDPRLINENUM", prlineMbo.getString("PRLINENUM"),2L);
                    targetMbo.setValue("PRONUM", vMbo.getString("PRONUM"),2L);
                    prlineMbo.setValue("UDPRNUM", targetMbo.getString("PRNUM"),2L);
                    prlineMbo.setValue("UDPRLINENUM", targetMbo.getString("PRLINENUM"),2L);
                    prlineSet.save();
                    targetMbo.getThisMboSet().save();
                }
            }
        }
        app.getAppBean().refreshTable();
        app.getAppBean().refreshTable();
        targetDataBean.reloadTable();
        targetDataBean.refreshTable();

        return ex;
    }
}
