package com.app.hxqh.udoutser;

import psdi.mbo.Mbo;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSet;
import psdi.mbo.MboSetRemote;
import psdi.security.UserInfo;
import psdi.server.MXServer;
import psdi.util.MXException;

import java.rmi.RemoteException;

/**
 * 委外服务
 * createby liuxq  2023-8-17
 */
public class UdoutserMbo extends Mbo implements MboRemote {

    public UdoutserMbo(MboSet ms) throws MXException, RemoteException {
        super(ms);
    }

    @Override
    public void add() throws MXException, RemoteException {
        super.add();
        try {
            UserInfo user = getThisMboSet().getUserInfo();
            String CREATEBY = user.getPersonId();
            String ORGID = getString("ORGID");
            String SITEID = getString("SITEID");
            String DEPARTMENT = "";
            MboSetRemote personSet = MXServer.getMXServer().getMboSet("PERSON",MXServer.getMXServer().getSystemUserInfo());
            personSet.setWhere("personid='"+CREATEBY+"'");
            personSet.reset();
            if(personSet!=null && !personSet.isEmpty()){
                MboRemote personMbo = personSet.getMbo(0);
                DEPARTMENT = personMbo.getString("DEPARTMENT");
            }
            MboSetRemote UDCBZXYDLXYSSet = MXServer.getMXServer().getMboSet("UDCBZXYDLXYS",MXServer.getMXServer().getSystemUserInfo());
            UDCBZXYDLXYSSet.setWhere("orgid='"+ORGID+"' AND SITEID='"+SITEID+"' AND WHS_LOCATION_CODE='"+DEPARTMENT+"'");
            UDCBZXYDLXYSSet.reset();
            if(UDCBZXYDLXYSSet!=null && !UDCBZXYDLXYSSet.isEmpty()){
                MboRemote udcbzxMbo = UDCBZXYDLXYSSet.getMbo(0);
                String COST_CENTER =  udcbzxMbo.getString("COST_CENTER");
                this.setValue("COSTCENTER",COST_CENTER,2L);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
            e.getMessage();
        }
    }

    @Override
    public void init() throws MXException {
        super.init();
        try {
            String status = getString("STATUS");
            String[] mainFields = new String[] {"UDOUTSERNUM","DESCRIPTION","URGENCY","CGDD","ISLX","PROJECTNUM","COSTCENTER","WLZ","NBDD","REMARK","STATUS","CGJL","COIN","PLANDATE"};
            if("EDIT".equalsIgnoreCase(status) || "CAN".equalsIgnoreCase(status) ){//草稿 / OA驳回
                setFieldFlag(mainFields, 7L, false);
                getMboSet("FZ").setFlag(7L, false);
            }
            if("APPR".equalsIgnoreCase(status) || "COMP".equalsIgnoreCase(status) ){//已提交OA / OA归档
                setFieldFlag(mainFields, 7L, true);
                getMboSet("FZ").setFlag(7L, true);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void save() throws MXException, RemoteException {
        super.save();

    }

}
