|  |  |  | 
|---|
|  |  |  | 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.dto.DtoRecharge; | 
|---|
|  |  |  | 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 org.springframework.validation.annotation.Validated; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.text.DecimalFormat; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.Optional; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 注册电子钱包账户 | 
|---|
|  |  |  | * 注册钱先验证该农户是否已经注册电子钱包 | 
|---|
|  |  |  | * 需要考虑已注册账户的情况 | 
|---|
|  |  |  | * @param clientId | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 电子钱包账户充值 | 
|---|
|  |  |  | * 需要考虑未注册账户的情况 | 
|---|
|  |  |  | * @param po | 
|---|
|  |  |  | * @param bindingResult | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | 
|---|
|  |  |  | @PostMapping(path = "add_recharge", consumes = MediaType.APPLICATION_JSON_VALUE) | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | @SsoAop() | 
|---|
|  |  |  | public BaseResponse<Boolean> addRecharge(@RequestBody @Valid DtoRecharge po, BindingResult bindingResult){ | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 添加充值记录 | 
|---|
|  |  |  | SeWalletRecharge seWalletRecharge = new SeWalletRecharge(); | 
|---|
|  |  |  | seWalletRecharge.setWalletId(wallerId); | 
|---|
|  |  |  | seWalletRecharge.setClientId(clientId); | 
|---|
|  |  |  | seWalletRecharge.setMoney(money); | 
|---|
|  |  |  | seWalletRecharge.setAmount(amount); | 
|---|
|  |  |  | seWalletRecharge.setAfterRecharge(afterRrecharge); | 
|---|
|  |  |  | seWalletRecharge.setRechargeTime(new Date()); | 
|---|
|  |  |  | Long rec = Optional.ofNullable(walletSv.addRecharge(seWalletRecharge)).orElse(0L); | 
|---|
|  |  |  | 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.WALLET_OPEN_ACCOUNT_FAIL.getMessage()); | 
|---|
|  |  |  | 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 = "操作结果:true:成功,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 = "操作结果:true:成功,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 = "操作结果:true:成功,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) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|