|
|
@@ -10,6 +10,7 @@ import org.springblade.common.enums.OrderType;
|
|
|
import org.springblade.common.enums.ResCode;
|
|
|
import org.springblade.common.enums.SystemConstant;
|
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
|
+import org.springblade.gateway.common_gateway.entity.BillSuccessEvent;
|
|
|
import org.springblade.ldt.bills.entity.BalanceBills;
|
|
|
import org.springblade.ldt.bills.entity.Bills;
|
|
|
import org.springblade.ldt.bills.entity.PointBills;
|
|
|
@@ -28,6 +29,7 @@ import org.springblade.payment.entity.PaymentVO;
|
|
|
import org.springblade.payment.entity.SuccessParams;
|
|
|
import org.springblade.payment.event.UserPayEvent;
|
|
|
import org.springblade.webSocket.WebSocketServer;
|
|
|
+import org.springframework.context.ApplicationEventPublisher;
|
|
|
import org.springframework.context.event.EventListener;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -55,50 +57,63 @@ public class UserPayCallback {
|
|
|
private IMallService mallService;
|
|
|
private IUserChannelPointService userChannelPointService;
|
|
|
private WebSocketServer webSocketServer;
|
|
|
+ private ApplicationEventPublisher applicationEventPublisher;
|
|
|
|
|
|
|
|
|
@EventListener
|
|
|
@Async
|
|
|
@Transactional
|
|
|
- public void PaySuccess(UserPayEvent userPayEvent){
|
|
|
+ public void PaySuccess(UserPayEvent userPayEvent) {
|
|
|
SuccessParams successParams = userPayEvent.getSuccessParams();
|
|
|
PaymentVO paymentVO = PaymentVO.builder().isSuccess(Boolean.FALSE).msg("订单付款异常!").build();
|
|
|
- try{
|
|
|
+ try {
|
|
|
//获取对应交易的积分账单、余额账单、支付账单以及交易用户
|
|
|
LoginUser user = loginUserService.getById(successParams.getUserId());
|
|
|
- Bills bills = Objects.isNull(successParams.getBillId())? null: billsService.getById(successParams.getBillId());
|
|
|
- PointBills pointBills = Objects.isNull(successParams.getPointBillsId())? null: pointBillsService.getById(successParams.getPointBillsId());
|
|
|
- BalanceBills balanceBills = Objects.isNull(successParams.getBalanceBillsId())? null: balanceBillsService.getById(successParams.getBalanceBillsId());
|
|
|
+ Bills bills = successParams.getBills();
|
|
|
+ PointBills pointBills = Objects.isNull(successParams.getPointBillsId()) ? null : pointBillsService.getById(successParams.getPointBillsId());
|
|
|
+ BalanceBills balanceBills = Objects.isNull(successParams.getBalanceBillsId()) ? null : balanceBillsService.getById(successParams.getBalanceBillsId());
|
|
|
|
|
|
//更新各个账单的状态
|
|
|
- if(!Objects.isNull(pointBills)){
|
|
|
- Assert.isTrue(handlePointBills(pointBills,user,successParams.getChannelId()),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ if (!Objects.isNull(pointBills)) {
|
|
|
+ Assert.isTrue(handlePointBills(pointBills, user, successParams.getChannelId()), () -> {
|
|
|
+ throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- if(!Objects.isNull(balanceBills)){
|
|
|
- Assert.isTrue(handleBalanceBills(balanceBills,user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ if (!Objects.isNull(balanceBills)) {
|
|
|
+ Assert.isTrue(handleBalanceBills(balanceBills, user), () -> {
|
|
|
+ throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- if(!Objects.isNull(bills)){
|
|
|
- Assert.isTrue(handleBills(bills,user,pointBills,balanceBills),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- }
|
|
|
+ bills.setPaystatus(SystemConstant.BillPayStatus.付款成功.name());
|
|
|
+
|
|
|
+ //更新订单信息
|
|
|
+ Assert.isTrue(billsService.saveOrUpdate(bills), () -> {
|
|
|
+ throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);
|
|
|
+ });
|
|
|
|
|
|
//修改用户积分余额
|
|
|
- Assert.isTrue(loginUserService.saveOrUpdate(user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ Assert.isTrue(loginUserService.saveOrUpdate(user), () -> {
|
|
|
+ throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);
|
|
|
+ });
|
|
|
|
|
|
//更新商家账户余额
|
|
|
- Assert.isTrue(updateShopBalance(bills,pointBills),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ Assert.isTrue(updateShopBalance(bills, pointBills), () -> {
|
|
|
+ throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);
|
|
|
+ });
|
|
|
+
|
|
|
|
|
|
- //赠送用户积分
|
|
|
- Assert.isTrue(sendPoint(bills,user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
+ //通知处理 赠送用户积分 代理费等
|
|
|
+ applicationEventPublisher.publishEvent(new BillSuccessEvent(successParams));
|
|
|
|
|
|
paymentVO.setIsSuccess(Boolean.TRUE);
|
|
|
paymentVO.setMsg("交易成功!");
|
|
|
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
log.error(e.getMessage());
|
|
|
throw new ServiceException(ResCode.TRADE_ERROR);
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
webSocketServer.sendInfo(
|
|
|
Convert.toStr(successParams.getShopId()),
|
|
|
JSON.toJSONString(paymentVO)
|
|
|
@@ -107,25 +122,20 @@ public class UserPayCallback {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 修改用户积分以及积分账单状态
|
|
|
- * */
|
|
|
- private boolean handlePointBills(PointBills pointBills, LoginUser user,Long channelId) {
|
|
|
+ */
|
|
|
+ private boolean handlePointBills(PointBills pointBills, LoginUser user, Long channelId) {
|
|
|
UserChannelPoint channelPoint = userChannelPointService.getById(channelId);
|
|
|
channelPoint.setAvailable(channelPoint.getAvailable().subtract(pointBills.getPrice()));
|
|
|
user.setChannelPoint(user.getChannelPoint().subtract(pointBills.getPrice()));
|
|
|
pointBills.setStatus(SystemConstant.BillPayStatus.付款成功.name());
|
|
|
-
|
|
|
return pointBillsService.saveOrUpdate(pointBills) && userChannelPointService.saveOrUpdate(channelPoint);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改用户账户余额以及余额账单状态
|
|
|
- * */
|
|
|
+ */
|
|
|
private boolean handleBalanceBills(BalanceBills balanceBills, LoginUser user) {
|
|
|
user.setBalance(user.getBalance().subtract(balanceBills.getPrice()));
|
|
|
balanceBills.setStatus(SystemConstant.BillPayStatus.付款成功.name());
|
|
|
@@ -134,156 +144,38 @@ public class UserPayCallback {
|
|
|
|
|
|
/**
|
|
|
* 修改支付账单状态
|
|
|
- * */
|
|
|
+ */
|
|
|
private boolean handleBills(Bills bills, LoginUser user, PointBills pointBills, BalanceBills balanceBills) {
|
|
|
//补充更新订单信息
|
|
|
- if(!Objects.isNull(pointBills)){
|
|
|
+ if (!Objects.isNull(pointBills)) {
|
|
|
bills.setPointNum(pointBills.getPrice());
|
|
|
}
|
|
|
- if(!Objects.isNull(balanceBills)){
|
|
|
+ if (!Objects.isNull(balanceBills)) {
|
|
|
bills.setBalanceNum(balanceBills.getPrice());
|
|
|
}
|
|
|
bills.setPaystatus(SystemConstant.BillPayStatus.付款成功.name());
|
|
|
- return billsService.saveOrUpdate(bills) && sendPoint(bills,user);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 商场、商家赠送用户积分
|
|
|
- **/
|
|
|
- private boolean sendPoint(Bills bills,LoginUser user){
|
|
|
- if(!Objects.isNull(bills)){
|
|
|
- //赠送用户积分
|
|
|
- BigDecimal payNum = bills.getPrice();
|
|
|
- Shop shop = shopService.getById(bills.getReceiveId());
|
|
|
- Mall mall = mallService.getById(shop.getMallId());
|
|
|
-
|
|
|
- mallSendPoint(mall,user,payNum);
|
|
|
- shopSendPoint(shop,user,payNum);
|
|
|
- }
|
|
|
- return true;
|
|
|
+ return billsService.saveOrUpdate(bills);
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 商家账户余额更新
|
|
|
+ * 商家账户余额更新
|
|
|
*
|
|
|
- * @param bills*/
|
|
|
- private boolean updateShopBalance(Bills bills,PointBills pointBills){
|
|
|
+ * @param bills
|
|
|
+ */
|
|
|
+ private boolean updateShopBalance(Bills bills, PointBills pointBills) {
|
|
|
Shop shop = null;
|
|
|
- if(!Objects.isNull(bills)){
|
|
|
- shop = shopService.getById(bills.getReceiveId());
|
|
|
+ if (!Objects.isNull(bills)) {
|
|
|
+ shop = shopService.getById(bills.getReceiveId());
|
|
|
shop.setBalance(shop.getBalance().add(bills.getPrice()));
|
|
|
}
|
|
|
- if(!Objects.isNull(pointBills)){
|
|
|
- shop = shopService.getById(pointBills.getReceiveId());
|
|
|
+ if (!Objects.isNull(pointBills)) {
|
|
|
+ shop = shopService.getById(pointBills.getReceiveId());
|
|
|
shop.setCharge(shop.getCharge().add(pointBills.getPrice()));
|
|
|
}
|
|
|
- return Objects.isNull(shop)? true: shopService.saveOrUpdate(shop);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 商家赠送积分
|
|
|
- * */
|
|
|
- public boolean shopSendPoint(Shop shop, LoginUser user, BigDecimal payNum){
|
|
|
- //商家若是开启了会员中心则赠送积分
|
|
|
- if(shop.getIsOpenMember()!=0){
|
|
|
- BalanceBills sendBill = new BalanceBills();
|
|
|
- sendBill.setType(OrderType.SHOP_SEND.name());
|
|
|
- sendBill.setStatus(SystemConstant.BillPayStatus.待付款.name());
|
|
|
- sendBill.setTitle("商家赠送积分");
|
|
|
- sendBill.setReceiveId(user.getId());
|
|
|
- sendBill.setPayId(shop.getId());
|
|
|
-
|
|
|
- if(!Objects.isNull(shop)){
|
|
|
- BigDecimal balance = shop.getBalance();
|
|
|
- BigDecimal pointRate = shop.getPointRate();
|
|
|
- BigDecimal sendNum = payNum.multiply(pointRate).multiply(shop.getSendPointUnit());
|
|
|
-
|
|
|
-
|
|
|
- if( sendNum.compareTo(BigDecimal.ONE)>=0 && balance.compareTo(sendNum)>=0 ){
|
|
|
- sendBill.setBillDesc("账户余额不足或赠送积分值小于1");
|
|
|
- }else{
|
|
|
- user.setBalance(user.getBalance().add(sendNum));
|
|
|
- shop.setBalance(shop.getBalance().subtract(sendNum));
|
|
|
- sendBill.setStatus(SystemConstant.BillPayStatus.付款成功.name());
|
|
|
- Assert.isTrue(loginUserService.saveOrUpdate(user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- Assert.isTrue(shopService.saveOrUpdate(shop),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- }
|
|
|
-
|
|
|
- }else{
|
|
|
- sendBill.setBillDesc("商家不存在");
|
|
|
- }
|
|
|
- Assert.isTrue(balanceBillsService.saveOrUpdate(sendBill),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- }
|
|
|
- return true;
|
|
|
+ return Objects.isNull(shop) ? true : shopService.saveOrUpdate(shop);
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- /**
|
|
|
- * 商场赠送积分
|
|
|
- * */
|
|
|
- public boolean mallSendPoint(Mall mall, LoginUser user, BigDecimal payNum){
|
|
|
- //商场若是开启了会员中心则赠送积分
|
|
|
- if(mall.getUseMember()!=0){
|
|
|
- PointBills sendBill = new PointBills();
|
|
|
- sendBill.setType(OrderType.MALL_SEND.name());
|
|
|
- sendBill.setStatus(SystemConstant.BillPayStatus.待付款.name());
|
|
|
- sendBill.setTitle("商场赠送积分");
|
|
|
- sendBill.setReceiveId(user.getId());
|
|
|
- sendBill.setPayId(mall.getId());
|
|
|
-
|
|
|
- if(!Objects.isNull(mall)){
|
|
|
- BigDecimal balance = mall.getBalance();
|
|
|
- BigDecimal pointRate = mall.getPointRate();
|
|
|
- BigDecimal sendNum = payNum.multiply(pointRate).multiply(mall.getSendPointUnnit());
|
|
|
-
|
|
|
- if( sendNum.compareTo(BigDecimal.ONE)>=0 && balance.compareTo(sendNum)>=0 ){
|
|
|
- sendBill.setBillsDesc("账户余额不足或赠送积分值小于1");
|
|
|
- }else{
|
|
|
- user.setChannelPoint(user.getChannelPoint().add(sendNum));
|
|
|
- mall.setBalance(mall.getBalance().subtract(sendNum));
|
|
|
- updateChannelAccount(mall,sendNum,user);
|
|
|
- sendBill.setStatus(SystemConstant.BillPayStatus.付款成功.name());
|
|
|
- Assert.isTrue(loginUserService.saveOrUpdate(user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- Assert.isTrue(updateChannelAccount(mall,sendNum,user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- Assert.isTrue(mallService.saveOrUpdate(mall),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- }
|
|
|
|
|
|
- }else{
|
|
|
- sendBill.setBillsDesc("商场不存在");
|
|
|
- }
|
|
|
- Assert.isTrue(pointBillsService.saveOrUpdate(sendBill),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- Assert.isTrue(loginUserService.saveOrUpdate(user),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- Assert.isTrue(mallService.saveOrUpdate(mall),() ->{throw new ServiceException(ResCode.USER_PAY_CALLBACK_ERROR);});
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 修改渠道积分账户
|
|
|
- * @param mall
|
|
|
- * @param sendNum
|
|
|
- * @param user*/
|
|
|
- private boolean updateChannelAccount(Mall mall, BigDecimal sendNum, LoginUser user){
|
|
|
- UserChannelPoint channelPoint = userChannelPointService.getOne(new QueryWrapper<UserChannelPoint>()
|
|
|
- .lambda()
|
|
|
- .eq(UserChannelPoint::getChannelId, mall.getId())
|
|
|
- .eq(UserChannelPoint::getUserId, user.getId()));
|
|
|
-
|
|
|
- if(Objects.isNull(channelPoint)){
|
|
|
- channelPoint = new UserChannelPoint();
|
|
|
- channelPoint.setChannelId(mall.getId());
|
|
|
- channelPoint.setUserId(user.getId());
|
|
|
- channelPoint.setTotalPoint(BigDecimal.ZERO);
|
|
|
- channelPoint.setAvailable(BigDecimal.ZERO);
|
|
|
- channelPoint.setChannelLogo(mall.getLogo());
|
|
|
- channelPoint.setChannelName(mall.getMallName());
|
|
|
- channelPoint.setPointRate(mall.getPointRate());
|
|
|
- channelPoint.setUsedPoint(BigDecimal.ZERO);
|
|
|
- }
|
|
|
- channelPoint.setAvailable(channelPoint.getAvailable().add(sendNum));
|
|
|
- channelPoint.setTotalPoint(channelPoint.getTotalPoint().add(sendNum));
|
|
|
- return userChannelPointService.saveOrUpdate(channelPoint);
|
|
|
- }
|
|
|
-}
|