// Source File Name:   FldToolEquipment.java

package psdi.app.workorder;
import java.rmi.*;
import java.sql.*;

import com.dbms.dbcon.*;
import psdi.mbo.*;
import psdi.security.*;
import psdi.util.*;

public class FldHdongroup extends MboValueAdapter
{
  psdi.mbo.MboValue test;
  public FldHdongroup(MboValue mbovalue)
      throws MXException
  {
    super(mbovalue);
  }

  public void validate()
      throws MXException, RemoteException
  {
  }
  public void action()
      throws MXException, RemoteException
  {
    try
    {
      String sPersonid = getMboValue().getMbo().getUserInfo().getPersonId();
      sPersonid =(sPersonid == null) ?"":sPersonid.trim();
      sPersonid = sPersonid.toUpperCase();
      if(sPersonid.length() >0){
        String curHdongroup = getMboValue("HDONGROUP").getCurrentValue().asString();
        curHdongroup=(curHdongroup == null)?"":curHdongroup.trim();
        if(curHdongroup.length() > 0) {
          return;
        }
        String deptNum = getLaborDept(sPersonid);
        deptNum =(deptNum == null)?"":deptNum.trim();
        //System.out.println("deptNum group v1.1000="+deptNum);
        //getMboValue("HDDEPT").setValue(deptNum.substring(0,2));
        getMboValue("HDONGROUP").setValue(deptNum);
        getMboValue("WO18").setValue(sPersonid);
      }
    }
    catch(Exception exception) { }
  }

  private  String  getLaborDept(String sPersionid)
      throws MXException, RemoteException
  {
    String reKeyStr = "";
    try
    {
      //查找本年月自动编号的值
      StringBuffer sqllocal = new StringBuffer(300);
      sqllocal.append("select DEPARTMENT").append(" from ").append("PERSON").append(" where UPPER(TRIM(PERSONID)) ="+"\'"+sPersionid+"\'");
      //取出连接关键字
      ConnectionKey connKey = getMboValue().getMbo().getUserInfo().getConnectionKey();
      //取出连接--测试成功
      Connection conn = getMboValue().getMbo().getMboServer().getDBConnection(connKey);
      //执行查询，返回一个结果集合
      QueryResult result = DBCon.runQuery(sqllocal.toString(),conn);
      if(result.size() >0){
        ResultRow resultRow = result.getRow(0);
        reKeyStr = resultRow.getValue("DEPARTMENT");
      }
      return reKeyStr;
    }
    catch(Exception exception) { }
    return reKeyStr;
  }

}

