From 035edfa382d349ba66240fbfef68c14c7cfc95d1 Mon Sep 17 00:00:00 2001 From: admin <1101184511@qq.com> Date: 星期五, 08 八月 2025 01:20:21 +0800 Subject: [PATCH] 功能完善 --- src/main/java/com/taoke/autopay/manager/UserCreditExchangeManager.java | 111 +++++++++++++++++++++++++++++++------------------------ 1 files changed, 62 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/taoke/autopay/manager/UserCreditExchangeManager.java b/src/main/java/com/taoke/autopay/manager/UserCreditExchangeManager.java index 293d64d..2343e2a 100644 --- a/src/main/java/com/taoke/autopay/manager/UserCreditExchangeManager.java +++ b/src/main/java/com/taoke/autopay/manager/UserCreditExchangeManager.java @@ -1,10 +1,12 @@ package com.taoke.autopay.manager; +import com.alipay.api.AlipayApiException; import com.taoke.autopay.dao.credit.CreditExchangeRecordMapper; import com.taoke.autopay.dao.credit.ExchangeRateMapper; import com.taoke.autopay.entity.credit.*; import com.taoke.autopay.exception.UserCreditExchangeException; import com.taoke.autopay.service.credit.*; +import com.taoke.autopay.utils.AlipayUtil; import com.taoke.autopay.utils.TimeUtil; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -12,6 +14,7 @@ import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; +import java.util.Arrays; import java.util.Date; import java.util.List; @@ -49,16 +52,15 @@ public Long exchangeCredit(CreditExchangeRecord request) throws UserCreditExchangeException { Long userId = request.getUid(); CreditExchangeRecord.ExchangeType exchangeType = request.getExchangeType(); - if(exchangeType!=CreditExchangeRecord.ExchangeType.FUND_EXCHANGE){ - throw new UserCreditExchangeException(UserCreditExchangeException.CODE_COMMON,"鍙敮鎸佺孩鍖呭厬鎹�"); + if (exchangeType != CreditExchangeRecord.ExchangeType.FUND_EXCHANGE) { + throw new UserCreditExchangeException(UserCreditExchangeException.CODE_COMMON, "鍙敮鎸佺孩鍖呭厬鎹�"); } // 妫�鏌ョ敤鎴风Н鍒嗕綑棰濇槸鍚﹁冻澶� UserCreditBalance balance = userCreditBalanceService.getCreditBalanceByUserId(userId); - if (balance == null || balance.getCreditBalance() <request.getConsumedCredits()) { + if (balance == null || balance.getCreditBalance() < request.getConsumedCredits()) { throw new UserCreditExchangeException(UserCreditExchangeException.CODE_BALANCE_NOT_ENOUGH, "鐢ㄦ埛绉垎涓嶈冻"); } - // 濡傛灉鏄孩鍖呭厬鎹紝鍒ゆ柇鐢ㄦ埛鍏戞崲棰戠巼 @@ -68,7 +70,7 @@ // 鏌ヨ鐢ㄦ埛缁戝畾鐨勬敮浠樺疂璐︽埛 List<UserAlipayBinding> alipayBindings = userAlipayBindingService.getBindingsByUid(userId); - if (alipayBindings == null|| alipayBindings.isEmpty()) { + if (alipayBindings == null || alipayBindings.isEmpty()) { throw new UserCreditExchangeException(UserCreditExchangeException.CODE_NOT_BIND_ALIPAY_ACCOUNT, "鐢ㄦ埛鏈粦瀹氭敮浠樺疂璐︽埛"); } @@ -76,7 +78,7 @@ if (exchangeType == CreditExchangeRecord.ExchangeType.FUND_EXCHANGE) { // 璁$畻鍙厬鎹㈤噾棰濓紙濡傛灉鏄孩鍖呭厬鎹級 - BigDecimal exchangeAmount = calculateExchangeAmount(request.getUid(), request.getConsumedCredits()); + BigDecimal exchangeAmount = calculateExchangeAmount(request.getUid(), request.getConsumedCredits(), true); exchangeRecord.setExchangeValue(exchangeAmount); } @@ -84,7 +86,7 @@ exchangeRecord.setUid(userId); exchangeRecord.setExchangeType(exchangeType); exchangeRecord.setConsumedCredits(request.getConsumedCredits()); - exchangeRecord.setCreditBalance(balance.getCreditBalance()-request.getConsumedCredits()); + exchangeRecord.setCreditBalance(balance.getCreditBalance() - request.getConsumedCredits()); exchangeRecord.setExchangeStatus(CreditExchangeRecord.STATUS_NOT_VERIFY); exchangeRecord.setExchangeStatusDescription("鏈鏍�"); exchangeRecord.setExchangeInfo1(alipayBindings.get(0).getAlipayName()); // 璁剧疆鏀粯瀹濊处鎴� @@ -98,7 +100,7 @@ .creditAmount(request.getConsumedCredits()) .consumptionMethod(UserCreditRecord.ConsumptionMethod.EXCHANGE_RED_PACKET) .direction(UserCreditRecord.DIRECTION_CONSUME) - .identifierId(exchangeRecord.getId()+"") + .identifierId(exchangeRecord.getId() + "") .uid(exchangeRecord.getUid()) .description("绾㈠寘鍏戞崲") .build()); @@ -111,28 +113,36 @@ * @param exchangeRecordId 鍏戞崲璁板綍ID */ @Transactional(rollbackFor = Exception.class) - public void approveExchange(Long exchangeRecordId) throws UserCreditExchangeException{ + public void approveExchange(Long exchangeRecordId) throws UserCreditExchangeException { CreditExchangeRecord exchangeRecord = userCreditExchangeRecordService.getExchangeRecordByIdForUpdate(exchangeRecordId); if (exchangeRecord == null) { throw new UserCreditExchangeException(UserCreditExchangeException.CODE_NOT_BIND_ALIPAY_ACCOUNT, "鍏戞崲璁板綍涓嶅瓨鍦�"); } - if(exchangeRecord.getExchangeStatus() != CreditExchangeRecord.STATUS_NOT_VERIFY){ + if (exchangeRecord.getExchangeStatus() != CreditExchangeRecord.STATUS_NOT_VERIFY) { throw new UserCreditExchangeException(UserCreditExchangeException.CODE_COMMON, "鍏戞崲宸插鐞�"); } + // 濡傛灉鏄孩鍖呭厬鎹紝璋冪敤閫氳繃鍏戞崲閫昏緫锛圱ODO锛� if (exchangeRecord.getExchangeType() == CreditExchangeRecord.ExchangeType.FUND_EXCHANGE) { - // TODO: 璋冪敤閫氳繃鍏戞崲閫昏緫 + try { + AlipayUtil.transfer("credit_exchange_" + exchangeRecordId, exchangeRecord.getExchangeInfo2(), exchangeRecord.getExchangeInfo1(), exchangeRecord.getExchangeValue(), "绾㈠寘鍏戞崲", "绾㈠寘鍏戞崲"); + } catch (AlipayApiException e) { + throw new UserCreditExchangeException(UserCreditExchangeException.CODE_ALIPAY_TRANSFER_FAILED, e.getErrCode() + ":" + e.getErrMsg()); + } catch (AlipayUtil.AlipayTransferException e) { + throw new UserCreditExchangeException(UserCreditExchangeException.CODE_ALIPAY_TRANSFER_FAILED, e.getMessage()); + } } // 鏀瑰彉鍏戞崲璁板綍鐘舵�� userCreditExchangeRecordService.updateExchangeRecord(CreditExchangeRecord.builder() - .id(exchangeRecordId) - .exchangeStatus(CreditExchangeRecord.STATUS_PASSED) - .exchangeStatusDescription("鍏戞崲宸查�氳繃") - .updateTime(new Date()) + .id(exchangeRecordId) + .exchangeStatus(CreditExchangeRecord.STATUS_PASSED) + .exchangeStatusDescription("鍏戞崲宸查�氳繃") + .updateTime(new Date()) .build()); + } /** @@ -141,13 +151,13 @@ * @param exchangeRecordId 鍏戞崲璁板綍ID */ @Transactional(rollbackFor = Exception.class) - public void rejectExchange(Long exchangeRecordId) throws UserCreditExchangeException{ + public void rejectExchange(Long exchangeRecordId) throws UserCreditExchangeException { CreditExchangeRecord exchangeRecord = userCreditExchangeRecordService.getExchangeRecordByIdForUpdate(exchangeRecordId); if (exchangeRecord == null) { throw new UserCreditExchangeException(UserCreditExchangeException.CODE_NOT_BIND_ALIPAY_ACCOUNT, "鍏戞崲璁板綍涓嶅瓨鍦�"); } - if(exchangeRecord.getExchangeStatus() != CreditExchangeRecord.STATUS_NOT_VERIFY){ + if (exchangeRecord.getExchangeStatus() != CreditExchangeRecord.STATUS_NOT_VERIFY) { throw new UserCreditExchangeException(UserCreditExchangeException.CODE_COMMON, "鍏戞崲宸插鐞�"); } @@ -162,7 +172,7 @@ .identifierId(exchangeRecord.getId().toString()) .acquisitionMethod(UserCreditRecord.AcquisitionMethod.EXCHANGE_RETURN) .description("鍏戞崲閫�鍥�") - .createTime(new Date()) + .createTime(new Date()) .updateTime(new Date()).build()); userCreditExchangeRecordService.updateExchangeRecord(CreditExchangeRecord.builder() .id(exchangeRecordId) @@ -177,65 +187,68 @@ * * @param userId 鐢ㄦ埛ID */ - private void checkRedPacketExchangeFrequency(Long userId) throws UserCreditExchangeException{ + private void checkRedPacketExchangeFrequency(Long userId) throws UserCreditExchangeException { //瀹炵幇绾㈠寘鍏戞崲棰戠巼妫�鏌ラ�昏緫 // 鑾峰彇浠婃棩鍏戞崲鐨勬鏁� - long nowTimeStamp=System.currentTimeMillis(); - long count = userCreditExchangeRecordService.countExchangeRecords(CreditExchangeRecordMapper.DaoQuery.builder() - .uid(userId) - .minCreateTime(new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(nowTimeStamp,"yyyyMMdd"),"yyyyMMdd"))) - .maxCreateTime(new Date(nowTimeStamp)) + long nowTimeStamp = System.currentTimeMillis(); + long count = userCreditExchangeRecordService.countExchangeRecords(CreditExchangeRecordMapper.DaoQuery.builder() + .uid(userId) + .minCreateTime(new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(nowTimeStamp, "yyyyMMdd"), "yyyyMMdd"))) + .maxCreateTime(new Date(nowTimeStamp)) .build()); - CreditSetting setting = creditSettingService.getSettingCacheByType(CreditSetting.CreditSettingType.DAILY_EXCHANGE_LIMIT, new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(nowTimeStamp,"yyyyMMddHHmm"),"yyyyMMddHHmm"))); - if(setting==null){ - throw new UserCreditExchangeException(UserCreditExchangeException.CODE_EXCHANGE_FREQUENCY_LIMIT,"鍏戞崲棰戠巼鏈缃�"); + CreditSetting setting = creditSettingService.getSettingCacheByType(CreditSetting.CreditSettingType.DAILY_EXCHANGE_LIMIT, new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(nowTimeStamp, "yyyyMMddHHmm"), "yyyyMMddHHmm"))); + if (setting == null) { + throw new UserCreditExchangeException(UserCreditExchangeException.CODE_EXCHANGE_FREQUENCY_LIMIT, "鍏戞崲棰戠巼鏈缃�"); } - if(count>=Integer.parseInt(setting.getValue())){ + if (count >= Integer.parseInt(setting.getValue())) { throw new UserCreditExchangeException(UserCreditExchangeException.CODE_EXCHANGE_FREQUENCY_LIMIT, String.format("姣忓ぉ鍙兘鍏戞崲%s娆�", setting.getValue())); } } /** * 璁$畻鍏戞崲閲戦 + * * @param uid * @param credit * @return */ - public BigDecimal calculateExchangeAmount(Long uid, int credit) throws UserCreditExchangeException{ - long count = userCreditExchangeRecordService.countExchangeRecords(CreditExchangeRecordMapper.DaoQuery.builder() - .uid(uid).build()); - Date nowDate = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(System.currentTimeMillis(),"yyyyMMddHHmm"),"yyyyMMddHHmm")); + public BigDecimal calculateExchangeAmount(Long uid, int credit, boolean forExchange) throws UserCreditExchangeException { + long count = userCreditExchangeRecordService.countExchangeRecords(CreditExchangeRecordMapper.DaoQuery.builder() + .exchangeStatusList(Arrays.asList(new Integer[]{CreditExchangeRecord.STATUS_PASSED, CreditExchangeRecord.STATUS_NOT_VERIFY})) + .uid(uid).build()); + Date nowDate = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMddHHmm"), "yyyyMMddHHmm")); - BigDecimal money =null; - if(count<=0){ + BigDecimal money = null; + if (count <= 0) { // 鏂颁汉鍏戞崲 - List<ExchangeRate> rates = exchangeRateService.listExchangeRates(ExchangeRateMapper.DaoQuery.builder() - .exchangeType(ExchangeRate.ExchangeRateType.NEW_USER_EXCHANGE) - .maxStartTime(nowDate) - .minEndTime(nowDate) - .count(Integer.MAX_VALUE) + List<ExchangeRate> rates = exchangeRateService.listExchangeRates(ExchangeRateMapper.DaoQuery.builder() + .exchangeType(ExchangeRate.ExchangeRateType.NEW_USER_EXCHANGE) + .maxStartTime(nowDate) + .minEndTime(nowDate) + .count(Integer.MAX_VALUE) .build()); - if(rates.isEmpty()) { - throw new UserCreditExchangeException(UserCreditExchangeException.CODE_COMMON,"鏂颁汉鍏戞崲姹囩巼鏈缃�"); + if (rates.isEmpty()) { + throw new UserCreditExchangeException(UserCreditExchangeException.CODE_COMMON, "鏂颁汉鍏戞崲姹囩巼鏈缃�"); } money = new BigDecimal(credit).multiply(rates.get(0).getRate()).setScale(2, RoundingMode.HALF_UP); - }else{ + } else { // 鑰佺敤鎴峰厬鎹� - List<ExchangeRate> rates = exchangeRateService.listExchangeRates(ExchangeRateMapper.DaoQuery.builder() + List<ExchangeRate> rates = exchangeRateService.listExchangeRates(ExchangeRateMapper.DaoQuery.builder() .exchangeType(ExchangeRate.ExchangeRateType.GENERAL_EXCHANGE) .maxStartTime(nowDate) .minEndTime(nowDate) .count(Integer.MAX_VALUE) .build()); - if(rates.isEmpty()) { - throw new UserCreditExchangeException(UserCreditExchangeException.CODE_COMMON,"鑰佺敤鎴峰厬鎹㈡眹鐜囨湭璁剧疆"); + if (rates.isEmpty()) { + throw new UserCreditExchangeException(UserCreditExchangeException.CODE_COMMON, "鑰佺敤鎴峰厬鎹㈡眹鐜囨湭璁剧疆"); } money = new BigDecimal(credit).multiply(rates.get(0).getRate()).setScale(2, RoundingMode.HALF_UP); } - - CreditSetting setting = creditSettingService.getSettingCacheByType(CreditSetting.CreditSettingType.MINIMUM_EXCHANGE_AMOUNT, nowDate); - if(setting!=null&& new BigDecimal(setting.getValue()).compareTo(money)>0){ - throw new UserCreditExchangeException(UserCreditExchangeException.CODE_COMMON,String.format("鍏戞崲閲戦涓嶈兘浣庝簬%s鍏�",setting.getValue())); + if (forExchange) { + CreditSetting setting = creditSettingService.getSettingCacheByType(CreditSetting.CreditSettingType.MINIMUM_EXCHANGE_AMOUNT, nowDate); + if (setting != null && new BigDecimal(setting.getValue()).compareTo(money) > 0) { + throw new UserCreditExchangeException(UserCreditExchangeException.CODE_COMMON, String.format("鍏戞崲閲戦涓嶈兘浣庝簬%s鍏�", setting.getValue())); + } } return money; } -- Gitblit v1.8.0