| | |
| | | package com.ruoyi.account.controller; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.account.api.model.AppUser; |
| | | import com.ruoyi.account.api.model.BalanceChangeRecord; |
| | | import com.ruoyi.account.service.BalanceChangeRecordService; |
| | | import com.ruoyi.account.service.WalletService; |
| | | import com.ruoyi.account.api.model.BalanceChangeRecordCopy; |
| | | import com.ruoyi.account.api.model.BalancePayment; |
| | | import com.ruoyi.account.service.*; |
| | | import com.ruoyi.account.util.payment.PaymentUtil; |
| | | import com.ruoyi.account.util.payment.model.UniPayCallbackResult; |
| | | import com.ruoyi.account.util.payment.model.UniPayResult; |
| | | import com.ruoyi.account.vo.UserPointDetailVO; |
| | | import com.ruoyi.account.vo.WalletVO; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.web.controller.BaseController; |
| | | import com.ruoyi.common.core.web.page.PageInfo; |
| | | import com.ruoyi.common.security.service.TokenService; |
| | | import com.ruoyi.common.security.utils.SecurityUtils; |
| | | import com.ruoyi.order.feignClient.RemoteOrderGoodsClient; |
| | | import com.ruoyi.order.model.Order; |
| | | import com.ruoyi.other.api.domain.BaseSetting; |
| | | import com.ruoyi.other.api.domain.VipSetting; |
| | | import com.ruoyi.other.api.feignClient.BaseSettingClient; |
| | | import com.ruoyi.system.api.model.LoginUser; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import io.swagger.annotations.ApiParam; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.format.annotation.DateTimeFormat; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | 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 javax.annotation.Resource; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.PrintWriter; |
| | | import java.math.BigDecimal; |
| | | import java.time.*; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Api(tags = "小程序-个人中心-我的钱包") |
| | | @RestController |
| | |
| | | @Resource |
| | | private BalanceChangeRecordService balanceChangeRecordService; |
| | | @Resource |
| | | private BalanceChangeRecordCopyService balanceChangeRecordCopyService; |
| | | @Resource |
| | | private TokenService tokenService; |
| | | @Resource |
| | | private WalletService walletService; |
| | | @Resource |
| | | private RemoteOrderGoodsClient remoteOrderGoodsClient; |
| | | @Resource |
| | | private AppUserService appUserService; |
| | | @Resource |
| | | private VipSettingService vipSettingService; |
| | | @Resource |
| | | private BalancePaymentService balancePaymentService; |
| | | @Resource |
| | | private RedisTemplate redisTemplate; |
| | | @Resource |
| | | private BaseSettingClient baseSettingClient; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 钱包详情 |
| | |
| | | @ApiOperation(value = "钱包详情", notes = "钱包详情") |
| | | public R<WalletVO> detail() { |
| | | LoginUser loginUserApplet = tokenService.getLoginUserApplet(); |
| | | VipSetting vipSetting = vipSettingService.getVipSettingByUserId(loginUserApplet.getUserid()); |
| | | WalletVO walletVO = walletService.getWalletByUserId(loginUserApplet.getUserid()); |
| | | walletVO.setVipWithdrawalFee(vipSetting.getVipWithdrawalFee()); |
| | | return R.ok(walletVO); |
| | | } |
| | | |
| | |
| | | */ |
| | | @ApiOperation(value = "变更明细", notes = "变更明细") |
| | | @GetMapping("change") |
| | | public R<List<BalanceChangeRecord>> change(@ApiParam(value = "变更类型", required = false) |
| | | public R<IPage<BalanceChangeRecordCopy>> change(@ApiParam(value = "变更类型") |
| | | @RequestParam(required = false) Integer changeType, |
| | | @ApiParam(value = "创建时间", required = false) |
| | | @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") |
| | | Integer pageCurr, |
| | | Integer pageSize, |
| | | @ApiParam(value = "创建时间") |
| | | @RequestParam(required = false) |
| | | @DateTimeFormat(pattern = "yyyy-MM-dd") |
| | | LocalDate createTime) { |
| | | Long userId = SecurityUtils.getUserId(); |
| | | |
| | | LocalDateTime localDateTime = null; |
| | | LocalDateTime startTime = null; |
| | | LocalDateTime endTime = null; |
| | | if (createTime != null) { |
| | | LocalTime specificTime = LocalTime.of(0, 0); |
| | | localDateTime = createTime.atTime(specificTime); |
| | | } |
| | | // 将 createTime 设置为当天的开始时间 (00:00) |
| | | startTime = createTime.atStartOfDay(); |
| | | |
| | | return R.ok(balanceChangeRecordService.list(new LambdaQueryWrapper<BalanceChangeRecord>() |
| | | .eq(changeType != null, BalanceChangeRecord::getChangeType, changeType) |
| | | .eq(createTime != null, BalanceChangeRecord::getCreateTime, localDateTime) |
| | | .eq(BalanceChangeRecord::getAppUserId, userId))); |
| | | // 使用 YearMonth 来获取该月的最后一天 |
| | | YearMonth yearMonth = YearMonth.from(createTime); |
| | | LocalDate lastDayOfMonth = yearMonth.atEndOfMonth(); |
| | | |
| | | // 将最后一天转换为 LocalDateTime,并设置为当天的最后一秒 (23:59:59.999) |
| | | endTime = lastDayOfMonth.atTime(LocalTime.MAX); |
| | | } |
| | | Page<BalanceChangeRecordCopy> pageInfo = new Page<>(pageCurr, pageSize); |
| | | IPage<BalanceChangeRecordCopy> change = balanceChangeRecordService.change(pageInfo, changeType, startTime, endTime, userId); |
| | | return R.ok(change); |
| | | } |
| | | |
| | | // /** |
| | | // * 充值 |
| | | // */ |
| | | // @GetMapping("recharge") |
| | | // @ApiOperation(value = "充值", notes = "钱包充值") |
| | | // public AjaxResult recharge(@ApiParam(value = "充值金额", required = true) @RequestParam BigDecimal amount) { |
| | | // Long userId = SecurityUtils.getUserId(); |
| | | // String openId; |
| | | // // 商户号 |
| | | // String partnerTradeNo; |
| | | // // TODO 充值 |
| | | // return AjaxResult.success(); |
| | | // } |
| | | /** |
| | | * 充值 |
| | | */ |
| | | @GetMapping("recharge") |
| | | @ApiOperation(value = "充值", notes = "钱包充值") |
| | | public R<String> recharge(@ApiParam(value = "充值金额", required = true) @RequestParam BigDecimal amount) { |
| | | |
| | | R<BaseSetting> baseSetting = baseSettingClient.getBaseSetting(7); |
| | | BaseSetting data = baseSetting.getData(); |
| | | if (data != null){ |
| | | String content = data.getContent(); |
| | | if (!content.equals("-1")){ |
| | | if (amount.compareTo(new BigDecimal(content)) > 0){ |
| | | return R.fail("充值金额不能大于" + content + "元"); |
| | | } |
| | | } |
| | | } |
| | | LoginUser loginUserApplet = tokenService.getLoginUserApplet(); |
| | | AppUser appUser = appUserService.getById(loginUserApplet.getUserid()); |
| | | BalancePayment balancePayment = new BalancePayment(); |
| | | balancePayment.setAppUserId(appUser.getId()); |
| | | balancePayment.setMoney(amount); |
| | | balancePayment.setStatus(1); |
| | | balancePayment.setCreateTime(LocalDateTime.now()); |
| | | balancePaymentService.save(balancePayment); |
| | | //调起支付 |
| | | UniPayResult uniPayResult = PaymentUtil.uniPay(balancePayment.getId().toString(), amount.doubleValue(), "余额充值", "余额充值", "", |
| | | "/account/wallet/rechargeCallback", appUser.getWxOpenid(), null); |
| | | if(null == uniPayResult || !"100".equals(uniPayResult.getRa_Code())){ |
| | | return R.fail(null == uniPayResult ? "支付失败" : uniPayResult.getRb_CodeMsg()); |
| | | } |
| | | String rc_result = uniPayResult.getRc_Result(); |
| | | JSONObject jsonObject = JSON.parseObject(rc_result); |
| | | //将支付数据添加到redis队列中,便于定时任务去校验是否完成支付,没有完成支付支付,15分钟后关闭订单。 |
| | | long second = LocalDateTime.now().plusMinutes(15).toEpochSecond(ZoneOffset.UTC); |
| | | redisTemplate.opsForZSet().add("BalanceRecharge", balancePayment.getId(), second); |
| | | return R.ok(jsonObject.toJSONString()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取充值上限金额 |
| | | */ |
| | | @GetMapping("rechargeLimit") |
| | | public R<BigDecimal> rechargeLimit(){ |
| | | R<BaseSetting> baseSetting = baseSettingClient.getBaseSetting(7); |
| | | BaseSetting data = baseSetting.getData(); |
| | | if (data != null){ |
| | | String content = data.getContent(); |
| | | return R.ok(new BigDecimal(content)); |
| | | } |
| | | return R.ok(new BigDecimal("-1")); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 充值支付回调 |
| | | * @param callback |
| | | * @param response |
| | | */ |
| | | @ResponseBody |
| | | @GetMapping("/rechargeCallback") |
| | | public void rechargeCallback(UniPayCallbackResult callback, HttpServletResponse response){ |
| | | String r2_orderNo = callback.getR2_OrderNo(); |
| | | BalancePayment balancePayment = balancePaymentService.getById(r2_orderNo); |
| | | if(null == balancePayment || balancePayment.getStatus() != 1){ |
| | | response.setStatus(200); |
| | | PrintWriter out = null; |
| | | try { |
| | | out = response.getWriter(); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | out.println("success"); |
| | | out.flush(); |
| | | out.close(); |
| | | return; |
| | | } |
| | | if("100".equals(callback.getR6_Status())){ |
| | | //添加账户变动流水 |
| | | BigDecimal money = balancePayment.getMoney(); |
| | | AppUser appUser = appUserService.getById(balancePayment.getAppUserId()); |
| | | BigDecimal balance = appUser.getBalance(); |
| | | appUser.setBalance(appUser.getBalance().add(money)); |
| | | appUser.setWithdrawableAmount(appUser.getWithdrawableAmount().add(money)); |
| | | appUser.setTotalRechargeAmount(appUser.getTotalRechargeAmount().add(money)); |
| | | appUserService.updateById(appUser); |
| | | //流水 |
| | | BalanceChangeRecord balanceChangeRecord = new BalanceChangeRecord(); |
| | | balanceChangeRecord.setAppUserId(appUser.getId()); |
| | | balanceChangeRecord.setVipId(appUser.getVipId()); |
| | | balanceChangeRecord.setOrderId(balanceChangeRecord.getId()); |
| | | balanceChangeRecord.setChangeType(1); |
| | | balanceChangeRecord.setChangeAmount(money); |
| | | balanceChangeRecord.setDelFlag(0); |
| | | balanceChangeRecord.setCreateTime(LocalDateTime.now()); |
| | | balanceChangeRecord.setChangeDirection(1); |
| | | balanceChangeRecordService.save(balanceChangeRecord); |
| | | |
| | | balancePayment.setStatus(2); |
| | | balancePayment.setPaymentTime(LocalDateTime.now()); |
| | | balancePayment.setSerialNumber(callback.getR8_BankOrderNo()); |
| | | balancePayment.setBalanceChangeRecordId(balanceChangeRecord.getId()); |
| | | balancePaymentService.updateById(balancePayment); |
| | | |
| | | response.setStatus(200); |
| | | PrintWriter out = null; |
| | | try { |
| | | out = response.getWriter(); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | out.println("success"); |
| | | out.flush(); |
| | | out.close(); |
| | | } |
| | | } |
| | | } |