Search in sources :

Example 1 with CertificationChargingHandler

use of com.itrus.portal.service.CertificationChargingHandler in project portal by ixinportal.

the class CertificationChargingController method createOrUpdate.

// 新建和修改处理
@RequestMapping(value = "/createOrUpdate", method = RequestMethod.POST, produces = "text/html")
@ResponseBody
public synchronized Map<String, Object> createOrUpdate(@RequestParam("certificationChargingStr") String certificationChargingStr) {
    Map<String, Object> retMap = new HashMap<String, Object>();
    retMap.put("flag", false);
    DefaultTransactionDefinition dtd = new DefaultTransactionDefinition();
    // 事物的传播行为,使用同一个事物
    dtd.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    TransactionStatus status = transactionManager.getTransaction(dtd);
    try {
        CertificationChargingWrap ccw = jsonTool.readValue(certificationChargingStr, CertificationChargingWrap.class);
        List<CertificationChargingList> certificationChargingLists = ccw.getCertificationChargingLists();
        // 1.新增或修改CertificationCharging
        for (int i = 0; i < certificationChargingLists.size(); i++) {
            // 取出单个的服务名称主表对象和服务名封装list
            CertificationCharging cc = certificationChargingLists.get(i).getCertificationCharging();
            if (null == cc.getChargingType()) {
                cc.setChargingType(ComNames.CHARGING_TYPE_SMRZ_1);
            }
            List<ServiceNameList> serviceNameLists = certificationChargingLists.get(i).getServiceNameLists();
            Map<String, Object> mapCC = new HashMap<String, Object>();
            mapCC = certificationChargingService.saveOrUpdateCertificationCharging(cc);
            boolean retCodeCC = false;
            retCodeCC = (boolean) mapCC.get("flag");
            // 新增或修改CertificationCharging成功
            if (retCodeCC) {
                cc = (CertificationCharging) mapCC.get("certificationCharging");
                CertificationCharging oldCC = (CertificationCharging) mapCC.get("oldCertificationCharging");
                // 将CertificationCharging的id值赋值给serviceNameList中新增的服务
                serviceNameLists = serviceNameService.insertIdIntoServiceName(cc, serviceNameLists);
                // 2.新增或修改ServiceName
                for (int j = 0; j < serviceNameLists.size(); j++) {
                    ServiceName serviceName = serviceNameLists.get(j).getServiceName();
                    ChargingPriceList chargingPriceList = serviceNameLists.get(j).getChargingPriceList();
                    boolean retCodeSN = false;
                    Map<String, Object> mapServiceName = new HashMap<String, Object>();
                    mapServiceName = serviceNameService.saveOrUpdateServiceName(serviceName, cc, oldCC);
                    retCodeSN = (boolean) mapServiceName.get("flag");
                    // 新增或修改ServiceName成功
                    if (retCodeSN) {
                        serviceName = (ServiceName) mapServiceName.get("serviceName");
                        ServiceName oldServiceName = (ServiceName) mapServiceName.get("oldServiceName");
                        List<ServiceName> oldServiceNames = (List<ServiceName>) mapServiceName.get("serviceNames");
                        // 将serviceNameId,CertificationCharging的id值赋值给chargingPriceList中计费以及价格区间
                        chargingPriceList = chargingPriceService.insertIdIntoChargingPrice(serviceName, chargingPriceList);
                        List<ChargingPrice> chargingPrices = chargingPriceList.getChargingPriceLists();
                        // 3.charprice新增或修改
                        for (int k = 0; k < chargingPrices.size(); k++) {
                            Map<String, Object> mapChargingPrice = new HashMap<String, Object>();
                            mapChargingPrice = chargingPriceService.saveOrUpdateChargingPrice(cc, serviceName, chargingPrices.get(k), oldCC, oldServiceName, oldServiceNames);
                            boolean retCodeChargingPrice = false;
                            retCodeChargingPrice = (boolean) mapChargingPrice.get("flag");
                            // 新增或修改ChargingPrice失败
                            if (!retCodeChargingPrice) {
                                transactionManager.rollback(status);
                                retMap = mapChargingPrice;
                                return retMap;
                            }
                        }
                    } else {
                        // TODO 新增或修改ServiceName错误,返回异常信息
                        transactionManager.rollback(status);
                        retMap = mapServiceName;
                        return retMap;
                    }
                }
            } else {
                // TODO 新增或修改CertificationCharging错误,返回异常信息
                transactionManager.rollback(status);
                retMap = mapCC;
                return retMap;
            }
        }
        LogUtil.adminlog(sqlSession, "新增计费规则", "新增了计费规则");
        transactionManager.commit(status);
        retMap.put("flag", true);
        // 修改应用对应的服务:将应用和服务关联起来
        Set<Long> set = certificationChargingService.getAllAppId();
        if (null != set && set.size() > 0) {
            for (Long long1 : set) {
                Set<Long> AppserviceIDs = certificationChargingService.getAppserviceIDsByAppId(long1);
                applicationInfoService.addServiceByAppInfo(long1, AppserviceIDs);
            }
        }
        // 通知其他机器,加载缓存
        CertificationChargingHandler cch = new CertificationChargingHandler();
        QueueThread.buildCertificationTask(cch);
        cacheCustomer.initChargeRule();
        return retMap;
    } catch (JsonParseException e) {
        transactionManager.rollback(status);
        retMap.put("retMsg", e.getMessage());
        return retMap;
    } catch (JsonMappingException e) {
        transactionManager.rollback(status);
        retMap.put("retMsg", e.getMessage());
        return retMap;
    } catch (IOException e) {
        transactionManager.rollback(status);
        retMap.put("retMsg", e.getMessage());
        return retMap;
    } catch (Exception e) {
        transactionManager.rollback(status);
        retMap.put("retMsg", e.getMessage());
        return retMap;
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) HashMap(java.util.HashMap) ChargingPriceList(com.itrus.portal.entity.ChargingPriceList) TransactionStatus(org.springframework.transaction.TransactionStatus) CertificationChargingList(com.itrus.portal.entity.CertificationChargingList) JsonParseException(org.codehaus.jackson.JsonParseException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) CertificationChargingList(com.itrus.portal.entity.CertificationChargingList) ArrayList(java.util.ArrayList) ServiceNameList(com.itrus.portal.entity.ServiceNameList) ChargingPriceList(com.itrus.portal.entity.ChargingPriceList) List(java.util.List) ServiceNameList(com.itrus.portal.entity.ServiceNameList) CertificationCharging(com.itrus.portal.db.CertificationCharging) IOException(java.io.IOException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) CertificationChargingWrap(com.itrus.portal.entity.CertificationChargingWrap) ServiceName(com.itrus.portal.db.ServiceName) ChargingPrice(com.itrus.portal.db.ChargingPrice) CertificationChargingHandler(com.itrus.portal.service.CertificationChargingHandler) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with CertificationChargingHandler

use of com.itrus.portal.service.CertificationChargingHandler in project portal by ixinportal.

the class MobileChargingFlowTask method charging.

/**
 * 遍历所有计费失败的记录,重新计费
 *
 * @Scheduled(fixedRate = 1000 * 60 * 60 * 24) 设置为每天凌晨2点触发
 * @Scheduled(cron = "0 0 2 * * ?")
 * "0 0 12 * * ?" 每天中午12点触发
 * 	@Scheduled(cron = "0/10 * * * * ?")
 */
// @Scheduled(fixedRate = 1000 * 60 * 60 * 24) //设置为每天凌晨2点触发
@Scheduled(cron = "0 0 2 * * ?")
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
            MobileChargingFlowExample cfe2 = new MobileChargingFlowExample();
            MobileChargingFlowExample.Criteria criteria2 = cfe2.or();
            criteria2.andIsChargingEqualTo(false);
            cfe2.setLimit(size);
            // 当失败次数大于0的时候,设置将失败次数加入偏移量中,避免失败很多的时候,全部在循环那些失败的.
            if (failTotalNum > 0) {
                cfe2.setOffset(offset + failTotalNum);
            } else {
                cfe2.setOffset(offset);
            }
            List<MobileChargingFlow> chargingFlows = chargingFlowService.selectListByExample(cfe2);
            for (MobileChargingFlow 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_FOUR)) {
                        // 移动端计费
                        retMap = appSeviceChargingService.chargingTask(// chargingFlow.getTransinfoId(),
                        applicationInfo, // chargingFlow.getTransinfoPrimaryId(),
                        chargingFlow.getTransinfoTableName(), chargingFlow.getId());
                    }
                    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 : Charging(com.itrus.portal.db.Charging) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) HashMap(java.util.HashMap) ChargingFlowExample(com.itrus.portal.db.ChargingFlowExample) MobileChargingFlowExample(com.itrus.portal.db.MobileChargingFlowExample) ApplicationInfo(com.itrus.portal.db.ApplicationInfo) TransactionStatus(org.springframework.transaction.TransactionStatus) Date(java.util.Date) MobileChargingFlowExample(com.itrus.portal.db.MobileChargingFlowExample) MobileChargingFlow(com.itrus.portal.db.MobileChargingFlow) CertificationChargingHandler(com.itrus.portal.service.CertificationChargingHandler) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 3 with CertificationChargingHandler

use of com.itrus.portal.service.CertificationChargingHandler in project portal by ixinportal.

the class MobileAppserviceChargingService method chargingTask.

/**
 * 供定时任务调用,批量流水,进行记账
 * @param transinfoName,关联服务配置表名称
 * @param transinfoId,关联服务配置id
 * @param applicationInfo,应用appid对应的applicationInfo对象
 * @param appService,服务编码对应的appService对象
 * @param transinfoPrimaryId,服务调用记录生成的主键id
 * @param transinfoTableName,服务调用记录的表名称
 * @return
 */
public Map<String, Object> chargingTask(/*String transinfoName, Long transinfoId,*/
ApplicationInfo applicationInfo, /*AppService appService, Long transinfoPrimaryId,*/
String transinfoTableName, Long chargingFlow) {
    synchronized (chargingLock) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("retCode", 0);
        // String key = applicationInfo.getId() + "," + appService.getId();
        ConcurrentHashMap<Long, List<MobileChargingPrice>> chargeRuleMap = cacheCustomer.getMOBILE_CHARGERULE_MAP();
        List<MobileChargingPrice> chargingPrices = chargeRuleMap.get(applicationInfo.getId());
        Date date = new Date();
        Map<String, Object> insertRetMap = new HashMap<String, Object>();
        try {
            // 获取第一条计费规则
            for (int i = 0; i < chargingPrices.size(); i++) {
                MobileChargingPrice chargingPrice = chargingPrices.get(i);
                // 判断当前时间是否处于计费时间之内,防止空白期
                MobileAppserviceCharging certificationCharging = CacheCustomer.getMOBILE_CERTIFICATIONCHARGING_MAP().get(chargingPrice.getAppserviceCharging());
                boolean dateFlag = compareDate(new Date(), certificationCharging.getStartTime(), certificationCharging.getEndTime());
                if (dateFlag) {
                    continue;
                }
                boolean flag = appServiceNameService.checkNumber(chargingPrice.getAppserviceName(), chargingPrice);
                // 若当前计费规则可用,进行记录交易信息,并返回成功标识
                if (flag) {
                    insertRetMap = chargingService.insertOneIntoCharging(applicationInfo.getId(), /*appService.getId(),*/
                    chargingPrice, /*transinfoName, transinfoId,appService.getType(),transinfoPrimaryId,*/
                    transinfoTableName, chargingFlow);
                    boolean insertFlag = (boolean) insertRetMap.get("flag");
                    // 若插入成功
                    if (insertFlag) {
                        Charging charging = (Charging) insertRetMap.get("charging");
                        retMap.put("retCode", 1);
                        retMap.put("charging", charging);
                        return retMap;
                    } else {
                        // TODO 20170315 插入失败
                        retMap.put("retCode", 0);
                        retMap.put("retMsg", insertRetMap.get("retMsg"));
                        return retMap;
                    }
                } else {
                    // 本机重新初始化计费规则
                    cacheCustomer.initChargeRule();
                    // 计费规则已经失效了,从系统缓存中移除改规则,并通知其他主机进行更新
                    CertificationChargingHandler cch = new CertificationChargingHandler();
                    QueueThread.buildCertificationTask(cch);
                }
            }
        } catch (Exception e) {
            retMap.put("retCode", 0);
            retMap.put("retMsg", e.getMessage());
            return retMap;
        }
        // 能走到这里,证明没有可用的计费规则(总次数已经用完了)
        retMap.put("retCode", 0);
        retMap.put("retMsg", "应用名称:" + applicationInfo.getName() + /* + ",服务名称:"
					+ appService.getAppServiceName()*/
        ",原因:" + "没有可用的计费规则");
        return retMap;
    }
}
Also used : MobileAppserviceCharging(com.itrus.portal.db.MobileAppserviceCharging) MobileAppserviceCharging(com.itrus.portal.db.MobileAppserviceCharging) Charging(com.itrus.portal.db.Charging) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MobileChargingPrice(com.itrus.portal.db.MobileChargingPrice) Date(java.util.Date) ArrayList(java.util.ArrayList) List(java.util.List) CertificationChargingHandler(com.itrus.portal.service.CertificationChargingHandler)

Example 4 with CertificationChargingHandler

use of com.itrus.portal.service.CertificationChargingHandler in project portal by ixinportal.

the class MobileAppserviceChargingController method createOrUpdate.

// 新建和修改处理
@RequestMapping(value = "/createOrUpdate", method = RequestMethod.POST, produces = "text/html")
@ResponseBody
public synchronized Map<String, Object> createOrUpdate(@RequestParam("appserviceChargingStr") String appserviceChargingStr) {
    Map<String, Object> retMap = new HashMap<String, Object>();
    retMap.put("flag", false);
    DefaultTransactionDefinition dtd = new DefaultTransactionDefinition();
    // 事物的传播行为,使用同一个事物
    dtd.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    TransactionStatus status = transactionManager.getTransaction(dtd);
    try {
        AppserviceChargingWrap acw = jsonTool.readValue(appserviceChargingStr, AppserviceChargingWrap.class);
        List<AppserviceChargingList> appserviceChargingLists = acw.getAppserviceChargingLists();
        for (int i = 0; i < appserviceChargingLists.size(); i++) {
            // 取出单个的服务名称主表对象和服务名封装list
            MobileAppserviceCharging appCharging = appserviceChargingLists.get(i).getAppserviceCharging();
            // if (null == appCharging.getChargingType()) {
            // cc.setChargingType(ComNames.CHARGING_TYPE_SMRZ_1);
            // }
            List<AppserviceNameList> appserviceNameLists = appserviceChargingLists.get(i).getAppserviceNameLists();
            Map<String, Object> mapAC = new HashMap<String, Object>();
            mapAC = appserviceChargingService.saveOrUpdateAppserviceCharging(appCharging);
            boolean retCodeAC = false;
            retCodeAC = (boolean) mapAC.get("flag");
            // 新增或修改MobileAppserviceCharging成功
            if (retCodeAC) {
                appCharging = (MobileAppserviceCharging) mapAC.get("appserviceCharging");
                MobileAppserviceCharging oldAC = (MobileAppserviceCharging) mapAC.get("oldAppserviceCharging");
                // 将MobileAppserviceCharging的id值赋值给serviceNameList中新增的服务
                appserviceNameLists = appserviceNameService.insertIdIntoAppserviceName(appCharging, appserviceNameLists);
                // 2.新增或修改ServiceName
                for (int j = 0; j < appserviceNameLists.size(); j++) {
                    MobileAppserviceName appserviceName = appserviceNameLists.get(j).getAppserviceName();
                    ChargingPriceList chargingPriceList = appserviceNameLists.get(j).getChargingPriceList();
                    boolean retCodeSN = false;
                    Map<String, Object> mapAppserviceName = new HashMap<String, Object>();
                    mapAppserviceName = appserviceNameService.saveOrUpdateServiceName(appserviceName, appCharging, oldAC);
                    retCodeSN = (boolean) mapAppserviceName.get("flag");
                    // 新增或修改ServiceName成功
                    if (retCodeSN) {
                        appserviceName = (MobileAppserviceName) mapAppserviceName.get("appserviceName");
                        MobileAppserviceName oldAppserviceName = (MobileAppserviceName) mapAppserviceName.get("oldAppserviceName");
                        List<MobileAppserviceName> oldAppserviceNames = (List<MobileAppserviceName>) mapAppserviceName.get("appserviceNames");
                        // 将serviceNameId,CertificationCharging的id值赋值给chargingPriceList中计费以及价格区间
                        chargingPriceList = chargingPriceService.insertIdIntoChargingPrice(appserviceName, chargingPriceList);
                        List<MobileChargingPrice> chargingPrices = chargingPriceList.getChargingPriceLists();
                        // 3.charprice新增或修改
                        for (int k = 0; k < chargingPrices.size(); k++) {
                            Map<String, Object> mapChargingPrice = new HashMap<String, Object>();
                            mapChargingPrice = chargingPriceService.saveOrUpdateChargingPrice(appCharging, appserviceName, chargingPrices.get(k), oldAC, oldAppserviceName, oldAppserviceNames);
                            boolean retCodeChargingPrice = false;
                            retCodeChargingPrice = (boolean) mapChargingPrice.get("flag");
                            // 新增或修改ChargingPrice失败
                            if (!retCodeChargingPrice) {
                                transactionManager.rollback(status);
                                retMap = mapChargingPrice;
                                return retMap;
                            }
                        }
                    } else {
                        // TODO 新增或修改ServiceName错误,返回异常信息
                        transactionManager.rollback(status);
                        retMap = mapAppserviceName;
                        return retMap;
                    }
                }
            } else {
                // TODO 新增或修改CertificationCharging错误,返回异常信息
                transactionManager.rollback(status);
                retMap = mapAC;
                return retMap;
            }
        }
        LogUtil.adminlog(sqlSession, "新增计费规则", "新增了计费规则");
        transactionManager.commit(status);
        retMap.put("flag", true);
        // 修改应用对应的服务:将应用和服务关联起来
        // Set<Long> set = appserviceChargingService.getAllAppId();
        // if (null != set && set.size() >0) {
        // for (Long long1 : set) {
        // Set<Long> AppserviceIDs = appserviceChargingService.getAppserviceIDsByAppId(long1);
        // applicationInfoService.addServiceByAppInfo(long1,AppserviceIDs);
        // }
        // }
        // 通知其他机器,加载缓存
        CertificationChargingHandler cch = new CertificationChargingHandler();
        QueueThread.buildCertificationTask(cch);
        // cacheCustomer.initChargeRule();
        return retMap;
    } catch (JsonParseException e) {
        transactionManager.rollback(status);
        e.printStackTrace();
        retMap.put("retMsg", e.getMessage());
        return retMap;
    } catch (JsonMappingException e) {
        transactionManager.rollback(status);
        retMap.put("retMsg", e.getMessage());
        e.printStackTrace();
        return retMap;
    } catch (IOException e) {
        transactionManager.rollback(status);
        retMap.put("retMsg", e.getMessage());
        e.printStackTrace();
        return retMap;
    } catch (Exception e) {
        transactionManager.rollback(status);
        retMap.put("retMsg", e.getMessage());
        e.printStackTrace();
        return retMap;
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) HashMap(java.util.HashMap) AppserviceNameList(com.itrus.portal.mobile.entity.AppserviceNameList) MobileChargingPrice(com.itrus.portal.db.MobileChargingPrice) ChargingPriceList(com.itrus.portal.mobile.entity.ChargingPriceList) TransactionStatus(org.springframework.transaction.TransactionStatus) JsonParseException(org.codehaus.jackson.JsonParseException) MobileAppserviceName(com.itrus.portal.db.MobileAppserviceName) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) List(java.util.List) CertificationChargingList(com.itrus.portal.entity.CertificationChargingList) AppserviceNameList(com.itrus.portal.mobile.entity.AppserviceNameList) ArrayList(java.util.ArrayList) ChargingPriceList(com.itrus.portal.mobile.entity.ChargingPriceList) AppserviceChargingList(com.itrus.portal.mobile.entity.AppserviceChargingList) ServiceNameList(com.itrus.portal.entity.ServiceNameList) MobileAppserviceCharging(com.itrus.portal.db.MobileAppserviceCharging) IOException(java.io.IOException) AppserviceChargingWrap(com.itrus.portal.mobile.entity.AppserviceChargingWrap) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) CertificationChargingHandler(com.itrus.portal.service.CertificationChargingHandler) AppserviceChargingList(com.itrus.portal.mobile.entity.AppserviceChargingList) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with CertificationChargingHandler

use of com.itrus.portal.service.CertificationChargingHandler 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

CertificationChargingHandler (com.itrus.portal.service.CertificationChargingHandler)5 HashMap (java.util.HashMap)5 TransactionStatus (org.springframework.transaction.TransactionStatus)4 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)4 Charging (com.itrus.portal.db.Charging)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 List (java.util.List)3 ApplicationInfo (com.itrus.portal.db.ApplicationInfo)2 ChargingFlowExample (com.itrus.portal.db.ChargingFlowExample)2 MobileAppserviceCharging (com.itrus.portal.db.MobileAppserviceCharging)2 MobileChargingPrice (com.itrus.portal.db.MobileChargingPrice)2 CertificationChargingList (com.itrus.portal.entity.CertificationChargingList)2 ServiceNameList (com.itrus.portal.entity.ServiceNameList)2 IOException (java.io.IOException)2 JsonParseException (org.codehaus.jackson.JsonParseException)2 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 AppService (com.itrus.portal.db.AppService)1