限時交易(可以跨天設置)

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/damoneric_guo/article/details/71043347
主方法處理:
@Resource(name = "dateLimit")
private DateLimit dateLimit;

public boolean preactRepaymentRdTx(final String flowCode, final BigDecimal serviceFee, final BigDecimal amount, final BigDecimal defaultAmount, final List payer, RepayChannelEnum channel) throws BusinessException {
Date now = new Date();
Date beforeDate = new Date();

try {
beforeDate = dateLimit.dealTimeBeforeLimit(now);
} catch (Exception e) {
}

Date afterDate = new Date();

try {
afterDate = dateLimit.dealTimeAfterLimit(now);
} catch (Exception e) {
}

// 0點前後5分鐘停止交易
if (beforeDate.before(afterDate)) {
if ((now.after(beforeDate) || now.compareTo(beforeDate) == 0) && (now.before(afterDate) || now.compareTo(afterDate) == 0)) {
logger.error("限定時間內停止交易,禁止調用系統接口!");
throw new BusinessException("限定時間內停止交易,禁止調用系統接口!");
} else {
//邏輯處理
} else if (beforeDate.after(afterDate)) {
if (now.after(afterDate) && now.before(beforeDate)) {
//邏輯處理
} else {
logger.error("限定時間內停止交易,禁止調用系統接口!");
throw new BusinessException("限定時間內停止交易,禁止調用系統接口!");
}
} else {
return this.preactRepayment(flowCode, serviceFee, amount, defaultAmount, payer, channel);
}

}

調用的方法:

String getItemFinancialTimeLimit = 17:00:00-00:20:00 //跨天設置限制(當天限時17:00:00到第二天00:20:00不能交易

String getItemFinancialTimeLimit = 17:00:00-18:20:00 //當天設置限制(當天限時17:00:00-18:20:00不能交易)

public Date dealTimeBeforeLimit(Date dealTime) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(dealTime);
int year = cal.get(Calendar.YEAR);// 獲取年份
int month = cal.get(Calendar.MONTH) + 1;// 獲取月份
int day = cal.get(Calendar.DATE);// 獲取日
// "2012-1-13 23:55:00"
String timeBeforeLimitStr = sysConfig.getItemFinancialTimeLimit(); // "23:55:00";
String[] strs = timeBeforeLimitStr.split("-");
String str = strs[0];
str = year + "-" + month + "-" + day + " " + timeBeforeLimitStr; // 要跟上面sdf定義的格式一樣
Date date = sdf.parse(str);
return date;
}

public Date dealTimeAfterLimit(Date dealTime) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(dealTime);
int year = cal.get(Calendar.YEAR);// 獲取年份
int month = cal.get(Calendar.MONTH) + 1;// 獲取月份
int day = cal.get(Calendar.DATE);// 獲取日
// "2012-1-13 17:26:33"
String timeAfterLimitStr = sysConfig.getItemFinancialTimeLimit(); // "00:05:00";
String[] strs = timeAfterLimitStr.split("-");
String str2 = strs[1];
str2 = year + "-" + month + "-" + day + " " + str2; // 要跟上面sdf定義的格式一樣
Date date2 = sdf.parse(str2);
// 判斷參數平臺配置是不是同一天段時間
return date2;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章