package com.webclient.beans.pmstdard;

import com.hp.hpl.jena.graph.compose.Delta;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.server.MXServer;
import psdi.util.MXApplicationYesNoCancelException;
import psdi.util.MXException;
import psdi.webclient.system.beans.AppBean;
import psdi.webclient.system.beans.ResultsBean;
import psdi.webclient.system.controller.WebClientEvent;

import javax.ws.rs.DELETE;
import java.rmi.RemoteException;

/**
 * 点检保养标准AppBean
 * edit 2023-10-17
 */
public class PmstdardAppBean extends AppBean {

    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)){
//                set.setWhere(" ORGID='"+orgid+"' and SITEID='"+siteid+"' "); 已取消地点级
                set.setWhere(" ORGID='"+orgid+"' ");
            }else if("Level 4".equalsIgnoreCase(aut)){
                set.setWhere(" ORGID='"+orgid+"'  and CREATEBY='"+userid+"'");
            }else{
                //没有等级
                set.setWhere("1=2");
            }
        }
        pset.close();

    }

    @Override
    public int DELETE() throws MXException, RemoteException {
        MboRemote appMbo=app.getAppBean().getMbo();
        int msgRet = MXApplicationYesNoCancelException.getUserInput("confirmcontinueid", MXServer.getMXServer(),this.clientSession.getUserInfo());
        if (msgRet < 0) {
            throw new MXApplicationYesNoCancelException("confirmcontinueid", "jspmessages", "deletassetdj");
        }
        if (msgRet == 8) {
            String sql="ASSETNUM in(SELECT ASSETNUM FROM ASSET WHERE TEMPLATEID='"+appMbo.getString("TEMPLATEID")+"' AND ORGID='"+appMbo.getString("ORGID")+"') AND ORGID='"+appMbo.getString("ORGID")+"'";
            MboSetRemote pmstdardlinesSet = appMbo.getMboSet("$PMSTDARDLINES","PMSTDARDLINES",sql);
            if(null!=pmstdardlinesSet && !pmstdardlinesSet.isEmpty()){
                pmstdardlinesSet.deleteAll(2L);
                pmstdardlinesSet.save();
            }
            MboSetRemote pmSet=appMbo.getMboSet("$PMSTDARD", "PMSTDARD", "TEMPLATEID='"+appMbo.getString("TEMPLATEID")+"' and ORGID='"+appMbo.getString("ORGID")+"'");
            if(null!=pmSet && !pmSet.isEmpty()){
                pmSet.deleteAll(2L);
                pmSet.save();
                this.clientSession.showMessageBox(this.clientSession.getCurrentEvent(), "system", "deleterecord", (Object[])null);
                this.gotoTab(this.clientSession, "list");
            }
        }
        return 1;
    }
}
