@Override
@Transactional
public JSONObject openToAccount(JSONObject params)throws Exception {
JSONObject json = new JSONObject();
// 先判断是否存在用户账户记录
if(userAccountDao.isExistUserAmountRec(params.getString("userId")) <= 0){ // 不存在
// 初始化用户账户记录
int initcount = userAccountDao.initUserAccount(params.getString("userId"));
if(initcount <= 0){
throw new BusinessException("初始化用户账户记录异常");
}
}else{ //存在
// 获取用户账号对象
UserAccounct userAccounct = userAccountDao.getUserAmountByUserId(params.getString("userId"));
// 是否存在
if(userAccounct != null){
logger.info(" userAccount Record is Exist ! ");
// 个人收益是否大于0
if(userAccounct.getPlatformRevenue().compareTo(BigDecimal.ZERO) == 1){
try {
logger.info(" userAccount record incomeAmount is ¥ " + userAccounct.getPlatformRevenue());
// 先修改本地记录 将用户收益归零
int updateUserAccountCount = userAccountDao.setPlatformRevenueByUserId(params.getString("userId"), BigDecimal.ZERO);
if(updateUserAccountCount <= 0){
throw new BusinessException("重置该用户收益金额异常");
}
logger.info(" userAccount record incomeAmount updated to Zero ");
// 将所有属于该用户的订单更新为已折现
int updatePaymentCount = paymentDao.updateIsDiscountedByUserId(params.getString("userId"));
if(updatePaymentCount <= 0){
throw new BusinessException("用户订单信息折现状态更新异常");
}
logger.info(" payment record isdiscounted updated to 'true' ");
// 封装数据 请求openToAccount接口
List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
// 根据终端匹配AppId
if(params.getString("terminal") == "ios" || "ios".equals(params.getString("terminal"))){ // iosAppId
paramsList.add(new BasicNameValuePair("client_id", iosAppId));
}
if(params.getString("terminal") == "android" || "android".equals(params.getString("terminal"))){// andoriApp
paramsList.add(new BasicNameValuePair("client_id", androidAppId));
}
logger.info(" access_token is "+ params.getString("access_token"));
logger.info(" openid "+ params.getString("openId"));
logger.info(" amount "+ userAccounct.getPlatformRevenue().toString());
paramsList.add(new BasicNameValuePair("access_token", params.getString("access_token"))); // access_token移动端传递
paramsList.add(new BasicNameValuePair("openid", params.getString("openId"))); // openId移动端传递
paramsList.add(new BasicNameValuePair("amount",userAccounct.getPlatformRevenue().toString())); // 金额 目前为全部折现
String r = HttpsClientUtils.doPost(openToAccountUrl, paramsList); // 调用开放平台openToAccount接口
JSONObject returnJson = JSONObject.parseObject(r); // openToAccount接口返回 JsonObject
String code = returnJson.getString("code"); // 返回参数 code
String success = returnJson.getString("success"); // 返回参数 success
String msg = returnJson.getString("msg"); // 返回参数 msg
logger.info(" UTOUU Open platform OpenToAcount return----->code:{}-----msg:{}-----success:{}",code,msg,success);
// 根据openToCas接口返回的code码 判断 折现成功
if(code == ConfessionConstants.DISCOUNTED_SUCCESS_CODE || ConfessionConstants.DISCOUNTED_SUCCESS_CODE.equals(code)){
logger.info(" OpenToAcount return code is " + code);// 返回0为成功
// 返回折现成功消息
json = JsonUtils.getSucResultJson(ResultCodeAndMsg.RSP_SUCCESS_CODE, ResultCodeAndMsg.REVENUE_DISCOUNT_SUCCESS_MSG);
return json;
}
// 折现失败
logger.info(" OpenToAcount return code is " + code);
json = JsonUtils.getFailResultJson(ResultCodeAndMsg.RSP_FAIL_CODE, ResultCodeAndMsg.RSP_FAIL_MSG);
throw new BusinessException("个人收益转出失败!");
} catch (Exception e) {
e.printStackTrace();
}
}else{
// 返回没有可以折现的收益金额信息
json = JsonUtils.getFailResultJson(ResultCodeAndMsg.RSP_SUCCESS_CODE, ResultCodeAndMsg.REVENUE_IS_ZREO);
return json;
}
}else{
throw new BusinessException("未找到该用户账户记录异常");
}
}
json = JsonUtils.getFailResultJson(ResultCodeAndMsg.RSP_FAIL_CODE, ResultCodeAndMsg.RSP_FAIL_MSG);
return json;
}
这个是我的一个业务实现类、大致用途是关于金额转出的、其中在代码中还请求了第三方服务的接口
现在我的问题主要有两个:
1:为什么我根据第三方服务返回的结果判断后(当然是我不想得到的结果时)throw 异常 后该service层调用的所有DAO不回滚?
2:如何防止并发呢?我这里是可以多点登录的、即同一个用户在不同的设备上同时执行了该业务类,这时候我就傻了~~~请问该如何处理?
谢谢大家了