| package com.dy.pipIrrWechat.virtualCard; | 
|   | 
| import com.dy.common.webUtil.BaseResponse; | 
| import com.dy.common.webUtil.BaseResponseUtils; | 
| import com.dy.common.webUtil.QueryResultVo; | 
| import com.dy.pipIrrGlobal.pojoSe.SeVcOperate; | 
| import com.dy.pipIrrGlobal.pojoSe.SeVirtualCard; | 
| import com.dy.pipIrrGlobal.voSe.VoVcRecharge; | 
| import com.dy.pipIrrGlobal.voSe.VoVirtualCard; | 
| import com.dy.pipIrrWechat.result.WechatResultCode; | 
| import com.dy.pipIrrWechat.util.PayHelper; | 
| import com.dy.pipIrrWechat.virtualCard.dto.DtoRegist; | 
| import com.dy.pipIrrWechat.virtualCard.dto.DtoVcRecharge; | 
| import com.dy.pipIrrWechat.virtualCard.enums.LastOperateENUM; | 
| import io.swagger.v3.oas.annotations.tags.Tag; | 
| import jakarta.validation.Valid; | 
| import lombok.RequiredArgsConstructor; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.transaction.annotation.Transactional; | 
| import org.springframework.validation.BindingResult; | 
| import org.springframework.validation.annotation.Validated; | 
| import org.springframework.web.bind.annotation.*; | 
|   | 
| import java.util.Date; | 
| import java.util.List; | 
| import java.util.Objects; | 
| import java.util.Optional; | 
|   | 
| /** | 
|  * @author ZhuBaoMin | 
|  * @date 2024-07-15 9:55 | 
|  * @LastEditTime 2024-07-15 9:55 | 
|  * @Description | 
|  */ | 
|   | 
| @Slf4j | 
| @Tag(name = "虚拟卡管理", description = "虚拟卡管理") | 
| @RestController | 
| @RequestMapping(path="virtual_card") | 
| @RequiredArgsConstructor | 
| @Validated | 
| public class VirtualCardCtrl { | 
|     private final VirtualCardSv virtualCardSv; | 
|     private final PayHelper payHelper; | 
|   | 
|     /** | 
|      * 注册虚拟卡 | 
|      * @param po | 
|      * @param bindingResult | 
|      * @return | 
|      */ | 
|     @PostMapping(path = "add_vc") | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public BaseResponse<Boolean> addVC(@RequestBody @Valid DtoRegist po, BindingResult bindingResult) { | 
|         if(bindingResult != null && bindingResult.hasErrors()){ | 
|             return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
|         } | 
|         Long clientId = po.getClientId(); | 
|   | 
|         // 获取5级行政区划串areaCode | 
|         Long areaCodeL = virtualCardSv.getAreaCodeById(clientId); | 
|         if(areaCodeL == null) { | 
|             return BaseResponseUtils.buildErrorMsg(WechatResultCode.AREA_CODE_MISTAKE.getMessage()); | 
|         } | 
|         String areaCode = String.valueOf(areaCodeL); | 
|   | 
|         /** | 
|          * 根据行政区划串(areaCode)在虚拟卡表中针对虚拟卡编号(vcNum)进行模糊查询 | 
|          * 如果5位顺序号已经达到最大值,提示用户联系系统管理员 | 
|          * 如果5位顺序号未达到最大值,则加1 | 
|          * cardNum为新的卡号 | 
|          */ | 
|         String vcNum = Optional.ofNullable(virtualCardSv.getVcCardNumOfMax(areaCode)).orElse(""); | 
|         if(vcNum != null && vcNum.trim().length() > 0) { | 
|             Integer number = Integer.parseInt(vcNum.substring(12)); | 
|             number = number + 1; | 
|             if(number > 65535) { | 
|                 return BaseResponseUtils.buildErrorMsg(WechatResultCode.CARD_NUMBER_OVERRUN.getMessage()); | 
|             } | 
|             vcNum = vcNum.substring(0, 12) + String.format("%05d", number); | 
|         } else { | 
|             vcNum = areaCode + "00001"; | 
|         } | 
|   | 
|         // 生成虚拟卡记录 | 
|         SeVirtualCard seVirtualCard = new SeVirtualCard(); | 
|         seVirtualCard.setVcNum(Long.parseLong(vcNum)); | 
|         seVirtualCard.setClientId(clientId); | 
|         seVirtualCard.setMoney(0d); | 
|         seVirtualCard.setLastOperate(LastOperateENUM.OPEN_ACCOUNT.getCode()); | 
|         seVirtualCard.setLastOperateTime(new Date()); | 
|         seVirtualCard.setInUse((byte) 0); | 
|         seVirtualCard.setCreateTime(new Date()); | 
|         Long vcId = virtualCardSv.insertVirtualCard(seVirtualCard); | 
|         if(vcId == null) { | 
|             return BaseResponseUtils.buildErrorMsg(WechatResultCode.VC_OPEN_ACCOUNT_FAIL.getMessage()); | 
|         } | 
|   | 
|         // 生成虚拟卡操作记录,注册虚拟卡操作人为农户 | 
|         SeVcOperate seVcOperate = new SeVcOperate(); | 
|         seVcOperate.setVcId(vcId); | 
|         seVcOperate.setClientId(clientId); | 
|         seVcOperate.setOperateType(LastOperateENUM.OPEN_ACCOUNT.getCode()); | 
|         seVcOperate.setOperator(clientId); | 
|         seVcOperate.setOperateTime(new Date()); | 
|         Long vcOperateId = virtualCardSv.insertVcOperate(seVcOperate); | 
|         if(vcOperateId == null) { | 
|             return BaseResponseUtils.buildErrorMsg(WechatResultCode.VC_OPEN_ACCOUNT_FAIL.getMessage()); | 
|         } | 
|   | 
|         return BaseResponseUtils.buildSuccess(true) ; | 
|     } | 
|   | 
|     /** | 
|      * 获取农户全部虚拟卡 | 
|      * @return | 
|      */ | 
|     @GetMapping(path = "/get") | 
|     public BaseResponse<List<VoVirtualCard>> getVCs(Long clientId){ | 
|         try { | 
|             return BaseResponseUtils.buildSuccess(virtualCardSv.getVCs(clientId)); | 
|         } catch (Exception e) { | 
|             log.error("获取支付方式记录异常", e); | 
|             return BaseResponseUtils.buildException(e.getMessage()) ; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 根据虚拟卡ID获取虚拟卡对象 | 
|      * @param vcId | 
|      * @return | 
|      */ | 
|     @GetMapping(path = "/getVcById") | 
|     public BaseResponse<VoVirtualCard> getVcById(@RequestParam Long vcId){ | 
|         try { | 
|             return BaseResponseUtils.buildSuccess(virtualCardSv.getVcById(vcId)); | 
|         } catch (Exception e) { | 
|             log.error("获取支付方式记录异常", e); | 
|             return BaseResponseUtils.buildException(e.getMessage()) ; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 用户申请退款 | 
|      * @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) | 
|     //public BaseResponse<Boolean> addRefund(@RequestBody @Valid DtoRefund po, BindingResult bindingResult){ | 
|     //    if(bindingResult != null && bindingResult.hasErrors()){ | 
|     //        return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
|     //    } | 
|     // | 
|     //    Long virtualId = po.getVirtualId(); | 
|     //    Integer refundAmount = po.getRefundAmount(); | 
|     // | 
|     //    // 根据虚拟卡ID获取虚拟卡对象 | 
|     //    SeVirtualCard seVirtualCard = virtualCardSv.selectVirtuCardById(virtualId); | 
|     //    if(seVirtualCard == null) { | 
|     //        return BaseResponseUtils.buildFail(WechatResultCode.VIRTUAL_CARD_NOT_EXIST.getMessage()); | 
|     //    } | 
|     //    Long clientId = seVirtualCard.getClientId(); | 
|     //    Double money = seVirtualCard.getMoney(); | 
|     // | 
|     //    // 验证退款金额是否大于余额 | 
|     //    if(refundAmount > money) { | 
|     //        return BaseResponseUtils.buildFail(WechatResultCode.REFUND_AMOUNT_CANNOT_GREATER_THAN_MONEY.getMessage()); | 
|     //    } | 
|     // | 
|     //    // 计算消费后余额 | 
|     //    Double afterRefund = money - refundAmount; | 
|     // | 
|     //    SeVcRefund seVcRefund = new SeVcRefund(); | 
|     //    seVcRefund.setVcId(virtualId); | 
|     //    seVcRefund.setClientId(clientId); | 
|     //    seVcRefund.setMoney(money); | 
|     //    seVcRefund.setRefundAmount(refundAmount); | 
|     //    seVcRefund.setAfterRefund(afterRefund); | 
|     //    seVcRefund.setApplicationTime(new Date()); | 
|     //    seVcRefund.setRefundStatus(RefundStateENUM.TO_AUDIT.getCode()); | 
|     // | 
|     //    Long rec = virtualCardSv.addRefund(seVcRefund); | 
|     //    if(rec == 0) { | 
|     //        return BaseResponseUtils.buildFail(WechatResultCode.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) | 
|     //public BaseResponse<Boolean> auditRefund(@RequestBody @Valid DtoAudit po, BindingResult bindingResult) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, IOException, SignatureException, InvalidKeyException { | 
|     //    if(bindingResult != null && bindingResult.hasErrors()){ | 
|     //        return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
|     //    } | 
|     // | 
|     //    // 根据退款ID获取退款对象,并更新审核人、审核时间、审核备注、退款状态字段 | 
|     //    SeVcRefund seVcRefund = virtualCardSv.selectRefundByRefundId(po.getRefundId()); | 
|     //    Long virtualId = seVcRefund.getVcId(); | 
|     //    Integer refundAmount = seVcRefund.getRefundAmount(); | 
|     //    seVcRefund.setAuditor(po.getAuditor()); | 
|     //    seVcRefund.setAuditTime(new Date()); | 
|     //    seVcRefund.setRemarks(po.getRemarks()); | 
|     //    seVcRefund.setRefundStatus(RefundStateENUM.TO_REFUND.getCode()); | 
|     //    Integer rec = virtualCardSv.updateRefund(seVcRefund); | 
|     //    if(rec == 0) { | 
|     //        return BaseResponseUtils.buildFail(WechatResultCode.AUDIT_REFUND_FAIL.getMessage()); | 
|     //    } | 
|     // | 
|     //    // 完成审核后获取待退款订单列表 | 
|     //    List<ToRefund> list_ToRefund = payHelper.getToRefunds(virtualId, refundAmount); | 
|     //    if(list_ToRefund == null || list_ToRefund.size() <=0) | 
|     //        return BaseResponseUtils.buildFail(WechatResultCode.NOT_SUFFICIENT_FUNDS.getMessage()); | 
|     // | 
|     //    //遍历待退款列表 | 
|     //    JSONArray array_ToRefund = (JSONArray) JSON.toJSON(list_ToRefund); | 
|     //    for(int i = 0; i < array_ToRefund.size(); i++) { | 
|     //        JSONObject job_ToRefund = array_ToRefund.getJSONObject(i); | 
|     //        String orderNumber_ToRefund = job_ToRefund.getString("orderNumber"); | 
|     //        Integer refundAmount_ToRefund = job_ToRefund.getInteger("refundAmount"); | 
|     // | 
|     //        // 生成退款分项记录 | 
|     //        SeVcRefundItem seVcRefundItem = new SeVcRefundItem(); | 
|     //        seVcRefundItem.setRefundId(po.getRefundId()); | 
|     //        seVcRefundItem.setOrderNumber(orderNumber_ToRefund); | 
|     //        String refundNumber = virtualCardSv.generateRefundNumber(orderNumber_ToRefund); | 
|     //        seVcRefundItem.setRefundNumber(refundNumber); | 
|     //        seVcRefundItem.setRefundAmount(refundAmount_ToRefund); | 
|     //        seVcRefundItem.setCreateTime(new Date()); | 
|     //        seVcRefundItem.setRefundStatus(RefundItemStateENUM.NO_REFUND.getCode()); | 
|     //        Long refundItemId = virtualCardSv.addRefundItem(seVcRefundItem); | 
|     // | 
|     //        // 调用微信退款申请接口 | 
|     //        Refund refund = new Refund(); | 
|     //        refund.setTradeNo(orderNumber_ToRefund); | 
|     //        refund.setRefundNo(refundNumber); | 
|     //        refund.setRefund(refundAmount_ToRefund); | 
|     //        BaseResponse rep = payHelper.refunds(refund); | 
|     //    } | 
|     // | 
|     //    return BaseResponseUtils.buildSuccess(true) ; | 
|     //} | 
|   | 
|     /** | 
|      * 获取虚拟卡充值记录 | 
|      * @return | 
|      */ | 
|     @GetMapping(path = "/getVcRechargeRecords") | 
|     public BaseResponse<QueryResultVo<List<VoVcRecharge>>> getVcRechargeRecords(DtoVcRecharge dtoVcRecharge){ | 
|         try { | 
|             QueryResultVo<List<VoVcRecharge>> res = virtualCardSv.getVcRechargeRecords(dtoVcRecharge); | 
|             return BaseResponseUtils.buildSuccess(res); | 
|         } catch (Exception e) { | 
|             log.error("获取虚拟卡充值记录异常", e); | 
|             return BaseResponseUtils.buildException(e.getMessage()) ; | 
|         } | 
|     } | 
| } |