/**
  * 激活流程节点
  *
  * @param processName
  * @param taskUserLoginName
  * @param incidentno
  * @param steplabel
  * @param summary
  * @param memo
  * @param map
  * @return
  */
 private boolean launchProcessStep(
     String processName,
     String taskUserLoginName,
     int incidentno,
     String steplabel,
     String summary,
     String memo,
     Map<String, Object> map) {
   log.debug("激活流程开始");
   boolean flag =
       PWSUtil.completeStepTest(
           processName, taskUserLoginName, incidentno, steplabel, summary, memo, map);
   log.debug("激活流程结束,flag=" + flag);
   return flag;
 }
/**
 * @author XFZ
 * @version 1.0 2012-8-22
 */
@Service("todoManager")
public class TodoManager {
  private static TodoInstance[] services = {new DeptContactTodoInstance()};

  static Logger log = SimpleLogger.getLogger(TodoManager.class);

  private static DbUtil dbUtil;
  private static PlatformTransactionManager transactionManager;

  public static void operate() {
    for (int i = 0; i < services.length; i++) {
      try {
        final TodoInstance instance = services[i];

        TransactionTemplate tt = new TransactionTemplate(transactionManager);
        tt.execute(
            new TransactionCallback() {
              public Object doInTransaction(TransactionStatus status) {
                try {
                  JdbcTemplate jt = dbUtil.getJdbcTemplate();
                  instance.action(jt);
                } catch (Exception e) {
                  e.printStackTrace();
                  status.setRollbackOnly();
                }
                return null;
              }
            });
      } catch (Exception e) {
        log.error("service error:" + e.getMessage());
      }
    }
  }

  public static DbUtil getDbUtil() {
    return dbUtil;
  }

  @Autowired(required = false)
  public void setDbUtil(@Qualifier("dbUtil") DbUtil dbUtil) {
    TodoManager.dbUtil = dbUtil;
  }

  public static PlatformTransactionManager getTransactionManager() {
    return transactionManager;
  }

  @Resource(name = "dsTransactionManager")
  public void setTransactionManager(PlatformTransactionManager transactionManager) {
    TodoManager.transactionManager = transactionManager;
  }
}
  /**
   * Begin节点
   *
   * @param mainVo
   * @param userInfo
   * @param params_dc
   * @throws Exception
   */
  public int flowBegin() {
    String taskUserLoginName = LoginUtil.getUserLoginName(userInfo);

    Map<String, Object> map = new HashMap<String, Object>();

    String sign_leader =
        StringUtil.getNotNullValueString(
            params.getParamObject(DeptContactConstants.PARAMS_KEY_SIGN_LEADER));

    String ip = IpUtil.getIpByStartString(DeptContactConstants.IP_PREFIX);
    String port =
        StringUtil.getNotNullValueString(params.getParamObject(DeptContactConstants.IP_PORT));
    String contextPath =
        StringUtil.getNotNullValueString(
            params.getParamObject(DeptContactConstants.IP_CONTEXT_PATH));

    map.put(DeptContactFlowConstants.SERVERIP, "http://" + ip + ":" + port + "/" + contextPath);

    map.put(DeptContactFlowConstants.USER_BEGIN, taskUserLoginName);
    map.put(DeptContactFlowConstants.USER_APPLY, taskUserLoginName);
    map.put(DeptContactFlowConstants.USER_SIGN, sign_leader);

    map.put(DeptContactFlowConstants.DEPTID, userInfo.getDeptId());
    map.put(DeptContactFlowConstants.TARGET, DeptContactFlowConstants.STEP_SIGN);
    map.put("短信内容", mainBo.getTheme());

    // log.debug("发起流程开始");
    map.put(DeptContactFlowConstants.LINK_PROCESS_KEY, params.treeBo.getId());

    int incidentNo =
        this.launchProcess(mainBo.getProcessname(), taskUserLoginName, mainBo.getTheme(), map);

    log.debug("发起流程结束,incidentNo=" + incidentNo);

    return incidentNo;
  }
 /**
  * 发起新流程
  *
  * @param processName
  * @param taskUserLoginName
  * @param summary
  * @param map
  * @return
  */
 private int launchProcess(
     String processName, String taskUserLoginName, String summary, Map<String, Object> map) {
   log.debug("发起流程开始");
   int incidentNo = PWSUtil.launchIncident(processName, taskUserLoginName, summary, map);
   return incidentNo;
 }
  @Override
  public void flowStepAdd(DeptContactParamVo params) {
    // log.debug(resultInfo.checkFlag+" "+resultInfo.checkOnly);
    if (resultInfo.getOperateFlag()) {

      String taskUserLoginName = LoginUtil.getUserLoginName(userInfo);
      String processName = DeptContactConstants.PROCESSNAME;

      /** 创建mainBo */
      mainBo = new TDeptContactMain();
      try {
        BeanUtils.copyProperties(mainBo, mainVo);
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }

      mainBo.setCreateDeptid(userInfo.getDeptId());
      mainBo.setCreateDeptname(userInfo.getDeptName());

      mainBo.setInitiator(userInfo.getLoginName());
      mainBo.setInitiatorName(userInfo.getUserName());

      mainBo.setStartTime(time);

      mainBo.setOperateDate(time);
      mainBo.setOperateUser(taskUserLoginName);
      mainBo.setOperateName(userInfo.getUserName());

      mainBo.setProcessname(processName);

      /** 添加关联信息 */
      TDeptContactTree treeBo = DeptContactUtil.generateTreeBo();
      params.treeBo = treeBo;

      treeBo.setCname(processName);
      treeBo.setType(DeptContactConstants.STATUS_MAIN);

      commonService.save(treeBo);

      /** 发起流程 */
      int incidentNo = flowBegin();

      if (incidentNo > 0) {

        String incidentNoStr = String.valueOf(incidentNo);
        mainBo.setIncidentno(incidentNoStr);
        commonService.save(mainBo);

        String id = StringUtil.getNotNullValueString(mainBo.getId());
        if (id.length() == 0) {
          log.warn(DeptContactMessage.FAIL_TO_SAVE_DATA);
          resultInfo.addErrors(DeptContactMessage.FAIL_TO_SAVE_DATA);
          throw new RuntimeException(DeptContactMessage.FAIL_TO_SAVE_DATA.textCn);
        }

        DeptContactUtil.copyOperateInfo(treeBo, mainBo);
        /** 更新关联信息 */
        treeBo.setCId(id);
        treeBo.setCincident(incidentNoStr);
        commonService.update(treeBo);

        try {

          params.addProcessParam("cname", mainBo.getProcessname());
          params.addProcessParam("cincident", mainBo.getIncidentno());

          params.mainBo = mainBo;
          deptContactCommonService.saveReferences(params);

          deptContactCommonService.saveApprovedInfo(params);

        } catch (Exception e) {
          e.printStackTrace();
        }

        /** 跳转URL */
        resultInfo.url =
            TextUtil.generateDeptContactUrl(
                mainBo.getProcessname(), incidentNoStr, mainBo.getProcessname(), incidentNoStr, "");
      } else {
        log.warn(DeptContactMessage.FAIL_TO_LAUNCH_PROCESS);
        resultInfo.addErrors(DeptContactMessage.FAIL_TO_LAUNCH_PROCESS);
        throw new RuntimeException(DeptContactMessage.FAIL_TO_LAUNCH_PROCESS.textCn);
      }
    }
  }