Search in sources :

Example 1 with ActivityMsgTempExample

use of com.itrus.portal.db.ActivityMsgTempExample in project portal by ixinportal.

the class ActMsgCollectService method setAmTimes.

private void setAmTimes(ActivityMsgTemp activityMsg, String runStatus, Long lifeTime) throws ServiceNullException {
    // 若不为插入状态且持续时间为null或小于0或大于最大时间,则直接抛出异常,放弃处理
    if (!"0".equals(runStatus) && (lifeTime == null || lifeTime < 0 || lifeTime > MAX_LIFE_TIME)) {
        // logger.error("ServiceNullException: keySn= "+activityMsg.getKeySn()+", runStatus = " + runStatus + ", lifeTime = " + lifeTime);
        throw new ServiceNullException();
    }
    // 若为插入状态,但持续时间异常,则设置持续时间为0.增加终端统计的准确性
    if ("0".equals(runStatus) && (lifeTime == null || lifeTime < 0 || lifeTime > MAX_LIFE_TIME))
        lifeTime = 0l;
    // 客户端已持续时间
    long dt = lifeTime;
    ActivityMsgTempExample acte = new ActivityMsgTempExample();
    ActivityMsgTempExample.Criteria actec = acte.createCriteria();
    actec.andKeySnEqualTo(activityMsg.getKeySn());
    actec.andCertCnEqualTo(activityMsg.getCertCn());
    actec.andThreadIdEqualTo(activityMsg.getThreadId());
    ActivityMsgTemp am = sqlSession.selectOne("com.itrus.portal.db.ActivityMsgTempMapper.selectByExample", acte);
    Calendar calendar = Calendar.getInstance();
    // 数据库没有记录数据
    if (null == am) {
        long nowLong = calendar.getTimeInMillis();
        // 没有插入的数据,记录插入的数据
        if ("1".equals(runStatus)) {
            // 获取当前时间毫秒数
            calendar.setTimeInMillis(nowLong - dt);
            // 设置当前时间
            activityMsg.setCreateTime(calendar.getTime());
            // 先获取开始时间
            activityMsg.setOnLineTime(calendar.getTime());
            dt += ComNames.DELAY_TIME;
            calendar.setTimeInMillis(nowLong + dt);
            activityMsg.setOffLineTime(calendar.getTime());
            activityMsg.setLifeTime(activityMsg.getOffLineTime().getTime() - activityMsg.getOnLineTime().getTime());
        } else if ("2".equals(runStatus)) {
            // 没有插入的数据,只有拔出的数据
            // 设置当前时间为结束时间
            activityMsg.setOffLineTime(calendar.getTime());
            calendar.setTimeInMillis(nowLong - dt);
            // 设置当前时间
            activityMsg.setCreateTime(calendar.getTime());
            // 先获取开始时间
            activityMsg.setOnLineTime(calendar.getTime());
            activityMsg.setLifeTime(activityMsg.getOffLineTime().getTime() - activityMsg.getOnLineTime().getTime());
        }
    } else {
        // 客户端已持续时间 0:key插入状态 1:key持续插入状态 2:key拔出状态
        activityMsg.setCreateTime(null == am.getCreateTime() ? calendar.getTime() : am.getCreateTime());
        activityMsg.setOnLineTime(null == am.getOnLineTime() ? calendar.getTime() : am.getOnLineTime());
        if ("2".equals(runStatus)) {
            activityMsg.setCertCn("".equals(activityMsg.getCertCn()) ? am.getCertCn() : activityMsg.getCertCn());
            calendar.setTimeInMillis(am.getOnLineTime().getTime() + dt);
            activityMsg.setOffLineTime(calendar.getTime());
            activityMsg.setLifeTime(activityMsg.getOffLineTime().getTime() - activityMsg.getOnLineTime().getTime());
        } else if ("1".equals(runStatus)) {
            calendar.setTimeInMillis(calendar.getTimeInMillis() + dt);
            activityMsg.setOffLineTime(calendar.getTime());
            activityMsg.setLifeTime(activityMsg.getOffLineTime().getTime() - activityMsg.getOnLineTime().getTime());
        }
    }
}
Also used : ActivityMsgTemp(com.itrus.portal.db.ActivityMsgTemp) Calendar(java.util.Calendar) ActivityMsgTempExample(com.itrus.portal.db.ActivityMsgTempExample) ServiceNullException(com.itrus.portal.exception.ServiceNullException)

Aggregations

ActivityMsgTemp (com.itrus.portal.db.ActivityMsgTemp)1 ActivityMsgTempExample (com.itrus.portal.db.ActivityMsgTempExample)1 ServiceNullException (com.itrus.portal.exception.ServiceNullException)1 Calendar (java.util.Calendar)1