package com.app.hxqh.udpgth;

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

import java.rmi.RemoteException;

/**
 * 人员组人员批量替换
 * createby 2023-12-15
 */
public class UdPGTHMbo extends Mbo implements MboRemote {

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

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

    @Override
    public void init() throws MXException {
        super.init();
        try{
            String status = getString("STATUS");
            if(!"EDIT".equalsIgnoreCase(status) && !isNew()){
//                getMboSet("UDTRANSFER").setFlag(7L, true);//主表只读
//                getMboSet("UDTRANSFERLINE").setFlag(7L, true);//子表只读
                setFlag(7L,true);
            }
        }catch (Exception e){

        }
    }

    @Override
    public void save() throws MXException, RemoteException {
        super.save();
        String PGTHNUM = getString("PGTHNUM");//单号
        String ORGID = getString("ORGID");
        String SITEID = getString("SITEID");
        String QPERSON = getString("QPERSON");//变更前人员
        MboSetRemote lineSet = MXServer.getMXServer().getMboSet("UDPGTHLINE",MXServer.getMXServer().getSystemUserInfo());//子表
        MboSetRemote teamSet = MXServer.getMXServer().getMboSet("PERSONGROUPTEAM",MXServer.getMXServer().getSystemUserInfo());//人员组子表
        if(!toBeDeleted()){
            //保存后，子表写入变更前人员所在的人员组
            teamSet.setWhere("RESPPARTYGROUP='"+QPERSON+"' and USEFORORG='"+ORGID+"' and USEFORSITE='"+SITEID+"'");
            teamSet.reset();
            if(teamSet!=null && !teamSet.isEmpty()){
                for(int i=0;i<teamSet.count();i++){
                    MboRemote teamMbo = teamSet.getMbo(i);
                    String PERSONGROUP = teamMbo.getString("PERSONGROUP");
                    lineSet.setWhere("PGTHNUM='"+PGTHNUM+"' and orgid='"+ORGID+"' and siteid='"+SITEID+"' and PERSONGROUP='"+PERSONGROUP+"'");
                    lineSet.reset();
                    if(lineSet!=null && !lineSet.isEmpty()){
                        continue;
                    }
                    MboRemote lineMbo = lineSet.add();
                    lineMbo.setValue("PGTHNUM",PGTHNUM,2L);
                    lineMbo.setValue("ORGID",ORGID,2L);
                    lineMbo.setValue("SITEID",SITEID,2L);
                    lineMbo.setValue("PERSONGROUP",PERSONGROUP,2L);//人员组
                    lineMbo.getThisMboSet().save();
                }
            }
        }
    }
}
