// 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 FldWopm1 extends MboValueAdapter
{
  psdi.mbo.MboValue test;
  public FldWopm1(MboValue mbovalue)
      throws MXException
  {
    super(mbovalue);
  }

  public void validate() throws MXException, RemoteException {
    String sWorktype = getMboValue("WORKTYPE").getCurrentValue().asString();
    sWorktype = (sWorktype == null) ? "" : sWorktype.trim();
    if (sWorktype.length() == 0) {
      throw new MXApplicationException("woreq", "WorktypeNotCanNull");
    }
  }

  public void action() throws MXException, RemoteException
  {
    String sWorktype = getMboValue("WORKTYPE").getCurrentValue().asString();
    sWorktype =(sWorktype == null)?"":sWorktype.trim();
    if (sWorktype.equalsIgnoreCase("CM")) {
      String sWopm1 = getMboValue("WOPM1").getCurrentValue().asString();
      sWopm1 = (sWopm1 == null) ? "" : sWopm1.trim();
      String sWopm2 = sWopm1(sWopm1);
      getMboValue("WOPM2").setValue(sWopm2);
      getMboValue("WO17").setValue("WOWAIT");
      getMboValue("WOLO10").setValue("0");
    }
    String sWopm1 = getMboValue("WOPM1").getCurrentValue().asString();
    sWopm1 = (sWopm1 == null) ? "" : sWopm1.trim();
    String vWopm1[] = {
        "31", "32", "33", "34", "35", "36"};
    String vWopm5[] = {
        "2102", "2103", "2104", "2105", "2106", "2107"};
    int iloop = vWopm1.length;
    for (int i = 0; i < iloop; i++) {
      String s1 = vWopm1[i];
      if (sWopm1.equalsIgnoreCase(s1)) {
        getMboValue("WOPM5").setValue(vWopm5[i]);
      }
    }
  }

  public void init() throws MXException {
    try {
      super.init();
      getMboValue().setReadOnly(false);
    }
    catch (Exception exception) {}
  }

  private  String sWopm1(String sWopm1)
      throws MXException, RemoteException
  {
    String rStr = "";
    try
    {
      //查找本年月自动编号的值
      StringBuffer sqllocal = new StringBuffer(300);
      sqllocal.append("select DEPT1,DEPTNUM ").append(" from ").append("DEPT");
      //取出连接关键字
      ConnectionKey connKey = getMboValue().getMbo().getUserInfo().getConnectionKey();
      //取出连接--测试成功
      Connection conn = getMboValue().getMbo().getMboServer().getDBConnection(connKey);
      //执行查询，返回一个结果集合
      QueryResult result = DBCon.runQuery(sqllocal.toString(),conn);
      int loop = result.size();
      if(result.size() >0){
        for(int i=0;i<loop;i++){
          ResultRow resultRow = result.getRow(i);
          String sWoreqSpStr = resultRow.getValue("DEPT1");
          sWoreqSpStr =(sWoreqSpStr==null)?"":sWoreqSpStr.trim();
          String sDeptNum = resultRow.getValue("DEPTNUM");
          sDeptNum =(sDeptNum==null)?"":sDeptNum.trim();
          if(sWoreqSpStr.indexOf(sWopm1,0) > -1){
            rStr = sDeptNum;
            break;
          }
        }
      }
      return rStr;
    }
    catch(Exception exception) { }
    return "";
  }
}

