| | |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.im.comm.Result; |
| | | import com.ruoyi.im.service.InsuranceProductService; |
| | | import com.ruoyi.im.service.UserKycService; |
| | | import com.ruoyi.im.service.UserPolicyService; |
| | | import com.ruoyi.im.util.PayService; |
| | | import com.ruoyi.im.util.ValidatorUtil; |
| | | import com.ruoyi.system.domain.InsuranceProduct; |
| | | import com.ruoyi.system.domain.UserAccount; |
| | | import com.ruoyi.system.domain.UserPolicy; |
| | | import com.ruoyi.imenum.PaymentMode; |
| | | import com.ruoyi.system.domain.*; |
| | | import com.ruoyi.system.domain.dto.UserPolicyDto; |
| | | import com.ruoyi.system.mapper.SystemConfigurationMapper; |
| | | import com.ruoyi.system.mapper.UserPolicyMapper; |
| | | import com.ruoyi.system.service.FundsLogService; |
| | | import com.ruoyi.system.service.GroupWelcomeConfigService; |
| | | import com.ruoyi.system.service.PaymentRecordService; |
| | | import com.ruoyi.system.service.UserAccountService; |
| | | import org.json.JSONObject; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | UserAccountService userAccountService; |
| | | @Autowired |
| | | private PayService payService; |
| | | @Autowired |
| | | private PaymentRecordService paymentRecordService; |
| | | @Autowired |
| | | private UserKycService userKycService; |
| | | @Autowired |
| | | private GroupWelcomeConfigService groupWelcomeConfigService; |
| | | @Autowired |
| | | private FundsLogService fundsLogService; |
| | | @Autowired |
| | | private SystemConfigurationMapper systemConfigurationMapper; |
| | | @Autowired |
| | | private UserPolicyService userPolicyService; |
| | | |
| | | |
| | | @Override |
| | |
| | | if(!isPhoneValid){ |
| | | return Result.error("手机号格式不正确!"); |
| | | } |
| | | |
| | | SystemConfiguration systemConfiguration = systemConfigurationMapper.selectById(1); |
| | | if(ObjectUtil.isEmpty(systemConfiguration)){ |
| | | return Result.error("无可用支付方式!"); |
| | | } |
| | | long count = count(new LambdaQueryWrapper<UserPolicy>() |
| | | .eq(UserPolicy::getUserId, userAccount.getId()) |
| | | .eq(UserPolicy::getProductId,dto.getProductId()) |
| | | .eq(UserPolicy::getPolicyStatus, UserPolicy.PolicyStatus.PENDING) |
| | | .and(a-> a.eq(UserPolicy::getApprovalStatus, 0) |
| | | .and(a-> a.eq(UserPolicy::getApprovalStatus, 1) |
| | | .or() |
| | | .eq(UserPolicy::getApprovalStatus, 1)) |
| | | .eq(UserPolicy::getPayStatus, 2)) |
| | | |
| | | ); |
| | | if(count > 0){ |
| | | return Result.error("你已申购此保险,请勿重复申购"); |
| | | return Result.error("你已购此保险,请勿重复申请!"); |
| | | } |
| | | InsuranceProduct insuranceProduct = insuranceProductService.getById(dto.getProductId()); |
| | | if(ObjectUtil.isEmpty(insuranceProduct)){ |
| | | return Result.error("该产品停止购买或已下架!"); |
| | | } |
| | | if(userAccount.getBalance().compareTo(insuranceProduct.getPremium()) < 0){ |
| | | |
| | | if(!dto.getPayProductId().equals("0")){ |
| | | |
| | | GroupWelcomeConfig groupWelcomeConfig = groupWelcomeConfigService.getOne(new LambdaQueryWrapper<>(GroupWelcomeConfig.class) |
| | | .eq(GroupWelcomeConfig::getConfigurationName, "IM-BASICS").last(" limit 1")); |
| | | if(ObjectUtil.isNotEmpty(groupWelcomeConfig) && ObjectUtil.isNotEmpty(groupWelcomeConfig.getPaymentSwitch()) && !groupWelcomeConfig.getPaymentSwitch()){ |
| | | return Result.error("无可用支付方式!"); |
| | | } |
| | | |
| | | String orderNo = generateOrderNo(); |
| | | String payOrder = payService.createOrder( |
| | | insuranceProduct.getPremium(), |
| | | orderNo, |
| | | dto.getPayProductId() |
| | | dto.getPayProductId(), |
| | | systemConfiguration.getCallBackUrl() |
| | | ); |
| | | |
| | | Map<String, Object> parse = (Map<String, Object>) JSONUtils.parse(payOrder); |
| | |
| | | |
| | | userAccountService.updateById(userAccount); |
| | | |
| | | UserPolicy userPolicy = new UserPolicy(); |
| | | userPolicy.setAccount(userAccount.getAccount()); |
| | | userPolicy.setProductName(insuranceProduct.getProductName()); |
| | | userPolicy.setUserId(userAccount.getId()); |
| | | userPolicy.setProductId(insuranceProduct.getId()); |
| | | userPolicy.setCoverageAmount(insuranceProduct.getCoverageAmount()); |
| | | userPolicy.setPremium(insuranceProduct.getPremium()); |
| | | userPolicy.setTerm(insuranceProduct.getTerm()); |
| | | userPolicy.setName(dto.getName()); |
| | | userPolicy.setGender(dto.getGender()); |
| | | userPolicy.setPayStatus(0); |
| | | userPolicy.setOrderNo(orderNo); |
| | | userPolicy.setBirthDate(LocalDate.parse(dto.getBirthDate())); |
| | | |
| | | userPolicy.setOccupation(dto.getOccupation()); |
| | | userPolicy.setIdCard(dto.getIdCard()); |
| | | userPolicy.setPhone(dto.getPhone()); |
| | | userPolicy.setPolicyNumber(generatePolicyNumber()); |
| | | userPolicy.setPolicyStatus(UserPolicy.PolicyStatus.PENDING); |
| | | userPolicy.setCreatedAt(new Date()); |
| | | userPolicy.setUpdatedAt(new Date()); |
| | | userPolicy.setApprovalStatus(0); |
| | | userPolicy.setIsLifelong(insuranceProduct.getTerm() == 0 ? 0 : 1); |
| | | UserPolicy one = getOne(new LambdaQueryWrapper<UserPolicy>() |
| | | .eq(UserPolicy::getUserId, userAccount.getId()) |
| | | .eq(UserPolicy::getProductId, dto.getProductId()) |
| | | .eq(UserPolicy::getPolicyStatus, UserPolicy.PolicyStatus.PENDING) |
| | | .eq(UserPolicy::getApprovalStatus, 0) |
| | | .ne(UserPolicy::getPayStatus, 2) |
| | | ); |
| | | PaymentMode payment = PaymentMode.getByCode(dto.getPayProductId()); |
| | | if(ObjectUtil.isEmpty(one)){ |
| | | UserPolicy userPolicy = new UserPolicy(); |
| | | userPolicy.setAccount(userAccount.getAccount()); |
| | | userPolicy.setProductName(insuranceProduct.getProductName()); |
| | | userPolicy.setUserId(userAccount.getId()); |
| | | userPolicy.setProductId(insuranceProduct.getId()); |
| | | userPolicy.setCoverageAmount(insuranceProduct.getCoverageAmount()); |
| | | userPolicy.setPremium(insuranceProduct.getPremium()); |
| | | userPolicy.setTerm(insuranceProduct.getTerm()); |
| | | userPolicy.setName(dto.getName()); |
| | | userPolicy.setGender(dto.getGender()); |
| | | userPolicy.setPayStatus(1); |
| | | userPolicy.setOrderNo(orderNo); |
| | | userPolicy.setBirthDate(LocalDate.parse(dto.getBirthDate())); |
| | | userPolicy.setNumberDays(insuranceProduct.getNumberDays()); |
| | | userPolicy.setOccupation(dto.getOccupation()); |
| | | userPolicy.setIdCard(dto.getIdCard()); |
| | | userPolicy.setPhone(dto.getPhone()); |
| | | userPolicy.setPolicyNumber(generatePolicyNumber()); |
| | | userPolicy.setPolicyStatus(UserPolicy.PolicyStatus.PENDING); |
| | | userPolicy.setCreatedAt(new Date()); |
| | | userPolicy.setUpdatedAt(new Date()); |
| | | userPolicy.setApprovalStatus(0); |
| | | if (payment != null) { |
| | | userPolicy.setModePayment(payment.getMode()); |
| | | } else { |
| | | // 处理未知支付方式 |
| | | userPolicy.setModePayment(0); |
| | | } |
| | | userPolicy.setIsLifelong(insuranceProduct.getTerm() == 0 ? 0 : 1); |
| | | |
| | | save(userPolicy); |
| | | return Result.success(payUrl); |
| | | |
| | | save(userPolicy); |
| | | extracted(userAccount, userPolicy.getId(), orderNo,PaymentRecord.PaymentStatus.PENDING.getCode(),userPolicy.getProductId(),userPolicy.getProductName(),payment.getMode()); |
| | | return Result.success(payUrl); |
| | | }else{ |
| | | extracted(userAccount, one.getId(), orderNo,PaymentRecord.PaymentStatus.PENDING.getCode(),one.getProductId(),one.getProductName(),payment.getMode()); |
| | | return Result.success(payUrl); |
| | | } |
| | | case 401: |
| | | extracted(userAccount, null, orderNo,PaymentRecord.PaymentStatus.FAILED.getCode(),null,"获取支付通道失败",0); |
| | | return Result.error("未授权访问支付系统"); |
| | | |
| | | case 403: |
| | | extracted(userAccount, null, orderNo,PaymentRecord.PaymentStatus.FAILED.getCode(),null,"获取支付通道失败",0); |
| | | return Result.error("禁止访问支付系统"); |
| | | |
| | | case 404: |
| | | extracted(userAccount, null, orderNo,PaymentRecord.PaymentStatus.FAILED.getCode(),null,"获取支付通道失败",0); |
| | | return Result.error("支付接口不存在"); |
| | | |
| | | case 0014: // 注意:0014可能是字符串,需要根据实际情况处理 |
| | | extracted(userAccount, null, orderNo,PaymentRecord.PaymentStatus.FAILED.getCode(),null,"获取支付通道失败",0); |
| | | return Result.error("当前支付不可用,请更换其他支付方式!"); |
| | | |
| | | default: |
| | | extracted(userAccount, null, orderNo,PaymentRecord.PaymentStatus.FAILED.getCode(),null,"获取支付通道失败",0); |
| | | // 其他错误码 |
| | | String message = (String) parse.get("message"); |
| | | if (message != null && !message.isEmpty()) { |
| | |
| | | } |
| | | } |
| | | }else{ |
| | | if(userAccount.getBalance().compareTo(insuranceProduct.getPremium()) < 0){ |
| | | return Result.error("余额不足!"); |
| | | } |
| | | userAccount.setBalance(userAccount.getBalance().subtract(insuranceProduct.getPremium())); |
| | | |
| | | userAccountService.updateById(userAccount); |
| | | |
| | | String orderNo = generateOrderNo(); |
| | | |
| | | UserPolicy userPolicy = new UserPolicy(); |
| | | userPolicy.setAccount(userAccount.getAccount()); |
| | | userPolicy.setProductName(insuranceProduct.getProductName()); |
| | | userPolicy.setUserId(userAccount.getId()); |
| | | userPolicy.setProductId(insuranceProduct.getId()); |
| | | userPolicy.setCoverageAmount(insuranceProduct.getCoverageAmount()); |
| | | userPolicy.setPremium(insuranceProduct.getPremium()); |
| | | userPolicy.setTerm(insuranceProduct.getTerm()); |
| | | userPolicy.setName(dto.getName()); |
| | | userPolicy.setGender(dto.getGender()); |
| | | userPolicy.setPayStatus(2); |
| | | userPolicy.setOrderNo(orderNo); |
| | | userPolicy.setBirthDate(LocalDate.parse(dto.getBirthDate())); |
| | | |
| | | userPolicy.setOccupation(dto.getOccupation()); |
| | | userPolicy.setIdCard(dto.getIdCard()); |
| | | userPolicy.setPhone(dto.getPhone()); |
| | | userPolicy.setPolicyNumber(generatePolicyNumber()); |
| | | userPolicy.setPolicyStatus(UserPolicy.PolicyStatus.PENDING); |
| | | userPolicy.setCreatedAt(new Date()); |
| | | userPolicy.setUpdatedAt(new Date()); |
| | | userPolicy.setApprovalStatus(0); |
| | | userPolicy.setIsLifelong(insuranceProduct.getTerm() == 0 ? 0 : 1); |
| | | UserPolicy one = getOne(new LambdaQueryWrapper<UserPolicy>() |
| | | .eq(UserPolicy::getUserId, userAccount.getId()) |
| | | .eq(UserPolicy::getProductId, dto.getProductId()) |
| | | .eq(UserPolicy::getPolicyStatus, UserPolicy.PolicyStatus.PENDING) |
| | | .eq(UserPolicy::getApprovalStatus, 0) |
| | | .ne(UserPolicy::getPayStatus, 2) |
| | | ); |
| | | if(ObjectUtil.isEmpty(one)){ |
| | | UserPolicy userPolicy = new UserPolicy(); |
| | | userPolicy.setAccount(userAccount.getAccount()); |
| | | userPolicy.setProductName(insuranceProduct.getProductName()); |
| | | userPolicy.setUserId(userAccount.getId()); |
| | | userPolicy.setProductId(insuranceProduct.getId()); |
| | | userPolicy.setCoverageAmount(insuranceProduct.getCoverageAmount()); |
| | | userPolicy.setPremium(insuranceProduct.getPremium()); |
| | | userPolicy.setTerm(insuranceProduct.getTerm()); |
| | | userPolicy.setName(dto.getName()); |
| | | userPolicy.setGender(dto.getGender()); |
| | | userPolicy.setPayStatus(2); |
| | | userPolicy.setOrderNo(orderNo); |
| | | userPolicy.setBirthDate(LocalDate.parse(dto.getBirthDate())); |
| | | userPolicy.setNumberDays(insuranceProduct.getNumberDays()); |
| | | userPolicy.setOccupation(dto.getOccupation()); |
| | | userPolicy.setIdCard(dto.getIdCard()); |
| | | userPolicy.setPhone(dto.getPhone()); |
| | | userPolicy.setPolicyNumber(generatePolicyNumber()); |
| | | userPolicy.setPolicyStatus(UserPolicy.PolicyStatus.PENDING); |
| | | userPolicy.setCreatedAt(new Date()); |
| | | userPolicy.setUpdatedAt(new Date()); |
| | | userPolicy.setApprovalStatus(0); |
| | | userPolicy.setModePayment(3); |
| | | userPolicy.setIsLifelong(insuranceProduct.getTerm() == 0 ? 0 : 1); |
| | | |
| | | save(userPolicy); |
| | | return Result.success(); |
| | | save(userPolicy); |
| | | |
| | | UserKyc userKyc = userKycService.getOne(new LambdaQueryWrapper<UserKyc>() |
| | | .eq(UserKyc::getAccount, userAccount.getAccount()) |
| | | ); |
| | | PaymentRecord paymentRecord = new PaymentRecord(); |
| | | paymentRecord.setUserId(userAccount.getId()); |
| | | paymentRecord.setPaymentStatus(PaymentRecord.PaymentStatus.PAID.getCode()); |
| | | paymentRecord.setProductId(userPolicy.getProductId()); |
| | | paymentRecord.setOrderId(userPolicy.getId()); |
| | | paymentRecord.setPayOrdeNo(orderNo); |
| | | paymentRecord.setAccount(userAccount.getAccount()); |
| | | paymentRecord.setName(userKyc.getName()); |
| | | paymentRecord.setInvitationCode(userAccount.getInvitationCode()); |
| | | paymentRecord.setProductName(insuranceProduct.getProductName()); |
| | | paymentRecord.setModePayment(3); |
| | | paymentRecordService.save(paymentRecord); |
| | | |
| | | fundsLogService.addLog(userAccount.getId(), userAccount.getAccount(), insuranceProduct.getPremium(), OperationType.USER_ORDER); |
| | | // 生成资金日志 |
| | | return Result.success(); |
| | | }else{ |
| | | one.setApprovalStatus(0); |
| | | one.setModePayment(3); |
| | | one.setPayStatus(2); |
| | | updateById(one); |
| | | |
| | | UserKyc userKyc = userKycService.getOne(new LambdaQueryWrapper<UserKyc>() |
| | | .eq(UserKyc::getAccount, userAccount.getAccount()) |
| | | ); |
| | | PaymentRecord paymentRecord = new PaymentRecord(); |
| | | paymentRecord.setUserId(userAccount.getId()); |
| | | paymentRecord.setPaymentStatus(PaymentRecord.PaymentStatus.PAID.getCode()); |
| | | paymentRecord.setProductId(one.getProductId()); |
| | | paymentRecord.setOrderId(one.getId()); |
| | | paymentRecord.setPayOrdeNo(orderNo); |
| | | paymentRecord.setAccount(userAccount.getAccount()); |
| | | paymentRecord.setName(userKyc.getName()); |
| | | paymentRecord.setInvitationCode(userAccount.getInvitationCode()); |
| | | paymentRecord.setProductName(insuranceProduct.getProductName()); |
| | | paymentRecord.setModePayment(3); |
| | | paymentRecordService.save(paymentRecord); |
| | | |
| | | fundsLogService.addLog(userAccount.getId(), userAccount.getAccount(), insuranceProduct.getPremium(), OperationType.USER_ORDER); |
| | | // 生成资金日志 |
| | | return Result.success(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | private void extracted(UserAccount userAccount, Integer userPolicyId, |
| | | String orderNo,Integer payCode,Integer productId,String productName,Integer modePayment) { |
| | | UserKyc userKyc = userKycService.getOne(new LambdaQueryWrapper<UserKyc>() |
| | | .eq(UserKyc::getAccount, userAccount.getAccount()) |
| | | ); |
| | | PaymentRecord paymentRecord = new PaymentRecord(); |
| | | paymentRecord.setUserId(userAccount.getId()); |
| | | paymentRecord.setAccount(userAccount.getAccount()); |
| | | paymentRecord.setName(userKyc.getName()); |
| | | paymentRecord.setInvitationCode(userAccount.getInvitationCode()); |
| | | paymentRecord.setProductName(productName); |
| | | paymentRecord.setPaymentStatus(payCode); |
| | | if(userPolicyId != null){ |
| | | paymentRecord.setOrderId(userPolicyId); |
| | | } |
| | | if(userPolicyId != null){ |
| | | paymentRecord.setOrderId(userPolicyId); |
| | | } |
| | | paymentRecord.setProductId(productId); |
| | | |
| | | paymentRecord.setPayOrdeNo(orderNo); |
| | | paymentRecord.setModePayment(modePayment); |
| | | paymentRecordService.save(paymentRecord); |
| | | } |
| | | |
| | | private final Random random = new Random(); |
| | | /** |
| | | * 生成随机订单号 (格式: 时间戳 + 6位随机数) |