|  |  | 
 |  |  | package com.dy.pipIrrWechat.virtualCard; | 
 |  |  |  | 
 |  |  | import com.dy.common.aop.SsoAop; | 
 |  |  | 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.voIr.VoPlanDetails; | 
 |  |  | import com.dy.pipIrrGlobal.voSe.VoRechargeProfile; | 
 |  |  | import com.dy.pipIrrGlobal.voSe.VoVcRecharge; | 
 |  |  | import com.dy.pipIrrGlobal.voSe.VoVirtualCard; | 
 |  |  | import com.dy.pipIrrWechat.util.PayHelper; | 
 |  |  | import com.dy.pipIrrWechat.virtualCard.enums.LastOperateENUM; | 
 |  |  | import com.dy.pipIrrWechat.result.WechatResultCode; | 
 |  |  | import com.dy.pipIrrWechat.util.PayHelper; | 
 |  |  | import com.dy.pipIrrWechat.virtualCard.dto.Client; | 
 |  |  | import com.dy.pipIrrWechat.virtualCard.dto.DtoCancel; | 
 |  |  | import com.dy.pipIrrWechat.virtualCard.dto.DtoRegist; | 
 |  |  | import com.dy.pipIrrWechat.virtualCard.dto.DtoVcRecharge; | 
 |  |  | import com.dy.pipIrrWechat.virtualCard.enums.LastOperateENUM; | 
 |  |  | import com.dy.pipIrrWechat.virtualCard.qo.QoVcRecharge; | 
 |  |  | 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; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @author ZhuBaoMin | 
 |  |  | 
 |  |  |     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()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         Map map_result = virtualCardSv.addVC(po.getClientId(), 0D); | 
 |  |  |         if(map_result.get("success").equals(false)) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(map_result.get("msg").toString()); | 
 |  |  |         } | 
 |  |  |         return BaseResponseUtils.buildSuccess() ; | 
 |  |  |  | 
 |  |  |         //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.setState((byte)1); | 
 |  |  |         //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) ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据虚拟卡ID注销虚拟卡 | 
 |  |  |      * @param po | 
 |  |  |      * @param bindingResult | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping(path = "cancel") | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public BaseResponse<Boolean> cancelVC(@RequestBody @Valid DtoCancel po, BindingResult bindingResult) { | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |             return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         Long clientId = po.getClientId(); | 
 |  |  |         Long vcId = po.getVcId(); | 
 |  |  |  | 
 |  |  |         // 根据农户ID及虚拟卡Id判断是否存在正常状态(可注销)的虚拟卡 | 
 |  |  |         SeVirtualCard seVirtualCard = virtualCardSv.getVcByClientIdAndVcId(clientId, vcId); | 
 |  |  |         if(seVirtualCard == null) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(WechatResultCode.NO_VC_FIT_THE_BILL.getMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 注销虚拟卡 | 
 |  |  |         Integer rec = virtualCardSv.cancelVc(clientId, vcId); | 
 |  |  |         if(rec == null || rec == 0) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(WechatResultCode.VC_CANCEL_FAIL.getMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 生成虚拟卡操作记录,注册虚拟卡操作人为农户 | 
 |  |  |         SeVcOperate seVcOperate = new SeVcOperate(); | 
 |  |  |         seVcOperate.setVcId(vcId); | 
 |  |  |         seVcOperate.setClientId(clientId); | 
 |  |  |         seVcOperate.setOperateType(LastOperateENUM.CLOSING_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") | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<List<VoVirtualCard>> getVCs(Long clientId){ | 
 |  |  |         try { | 
 |  |  |             List<VoVirtualCard> res = virtualCardSv.getVCs(clientId); | 
 |  |  |             return BaseResponseUtils.buildSuccess(res); | 
 |  |  |             return BaseResponseUtils.buildSuccess(virtualCardSv.getVCs(clientId)); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("获取支付方式记录异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()) ; | 
 |  |  | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @GetMapping(path = "/getVcById") | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<VoVirtualCard> getVcById(@RequestParam Long vcId){ | 
 |  |  |         try { | 
 |  |  |             return BaseResponseUtils.buildSuccess(virtualCardSv.getVcById(vcId)); | 
 |  |  | 
 |  |  |             log.error("获取支付方式记录异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()) ; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 虚拟卡账号注册 | 
 |  |  |      * @param po | 
 |  |  |      * @param bindingResult | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping(path = "add_vc") | 
 |  |  |     @SsoAop() | 
 |  |  |     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 | 
 |  |  |         String areaCode = String.valueOf(virtualCardSv.getAreaCodeById(clientId)); | 
 |  |  |         /** | 
 |  |  |          * 根据行政区划串(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.buildFail(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 rec = virtualCardSv.insertVirtualCard(seVirtualCard); | 
 |  |  |         if(rec == null) { | 
 |  |  |             return BaseResponseUtils.buildFail(WechatResultCode.VC_OPEN_ACCOUNT_FAIL.getMessage()); | 
 |  |  |         } | 
 |  |  |         return BaseResponseUtils.buildSuccess(true) ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |     //}) | 
 |  |  |     //@PostMapping(path = "add_refund", consumes = MediaType.APPLICATION_JSON_VALUE) | 
 |  |  |     //@Transactional(rollbackFor = Exception.class) | 
 |  |  |     //@SsoAop() | 
 |  |  |     //public BaseResponse<Boolean> addRefund(@RequestBody @Valid DtoRefund po, BindingResult bindingResult){ | 
 |  |  |     //    if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |     //        return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  | 
 |  |  |     //}) | 
 |  |  |     //@PostMapping(path = "audit_refund", consumes = MediaType.APPLICATION_JSON_VALUE) | 
 |  |  |     //@Transactional(rollbackFor = Exception.class) | 
 |  |  |     //@SsoAop() | 
 |  |  |     //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()); | 
 |  |  | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @GetMapping(path = "/getVcRechargeRecords") | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<QueryResultVo<List<VoVcRecharge>>> getVcRechargeRecords(DtoVcRecharge dtoVcRecharge){ | 
 |  |  |     public BaseResponse<QueryResultVo<List<VoVcRecharge>>> getVcRechargeRecords(QoVcRecharge dtoVcRecharge){ | 
 |  |  |         try { | 
 |  |  |             if(dtoVcRecharge.getVcId() == null) { | 
 |  |  |                 return BaseResponseUtils.buildErrorMsg("虚拟卡不能为空"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             QueryResultVo<List<VoVcRecharge>> res = virtualCardSv.getVcRechargeRecords(dtoVcRecharge); | 
 |  |  |             return BaseResponseUtils.buildSuccess(res); | 
 |  |  |         } catch (Exception e) { | 
 |  |  | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()) ; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取虚拟卡常用充值金额配置 | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @GetMapping(path = "/gerRechargeProfiles") | 
 |  |  |     public BaseResponse<List<VoRechargeProfile>> gerRechargeProfiles(){ | 
 |  |  |         try { | 
 |  |  |             return BaseResponseUtils.buildSuccess(virtualCardSv.gerRechargeProfiles()); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("获取虚拟卡常用充值金额配置异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()) ; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 绑定虚拟卡到取水口 | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping(path = "linkVcToIntake") | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public BaseResponse<Boolean> linkVcToIntake(@RequestBody @Valid Client client, BindingResult bindingResult) { | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         Long clientId = client.getClientId(); | 
 |  |  |         Map map_result = virtualCardSv.linkVcToIntake(clientId); | 
 |  |  |         if(map_result.get("success").equals(false)) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(map_result.get("msg").toString()); | 
 |  |  |         } | 
 |  |  |         return BaseResponseUtils.buildSuccess() ; | 
 |  |  |     } | 
 |  |  | } |