package com.webclient.beans.udaqdjlx;

import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.server.MXServer;
import psdi.util.MXApplicationException;
import psdi.util.MXApplicationYesNoCancelException;
import psdi.util.MXException;
import psdi.webclient.system.beans.AppBean;
import psdi.webclient.system.beans.ResultsBean;

import java.rmi.RemoteException;

/**
 * 安全点检路线AppBean
 * edit 2024-07-12
 */
public class UdaqdjlxAppBean 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(" SITEID='"+siteid+"' ");
            }else if("Level 4".equalsIgnoreCase(aut)){
                set.setWhere(" ORGID='"+orgid+"' and SITEID='"+siteid+"' and CREATEBY='"+userid+"'");
            }else{
                //没有等级
                set.setWhere("1=2");
            }
        }
        pset.close();

    }

    @Override
    public int DELETE() throws MXException, RemoteException {
        try {
            MboRemote appMbo=app.getAppBean().getMbo();
            String status=appMbo.getString("STATUS");
            if(!"EDIT".equalsIgnoreCase(status) ){
                throw new MXApplicationException("udaqdjlx", "udaqdjlx_no", new String[]{});
            }
            int msgRet = MXApplicationYesNoCancelException.getUserInput("confirmcontinueid", MXServer.getMXServer(),this.clientSession.getUserInfo());
            if (msgRet < 0) {
                throw new MXApplicationYesNoCancelException("confirmcontinueid", "jspmessages", "deletassetaqdj");
            }
            if (msgRet == 8) {
                MboSetRemote udaqdjlxlineSet=appMbo.getMboSet("UDAQDJLXLINE");
                if(null!=udaqdjlxlineSet && !udaqdjlxlineSet.isEmpty()){
                    udaqdjlxlineSet.deleteAll(2L);
                    udaqdjlxlineSet.save();
                }
                MboSetRemote udaqdjwzrwjhSet=appMbo.getMboSet("$UDAQDJWZRWJH", "UDAQDJWZRWJH","UDAQDJLXID="+appMbo.getInt("UDAQDJLXID")+" and SITEID='"+appMbo.getString("SITEID")+"'");
                if(null!=udaqdjwzrwjhSet && !udaqdjwzrwjhSet.isEmpty()){
                    udaqdjwzrwjhSet.deleteAll(2L);
                    udaqdjwzrwjhSet.save();
                }
                this.delete();
                this.save();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        this.gotoTab(this.clientSession, "list");
        return 1;
    }
}
