package com.webclient.beans.plusctmplt;

import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.plusc.webclient.beans.plusctmplt.PlusCAssetTemplateAppBean;
import psdi.server.MXServer;
import psdi.util.MXException;
import psdi.webclient.system.beans.ResultsBean;

import java.rmi.RemoteException;

/**
 * 资产模板AppBean
 * edit 2023-10-02
 */
public class PlusctmpltAppBean extends PlusCAssetTemplateAppBean {

    public void initializeApp() throws MXException, RemoteException {
        super.initializeApp();
        ResultsBean resultsbean = app.getResultsBean();
        MboSetRemote set=resultsbean.getMboSet();
        String userid=set.getUserInfo().getPersonId();
        MboSetRemote pset= MXServer.getMXServer().getMboSet("person", set.getUserInfo());
        pset.setWhere("personid='"+userid+"'");
        if(!pset.isEmpty() && pset.count()>0){
            MboRemote per=pset.getMbo(0);
            //权限等级
            String aut=per.getString("UDAUTGRADE");
            String orgid=per.getString("LOCATIONORG");
//            String siteid=per.getString("LOCATIONSITE");

            if("Level 1".equalsIgnoreCase(aut)){
                set.setWhere(" 1=1 ");
            }else if("Level 2".equalsIgnoreCase(aut)){
                set.setWhere(" ORGID='"+orgid+"' ");
            }else if("Level 3".equalsIgnoreCase(aut)){
                //资产模板没有地点数据限制
            }else if("Level 4".equalsIgnoreCase(aut)){
                //资产模板没有个人数据限制
            }else{
                //没有等级
                set.setWhere("1=2");
            }
        }
        pset.close();

    }

    @Override
    public int DUPLICATE() throws MXException, RemoteException {
        MboRemote appMbo=app.getAppBean().getMbo();
        String templateid=appMbo.getString("TEMPLATEID");
        String orgid=appMbo.getString("ORGID");
        String assetdescription=appMbo.getString("ASSETDESCRIPTION");
        String assettype=appMbo.getString("ASSETTYPE");
        String manufacturer=appMbo.getString("MANUFACTURER");
        String vendor=appMbo.getString("VENDOR");
        String usage=appMbo.getString("USAGE");
        double purchaseprice=appMbo.getDouble("PURCHASEPRICE");
        double replacecost=appMbo.getDouble("REPLACECOST");
        double budgetcost=appMbo.getDouble("BUDGETCOST");
        double priority=appMbo.getDouble("PRIORITY");
        String classificationid=appMbo.getString("CLASSIFICATIONID");
        MboSetRemote udfauclassSet=appMbo.getMboSet("$UDFAUCLASS","UDFAUCLASS","TEMPLATEID='"+templateid+"' and orgid='"+orgid+"'");
        int duplicate=super.DUPLICATE();
        MboRemote tagerMbo=app.getAppBean().getMbo();
        tagerMbo.setValue("ASSETDESCRIPTION", assetdescription, 2L);
        tagerMbo.setValue("ASSETTYPE", assettype, 2L);
        tagerMbo.setValue("MANUFACTURER", manufacturer, 2L);
        tagerMbo.setValue("VENDOR", vendor, 2L);
        tagerMbo.setValue("USAGE", usage, 2L);
        tagerMbo.setValue("PURCHASEPRICE", purchaseprice, 2L);
        tagerMbo.setValue("REPLACECOST", replacecost, 2L);
        tagerMbo.setValue("BUDGETCOST", budgetcost, 2L);
        tagerMbo.setValue("PRIORITY", priority, 2L);
        tagerMbo.setValue("TEMPLATEID", tagerMbo.getInt("PLUSCTEMPLATEID")+"", 2L);
        tagerMbo.setValue("CLASSIFICATIONID", classificationid, 2L);
        if(null!=udfauclassSet && !udfauclassSet.isEmpty()){
            MboSetRemote udfauclassCopySet= MXServer.getMXServer().getMboSet("UDFAUCLASS",MXServer.getMXServer().getSystemUserInfo());;
            for(int i=0;i<udfauclassSet.count();i++){
                MboRemote udfauclassMbo=udfauclassSet.getMbo(i);
                //TEMPLATEID=:TEMPLATEID and   orgid=:orgid
                MboRemote udfacopyMbo=udfauclassCopySet.add(2L);
//                udfacopyMbo.setValue("TEMPLATEID",tagerMbo.getInt("PLUSCTEMPLATEID")+"",2L);
                udfacopyMbo.setValue("ORGID",tagerMbo.getString("ORGID"),2L);
                udfacopyMbo.setValue("FAILURECODE",udfauclassMbo.getString("FAILURECODE"),2L);
                udfacopyMbo.getThisMboSet().save();
            }
        }
//        app.getAppBean().save();
        app.getAppBean().reloadTable();
        app.getAppBean().refreshTable();
        return duplicate;
    }

    /**
     * 发布至资产台账按钮
     * @throws MXException
     * @throws RemoteException
     */
    public void FBZZCTZ() throws MXException, RemoteException {
        MboRemote appMbo=app.getAppBean().getMbo();
        String templateid=appMbo.getString("TEMPLATEID");
        String orgid=appMbo.getString("ORGID");
        MboSetRemote zcBjSet = MXServer.getMXServer().getMboSet("SPAREPART",MXServer.getMXServer().getSystemUserInfo());
        zcBjSet.setWhere("assetnum in(SELECT assetnum FROM ASSET WHERE TEMPLATEID='"+templateid+"' AND ORGID='"+orgid+"') and siteid IN(SELECT siteid FROM SITE WHERE ORGID='"+orgid+"') AND ITEMNUM in(SELECT ITEMNUM FROM PLUSCTPSPAREPART WHERE templateid ='"+templateid+"' and orgid ='"+orgid+"')");
        zcBjSet.reset();
        if(null!=zcBjSet && !zcBjSet.isEmpty()){
            zcBjSet.deleteAll(2L);
            zcBjSet.save();
        }

        MboSetRemote assetSet = MXServer.getMXServer().getMboSet("ASSET",MXServer.getMXServer().getSystemUserInfo());
        assetSet.setWhere("TEMPLATEID='"+templateid+"' AND ORGID='"+orgid+"'");
        assetSet.reset();
        if(null!=assetSet && !assetSet.isEmpty()){
            MboSetRemote pluSet=appMbo.getMboSet("PLUSCTPSPAREPART");
            for(int i=0;i<assetSet.count();i++){
                if(null!=pluSet && !pluSet.isEmpty()){
                    for(int j=0;j<pluSet.count();j++){
                        MboRemote pluMbo=pluSet.getMbo(j);
                        MboRemote zcbjMbo=zcBjSet.add(2L);
                        zcbjMbo.setValue("assetnum", assetSet.getMbo(i).getString("assetnum"), 11L);
                        zcbjMbo.setValue("SITEID", assetSet.getMbo(i).getString("SITEID"), 11L);
                        zcbjMbo.setValue("ITEMNUM", pluMbo.getString("ITEMNUM"), 11L);
                        zcbjMbo.setValue("ITEMSETID", "SUNDASET", 11L);
                        zcbjMbo.setValue("QUANTITY", pluMbo.getDouble("QUANTITY"), 11L);
                        zcbjMbo.getThisMboSet().save();
                    }
                }
            }
        }
        this.clientSession.showMessageBox("commoninfo", "commoninfo_fb", new String[]{"发布成功!"});

    }


}
