package com.webclient.beans.udaqdjyh;

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.AppBean;
import psdi.webclient.system.beans.ResultsBean;
import psdi.workflow.WorkFlowServiceRemote;

import java.rmi.RemoteException;
import java.util.Date;

/**
 * 安全点检隐患AppBean
 * edit 20240711
 */
public class UdAqdjyhAppBean extends AppBean {

    public void ZAQDJYHCLGD() throws MXException, RemoteException {
        MboRemote appMbo=app.getAppBean().getMbo();
        //安全点检隐患处理工单
        MboSetRemote udaqdjyhgdSet = MXServer.getMXServer().getMboSet("UDAQDJYHGD",MXServer.getMXServer().getSystemUserInfo());
        String sql="UDAQDJYHID="+appMbo.getInt("UDAQDJYHID")+" AND  ORGID='"+appMbo.getString("ORGID")+"' AND SITEID='"+appMbo.getString("SITEID")+"' AND STATUS NOT IN('CLOSE','COMP') ";
        udaqdjyhgdSet.setWhere(sql);
        udaqdjyhgdSet.reset();
        if(udaqdjyhgdSet.isEmpty()){
            MboRemote udaqdjyhgdMbo=udaqdjyhgdSet.add();//安全点检隐患处理工单Mbo
            int udlldid=udaqdjyhgdMbo.getInt("UDAQDJYHGDID");
            udaqdjyhgdMbo.setValue("WONUM", udlldid+"", 2L);
            udaqdjyhgdMbo.setValue("DESCRIPTION", appMbo.getString("DESCRIPTION"), 2L);
            udaqdjyhgdMbo.setValue("STATUS", "EDIT", 2L);
            udaqdjyhgdMbo.setValue("STATUSTIME", new Date(), 2L);
            udaqdjyhgdMbo.setValue("CREATEBY", appMbo.getThisMboSet().getUserInfo().getPersonId(), 2L);
            udaqdjyhgdMbo.setValue("UDAQDJYHID", appMbo.getInt("UDAQDJYHID"), 2L);
            udaqdjyhgdMbo.setValue("ORGID", appMbo.getString("ORGID"), 2L);
            udaqdjyhgdMbo.setValue("SITEID", appMbo.getString("SITEID"), 2L);

//            ((WorkFlowServiceRemote) MXServer.getMXServer().lookup("WORKFLOW")).initiateWorkflow("UDPMWOORDE", workMbo);
            udaqdjyhgdMbo.getThisMboSet().save();
            udaqdjyhgdMbo.getThisMboSet().close();

            appMbo.setValue("YHCLGDID", udlldid, 2L);
            app.getAppBean().save();
            app.getAppBean().reloadTable();
            app.getAppBean().refreshTable();

            String appName = "UDAQDJYHGD";
            StringBuffer newUrl = new StringBuffer(this.clientSession.getMaximoRequestURI());
            newUrl.append("?event=gotoapp&value=" +appName+"&uniqueid="+udlldid);
            this.clientSession.getCurrentApp().put("forcereload", "true");
            this.clientSession.gotoApplink(newUrl.toString());
        }else{
            throw new MXApplicationException("udaqdjyhgd", "udaqdjyhgd_cf", new String[]{} );
        }

    }

    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();

    }
}

