use of com.itrus.portal.db.Charging in project portal by ixinportal.
the class TakeChargingServiceImpl method takeChargingTask.
/**
* 供定时任务调用,批量流水,进行记账
* @param transinfoName,关联服务配置表名称
* @param transinfoId,关联服务配置id
* @param applicationInfo,应用appid对应的applicationInfo对象
* @param appService,服务编码对应的appService对象
* @param transinfoPrimaryId,服务调用记录生成的主键id
* @param transinfoTableName,服务调用记录的表名称
* @return
*/
public Map<String, Object> takeChargingTask(String transinfoName, Long transinfoId, ApplicationInfo applicationInfo, AppService appService, Long transinfoPrimaryId, String transinfoTableName, ChargingFlow chargingFlow) {
synchronized (chargingLock) {
Map<String, Object> retMap = new HashMap<String, Object>();
retMap.put("retCode", 0);
String key = applicationInfo.getId() + "," + appService.getId();
ConcurrentHashMap<String, List<ChargingPrice>> chargeRuleMap = CacheCustomer.TAKE_CHARGERULE_MAP;
List<ChargingPrice> chargingPrices = chargeRuleMap.get(key);
Map<String, Object> insertRetMap = new HashMap<String, Object>();
try {
// 获取第一条计费规则
for (int i = 0; i < chargingPrices.size(); i++) {
ChargingPrice chargingPrice = chargingPrices.get(i);
// 判断当前时间是否处于计费时间之内,防止空白期
CertificationCharging certificationCharging = CacheCustomer.getCERTIFICATIONCHARGINGMAP().get(chargingPrice.getCertificationCharging());
boolean dateFlag = compareDate(new Date(), certificationCharging.getStartTime(), certificationCharging.getEndTime());
if (dateFlag) {
continue;
}
boolean flag = serviceNameService.checkSurplusNumberIsEn(chargingPrice.getServiceName(), chargingPrice, chargingFlow.getTotalSize());
// 若当前计费规则可用,进行记录交易信息,并返回成功标识
if (flag) {
Double totalPrice = chargingPrice.getUnitPrice() * chargingFlow.getTotalSize();
insertRetMap = chargingService.insertOneIntoTakeCharging(applicationInfo.getId(), appService.getId(), chargingPrice, transinfoName, transinfoId, chargingFlow.getChargingType(), transinfoPrimaryId, transinfoTableName, chargingFlow, totalPrice, chargingFlow.getTotalSize(), 0);
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 {
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;
}
}
use of com.itrus.portal.db.Charging in project portal by ixinportal.
the class StoreChargingServiceImpl method storeChargingTask.
/**
* 1.先判断是不是新的证据编号,如果是新的,则
* 先获取按次计费的.按次计费通过了,并且本次大小小于基准大小,则直接通过.
* 如果本次大小大于基准大小,并且存证按量计费(不存在则直接通过),并且按量计费剩余量大于超出值,则通过,否则不通过
*再根据按次计费进行
* 2.如果是补交证据,则查找是否有按量计费,有则按量计费;
* 如果没有,则判断是否曾经配有按量计费,如果没有,则通过,如果有.则不通过
*
* (存在的问题,如果第二次补交,没有配置有按量计费.)
* @param transinfoName,关联服务配置表名称
* @param transinfoId,关联服务配置id
* @param applicationInfo,应用appid对应的applicationInfo对象
* @param appService,服务编码对应的appService对象
* @param transinfoPrimaryId,服务调用记录生成的主键id
* @param transinfoTableName,服务调用记录的表名称
* @return
*/
public Map<String, Object> storeChargingTask(String transinfoName, Long transinfoId, ApplicationInfo applicationInfo, AppService appService, Long transinfoPrimaryId, String transinfoTableName, ChargingFlow chargingFlow) {
synchronized (chargingLock) {
Map<String, Object> retMap = new HashMap<String, Object>();
retMap.put("retCode", 0);
ConcurrentHashMap<String, List<ChargingPrice>> chargeRuleMap = CacheCustomer.getSTORE_CHARGERULE_MAP();
ConcurrentHashMap<String, List<ChargingPrice>> AllChargeRuleMap = CacheCustomer.ALL_STORE_CHARGERULE_MAP;
ConcurrentHashMap<Long, ServiceName> serviceNameMap = CacheCustomer.getSERVICENAMEMAP();
// 第二次补交.需要按量计费
String key_size = applicationInfo.getId() + "," + appService.getId() + "," + "1.0";
// 按次计费的key
String key_num = applicationInfo.getId() + "," + appService.getId() + "," + "0.0";
// 获取存证服务中,有没有按量计费的
List<ChargingPrice> allStorechargingPrices = AllChargeRuleMap.get(key_size);
List<ChargingPrice> chargingPrices = chargeRuleMap.get(key_num);
Date date = new Date();
Map<String, Object> insertRetMap = new HashMap<String, Object>();
Integer baseSize = chargingFlow.getBaseSize();
Integer size = chargingFlow.getTotalSize();
// 标识本次存证大小是否超出了基础空间大小:true为超出了,需要判断超出计费
boolean isPass = false;
if (size > baseSize) {
isPass = true;
}
try {
if (chargingFlow.getIsNew()) {
// 获取第一条计费规则
for (int i = 0; i < chargingPrices.size(); i++) {
ChargingPrice chargingPrice = chargingPrices.get(i);
// 判断当前时间是否处于计费时间之内,防止空白期
CertificationCharging certificationCharging = CacheCustomer.getCERTIFICATIONCHARGINGMAP().get(chargingPrice.getCertificationCharging());
boolean dateFlag = compareDate(new Date(), certificationCharging.getStartTime(), certificationCharging.getEndTime());
if (dateFlag) {
continue;
}
// 先按次校验
boolean flag = serviceNameService.checkNumber(chargingPrice.getServiceName(), chargingPrice);
// 若当前计费规则可用,进行记录交易信息,并返回成功标识
if (flag) {
// 超出了,需要判断按量计费
if (isPass) {
List<ChargingPrice> chargingPricesSize = chargeRuleMap.get(key_size);
// 当前没有有效的按量计费,且曾经也没有配置过按量计费,则直接通过.
if (null == chargingPricesSize || chargingPricesSize.isEmpty()) {
// 判断曾经是否有按量计费,没有则只按次计费,如果有,则报错
if (null == allStorechargingPrices || allStorechargingPrices.isEmpty()) {
insertRetMap = chargingService.insertOneIntoStoreCharging(applicationInfo.getId(), appService.getId(), chargingPrice, transinfoName, transinfoId, chargingFlow.getChargingType(), transinfoPrimaryId, transinfoTableName, chargingFlow, chargingPrice.getUnitPrice(), 1, 0);
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 {
retMap.put("retMsg", "按量计费已经使用完了,请联系系统管理员配置");
return retMap;
}
} else {
// 有有效的按次计费.获取有效的按次计费之后,按量计费,再按次计费
// 本次超出的大小
Integer passSize = size - baseSize;
for (ChargingPrice chargingPriceSize : chargingPricesSize) {
// 校验计费规则对应的时间,是否处于当前时间内,如果不是,则进行下一个计费规则
CertificationCharging certificationChargingSize = CacheCustomer.getCERTIFICATIONCHARGINGMAP().get(chargingPriceSize.getCertificationCharging());
boolean dateFlagSize = compareDate(date, certificationChargingSize.getStartTime(), certificationChargingSize.getEndTime());
if (dateFlagSize) {
continue;
}
ServiceName serviceNameSize = serviceNameMap.get(chargingPriceSize.getServiceName());
// 判断已经使用的总量+本次用量,是否超出了总的阶梯大小:如果没超出,则返回成功,如果超出了,则获取下一个循环
boolean anliang = (null != serviceNameSize && ((serviceNameSize.getTotalNumber() + passSize) <= chargingPriceSize.getMaximumNumber()) && ((serviceNameSize.getTotalNumber() + passSize) > chargingPriceSize.getMinimumNumber()));
if (anliang) {
// 按次计费
insertRetMap = chargingService.insertOneIntoStoreCharging(applicationInfo.getId(), appService.getId(), chargingPrice, transinfoName, transinfoId, chargingFlow.getChargingType(), transinfoPrimaryId, transinfoTableName, chargingFlow, chargingPrice.getUnitPrice(), 1, 0);
boolean insertFlag = (boolean) insertRetMap.get("flag");
// 若插入成功
if (insertFlag) {
// 按量计费
insertRetMap = chargingService.insertOneIntoStoreCharging(applicationInfo.getId(), appService.getId(), chargingPriceSize, transinfoName, transinfoId, chargingFlow.getChargingType(), transinfoPrimaryId, transinfoTableName, chargingFlow, chargingPriceSize.getUnitPrice() * passSize, passSize, 1);
boolean insertBySizeFlag = (boolean) insertRetMap.get("flag");
// 若插入成功
if (insertBySizeFlag) {
Charging charging = (Charging) insertRetMap.get("charging");
retMap.put("retCode", 1);
retMap.put("charging", charging);
return retMap;
} else {
retMap.put("retCode", 0);
retMap.put("retMsg", insertRetMap.get("retMsg"));
return retMap;
}
} else {
// TODO 20170315 插入失败
retMap.put("retCode", 0);
retMap.put("retMsg", insertRetMap.get("retMsg"));
return retMap;
}
}
}
// 进行到这里,表示按量计费余量不足
retMap.put("retMsg", "按量计费剩余量不足,请联系系统管理员配置");
return retMap;
}
} else {
// 未超出,直接按次计费
insertRetMap = chargingService.insertOneIntoStoreCharging(applicationInfo.getId(), appService.getId(), chargingPrice, transinfoName, transinfoId, chargingFlow.getChargingType(), transinfoPrimaryId, transinfoTableName, chargingFlow, chargingPrice.getUnitPrice(), 1, 0);
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);
}
}
} else {
// 有有效的按次计费.获取有效的按次计费之后,按量计费,再按次计费
// 本次超出的大小
List<ChargingPrice> chargingPricesSize = chargeRuleMap.get(key_size);
// 当计费规则都失效之后
if (null == chargingPricesSize || chargingPricesSize.isEmpty()) {
retMap.put("retMsg", "按量计费已经使用完了,请联系系统管理员配置");
return retMap;
}
for (ChargingPrice chargingPriceSize : chargingPricesSize) {
// 校验计费规则对应的时间,是否处于当前时间内,如果不是,则进行下一个计费规则
CertificationCharging certificationChargingSize = CacheCustomer.getCERTIFICATIONCHARGINGMAP().get(chargingPriceSize.getCertificationCharging());
boolean dateFlagSize = compareDate(date, certificationChargingSize.getStartTime(), certificationChargingSize.getEndTime());
if (dateFlagSize) {
continue;
}
ServiceName serviceNameSize = serviceNameMap.get(chargingPriceSize.getServiceName());
// 判断已经使用的总量+本次用量,是否超出了总的阶梯大小:如果没超出,则返回成功,如果超出了,则获取下一个循环
boolean anliang = (null != serviceNameSize && ((serviceNameSize.getTotalNumber() + size) <= chargingPriceSize.getMaximumNumber()) && ((serviceNameSize.getTotalNumber() + size) > chargingPriceSize.getMinimumNumber()));
if (anliang) {
// 按量计费
insertRetMap = chargingService.insertOneIntoStoreCharging(applicationInfo.getId(), appService.getId(), chargingPriceSize, transinfoName, transinfoId, chargingFlow.getChargingType(), transinfoPrimaryId, transinfoTableName, chargingFlow, chargingPriceSize.getUnitPrice() * size, size, 1);
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;
}
}
}
// 进行到这里,表示按量计费余量不足
retMap.put("retMsg", "按量计费剩余量不足,请联系系统管理员配置");
return retMap;
}
} 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;
}
}
use of com.itrus.portal.db.Charging 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;
}
}
}
}
Aggregations