package com.webclient.beans.assetdispo;

import psdi.app.iface.sap.Sap2EamHandler;
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.beans.asset.AssetAppBean;
import psdi.webclient.system.beans.ResultsBean;

import java.io.IOException;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.util.Date;

/**
 * 资产台账AppBean
 * edit 2023-10-02
 */
public class LpAssetAppBean extends AssetAppBean {

    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{
                //没有等级
                set.setWhere("1=2");
            }
        }
        pset.close();

    }

    @Override
    public int DUPLICATE() throws MXException, RemoteException {
        MboRemote appMbo=app.getAppBean().getMbo();
        String sdgdzcbm=appMbo.getString("SDGDZCBM");
        String templateid=appMbo.getString("TEMPLATEID");
        String parent=appMbo.getString("PARENT");
        String location=appMbo.getString("LOCATION");
        String sdunittype=appMbo.getString("SDUNITTYPE");//设备型号
        String sdmemo=appMbo.getString("SDMEMO");//备注
        Date installdate=appMbo.getDate("INSTALLDATE");//安装日期
        String sdgys=appMbo.getString("SDGYS");//供应商
        String sdzzs=appMbo.getString("SDZZS");//供应商
        double replacecost=appMbo.getDouble("REPLACECOST");//更换成本
        double purchaseprice=appMbo.getDouble("PURCHASEPRICE");//采购价格
        MboSetRemote jscsSet=appMbo.getMboSet("UDASSETSPEC");//技术参数子表
        MboSetRemote zxzSet=appMbo.getMboSet("UDCJZ");//执行组子表

        int duplicate=super.DUPLICATE();
        MboRemote tagerMbo=app.getAppBean().getMbo();
        tagerMbo.setValue("ASSETNUM", tagerMbo.getInt("ASSETUID")+"", 2L);
        tagerMbo.setValue("TEMPLATEID", templateid, 11L);
        tagerMbo.setValue("PARENT", parent, 2L);
        tagerMbo.setValue("LOCATION", location, 2L);
        tagerMbo.setValue("SDUNITTYPE", sdunittype, 2L);
        tagerMbo.setValue("SDMEMO", sdmemo, 2L);
        tagerMbo.setValue("INSTALLDATE", installdate, 2L);
        tagerMbo.setValue("SDGYS", sdgys, 2L);
        tagerMbo.setValue("SDZZS", sdzzs, 2L);
        tagerMbo.setValue("REPLACECOST", replacecost, 2L);
        tagerMbo.setValue("PURCHASEPRICE", purchaseprice, 2L);
        tagerMbo.setValue("SDGDZCBM", sdgdzcbm, 2L);

        if(null!=jscsSet && !jscsSet.isEmpty()){
            MboSetRemote jscs2Set=tagerMbo.getMboSet("UDASSETSPEC");//技术参数子表
            for(int i=0;i<jscsSet.count();i++){
                MboRemote jscsLine2Mbo=jscs2Set.add();
                //ASSETNUM=:ASSETNUM and siteid=:siteid
                jscsLine2Mbo.setValue("ASSETNUM", tagerMbo.getInt("ASSETUID")+"".replace(",", ""), 2L);
                jscsLine2Mbo.setValue("siteid", tagerMbo.getString("siteid"), 2L);
                jscsLine2Mbo.setValue("PARAKEY", jscsSet.getMbo(i).getString("PARAKEY"), 2L);
                jscsLine2Mbo.setValue("PARAVALUE", jscsSet.getMbo(i).getString("PARAVALUE"), 2L);
                jscsLine2Mbo.setValue("REMARK", jscsSet.getMbo(i).getString("REMARK"), 2L);
            }
        }

        if(null!=zxzSet && !zxzSet.isEmpty()){
            MboSetRemote zxz2Set=tagerMbo.getMboSet("UDCJZ");//执行组子表
            for(int j=0;j<zxzSet.count();j++){
                MboRemote zxzLMbo=zxz2Set.add();
                //ASSETID=:ASSETNUM and SITEID=:SITEID
                zxzLMbo.setValue("ASSETID", tagerMbo.getInt("ASSETUID")+"".replace(",", ""), 2L);
                zxzLMbo.setValue("siteid", tagerMbo.getString("siteid"), 2L);
                zxzLMbo.setValue("TYPE", zxzSet.getMbo(j).getString("TYPE"), 11L);
                zxzLMbo.setValue("UDZXZGH", zxzSet.getMbo(j).getString("UDZXZGH"),11L);
            }
        }

//        app.getAppBean().save();
        app.getAppBean().reloadTable();
        app.getAppBean().refreshTable();
        return duplicate;
    }

    public void HQSAPASSET() throws IOException, MXException, ParseException {
        System.out.println("--------start---------");
        Sap2EamHandler Sap2EamHandler = new Sap2EamHandler();
        Sap2EamHandler.Z_OA_SAP_ASSETS_DETAILS();
        System.out.println("-------end-------------");
    }
}


