Search in sources :

Example 6 with ChargingFlow

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

the class TakeChargingServiceImpl method takeCharing.

/**
 * 根据输入的appid和appserviceId,查找对应的计费规则,并进行一次计费,请注意,要保证输入的参数都不能为null,且应用和服务存在
 * @param transinfoName,关联服务配置表名称
 * @param transinfoId,关联服务配置id
 * @param applicationInfo,应用appid对应的applicationInfo对象
 * @param appService,服务编码对应的appService对象
 * @param transinfoPrimaryId,服务调用记录生成的主键id
 * @param transinfoTableName,服务调用记录的表名称
 * @param number,证据份数
 * @param serviceRequestPlatform 服务请求平台
 * @param hisApplicant 出证申请方
 * @return
 */
public Map<String, Object> takeCharing(String transinfoName, Long transinfoId, ApplicationInfo applicationInfo, AppService appService, Long transinfoPrimaryId, String transinfoTableName, Integer number, String serviceRequestPlatform, String hisApplicant) {
    Map<String, Object> retMap = new HashMap<String, Object>();
    retMap.put("retCode", 0);
    try {
        ChargingFlow chargingFlow = chargingFlowService.insertOneChargingFlowByTake(transinfoName, transinfoId, applicationInfo.getId(), appService.getId(), transinfoPrimaryId, transinfoTableName, number, serviceRequestPlatform, hisApplicant);
        retMap.put("retCode", 1);
        retMap.put("chargingFlow", chargingFlow);
        return retMap;
    } catch (Exception e) {
        // 如果上一次记录流水失败,则再次记录
        retMap.put("retMsg", e.getMessage());
        ChargingFlow chargingFlow = chargingFlowService.insertOneChargingFlowByTake(transinfoName, transinfoId, applicationInfo.getId(), appService.getId(), transinfoPrimaryId, transinfoTableName, number, serviceRequestPlatform, hisApplicant);
        LogUtil.syslog(sqlSession, "账单流水出现异常", "出现异常后再次记账的单号:" + chargingFlow.getId() + ",异常信息" + e.getMessage());
        retMap.put("retCode", 1);
        retMap.put("chargingFlow", chargingFlow);
    }
    return retMap;
}
Also used : ChargingFlow(com.itrus.portal.db.ChargingFlow) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 7 with ChargingFlow

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

the class RecordQueueThread method run.

@Override
public void run() {
    boolean isException = false;
    while (true) {
        log.debug("execute run,the queue size is [" + OBJECT_QUEUE.size() + "]");
        if (OBJECT_QUEUE.isEmpty()) {
            try {
                // 若队列中没有信息则等待3秒
                sleep(3 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            continue;
        }
        log.debug("execute run,the queue size is [" + OBJECT_QUEUE.size() + "]");
        // 签名记录
        Map<String, SignatureRecord> signatureRecordMap = new HashMap<>();
        // 在线记录
        Map<String, ActivityMsgTemp> activityMsgTempMap = new HashMap<>();
        // 环境检测
        Map<String, ConditionRecord> conditionRecordMap = new HashMap<>();
        // 流水表
        Map<Long, ChargingFlow> chargingFlowMap = new HashMap<>();
        for (int i = 0; !OBJECT_QUEUE.isEmpty() && i < 500; i++) {
            Object object = OBJECT_QUEUE.poll();
            if (object instanceof SignatureRecord) {
                SignatureRecord sr = (SignatureRecord) object;
                signatureRecordMap.put(sr.getBy1(), sr);
            } else if (object instanceof ActivityMsgTemp) {
                ActivityMsgTemp am = (ActivityMsgTemp) object;
                activityMsgTempMap.put(am.getThreadId(), am);
            } else if (object instanceof ConditionRecord) {
                ConditionRecord cr = (ConditionRecord) object;
                conditionRecordMap.put(cr.getBy1(), cr);
            } else if (object instanceof ChargingFlow) {
                ChargingFlow cr = (ChargingFlow) object;
                chargingFlowMap.put(cr.getId(), cr);
            }
        }
        // 保存签名记录
        if (!signatureRecordMap.isEmpty())
            try {
                sqlSession.insert("com.itrus.portal.db.SignatureRecordMapper.insertOrUpdate", signatureRecordMap.values().toArray());
            } catch (Exception e) {
                isException = true;
                // 将错误信息显示,不进行操作
                log.error(e);
            }
        // 保存在线记录
        if (!activityMsgTempMap.isEmpty())
            try {
                sqlSession.insert("com.itrus.portal.db.ActivityMsgTempMapper.insertOrUpdate", activityMsgTempMap.values().toArray());
            } catch (Exception e) {
                isException = true;
                // 将错误信息显示,不进行操作
                log.error(e);
            }
        // 保存环境检测记录
        if (!conditionRecordMap.isEmpty())
            try {
                sqlSession.insert("com.itrus.portal.db.ConditionRecordMapper.insertOrUpdate", conditionRecordMap.values().toArray());
            } catch (Exception e) {
                isException = true;
                // 将错误信息显示,不进行操作
                log.error(e);
            }
        // 暂时保存流水表记录
        if (!chargingFlowMap.isEmpty())
            try {
                sqlSession.insert("com.itrus.portal.db.ChargingFlowMapper.bulkInsert", chargingFlowMap.values().toArray());
            } catch (Exception e) {
                isException = true;
                // 将错误信息显示,不进行操作
                log.error(e);
            }
        log.debug("the exception is [" + isException + "]");
        // 若出现异常则暂停10秒钟
        if (isException) {
            try {
                // 若队列中没有信息则等待3秒
                sleep(3 * 1000);
            } catch (InterruptedException e) {
                log.error(e);
            } finally {
                isException = false;
            }
        }
    }
}
Also used : ChargingFlow(com.itrus.portal.db.ChargingFlow) HashMap(java.util.HashMap) ConditionRecord(com.itrus.portal.db.ConditionRecord) ActivityMsgTemp(com.itrus.portal.db.ActivityMsgTemp) SignatureRecord(com.itrus.portal.db.SignatureRecord)

Example 8 with ChargingFlow

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

the class ChargingFlowTask method charging.

/**
 * 遍历所有计费失败的记录,重新计费
 *
 * @Scheduled(fixedRate = 1000 * 60 * 60 * 24) 设置为每天凌晨2点触发
 * @Scheduled(cron = "0 0 2 * * ?")
 * "0 0 12 * * ?" 每天中午12点触发
 * 	@Scheduled(cron = "0/10 * * * * ?")
 */
public void charging() {
    synchronized (ChargingFlowTaskLock) {
        // start------------处理双机互斥----------
        if (null == taskFlag) {
            taskFlag = systemConfigService.isTimedTaskHost();
        }
        if (taskFlag.equals(false)) {
            return;
        }
        // end------------处理双机互斥----------
        // 计算没有记账的总数
        ChargingFlowExample cfe = new ChargingFlowExample();
        ChargingFlowExample.Criteria criteria = cfe.or();
        criteria.andIsChargingEqualTo(false);
        Integer count = sqlSession.selectOne("com.itrus.portal.db.ChargingFlowMapper.countByExample", cfe);
        if (count == 0) {
            return;
        }
        Map<Long, ApplicationInfo> applicationInfoMap = applicationInfoService.selectAppInformationMap();
        Map<Long, AppService> appServiceMap = appServiceExt.selectAppServicerMap();
        // 分页查询信息
        Integer page = 1;
        Integer size = 50;
        Integer offset = size * (page - 1);
        // 循环的次数
        Integer time = 0;
        // 批帐成功次数
        Integer successNum = 0;
        // 清空失败次数
        failTotalNum = 0;
        time = (count + size - 1) / size;
        for (int i = time; i > 0; i--) {
            // 查询条件设置一次即可,因为每次循环,都会把前面的false
            ChargingFlowExample cfe2 = new ChargingFlowExample();
            ChargingFlowExample.Criteria criteria2 = cfe2.or();
            criteria2.andIsChargingEqualTo(false);
            cfe2.setLimit(size);
            // 当失败次数大于0的时候,设置将失败次数加入偏移量中,避免失败很多的时候,全部在循环那些失败的.
            if (failTotalNum > 0) {
                cfe2.setOffset(offset + failTotalNum);
            } else {
                cfe2.setOffset(offset);
            }
            List<ChargingFlow> chargingFlows = chargingFlowService.selectListByExample(cfe2);
            for (ChargingFlow chargingFlow : chargingFlows) {
                DefaultTransactionDefinition dtd = new DefaultTransactionDefinition();
                // 事物的传播行为,使用同一个事物
                dtd.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
                TransactionStatus status = transactionManager.getTransaction(dtd);
                try {
                    ApplicationInfo applicationInfo = applicationInfoMap.get(chargingFlow.getApplicationInfo());
                    AppService appService = appServiceMap.get(chargingFlow.getAppService());
                    Map<String, Object> retMap = new HashMap<>();
                    if (chargingFlow.getChargingType().equals(ComNames.SERVICE_TYPE_CERTIFICATION)) {
                        // 实名认证计费
                        retMap = certificationChargingService.chargingTask(chargingFlow.getTransinfoName(), chargingFlow.getTransinfoId(), applicationInfo, appService, chargingFlow.getTransinfoPrimaryId(), chargingFlow.getTransinfoTableName(), chargingFlow.getId());
                    } else if (chargingFlow.getChargingType().equals(ComNames.SERVICE_TYPE_STORE)) {
                        // 存证服务计费
                        retMap = storeChargingService.storeChargingTask(chargingFlow.getTransinfoName(), chargingFlow.getTransinfoId(), applicationInfo, appService, chargingFlow.getTransinfoPrimaryId(), chargingFlow.getTransinfoTableName(), chargingFlow);
                    } else if (chargingFlow.getChargingType().equals(ComNames.SERVICE_TYPE_TAKE)) {
                        // 出证服务计费
                        retMap = takeChargingService.takeChargingTask(chargingFlow.getTransinfoName(), chargingFlow.getTransinfoId(), applicationInfo, appService, chargingFlow.getTransinfoPrimaryId(), chargingFlow.getTransinfoTableName(), chargingFlow);
                    }
                    Integer retCode = (Integer) retMap.get("retCode");
                    if (null != retCode && retCode == 1) {
                        successNum++;
                        Map<String, Long> map = new HashMap<>();
                        Charging charging = (Charging) retMap.get("charging");
                        chargingFlowService.recoderHasCharing(chargingFlow, charging.getId());
                        transactionManager.commit(status);
                    } else {
                        transactionManager.rollback(status);
                        // 失败总数加1
                        failTotalNum++;
                        // 获取失败记录的流水号id和上次记录的失败时间
                        Date date = failFlowIdMap.get(chargingFlow.getId());
                        if (null != date) {
                            long timeInMS = compareDate(date);
                            if (timeInMS > HALF_OF_THE_DAY) {
                                LogUtil.syslog(sqlSession, "计费批帐,单条流水失败", "错误信息:" + retMap.get("retMsg") + "流水号为:" + chargingFlow.getId());
                                // 更新失败记录时间
                                failFlowIdMap.put(chargingFlow.getId(), new Date());
                            }
                        } else {
                            // 流水号第一次失败
                            failFlowIdMap.put(chargingFlow.getId(), new Date());
                            LogUtil.syslog(sqlSession, "计费批帐,单条流水失败", "错误信息:" + retMap.get("retMsg") + "流水号为:" + chargingFlow.getId());
                        }
                    }
                } catch (Exception e) {
                    transactionManager.rollback(status);
                    // 失败总数加1
                    failTotalNum++;
                    // 获取失败记录的流水号id和上次记录的失败时间
                    Date date = failFlowIdMap.get(chargingFlow.getId());
                    if (null != date) {
                        long timeInMS = compareDate(date);
                        if (timeInMS > HALF_OF_THE_DAY) {
                            LogUtil.syslog(sqlSession, "计费批帐,单条流水失败", "异常信息:" + e.getMessage() + "流水号为:" + chargingFlow.getId());
                            // 更新失败记录时间
                            failFlowIdMap.put(chargingFlow.getId(), new Date());
                        }
                    } else {
                        // 流水号第一次失败
                        failFlowIdMap.put(chargingFlow.getId(), new Date());
                        LogUtil.syslog(sqlSession, "计费批帐,单条流水失败", "异常信息:" + e.getMessage() + "流水号为:" + chargingFlow.getId());
                    }
                }
            }
        }
        // 当有成功批帐的记录,就记录
        if (successNum > 0) {
            LogUtil.syslog(sqlSession, "计费批帐", "本次批帐成功的流水总数量为:" + successNum + ", 未批帐的流水总量为:" + (count - successNum >= 0 ? count - successNum : 0));
            // 批帐完成,本机重新初始化缓存,通知另外一台机器从数据库加载缓存数据
            cacheCustomer.initChargeRule();
            CertificationChargingHandler cch = new CertificationChargingHandler();
            QueueThread.buildCertificationTask(cch);
        } else {
            // 当全部是失败的时候,则判断失败总数和上次记录全部是失败的时间
            // 获取失败记录的流水号id和上次记录的失败时间
            Date date = failFlowTotalMap.get(failTotalNum);
            if (null != date) {
                long timeInMS = compareDate(date);
                if (timeInMS > HALF_OF_THE_DAY) {
                    LogUtil.syslog(sqlSession, "计费批帐,多条流水失败", "失败流水总量为:" + failTotalNum);
                    // 更新失败记录时间
                    failFlowTotalMap.put(failTotalNum, new Date());
                    return;
                }
            } else {
                // 流水失败总数第一次出现
                failFlowTotalMap.put(failTotalNum, new Date());
                LogUtil.syslog(sqlSession, "计费批帐,多条流水失败", "失败流水总量为:" + failTotalNum);
                return;
            }
        }
    }
}
Also used : AppService(com.itrus.portal.db.AppService) ChargingFlow(com.itrus.portal.db.ChargingFlow) Charging(com.itrus.portal.db.Charging) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) HashMap(java.util.HashMap) ChargingFlowExample(com.itrus.portal.db.ChargingFlowExample) ApplicationInfo(com.itrus.portal.db.ApplicationInfo) TransactionStatus(org.springframework.transaction.TransactionStatus) Date(java.util.Date) UnknownHostException(java.net.UnknownHostException) CertificationChargingHandler(com.itrus.portal.service.CertificationChargingHandler)

Aggregations

ChargingFlow (com.itrus.portal.db.ChargingFlow)8 HashMap (java.util.HashMap)5 Date (java.util.Date)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ActivityMsgTemp (com.itrus.portal.db.ActivityMsgTemp)1 AppService (com.itrus.portal.db.AppService)1 ApplicationInfo (com.itrus.portal.db.ApplicationInfo)1 Charging (com.itrus.portal.db.Charging)1 ChargingFlowExample (com.itrus.portal.db.ChargingFlowExample)1 ConditionRecord (com.itrus.portal.db.ConditionRecord)1 SignatureRecord (com.itrus.portal.db.SignatureRecord)1 CertificationChargingHandler (com.itrus.portal.service.CertificationChargingHandler)1 UnknownHostException (java.net.UnknownHostException)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)1