eas bos调用get类型接口导入组织方法

 定时任务的导入方式,每个组织的子级都需要生成一个名字为组织+本部的组织,编码为编码+-B的编号,并且组织的等级大于三级的时候,不在需要变更为成本中心,只需要行政组织(对GET接口的数据先进行排序,在解析)

package com.kingdee.eas.custom.oraUnit.app;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.MetaDataLoaderFactory;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.metadata.management.LanguageCollection;
import com.kingdee.bos.metadata.management.SolutionInfo;
import com.kingdee.bos.service.message.Message;
import com.kingdee.bos.service.message.agent.MessageFactory;
import com.kingdee.bos.service.message.agent.SenderAgent;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.message.BMCMessageFactory;
import com.kingdee.eas.base.message.BMCMessageInfo;
import com.kingdee.eas.base.message.MsgBizType;
import com.kingdee.eas.base.message.MsgPriority;
import com.kingdee.eas.base.message.MsgSourceStatus;
import com.kingdee.eas.base.message.MsgType;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.BizParentAndDelegateInfo;
import com.kingdee.eas.basedata.org.CompanyOrgUnitInfo;
import com.kingdee.eas.basedata.org.CostCenterOrgUnitFactory;
import com.kingdee.eas.basedata.org.CostCenterOrgUnitInfo;
import com.kingdee.eas.basedata.org.CostCenterType;
import com.kingdee.eas.basedata.org.CtrlUnitInfo;
import com.kingdee.eas.basedata.org.EconomicTypeEnum;
import com.kingdee.eas.basedata.org.FullOrgUnitCollection;
import com.kingdee.eas.basedata.org.FullOrgUnitFactory;
import com.kingdee.eas.basedata.org.FullOrgUnitInfo;
import com.kingdee.eas.basedata.org.IAdminOrgUnit;
import com.kingdee.eas.basedata.org.ICostCenterOrgUnit;
import com.kingdee.eas.basedata.org.IFullOrgUnit;
import com.kingdee.eas.basedata.org.INewOUInnerFacade;
import com.kingdee.eas.basedata.org.NewOUInnerFacadeFactory;
import com.kingdee.eas.basedata.org.OUPartAdminInfo;
import com.kingdee.eas.basedata.org.OUPartCostCenterInfo;
import com.kingdee.eas.basedata.org.OrgMoveParam;
import com.kingdee.eas.basedata.org.OrgType;
import com.kingdee.eas.basedata.org.OrgUnitInfo;
import com.kingdee.eas.basedata.org.OrgUnitLayerTypeInfo;
import com.kingdee.eas.basedata.org.OrgUnitRelationCollection;
import com.kingdee.eas.basedata.org.OrgUnitRelationInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.SysContextConstant;
import com.kingdee.eas.common.client.UIContext;
import com.kingdee.eas.custom.oraUnit.AjaxJson;
import com.kingdee.eas.custom.oraUnit.OrgInfo;
import com.kingdee.eas.fi.ar.app.webservice.util.BillProertiesConvertor;
import com.kingdee.eas.hr.plt.org.module.OrgListInfo;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.util.NumericExceptionSubItem;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

public class OrganizationFacadeControllerBean extends AbstractOrganizationFacadeControllerBean
{
  private static Logger logger = Logger.getLogger("com.kingdee.eas.custom.oraUnit.app.OrganizationFacadeControllerBean");

  private static String DEPT_TYPE = "00000000-0000-0000-0000-00000000000362824988";
  private static String COMPANY_TYPE = "00000000-0000-0000-0000-00000000000262824988";

  private OrgUnitLayerTypeInfo deptType = new OrgUnitLayerTypeInfo();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  private final String USER_AGENT = "Mozilla/5.0";

  private List<OrgInfo> transOrgList(JSONArray array)
  {
    List list = new ArrayList();
    if ((array != null) && (array.size() > 0)) {
      for (int i = 0; i < array.size(); i++) {
        JSONObject jo = array.getJSONObject(i);
        OrgInfo info = (OrgInfo)JSONObject.toJavaObject(jo, OrgInfo.class);
        list.add(info);
      }
      return list;
    }
    return null;
  }

  private List<OrgInfo> buildTree(List<OrgInfo> list) {
    if ((list == null) || (list.size() <= 0))
      return null;
    List nodeList = new ArrayList();
    for (OrgInfo node1 : list) {
      boolean mark = false;
      for (OrgInfo node2 : list) {
        if ((!StringUtils.isEmpty(node1.getParentDepartmentCode())) && 
          (node1.getParentDepartmentCode().equals(
          node2.getCode()))) {
          mark = true;
          if (node2.getChildren() == null)
            node2.setChildren(new ArrayList());
          node2.getChildren().add(node1);
          break;
        }
      }
      if (!mark) {
        nodeList.add(node1);
      }
    }
    return nodeList;
  }
  //一级组织部门
  private FullOrgUnitInfo getTopDept(Context ctx, FullOrgUnitInfo baseInfo)
    throws EASBizException, BOSException
  {
    SelectorItemCollection sic = new SelectorItemCollection();
    sic.add(new SelectorItemInfo("*"));
    sic.add(new SelectorItemInfo("unitLayerType.id"));
    sic.add(new SelectorItemInfo("parent.id"));
    sic.add(new SelectorItemInfo("parent.unitLayerType.id"));
    AdminOrgUnitInfo adminInfo = AdminOrgUnitFactory.getLocalInstance(ctx)
      .getAdminOrgUnitInfo(new ObjectUuidPK(baseInfo.getId()), sic);
    while ((adminInfo.getParent() != null) && (
      adminInfo.getParent().getUnitLayerType().getId().toString()
      .equals(DEPT_TYPE))) {
      adminInfo = AdminOrgUnitFactory.getLocalInstance(ctx)
        .getAdminOrgUnitInfo(
        new ObjectUuidPK(adminInfo.getParent().getId()), 
        sic);
    }
    if (adminInfo.getParent().getUnitLayerType().getId().toString().equals(
      COMPANY_TYPE))
      return FullOrgUnitFactory.getLocalInstance(ctx).getFullOrgUnitInfo(
        new ObjectUuidPK(adminInfo.getId()));
    return null;
  }

  private void setParnetCostCenter(Context ctx, FullOrgUnitInfo baseInfo, CostCenterOrgUnitInfo topCostCenterInfo, CompanyOrgUnitInfo companyInfo)
    throws EASBizException, BOSException
  {
    if (baseInfo.getParent() != null) {
      FullOrgUnitInfo parentInfo = FullOrgUnitFactory.getLocalInstance(
        ctx).getFullOrgUnitInfo(
        new ObjectUuidPK(baseInfo.getParent().getId()));
      if (!parentInfo.isIsCostOrgUnit())
        setParnetCostCenter(ctx, parentInfo, topCostCenterInfo, 
          companyInfo);
    }
    if (!baseInfo.isIsCostOrgUnit()) {
      costCenterOrgUnitVirtual(ctx, baseInfo, new ObjectUuidPK(
        baseInfo.getId()), baseInfo.getParent() == null ? null : 
        baseInfo.getParent().getId().toString(), topCostCenterInfo);
      FullOrgUnitInfo CostParentInfo2 = new FullOrgUnitInfo();
      CostParentInfo2.setNumber(baseInfo.getNumber() + "-B");
      CostParentInfo2.setName(baseInfo.getName() + "本部");
      CostParentInfo2.setParent(baseInfo);
      IObjectPK pk2 = FullOrgUnitFactory.getLocalInstance(ctx).submit(
        CostParentInfo2);
      CostParentInfo2 = FullOrgUnitFactory.getLocalInstance(ctx)
        .getFullOrgUnitInfo(pk2);

      AdminOrgUnit(ctx, CostParentInfo2, pk2, baseInfo.getId().toString());

      costCenterOrgUnitEntry(ctx, CostParentInfo2, pk2, 
        baseInfo.getParent().getId().toString(), topCostCenterInfo, 
        companyInfo);
    }
  }

  private void changeNumberAndName(Context ctx, FullOrgUnitInfo baseInfo, String number, String name) throws EASBizException, BOSException
  {
    IObjectPK pk = new ObjectUuidPK(baseInfo.getId());
    baseInfo.setNumber(number);
    baseInfo.setName(name);
    INewOUInnerFacade iNOUI = NewOUInnerFacadeFactory.getLocalInstance(ctx);
    BizParentAndDelegateInfo PDValueInfo = iNOUI
      .loadSuperiorAndDelegateUnits(pk);

    baseInfo.put("PDValueInfo", PDValueInfo);

    FullOrgUnitFactory.getLocalInstance(ctx).submit(baseInfo);
  }

  private void dealChildren(Context ctx, FullOrgUnitInfo baseInfo, int maxLevel, CostCenterOrgUnitInfo topCostCenter, FullOrgUnitInfo topDeptInfo, CompanyOrgUnitInfo companyInfo)
    throws BOSException, EASBizException
  {
    int topLevel = topDeptInfo.getLevel();
    FullOrgUnitCollection col = FullOrgUnitFactory.getLocalInstance(ctx)
      .getFullOrgUnitCollection(
      " where parent.id = '" + baseInfo.getId() + "'");
    if ((col != null) && (col.size() > 0))
      for (int i = 0; i < col.size(); i++) {
        FullOrgUnitInfo info = col.get(i);
        int thisLevel = info.getLevel();
        int level = thisLevel - topLevel + 1;
        if (level <= maxLevel)
        {
          CostCenterOrgUnitInfo costCenterInfo = null;
          if (info.isIsCostOrgUnit()) {
            costCenterInfo = CostCenterOrgUnitFactory.getLocalInstance(
              ctx).getCostCenterOrgUnitInfo(
              new ObjectUuidPK(info.getId()));
          }
          FullOrgUnitInfo parentInfo = info.getParent();
          if (level == maxLevel) {
            if (!info.isIsCostOrgUnit()) {
              if (info.isIsLeaf()) {
                costCenterOrgUnitEntry(ctx, info, new ObjectUuidPK(
                  info.getId()), 
                  parentInfo.getId().toString(), topCostCenter, companyInfo);
              } else {
                costCenterOrgUnitVirtual(ctx, info, 
                  new ObjectUuidPK(info.getId()), 
                  parentInfo == null ? null : 
                  parentInfo.getId().toString(), topCostCenter);
                FullOrgUnitInfo newBInfo = new FullOrgUnitInfo();
                newBInfo.setName(info.getName() + "本部");
                newBInfo.setNumber(info.getNumber() + "-B");
                newBInfo.setParent(info);
                IObjectPK pk = FullOrgUnitFactory.getLocalInstance(
                  ctx).submit(newBInfo);
                newBInfo = FullOrgUnitFactory.getLocalInstance(ctx)
                  .getFullOrgUnitInfo(pk);

                AdminOrgUnit(ctx, newBInfo, pk, 
                  info.getId().toString());
                costCenterOrgUnitEntry(ctx, newBInfo, pk, 
                  info.getId().toString(), topCostCenter, 
                  companyInfo);
              }

            }

          }
          else if (!info.isIsCostOrgUnit()) {
            if (info.isIsLeaf()) {
              costCenterOrgUnitEntry(ctx, info, new ObjectUuidPK(
                info.getId()), 
                parentInfo.getId().toString(), topCostCenter, companyInfo);
            } else {
              costCenterOrgUnitVirtual(ctx, info, 
                new ObjectUuidPK(info.getId()), 
                parentInfo == null ? null : 
                parentInfo.getId().toString(), topCostCenter);
              FullOrgUnitInfo newBInfo = new FullOrgUnitInfo();
              newBInfo.setName(info.getName() + "本部");
              newBInfo.setNumber(info.getNumber() + "-B");
              newBInfo.setParent(info);
              IObjectPK pk = FullOrgUnitFactory.getLocalInstance(
                ctx).submit(newBInfo);
              newBInfo = FullOrgUnitFactory.getLocalInstance(ctx)
                .getFullOrgUnitInfo(pk);

              AdminOrgUnit(ctx, newBInfo, pk, 
                info.getId().toString());

              costCenterOrgUnitEntry(ctx, newBInfo, pk, 
                info.getId().toString(), topCostCenter, 
                companyInfo);
            }
          }

          if (level <= maxLevel)
            dealChildren(ctx, info, maxLevel, topCostCenter, 
              topDeptInfo, companyInfo);
        }
      }
  }

  private void transTree2List(List<OrgInfo> treeList, List<OrgInfo> orgList)
  {
    for (OrgInfo info : treeList) {
      if ((!StringUtils.isEmpty(info.getCode())) && 
        (info.getCode().equals("100001"))) {
        info.setCode("A04");
      }
      if ((!StringUtils.isEmpty(info.getParentDepartmentCode())) && 
        (info.getParentDepartmentCode().equals("100001"))) {
        info.setParentDepartmentCode("A04");
      }
      orgList.add(info);
      if ((info.getChildren() != null) && (info.getChildren().size() > 0))
        transTree2List(info.getChildren(), orgList);
    }
  }
  //新增和修改组织
  protected String _orgSubmit(Context ctx)
    throws BOSException, EASBizException
  {
    AjaxJson j = new AjaxJson();
    try {
      StringBuffer sql = new StringBuffer();
      long updateTime = 0L;

      sql
        .append("/*dialect*/select updateTime from departmentList order by updateTime desc ");
      IRowSet rows = DbUtil.executeQuery(ctx, sql.toString());
      if ((rows != null) && (rows.size() > 0) && (rows.next())) {
        updateTime = rows.getLong("updateTime");
      }
      System.out.println("Testing 1 - Send Http GET request");
      String result = sendGet(updateTime);
      JSONObject jo = JSONObject.parseObject(result);
      if (jo == null) {
        throw new EASBizException(new NumericExceptionSubItem("100", 
          "json数据格式有误!"));
      }

      int resultStatus = jo.getIntValue("result");
      JSONArray array = null;
      if (resultStatus == 1) {
        array = jo.getJSONObject("data").getJSONArray("list");
      }

      CompanyOrgUnitInfo companyInfo = (CompanyOrgUnitInfo)
        BillProertiesConvertor.getValueObject("A04", CompanyOrgUnitInfo.class, null, ctx);

      List treeList = buildTree(transOrgList(array));

      if (treeList == null)
        return j.getJsonStr();
      long thisTime = 0L;

      List<OrgInfo> orgList = new ArrayList<OrgInfo>();
      transTree2List(treeList, orgList);

      for (OrgInfo orgInfo : orgList) {
        String parentNumber = orgInfo.getParentDepartmentCode();
        if (!StringUtils.isEmpty(parentNumber))
        {
          FullOrgUnitInfo parentBaseInfo = (FullOrgUnitInfo)
            BillProertiesConvertor.getValueObject(parentNumber, FullOrgUnitInfo.class, 
            null, ctx);
          String number = orgInfo.getCode();
          String name = orgInfo.getName();
          long upTime = orgInfo.getUpdateTime();
          if (upTime > thisTime) {
            thisTime = upTime;
          }
          if (parentBaseInfo == null)
            throw new EASBizException(new NumericExceptionSubItem(
              "100", "编码为[" + number + "]的上级组织单元[" + parentNumber + 
              "]不存在,请检查"));
          FullOrgUnitInfo baseInfo = (FullOrgUnitInfo)
            BillProertiesConvertor.getValueObject(number, FullOrgUnitInfo.class, null, 
            ctx);
          FullOrgUnitInfo topDeptInfo = getTopDept(ctx, parentBaseInfo);
          if (topDeptInfo == null)
            throw new EASBizException(new NumericExceptionSubItem(
              "100", "编码为[" + number + "]的归属的一级部门不存在,请检查"));
          if (!topDeptInfo.isIsCostOrgUnit())
            throw new EASBizException(new NumericExceptionSubItem(
              "100", "编码为[" + number + "]的归属的一级部门不是成本中心,请检查"));
          CostCenterOrgUnitInfo topCostCenter = 
            CostCenterOrgUnitFactory.getLocalInstance(ctx).getCostCenterOrgUnitInfo(
            new ObjectUuidPK(topDeptInfo.getId()));
          AdminOrgUnitInfo topAdminInfo = 
            AdminOrgUnitFactory.getLocalInstance(ctx).getAdminOrgUnitInfo(
            new ObjectUuidPK(topDeptInfo.getId()));
          if (topAdminInfo.getUnitLayerType().getId().toString().equals(
            COMPANY_TYPE)) {
            if (baseInfo == null) {
              FullOrgUnitInfo info = new FullOrgUnitInfo();
              info.setName(orgInfo.getName());
              info.setNumber(orgInfo.getCode());
              info.setParent(parentBaseInfo);
              IObjectPK pk = FullOrgUnitFactory.getLocalInstance(ctx)
                .submit(info);
              info = FullOrgUnitFactory.getLocalInstance(ctx)
                .getFullOrgUnitInfo(pk);

              AdminOrgUnit(ctx, info, pk, 
                parentBaseInfo.getId().toString());

              costCenterOrgUnitEntry(ctx, info, pk, 
                parentBaseInfo.getId().toString(), topCostCenter, companyInfo);
            } else {
              changeNumberAndName(ctx, baseInfo, number, name);
              OUUnSealUp(ctx, new ObjectUuidPK(baseInfo.getId()), 
                new ObjectUuidPK(parentBaseInfo.getId()));
            }
          }
          else
          {
            int maxLevel = topDeptInfo.getInt("maxLevel");
            if (maxLevel <= 0)
              throw new EASBizException(new NumericExceptionSubItem(
                "100", "编码为[" + number + "]的归属的一级部门成本中心层级未维护,请检查"));
            boolean isCostCenter = false;
            CostCenterOrgUnitInfo parentCostCenterInfo = null;
            if (parentBaseInfo.isIsCostOrgUnit()) {
              parentCostCenterInfo = 
                CostCenterOrgUnitFactory.getLocalInstance(ctx).getCostCenterOrgUnitInfo(
                new ObjectUuidPK(parentBaseInfo.getId()));
            }
            CostCenterOrgUnitInfo baseCostCenterInfo = null;
            if ((baseInfo != null) && (baseInfo.isIsCostOrgUnit())) {
              baseCostCenterInfo = 
                CostCenterOrgUnitFactory.getLocalInstance(ctx).getCostCenterOrgUnitInfo(
                new ObjectUuidPK(baseInfo.getId()));
            }
            int parentLevel = parentBaseInfo.getLevel();
            int topLevel = topDeptInfo.getLevel();
            int level = parentLevel - topLevel + 1;
            if (level < maxLevel)
              isCostCenter = true;
            if ((isCostCenter) && (topCostCenter == null)) {
              throw new EASBizException(new NumericExceptionSubItem(
                "100", "编码为[" + number + "]的归属的一级部门不是成本中心,请检查"));
            }

            if (baseInfo == null) {
              if (!isCostCenter) {
                FullOrgUnitInfo info = new FullOrgUnitInfo();
                info.setName(orgInfo.getName());
                info.setNumber(orgInfo.getCode());
                info.setParent(parentBaseInfo);
                IObjectPK pk = FullOrgUnitFactory.getLocalInstance(ctx)
                  .submit(info);
                info = FullOrgUnitFactory.getLocalInstance(ctx)
                  .getFullOrgUnitInfo(pk);

                AdminOrgUnit(ctx, info, pk, 
                  parentBaseInfo.getId().toString());
                if (info.getLevel() == 7) {//一级的层级等于7
                	DepartmentNotice(ctx, info);//新建一级部门通知消息
				}
              } else {
                if (!parentBaseInfo.isIsCostOrgUnit()) {
                  setParnetCostCenter(ctx, parentBaseInfo, 
                    topCostCenter, companyInfo);
                } else if (parentCostCenterInfo.isIsBizUnit()) {
                  String oldNumber = parentBaseInfo.getNumber();
                  String oldName = parentBaseInfo.getName();
                  ObjectUuidPK bPk = new ObjectUuidPK(
                    parentBaseInfo.getId());
                  changeNumberAndName(ctx, parentBaseInfo, oldNumber + 
                    "-B", oldName + "本部");
                  String parentId = parentBaseInfo.getId().toString();
                  FullOrgUnitInfo parentInfo = parentBaseInfo
                    .getParent();
                  parentBaseInfo = new FullOrgUnitInfo();
                  parentBaseInfo.setName(oldName);
                  parentBaseInfo.setNumber(oldNumber);
                  parentBaseInfo.setParent(parentInfo);
                  IObjectPK pk = FullOrgUnitFactory.getLocalInstance(
                    ctx).submit(parentBaseInfo);
                  parentBaseInfo = 
                    FullOrgUnitFactory.getLocalInstance(ctx).getFullOrgUnitInfo(pk);

                  AdminOrgUnit(ctx, parentBaseInfo, pk, 
                    parentInfo.getId().toString());

                  costCenterOrgUnitVirtual(ctx, parentBaseInfo, 
                    new ObjectUuidPK(parentBaseInfo.getId()), 
                    parentInfo.getParent() == null ? null : 
                    parentInfo.getId().toString(), 
                    topCostCenter);
                  OUUnSealUp(ctx, bPk, pk);
                  FullOrgUnitCollection col = 
                    FullOrgUnitFactory.getLocalInstance(ctx)
                    .getFullOrgUnitCollection(
                    " where parent.id = '" + parentId + "'");
                  if ((col != null) && (col.size() > 0)) {
                    for (int i = 0; i < col.size(); i++) {
                      FullOrgUnitInfo cInfo = col.get(i);
                      ObjectUuidPK cId = new ObjectUuidPK(
                        cInfo.getId());
                      OUUnSealUp(ctx, cId, pk);
                    }
                  }
                }
                FullOrgUnitInfo info = new FullOrgUnitInfo();
                info.setName(orgInfo.getName());
                info.setNumber(orgInfo.getCode());
                info.setParent(parentBaseInfo);
                IObjectPK pk = FullOrgUnitFactory.getLocalInstance(ctx)
                  .submit(info);
                info = FullOrgUnitFactory.getLocalInstance(ctx)
                  .getFullOrgUnitInfo(pk);

                AdminOrgUnit(ctx, info, pk, 
                  parentBaseInfo.getId().toString());

                costCenterOrgUnitEntry(ctx, info, pk, 
                  parentBaseInfo.getId().toString(), topCostCenter, companyInfo);
                if (info.getLevel() == 7) {//一级的层级等于7
                	DepartmentNotice(ctx, info);//新建一级部门通知消息
				}
              }
              
            }
            else {
              changeNumberAndName(ctx, baseInfo, number, name);
              ObjectUuidPK oldPk = new ObjectUuidPK(baseInfo.getId());
              if (!isCostCenter) {
                if ((parentCostCenterInfo != null) && (parentCostCenterInfo.isIsBizUnit())) {
                  String oldNumber = parentBaseInfo.getNumber();
                  String oldName = parentBaseInfo.getName();
                  ObjectUuidPK bPk = new ObjectUuidPK(
                    parentBaseInfo.getId());
                  changeNumberAndName(ctx, parentBaseInfo, oldNumber + 
                    "-B", oldName + "本部");
                  String parentId = parentBaseInfo.getId().toString();
                  FullOrgUnitInfo parentInfo = parentBaseInfo
                    .getParent();
                  parentBaseInfo = new FullOrgUnitInfo();
                  parentBaseInfo.setName(oldName);
                  parentBaseInfo.setNumber(oldNumber);
                  parentBaseInfo.setParent(parentInfo);
                  IObjectPK pk = FullOrgUnitFactory.getLocalInstance(
                    ctx).submit(parentBaseInfo);
                  parentBaseInfo = 
                    FullOrgUnitFactory.getLocalInstance(ctx).getFullOrgUnitInfo(
                    pk);

                  AdminOrgUnit(ctx, parentBaseInfo, pk, 
                    parentInfo.getId().toString());

                  costCenterOrgUnitVirtual(ctx, parentBaseInfo, 
                    new ObjectUuidPK(parentBaseInfo.getId()), 
                    parentInfo == null ? null : 
                    parentInfo.getId().toString(), topCostCenter);
                  OUUnSealUp(ctx, bPk, pk);
                  FullOrgUnitCollection col = 
                    FullOrgUnitFactory.getLocalInstance(ctx)
                    .getFullOrgUnitCollection(
                    " where parent.id = '" + parentId + 
                    "'");
                  if ((col != null) && (col.size() > 0)) {
                    for (int i = 0; i < col.size(); i++) {
                      FullOrgUnitInfo cInfo = col.get(i);
                      ObjectUuidPK cId = new ObjectUuidPK(
                        cInfo.getId());
                      OUUnSealUp(ctx, cId, pk);
                    }
                  }
                }
                ObjectUuidPK newParentPk = new ObjectUuidPK(
                  parentBaseInfo.getId());
                OUUnSealUp(ctx, oldPk, newParentPk);
              }
              else {
                if (!parentBaseInfo.isIsCostOrgUnit()) {
                  setParnetCostCenter(ctx, parentBaseInfo, 
                    topCostCenter, companyInfo);
                } else if (parentCostCenterInfo.isIsBizUnit()) {
                  String oldNumber = parentBaseInfo.getNumber();
                  String oldName = parentBaseInfo.getName();
                  ObjectUuidPK bPk = new ObjectUuidPK(
                    parentBaseInfo.getId());
                  changeNumberAndName(ctx, parentBaseInfo, oldNumber + 
                    "-B", oldName + "本部");
                  String parentId = parentBaseInfo.getId().toString();
                  FullOrgUnitInfo parentInfo = parentBaseInfo
                    .getParent();
                  parentBaseInfo = new FullOrgUnitInfo();
                  parentBaseInfo.setName(oldName);
                  parentBaseInfo.setNumber(oldNumber);
                  parentBaseInfo.setParent(parentInfo);
                  IObjectPK pk = FullOrgUnitFactory.getLocalInstance(
                    ctx).submit(parentBaseInfo);
                  parentBaseInfo = 
                    FullOrgUnitFactory.getLocalInstance(ctx).getFullOrgUnitInfo(
                    pk);

                  AdminOrgUnit(ctx, parentBaseInfo, pk, 
                    parentInfo.getId().toString());

                  costCenterOrgUnitVirtual(ctx, parentBaseInfo, 
                    new ObjectUuidPK(parentBaseInfo.getId()), 
                    parentInfo == null ? null : 
                    parentInfo.getId().toString(), topCostCenter);
                  OUUnSealUp(ctx, bPk, pk);
                  FullOrgUnitCollection col = 
                    FullOrgUnitFactory.getLocalInstance(ctx)
                    .getFullOrgUnitCollection(
                    " where parent.id = '" + parentId + 
                    "'");
                  if ((col != null) && (col.size() > 0)) {
                    for (int i = 0; i < col.size(); i++) {
                      FullOrgUnitInfo cInfo = col.get(i);
                      ObjectUuidPK cId = new ObjectUuidPK(
                        cInfo.getId());
                      OUUnSealUp(ctx, cId, pk);
                    }
                  }
                }

                ObjectUuidPK newParentPk = new ObjectUuidPK(
                  parentBaseInfo.getId());
                OUUnSealUp(ctx, oldPk, newParentPk);
                baseInfo = FullOrgUnitFactory.getLocalInstance(ctx)
                  .getFullOrgUnitInfo(oldPk);
                if (!baseInfo.isIsCostOrgUnit()) {
                  FullOrgUnitCollection col = 
                    FullOrgUnitFactory.getLocalInstance(ctx)
                    .getFullOrgUnitCollection(
                    " where parent.id = '" + 
                    baseInfo.getId() + "'");
                  if ((col != null) && (col.size() > 0)) {
                    costCenterOrgUnitVirtual(ctx, baseInfo, 
                      new ObjectUuidPK(baseInfo.getId()), 
                      baseInfo.getParent() == null ? null : 
                      baseInfo.getParent().getId()
                      .toString(), 
                      topCostCenter);
                    FullOrgUnitInfo newBInfo = new FullOrgUnitInfo();
                    newBInfo.setName(baseInfo.getName() + "本部");
                    newBInfo.setNumber(baseInfo.getNumber() + "-B");
                    newBInfo.setParent(baseInfo);
                    IObjectPK pk = 
                      FullOrgUnitFactory.getLocalInstance(ctx).submit(newBInfo);
                    newBInfo = FullOrgUnitFactory.getLocalInstance(
                      ctx).getFullOrgUnitInfo(pk);

                    AdminOrgUnit(ctx, newBInfo, pk, 
                      baseInfo.getId().toString());

                    costCenterOrgUnitEntry(ctx, newBInfo, pk, 
                      baseInfo.getId().toString(), 
                      topCostCenter, companyInfo);
                  }
                  else
                  {
                    costCenterOrgUnitEntry(ctx, baseInfo, 
                      new ObjectUuidPK(baseInfo.getId()), 
                      parentBaseInfo.getId().toString(), 
                      topCostCenter, companyInfo);
                  }
                } else {
                  baseCostCenterInfo.isIsBizUnit();
                }

                dealChildren(ctx, baseInfo, maxLevel, topCostCenter, 
                  topDeptInfo, companyInfo);
              }
              costCenterType(ctx, baseInfo.getNumber(), 
                Integer.valueOf(topCostCenter.getCostCenterType().getValue()));
            }
          }
        }
      }
      sql = new StringBuffer();
      sql.append("/*dialect*/insert into departmentList values(" + 
        thisTime + ") ");
      DbUtil.execute(ctx, sql.toString());
      return j.getJsonStr();
    } catch (Exception e) {
      j.setSuccess(false);
      j.setCode("-1");
      j.setMessage(e.getMessage());
    }
    return j.getJsonStr();
  }
    //修改成本中心类型
  private void costCenterType(Context ctx, String number, Integer costCenterType)
    throws BOSException
  {
    String sql = "update T_ORG_CostCenter set FCOSTCENTERTYPE = '" + 
      costCenterType + "' where FLONGNUMBER  LIKE '%" + number + 
      "%' ";
    DbUtil.execute(ctx, sql);

    String sql2 = "update T_ORG_OUPartCostCenter set  FCOSTCENTERTYPE  ='" + 
      costCenterType + 
      "' where  FUNITID  in (select fid from T_ORG_CostCenter where FLONGNUMBER LIKE '%" + 
      number + "%') ";
    DbUtil.execute(ctx, sql2);
  }

  private OrgMoveParam compositeParam(Context ctx, FullOrgUnitInfo oldParentId, IObjectPK newParentId)
    throws EASBizException, BOSException
  {
    OrgMoveParam orgMoveParam = new OrgMoveParam();
    orgMoveParam.setMovedOrgId(oldParentId.getId().toString());
    OrgUnitInfo newUnitParent = FullOrgUnitFactory.getLocalInstance(ctx)
      .getOrgUnitInfo(newParentId);
    if (newUnitParent != null) {
      orgMoveParam.setNewOUParentId(newUnitParent.getId().toString());
    }

    if (!oldParentId.getCU().getId().equals(newUnitParent.getCU().getId())) {
      orgMoveParam.setChangeCu(true);
    }

    orgMoveParam.setOldCuId(oldParentId.getCU().getId().toString());
    orgMoveParam.setNewCuId(newUnitParent.getCU().getId().toString());

    if (oldParentId.isIsAdminOrgUnit()) {
      orgMoveParam.addOldAndNewParent(
        Integer.valueOf(0), 
        oldParentId.getParent().getId().toString(), newParentId.toString());
    }
    if (oldParentId.isIsCostOrgUnit()) {
      orgMoveParam.addOldAndNewParent(
        Integer.valueOf(5), 
        oldParentId.getParent().getId().toString(), newParentId.toString());
    }
    return orgMoveParam;
  }
    //行政组织
  private void AdminOrgUnit(Context ctx, FullOrgUnitInfo info, IObjectPK pk, String parentPk)
    throws EASBizException, BOSException
  {
    String str = info.getOrgTypeStr() == null ? "" : info.getOrgTypeStr();
    if (str.indexOf(";Admin;") < 0) {
      str = str + ";Admin;";
      info.setOrgTypeStr(";" + str);
    }
    INewOUInnerFacade iNOUI = NewOUInnerFacadeFactory.getLocalInstance(ctx);
    BizParentAndDelegateInfo PDValueInfo = iNOUI
      .loadSuperiorAndDelegateUnits(pk);
    AdminOrgUnitInfo parentAdminInfo = 
      AdminOrgUnitFactory.getLocalInstance(ctx).getAdminOrgUnitInfo(
      new ObjectUuidPK(parentPk));
    PDValueInfo.setParentOrg(OrgType.Admin, parentAdminInfo);
    info.put("PDValueInfo", PDValueInfo);
    this.deptType.setId(
      BOSUuid.read("00000000-0000-0000-0000-00000000000362824988"));
    OUPartAdminInfo partInfo = new OUPartAdminInfo();
    partInfo.setUnit(info);
    partInfo.setUnitLayerType(this.deptType);
    partInfo.setIsVirtual(false);
    partInfo.setIndex(20);
    partInfo.setIsSealUp(false);
    partInfo.setVersionNumber("000");
    partInfo.setEconomicType(EconomicTypeEnum.ET0);
    partInfo.setIsChurchyard(true);
    partInfo.setIsJuridicalCompany(false);
    partInfo.setIsIndependence(false);
    info.setPartAdmin(partInfo);
    info.setIsAdminOrgUnit(true);
    FullOrgUnitFactory.getLocalInstance(ctx).submit(info);
  }
    //成本中心实体
  private void costCenterOrgUnitEntry(Context ctx, FullOrgUnitInfo info, IObjectPK pk, String parentPk, CostCenterOrgUnitInfo costInfo, CompanyOrgUnitInfo companyInfo)
    throws EASBizException, BOSException
  {
    info = FullOrgUnitFactory.getLocalInstance(ctx).getFullOrgUnitInfo(pk);
    String str = info.getOrgTypeStr() == null ? "" : info.getOrgTypeStr();
    if (str.indexOf(";CostCenter;") < 0) {
      str = str + ";CostCenter;";
      info.setOrgTypeStr(str);
    }
    INewOUInnerFacade iNOUI = NewOUInnerFacadeFactory.getLocalInstance(ctx);
    BizParentAndDelegateInfo PDValueInfo = iNOUI
      .loadSuperiorAndDelegateUnits(pk);
    CostCenterOrgUnitInfo parentCostCenterInfo = 
      CostCenterOrgUnitFactory.getLocalInstance(ctx).getCostCenterOrgUnitInfo(
      new ObjectUuidPK(parentPk));
    PDValueInfo.setParentOrg(OrgType.CostCenter, parentCostCenterInfo);
    info.put("PDValueInfo", PDValueInfo);
    OUPartCostCenterInfo costCenterInfo = new OUPartCostCenterInfo();
    costCenterInfo.setUnit(info);
    costCenterInfo.setIsBizUnit(true);
    costCenterInfo.setIsSealUp(false);
    costCenterInfo.setCostCenterType(costInfo.getCostCenterType());
    costCenterInfo.setVersionNumber("000");

    OrgUnitRelationCollection delegateCol = PDValueInfo
      .getDelegateCollection();
    Object value = null;
    value = companyInfo;
    FullOrgUnitInfo toUnitFI = ((OrgUnitInfo)value)
      .castToFullOrgUnitInfo();
    OrgUnitRelationInfo costDelegateFIInfo = new OrgUnitRelationInfo();
    costDelegateFIInfo.setFromUnit(info);
    costDelegateFIInfo.setToUnit(toUnitFI);
    costDelegateFIInfo.put("fromType", OrgType.CostCenter);
    costDelegateFIInfo.put("toType", OrgType.Company);
    delegateCol.add(costDelegateFIInfo);
    info.setPartCostCenter(costCenterInfo);
    info.setIsCostOrgUnit(true);
    FullOrgUnitFactory.getLocalInstance(ctx).submit(info);
  }
    //成本中心虚体
  private void costCenterOrgUnitVirtual(Context ctx, FullOrgUnitInfo info, IObjectPK pk, String parentPk, CostCenterOrgUnitInfo costInfo)
    throws EASBizException, BOSException
  {
    info = FullOrgUnitFactory.getLocalInstance(ctx).getFullOrgUnitInfo(pk);
    String str = info.getOrgTypeStr() == null ? "" : info.getOrgTypeStr();
    if (str.indexOf(";CostCenter;") < 0) {
      str = str + ";CostCenter;";
      info.setOrgTypeStr(str);
    }
    INewOUInnerFacade iNOUI = NewOUInnerFacadeFactory.getLocalInstance(ctx);
    BizParentAndDelegateInfo PDValueInfo = iNOUI
      .loadSuperiorAndDelegateUnits(pk);
    CostCenterOrgUnitInfo parentCostCenterInfo = 
      CostCenterOrgUnitFactory.getLocalInstance(ctx).getCostCenterOrgUnitInfo(
      new ObjectUuidPK(parentPk));
    if (parentCostCenterInfo != null) {
      PDValueInfo.setParentOrg(OrgType.CostCenter, parentCostCenterInfo);
      info.put("PDValueInfo", PDValueInfo);
      OUPartCostCenterInfo costCenterInfo = new OUPartCostCenterInfo();
      costCenterInfo.setUnit(info);
      costCenterInfo.setIsBizUnit(false);
      costCenterInfo.setIsSealUp(false);
      costCenterInfo.setCostCenterType(costInfo.getCostCenterType());
      costCenterInfo.setVersionNumber("000");
      info.setPartCostCenter(costCenterInfo);
      info.setIsCostOrgUnit(true);
      FullOrgUnitFactory.getLocalInstance(ctx).submit(info);
    }
  }
    //移动组织的方法
  private void OUUnSealUp(Context ctx, IObjectPK oldPk, IObjectPK newPk)
    throws EASBizException, BOSException
  {
    IObjectPK selectedOrgPk = oldPk;
    SelectorItemCollection orgSelectors = new SelectorItemCollection();
    orgSelectors.add(new SelectorItemInfo("*"));
    orgSelectors.add(new SelectorItemInfo("parent.*"));
    FullOrgUnitInfo movedUnitInfo = 
      FullOrgUnitFactory.getLocalInstance(ctx).getFullOrgUnitInfo(selectedOrgPk, 
      orgSelectors);
    if (movedUnitInfo.isIsCU()) {
      throw new EASBizException(new NumericExceptionSubItem("100", 
        "移动的组织是管理单元,不能进行迁移"));
    }

    IObjectPK newParentPK = newPk;
    if (newParentPK != null) {
      FullOrgUnitInfo parentUnit = FullOrgUnitFactory.getLocalInstance(
        ctx).getFullOrgUnitInfo(newPk);
      movedUnitInfo.setParent(parentUnit);
    }
    INewOUInnerFacade iNOUI = NewOUInnerFacadeFactory.getLocalInstance(ctx);
    iNOUI.ouShift(new ObjectUuidPK(movedUnitInfo.getId()), newPk);
    UIContext uiContext = new UIContext();
    uiContext.put("originOrg", movedUnitInfo);
    uiContext.put("Owner", this);
    OrgMoveParam orgMoveParam = compositeParam(ctx, movedUnitInfo, newPk);
    iNOUI = NewOUInnerFacadeFactory.getLocalInstance(ctx);
    iNOUI.ouShift(orgMoveParam);
  }
    //get的接口方法
  private String sendGet(long updateTime)
    throws Exception
  {
    String url = "http://mdm.ymmoa.com/mdm-api/mdm/hr/departmentList?updateTime=" + 
      updateTime;
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);

    request.addHeader("User-Agent", "Mozilla/5.0");
    HttpResponse response = client.execute(request);
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code :" + 
      response.getStatusLine().getStatusCode());
    BufferedReader rd = new BufferedReader(new InputStreamReader(
      response.getEntity().getContent(), "UTF-8"));
    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
      result.append(line);
    }

    return result.toString();
  }
  //消息通知方法
  public static void sendWorkFlowMessage(Context ctx,String Receiver,String Body,String title,String sender) throws EASBizException, BOSException{
  	BMCMessageInfo messageInfo = new BMCMessageInfo();
  	
  	messageInfo.setTitle(title);//表题
  	messageInfo.setBody(Body);//内容
  	messageInfo.setReceiver(Receiver);// TODO设置接收者,后面那参数是用户ID,多个ID可用分号";"分割
  	messageInfo.setType(MsgType.ONLINE); // 即时消息

  	messageInfo.setBizType(MsgBizType.ONLINE); // 消息类型 - 即时消息

  	messageInfo.setSender(sender);	//发送人

  	messageInfo.setPriority(MsgPriority.HIGH);// 消息级别

  	messageInfo.setSendTime(new Timestamp(new Date().getTime()));//接收时间

  	messageInfo.setOrgType(OrgType.NONE);//设置组织类型

  	messageInfo.setOrgID("11111111-1111-1111-1111-111111111111CCE7AED4");//设置管理单元

  	BMCMessageFactory.getLocalInstance(ctx).save(messageInfo);//发送消息
	  
  }
  
  //一级部门新增成功调用的通知消息方法
  public void DepartmentNotice(Context ctx,FullOrgUnitInfo info){
	  String  title = "";
	  String body = "";
	  boolean flag = info.getNumber() == null ? false : true;
	  if (flag) {
		  title = "一级部门通知消息";
		  body = title+",编码为:"+info.getNumber()+",名称为:"+info.getName();
	  }
	  Set<String> set = new HashSet<String>();
	  UserInfo currentUser = ContextUtil.getCurrentUserInfo(ctx);
	  String sender = currentUser.getName();
	  String receiver = "";  
	  try {
		  StringBuffer sql = new StringBuffer();
		  sql.append("/*dialect*/select tu.FID fd from  T_PM_User tu ");
		  sql.append("left join T_BD_Person tp ");
		  sql.append("on tp.FID = tu.FPERSONID ");
		  sql.append("where tp.CFMessage = '1' ");
		  IRowSet rows = DbUtil.executeQuery(ctx, sql.toString());
		  if (rows != null && rows.size() > 0) {
			while (rows.next()) {
				receiver = rows.getString("fd");
				set.add(receiver);
			}
		}
		  if (!("".equals(title) && "".equals(body))&& !((title == null )&&(body == null))) {
			//调用发送消息方法
			  if (set.size() > 0) {
				Iterator<String> it2 = set.iterator();
				while (it2.hasNext()) {
					 sendWorkFlowMessage(ctx,it2.next(),body,title,sender);
				}
			}
			 
		  }			
		
	} catch (Exception e) {
		e.printStackTrace();
	}
  }
 
}
发布了78 篇原创文章 · 获赞 32 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/a754315344/article/details/102784845
EAS