protected MocamResult endSuccessProcess(LocalTransaction localTransaction) {
    CardInfo card = cardInfoManager.getByCardNo(localTransaction.getCardNo());
    ApplicationVersion applicationVersion =
        applicationVersionManager.getByAidAndVersionNo(
            localTransaction.getAid(), localTransaction.getAppVersion());

    if (null != applicationVersion
        && (null == applicationVersion.getApplication().getNeedSubscribe()
            || !applicationVersion
                .getApplication()
                .getNeedSubscribe())) { // 如果应用不需要单独订购/退订,则删除使用即为退订,添加退订记录
      subscribeHistoryManager.unsubscribeApplication(card, applicationVersion);
    }

    // 删除终端上客户端记录
    List<CardClient> cardClients =
        cardClientManager.getByCardAndApplication(card, applicationVersion.getApplication());
    if (null != cardClients) {
      for (CardClient cardClient : cardClients) {
        cardClientManager.remove(cardClient);
      }
    }

    return super.endSuccessProcess(localTransaction);
  }
  protected String calcLoadPercent(LocalTransaction localTransaction) {
    ApplicationVersion applicationVersion =
        applicationVersionManager.getAidAndVersionNo(
            localTransaction.getAid(), localTransaction.getAppVersion());
    int totalCount = applicationVersion.getApplicationLoadFiles().size();
    int currentIndex = localTransaction.getCurrentLoadFileIndex();

    Integer percent = 5 + (int) (((new Double(currentIndex - 1) / totalCount) * 0.85) * 100);

    return percent.toString();
  }
  @SuppressWarnings({"rawtypes", "deprecation"})
  @Override
  public int getUndownloadUserAmountByApplicationVersionWithCardSecurityDomain(
      ApplicationVersion applicationVersion) {
    long appVerId = applicationVersion.getId();
    long appId = applicationVersion.getApplication().getId();
    String sql = " SELECT a.card_id ";
    sql += " FROM card_security_domain a";
    sql += " WHERE NOT EXISTS";
    sql += " (SELECT 1";
    sql += " FROM card_application b";
    sql += " WHERE a.card_id              = b.card_info_id";
    sql += " AND b.application_version_id = " + appVerId;
    sql +=
        " AND b.status                IN ("
            + CardApplication.STATUS_AVAILABLE
            + ","
            + CardApplication.STATUS_LOCKED
            + ","
            + CardApplication.STATUS_PERSONALIZED
            + ","
            + CardApplication.STATUS_AVAILABLE
            + ")";
    sql += " )";
    sql += " AND EXISTS";
    sql += " (SELECT 1 FROM application c WHERE a.security_id = c.sd_id AND c.id = " + appId;
    sql += " )";
    Session session = this.getSession();
    List list = session.createSQLQuery(sql).addScalar("card_id", Hibernate.LONG).list();
    if (list.isEmpty()) return 0;
    List<Long> cardIdList = new ArrayList<Long>();
    for (int index = 0; index < list.size(); index++) {
      Long cardId = (Long) list.get(index);
      String check =
          "SELECT COUNT(1) amount FROM card_application a WHERE EXISTS (SELECT 1 FROM application_version b WHERE a.application_version_id = b.id AND b.application_id = "
              + appId
              + " ) AND a.card_info_id = "
              + cardId
              + " AND a.application_version_id != "
              + appVerId
              + " AND a.status IN ("
              + CardApplication.STATUS_LOCKED
              + ","
              + CardApplication.STATUS_AVAILABLE
              + ","
              + CardApplication.STATUS_PERSONALIZED
              + ")";
      List l = session.createSQLQuery(check).addScalar("amount", Hibernate.LONG).list();
      long size = (Long) l.get(0);
      if (size > 0) continue;
      cardIdList.add(cardId);
    }

    if (cardIdList.isEmpty()) return 0;
    String cardIds = "";
    for (Long id : cardIdList) {
      cardIds += "," + id;
    }
    cardIds = cardIds.replaceFirst(",", "");

    String query = " SELECT COUNT(1) amount ";
    query += " FROM ";
    query += " (SELECT a.card_id, ";
    query += " a.free_non_volatile_space ram, ";
    query += " a.free_volatile_space rom ";
    query += " FROM card_security_domain a ";
    query += " WHERE a.card_id IN (" + cardIds + ") ";
    query += " AND EXISTS ";
    query +=
        " (SELECT 1 FROM application c WHERE a.security_id = c.sd_id AND c.id = " + appId + ") ";
    query += " ) a, ";
    query += " application_version b ";
    query += " WHERE b.id = " + appVerId;
    query += " AND a.ram >= b.non_volatile_space ";
    query += " AND a.rom >= b.volatile_space ";

    this.logger.debug("\n\n" + query + "\n\n");
    return getUndownloadUserAmountByApplicationVersion(query);
  }
 private List<Map<String, Object>> desiredOperationResult(
     List<DesiredOperation> requistionList, String executionStatus) {
   List<Map<String, Object>> mappedApplications =
       new ArrayList<Map<String, Object>>(requistionList.size());
   List<PropertyFilter> propertyFilters = new ArrayList<PropertyFilter>();
   List<Application> apps = new ArrayList<Application>();
   List<SecurityDomain> sds = new ArrayList<SecurityDomain>();
   Task task = null;
   for (DesiredOperation desiredOperation : requistionList) {
     if (desiredOperation.getPreProcess().intValue() == DesiredOperation.PREPROCESS_TURE
         && desiredOperation.getIsExcuted().intValue() == DesiredOperation.NOT_EXCUTED) {
       continue;
     }
     if (desiredOperation.getTaskId() != null) {
       task = taskManager.load(desiredOperation.getTaskId());
     } else {
       task = new Task();
     }
     CustomerCardInfo customerCardInfo = null;
     if (null != desiredOperation.getCustomerCardId()
         && 0 != desiredOperation.getCustomerCardId()) {
       customerCardInfo = customerCardInfoManager.load(desiredOperation.getCustomerCardId());
     }
     propertyFilters.clear();
     apps.clear();
     sds.clear();
     Map<String, Object> mappedDesiredOperation = desiredOperation.toMap(null, null);
     mappedDesiredOperation.put("cardNo", "");
     Long cciId;
     if (null != (cciId = desiredOperation.getCustomerCardId())) {
       CustomerCardInfo cci = customerCardInfoManager.load(cciId);
       mappedDesiredOperation.put("cardNo", cci.getCard().getCardNo());
     }
     mappedDesiredOperation.put("idStr", String.valueOf(desiredOperation.getId()));
     mappedDesiredOperation.put(
         "beginTime",
         CalendarUtils.parsefomatCalendar(task.getBeginTime(), CalendarUtils.LONG_FORMAT_LINE));
     if (null != customerCardInfo) {
       mappedDesiredOperation.put("cciName", customerCardInfo.getName());
     } else {
       mappedDesiredOperation.put("cciName", "");
     }
     mappedDesiredOperation.put(
         "endTime",
         CalendarUtils.parsefomatCalendar(task.getEndTime(), CalendarUtils.LONG_FORMAT_LINE));
     String result = desiredOperation.getResult();
     if (StringUtils.isBlank(result)) {
       LocalTransaction localTransaction =
           localTransactionManager.getBySessionId(desiredOperation.getSessionId());
       if (localTransaction != null) {
         mappedDesiredOperation.put("failMessage", localTransaction.getFailMessage());
       }
     } else {
       mappedDesiredOperation.put("failMessage", result);
     }
     // if (executionStatus != null && Integer.parseInt(executionStatus)
     // == DesiredOperation.NOT_FINISH_EXCUTED){
     // List<LocalTransaction> localTransactions =
     // task.getLocalTransactions();
     // for (LocalTransaction lt : localTransactions){
     // if(!StringUtils.isEmpty(lt.getFailMessage())){
     // mappedDesiredOperation.put("failMessage", lt.getFailMessage());
     // break;
     // }
     // }
     // }
     propertyFilters.add(
         new PropertyFilter("aid", MatchType.EQ, PropertyType.S, desiredOperation.getAid()));
     if (desiredOperation.getProcedureName().equals(Operation.CREATE_SD.toString())
         || desiredOperation.getProcedureName().equals(Operation.DELETE_SD.toString())
         || desiredOperation.getProcedureName().equals(Operation.LOCK_SD.toString())
         || desiredOperation.getProcedureName().equals(Operation.UNLOCK_SD.toString())
         || desiredOperation.getProcedureName().equals(Operation.SYNC_CARD_SD.toString())
         || desiredOperation.getProcedureName().equals(Operation.UPDATE_KEY.toString())) {
       sds = securityDomainManager.find(propertyFilters);
       if (sds != null && sds.size() != 0) {
         mappedDesiredOperation.put("appName", sds.get(0).getSdName());
         mappedDesiredOperation.put("hasIcon", "sd");
       }
     } else if (desiredOperation.getProcedureName().equals("LOCK_CARD")) {
       CustomerCardInfo cci = customerCardInfoManager.load(desiredOperation.getCustomerCardId());
       mappedDesiredOperation.put("appName", "");
       if (null != cci) {
         mappedDesiredOperation.put("cciIconId", cci.getMobileType().getId());
         mappedDesiredOperation.put("hasIcon", "LOCK_CARD");
       }
     } else if (desiredOperation.getProcedureName().equals("UNLOCK_CARD")) {
       CustomerCardInfo cci = customerCardInfoManager.load(desiredOperation.getCustomerCardId());
       mappedDesiredOperation.put("appName", "");
       if (null != cci) {
         mappedDesiredOperation.put("cciIconId", cci.getMobileType().getId());
         mappedDesiredOperation.put("hasIcon", "UNLOCK_CARD");
       }
     } else {
       apps = applicationManager.find(propertyFilters);
       if (apps != null && apps.size() != 0) {
         mappedDesiredOperation.put("appName", apps.get(0).getName());
         mappedDesiredOperation.put("application_id", apps.get(0).getId());
         mappedDesiredOperation.put("hasIcon", apps.get(0).getPcIcon() != null);
         boolean hasClient = false;
         List<ApplicationVersion> versions = apps.get(0).getVersions();
         for (ApplicationVersion av : versions) {
           if (av.getClients().size() != 0) {
             hasClient = true;
             break;
           }
         }
         mappedDesiredOperation.put("hasClient", hasClient);
       }
     }
     mappedApplications.add(mappedDesiredOperation);
     final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     if (Integer.valueOf(executionStatus) != 0) {
       Collections.sort(
           mappedApplications,
           new Comparator<Map<String, Object>>() {
             @Override
             public int compare(Map<String, Object> m1, Map<String, Object> m2) {
               if (StringUtils.isNotBlank((String) m1.get("beginTime"))
                   && StringUtils.isNotBlank((String) m2.get("beginTime"))) {
                 try {
                   Date date1 = sdf.parse((String) m1.get("beginTime"));
                   Date date2 = sdf.parse((String) m2.get("beginTime"));
                   return 0 - date1.compareTo(date2);
                 } catch (ParseException e) {
                   e.printStackTrace();
                 }
                 return 0;
               } else {
                 return 0;
               }
             }
           });
     }
   }
   return mappedApplications;
 }