From 3447c6321d90416d86bd6bd6515589bae3911c65 Mon Sep 17 00:00:00 2001
From: Administrator <zhubaomin>
Date: 星期三, 31 一月 2024 15:36:52 +0800
Subject: [PATCH] 2024-01-31 朱宝民 电子钱包账户消费接口、申请退款接口、审核退款申请接口

---
 pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wallet/WalletCtrl.java |  316 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 270 insertions(+), 46 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wallet/WalletCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wallet/WalletCtrl.java
index d45bf72..780d2dd 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wallet/WalletCtrl.java
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wallet/WalletCtrl.java
@@ -4,24 +4,30 @@
 import com.dy.common.webUtil.BaseResponse;
 import com.dy.common.webUtil.BaseResponseUtils;
 import com.dy.common.webUtil.ResultCodeMsg;
+import com.dy.pipIrrGlobal.pojoSe.SeConsume;
+import com.dy.pipIrrGlobal.pojoSe.SeRefund;
 import com.dy.pipIrrGlobal.pojoSe.SeWallet;
+import com.dy.pipIrrGlobal.pojoSe.SeWalletRecharge;
+import com.dy.pipIrrSell.result.SellResultCode;
+import com.dy.pipIrrSell.wallet.enums.LastOperateENUM;
+import com.dy.pipIrrSell.wallet.enums.RefundStatusENUM;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.media.Content;
 import io.swagger.v3.oas.annotations.media.Schema;
 import io.swagger.v3.oas.annotations.responses.ApiResponse;
 import io.swagger.v3.oas.annotations.responses.ApiResponses;
 import io.swagger.v3.oas.annotations.tags.Tag;
+import jakarta.validation.Valid;
 import jakarta.validation.constraints.NotNull;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.MediaType;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.validation.BindingResult;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
+import java.text.DecimalFormat;
 import java.util.Date;
 import java.util.Optional;
 
@@ -41,6 +47,12 @@
 public class WalletCtrl {
     private final WalletSv walletSv;
 
+    /**
+     * 娉ㄥ唽鐢靛瓙閽卞寘璐︽埛
+     * 闇�瑕佽�冭檻宸叉敞鍐岃处鎴风殑鎯呭喌
+     * @param clientId
+     * @return
+     */
     @Operation(summary = "娉ㄥ唽鐢靛瓙閽卞寘", description = "娉ㄥ唽鐢靛瓙閽卞寘")
     @ApiResponses(value = {
             @ApiResponse(
@@ -50,56 +62,268 @@
                             schema = @Schema(implementation = Boolean.class))}
             )
     })
-    @PostMapping(path = "add")
-    @Transactional(rollbackFor = Exception.class)
+    @PostMapping(path = "add_wallet")
     @SsoAop()
-    //public BaseResponse<Boolean> add(@RequestBody Long clientId){
-    public BaseResponse<Boolean> add(@RequestParam("clientId") @NotNull(message = "鍐滄埛缂栧彿涓嶈兘涓虹┖") Long clientId){
+    public BaseResponse<Boolean> addWallet(@RequestParam("clientId") @NotNull(message = "鍐滄埛缂栧彿涓嶈兘涓虹┖") Long clientId){
         if(clientId == null || clientId < 0) {
-            return BaseResponseUtils.buildFail("鍐滄埛缂栧彿涓嶈兘涓虹┖");
+            return BaseResponseUtils.buildFail(SellResultCode.CLIENT_ID_CANNOT_BE_NULL.getMessage());
+        }
+
+        if(walletSv.getWalletByClientId(clientId) != null) {
+            return BaseResponseUtils.buildFail(SellResultCode.WALLET_ACCOUNT_EXIST.getMessage());
         }
 
         SeWallet seWallet = new SeWallet();
-        seWallet.setClientid(clientId);
-        seWallet.setMoney(0f);
-        seWallet.setCreatedt(new Date());
-        Integer rec = Optional.ofNullable(walletSv.add(seWallet)).orElse(0);
+        seWallet.setClientId(clientId);
+        seWallet.setMoney(0d);
+        seWallet.setLastOperate(LastOperateENUM.OPEN_ACCOUNT.getCode());
+        seWallet.setLastOperateTime(new Date());
+        seWallet.setCreateTime(new Date());
+        Long rec = Optional.ofNullable(walletSv.addWallet(seWallet)).orElse(0L);
         if(rec == 0) {
-            return BaseResponseUtils.buildFail("娉ㄥ唽鐢靛瓙閽卞寘-鐢靛瓙閽卞寘璁板綍鍐欏叆寮傚父");
+            return BaseResponseUtils.buildFail(SellResultCode.WALLET_OPEN_ACCOUNT_FAIL.getMessage());
         }
-
         return BaseResponseUtils.buildSuccess(true) ;
     }
 
-    //@Operation(summary = "鐢靛瓙閽卞寘鍏呭��", description = "鐢靛瓙閽卞寘鍏呭��")
-    //@ApiResponses(value = {
-    //        @ApiResponse(
-    //                responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
-    //                description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�",
-    //                content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
-    //                        schema = @Schema(implementation = Boolean.class))}
-    //        )
-    //})
-    //@PostMapping(path = "addRecharge")
-    //@Transactional(rollbackFor = Exception.class)
-    //@SsoAop()
-    //public BaseResponse<Boolean> addRecharge(@RequestBody @Parameter(description = "form琛ㄥ崟json鏁版嵁", required = true) @Valid DtoWalletRecharge po, @Parameter(hidden = true) BindingResult bindingResult){
-    //    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
-    //    if(bindingResult != null && bindingResult.hasErrors()){
-    //        return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
-    //    }
-    //
-    //
-    //
-    //    SeWallet seWallet = new SeWallet();
-    //    seWallet.setClientid(clientId);
-    //    seWallet.setMoney(0f);
-    //    seWallet.setCreatedt(new Date());
-    //    Integer rec = Optional.ofNullable(walletSv.add(seWallet)).orElse(0);
-    //    if(rec == 0) {
-    //        return BaseResponseUtils.buildFail("娉ㄥ唽鐢靛瓙閽卞寘-鐢靛瓙閽卞寘璁板綍鍐欏叆寮傚父");
-    //    }
-    //
-    //    return BaseResponseUtils.buildSuccess(true) ;
-    //}
+    /**
+     * 鐢靛瓙閽卞寘璐︽埛鍏呭��
+     * 闇�瑕佽�冭檻鏈敞鍐岃处鎴风殑鎯呭喌
+     * @param po
+     * @param bindingResult
+     * @return
+     */
+    @Operation(summary = "鐢靛瓙閽卞寘鍏呭��", description = "鐢靛瓙閽卞寘鍏呭��")
+    @ApiResponses(value = {
+            @ApiResponse(
+                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
+                    description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�",
+                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
+                            schema = @Schema(implementation = Boolean.class))}
+            )
+    })
+    @PostMapping(path = "add_recharge", consumes = MediaType.APPLICATION_JSON_VALUE)
+    @Transactional(rollbackFor = Exception.class)
+    @SsoAop()
+    public BaseResponse<Boolean> addRecharge(@RequestBody @Valid SeWalletRecharge po, BindingResult bindingResult){
+        Long clientId = Optional.ofNullable(po .getClientId()).orElse(0L);
+        Double amount = Optional.ofNullable(po.getAmount()).orElse(0.0);
+
+        // 楠岃瘉璇ュ啘鎴锋槸鍚﹀凡缁忔敞鍐岀數瀛愰挶鍖呰处鎴峰苟鍙栧嚭ID鍙婂綋鍓嶈处鎴蜂綑棰�
+        SeWallet seWallet = walletSv.getWalletByClientId(clientId);
+        if(seWallet == null) {
+            return BaseResponseUtils.buildFail(SellResultCode.NO_ACCOUNT.getMessage());
+        }
+        Long wallerId = Optional.ofNullable(seWallet.getId()).orElse(0L);
+        Double money = Optional.ofNullable(seWallet.getMoney()).orElse(0.0);
+
+        // 璁$畻鍏呭�煎悗浣欓
+        Double afterRrecharge = money + amount;
+
+        // 淇敼鐢靛瓙閽卞寘浣欓銆佹渶鍚庢搷浣滅被鍨嬨�佹渶鍚庢搷浣滄椂闂�
+        seWallet.setMoney(afterRrecharge);
+        seWallet.setLastOperate(LastOperateENUM.RECHARGE.getCode());
+        seWallet.setLastOperateTime(new Date());
+        Integer rec_updateWaller = Optional.ofNullable(walletSv.updateWallet(seWallet)).orElse(0);
+        if(rec_updateWaller == 0) {
+            return BaseResponseUtils.buildFail(SellResultCode.UPDATE_ACCOUNT_FAIL.getMessage());
+        }
+
+        // 娣诲姞鍏呭�艰褰�
+        po.setWalletId(wallerId);
+        po.setClientId(clientId);
+        po.setMoney(money);
+        po.setAmount(amount);
+        po.setAfterRecharge(afterRrecharge);
+        po.setRechargeTime(new Date());
+        Long rec = Optional.ofNullable(walletSv.addRecharge(po)).orElse(0L);
+        if(rec == 0) {
+            return BaseResponseUtils.buildFail(SellResultCode.RECHARGE_FAIL.getMessage());
+        }
+        return BaseResponseUtils.buildSuccess(true) ;
+    }
+
+    /**
+     * 鐢靛瓙閽卞寘娑堣垂
+     * 闇�瑕佽�冭檻鏈敞鍐岃处鎴风殑鎯呭喌銆佷綑棰濅笉瓒崇殑鎯呭喌
+     * @param po
+     * @param bindingResult
+     * @return
+     */
+    @Operation(summary = "鐢靛瓙閽卞寘娑堣垂", description = "鐢靛瓙閽卞寘娑堣垂")
+    @ApiResponses(value = {
+            @ApiResponse(
+                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
+                    description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�",
+                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
+                            schema = @Schema(implementation = Boolean.class))}
+            )
+    })
+    @PostMapping(path = "add_consume", consumes = MediaType.APPLICATION_JSON_VALUE)
+    @Transactional(rollbackFor = Exception.class)
+    @SsoAop()
+    public BaseResponse<Boolean> addConsume(@RequestBody @Valid SeConsume po, BindingResult bindingResult){
+        Long clientId = po .getClientId();
+        Double consumption = po.getConsumption();
+        Date vot = po.getVot();
+        Date vct = po.getVct();
+
+        // 璁$畻寮�鍏抽榾鏃堕棿宸強鐢ㄦ按鏃堕暱
+        Long timeDifference = vct.getTime() - vot.getTime();
+        DecimalFormat df = new DecimalFormat("#.00");
+        Double duration = Double.parseDouble(df.format((float)timeDifference / (1000 * 60 * 60)));
+
+        // 楠岃瘉璇ュ啘鎴锋槸鍚﹀凡缁忔敞鍐岀數瀛愰挶鍖呰处鎴峰苟鍙栧嚭ID鍙婂綋鍓嶈处鎴蜂綑棰�
+        SeWallet seWallet = walletSv.getWalletByClientId(clientId);
+        if(seWallet == null) {
+            return BaseResponseUtils.buildFail(SellResultCode.NO_ACCOUNT.getMessage());
+        }
+        Long wallerId = Optional.ofNullable(seWallet.getId()).orElse(0L);
+        Double money = Optional.ofNullable(seWallet.getMoney()).orElse(0.0);
+
+        if(money < consumption) {
+            return BaseResponseUtils.buildFail(SellResultCode.BALANCE_IS_INSUFFICIENT.getMessage());
+        }
+        // 璁$畻娑堣垂鍚庝綑棰�
+        Double afterConsume = money - consumption;
+
+        // 淇敼鐢靛瓙閽卞寘浣欓銆佹渶鍚庢搷浣滅被鍨嬨�佹渶鍚庢搷浣滄椂闂�
+        seWallet.setMoney(afterConsume);
+        seWallet.setLastOperate(LastOperateENUM.CONSUME.getCode());
+        seWallet.setLastOperateTime(new Date());
+        Integer rec_updateWaller = Optional.ofNullable(walletSv.updateWallet(seWallet)).orElse(0);
+        if(rec_updateWaller == 0) {
+            return BaseResponseUtils.buildFail(SellResultCode.UPDATE_ACCOUNT_FAIL.getMessage());
+        }
+
+        // 娣诲姞娑堣垂璁板綍
+        po.setWalletId(wallerId);
+        po.setMoney(money);
+        po.setAfterConsume(afterConsume);
+        po.setDuration(duration);
+        Long rec = Optional.ofNullable(walletSv.addConsume(po)).orElse(0L);
+        if(rec == 0) {
+            return BaseResponseUtils.buildFail(SellResultCode.CONSUME_FAIL.getMessage());
+        }
+        return BaseResponseUtils.buildSuccess(true) ;
+    }
+
+    /**
+     * 鐢宠閫�娆�
+     * 闇�瑕佽�冭檻鐢宠閫�娆鹃噾棰濆ぇ浜庝綑棰濈殑鎯呭喌
+     * @param po
+     * @param bindingResult
+     * @return
+     */
+    @Operation(summary = "鐢靛瓙閽卞寘鐢宠閫�璐�", description = "鐢靛瓙閽卞寘鐢宠閫�璐�")
+    @ApiResponses(value = {
+            @ApiResponse(
+                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
+                    description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�",
+                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
+                            schema = @Schema(implementation = Boolean.class))}
+            )
+    })
+    @PostMapping(path = "add_refund", consumes = MediaType.APPLICATION_JSON_VALUE)
+    @Transactional(rollbackFor = Exception.class)
+    @SsoAop()
+    public BaseResponse<Boolean> addRefund(@RequestBody @Valid SeRefund po, BindingResult bindingResult){
+        Long clientId = po .getClientId();
+        Double refundAmount = po.getRefundAmount();
+
+        // 楠岃瘉璇ュ啘鎴锋槸鍚﹀凡缁忔敞鍐岀數瀛愰挶鍖呰处鎴峰苟鍙栧嚭ID鍙婂綋鍓嶈处鎴蜂綑棰�
+        SeWallet seWallet = walletSv.getWalletByClientId(clientId);
+        if(seWallet == null) {
+            return BaseResponseUtils.buildFail(SellResultCode.NO_ACCOUNT.getMessage());
+        }
+        Long wallerId = Optional.ofNullable(seWallet.getId()).orElse(0L);
+        Double money = Optional.ofNullable(seWallet.getMoney()).orElse(0.0);
+
+        // 楠岃瘉閫�娆鹃噾棰濇槸鍚﹀ぇ浜庝綑棰�
+        if(money < refundAmount) {
+            return BaseResponseUtils.buildFail(SellResultCode.REFUND_AMOUNT_CANNOT_GREATER_THAN_MONEY.getMessage());
+        }
+        // 璁$畻娑堣垂鍚庝綑棰�
+        Double afterRefund = money - refundAmount;
+
+        // 淇敼鐢靛瓙閽卞寘浣欓銆佹渶鍚庢搷浣滅被鍨嬨�佹渶鍚庢搷浣滄椂闂�
+        seWallet.setMoney(afterRefund);
+        seWallet.setLastOperate(LastOperateENUM.APPLY_REFUND.getCode());
+        seWallet.setLastOperateTime(new Date());
+        Integer rec_updateWaller = Optional.ofNullable(walletSv.updateWallet(seWallet)).orElse(0);
+        if(rec_updateWaller == 0) {
+            return BaseResponseUtils.buildFail(SellResultCode.UPDATE_ACCOUNT_FAIL.getMessage());
+        }
+
+        // 娣诲姞娑堣垂璁板綍
+        po.setWalletId(wallerId);
+        po.setMoney(money);
+        po.setAfterRefund(afterRefund);
+        po.setApplicationTime(new Date());
+        po.setRefundStatus(RefundStatusENUM.NO_REFUND.getCode());
+        Long rec = Optional.ofNullable(walletSv.addRefund(po)).orElse(0L);
+        if(rec == 0) {
+            return BaseResponseUtils.buildFail(SellResultCode.APPLICATION_REFUND_FAIL.getMessage());
+        }
+        return BaseResponseUtils.buildSuccess(true) ;
+    }
+
+    /**
+     * 瀹℃牳閫�娆剧敵璇�
+     * 闇�瑕佽�冭檻璇ョ數瀛愰挶鍖呮病鏈夊緟瀹℃牳鐨勯��娆剧敵璇风殑鎯呭喌
+     * @param po
+     * @param bindingResult
+     * @return
+     */
+    @Operation(summary = "鐢靛瓙閽卞寘瀹℃牳閫�娆剧敵璇�", description = "鐢靛瓙閽卞寘瀹℃牳閫�娆剧敵璇�")
+    @ApiResponses(value = {
+            @ApiResponse(
+                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
+                    description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�",
+                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
+                            schema = @Schema(implementation = Boolean.class))}
+            )
+    })
+    @PostMapping(path = "audit_refund", consumes = MediaType.APPLICATION_JSON_VALUE)
+    @Transactional(rollbackFor = Exception.class)
+    @SsoAop()
+    public BaseResponse<Boolean> auditRefund(@RequestBody @Valid SeRefund po, BindingResult bindingResult){
+        Long clientId = po.getClientId();
+        Byte refundStatus = po.getRefundStatus();
+        Long auditor = po.getAuditor();
+        String remarks = po.getRemarks();
+
+        // 楠岃瘉璇ュ啘鎴锋槸鍚﹀凡缁忔敞鍐岀數瀛愰挶鍖呰处鎴峰苟鍙栧嚭ID鍙婂綋鍓嶈处鎴蜂綑棰�
+        SeWallet seWallet = walletSv.getWalletByClientId(clientId);
+        if(seWallet == null) {
+            return BaseResponseUtils.buildFail(SellResultCode.NO_ACCOUNT.getMessage());
+        }
+        Long wallerId = Optional.ofNullable(seWallet.getId()).orElse(0L);
+        Double money = Optional.ofNullable(seWallet.getMoney()).orElse(0.0);
+
+        // 楠岃瘉璇ョ數瀛愰挶鍖呮槸鍚︽湁寰呭鏍哥殑閫�娆剧敵璇�(鏍规嵁閽卞寘ID鑾峰彇寰呭鏍哥殑閫�娆剧敵璇峰璞�)
+        SeRefund seRefund = walletSv.getRefundByWallerId(wallerId);
+        if(seRefund == null) {
+            return BaseResponseUtils.buildFail(SellResultCode.NO_TO_AUDIT_REFUND.getMessage());
+        }
+
+        // 淇敼鐢靛瓙閽卞寘鏈�鍚庢搷浣滅被鍨嬨�佹渶鍚庢搷浣滄椂闂�
+        seWallet.setLastOperate(LastOperateENUM.AUDIT_REFUND.getCode());
+        seWallet.setLastOperateTime(new Date());
+        Integer rec_updateWaller = Optional.ofNullable(walletSv.updateWallet(seWallet)).orElse(0);
+        if(rec_updateWaller == 0) {
+            return BaseResponseUtils.buildFail(SellResultCode.UPDATE_ACCOUNT_FAIL.getMessage());
+        }
+
+        // 娣诲姞娑堣垂璁板綍
+        seRefund.setRefundStatus(refundStatus);
+        seRefund.setAuditor(auditor);
+        seRefund.setAuditTime(new Date());
+        seRefund.setRemarks(remarks);
+        Integer rec = Optional.ofNullable(walletSv.auditRefund(seRefund)).orElse(0);
+        if(rec == 0) {
+            return BaseResponseUtils.buildFail(SellResultCode.AUDIT_REFUND_FAIL.getMessage());
+        }
+        return BaseResponseUtils.buildSuccess(true) ;
+    }
 }

--
Gitblit v1.8.0