package com.ruoyi.web.controller.api; import cn.hutool.http.HttpException; import com.alipay.api.AlipayApiException; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.common.core.domain.BasePage; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.framework.web.service.TokenService; import com.ruoyi.system.domain.*; import com.ruoyi.system.service.*; import com.ruoyi.system.vx.WeChatConfig; import com.ruoyi.system.vx.WeChatUtil; import com.ruoyi.web.controller.query.InformationQuery; import com.ruoyi.web.controller.query.PayDto; import com.ruoyi.web.controller.tool.AlipayTradePagePay; import com.ruoyi.web.controller.tool.AlipayTradeQuery; import com.ruoyi.web.controller.tool.PayMoneyUtil; import com.ruoyi.web.controller.tool.ResultUtil; import com.wechat.pay.java.core.exception.MalformedMessageException; import com.wechat.pay.java.core.exception.ServiceException; import com.wechat.pay.java.service.partnerpayments.app.model.PrepayResponse; import com.wechat.pay.java.service.payments.nativepay.NativePayService; import com.wechat.pay.java.service.refund.RefundService; import io.lettuce.core.api.async.RedisAclAsyncCommands; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.Arrays; import java.util.List; import java.util.Set; /** *

* 咨询列表 前端控制器 *

* * @author luodangjia * @since 2024-09-19 */ @RestController @RequestMapping("/t-information") public class TInformationController { @Resource private TInformationService informationService; @Resource private TRegionService regionService; @Resource private TTechnicalTitleService tTechnicalTitleService; @Resource private TTitleMajorService majorService; @Resource private TokenService tokenService; @Resource private TOrderService orderService; @Resource private NativePayService nativePayService; @Resource private WeChatConfig weChatConfig; @Resource private RedisCache redisCache; @Resource private RefundService refundService; @Resource private PayMoneyUtil payMoneyUtil; //新增咨询 @ApiOperation(value = "添加",tags = "后台-资料管理") @PostMapping(value = "/add") public R add(@RequestBody TInformation information) { informationService.save(information); return R.ok(); } //修改咨询 @ApiOperation(value = "修改",tags = "后台-资料管理") @PostMapping(value = "/edit") public R edit(@RequestBody TInformation information) { informationService.updateById(information); return R.ok(); } //删除咨询 @ApiOperation(value = "删除",tags = "后台-资料管理") @PostMapping(value = "/deleteByIds") public R deleteByIds(String ids) { informationService.removeByIds(Arrays.asList(ids.split(","))); return R.ok(); } //查询咨询 @ApiOperation(value = "查询",tags = {"后台-资料管理","web资料查询"}) @PostMapping(value = "/list") public R> list(@RequestBody InformationQuery informationQuery) { Long userId = tokenService.getLoginUser().getUserId(); Page page = informationService.lambdaQuery() .like(!StringUtils.isEmpty(informationQuery.getInformationName()), TInformation::getInformationName, informationQuery.getInformationName()) .eq(informationQuery.getRegionId() != null, TInformation::getRegionId, informationQuery.getRegionId()) .eq(informationQuery.getTechnicalId() != null, TInformation::getTechnicalId, informationQuery.getTechnicalId()) .eq(informationQuery.getMajorId() != null, TInformation::getMajorId, informationQuery.getMajorId()) .eq(informationQuery.getLevel() != null, TInformation::getLevel, informationQuery.getLevel()) .eq(informationQuery.getFree()!=null&&informationQuery.getFree() == 1, TInformation::getInformationPrice, BigDecimal.ZERO) .ne(informationQuery.getFree()!=null&&informationQuery.getFree() == 2, TInformation::getInformationPrice, BigDecimal.ZERO) .orderByDesc(TInformation::getCommitteeSort) .page(Page.of(informationQuery.getPageNum(), informationQuery.getPageSize())); Set cacheSet = redisCache.getCacheSet("INFORMATION:" + userId); for (TInformation record : page.getRecords()) { TRegion byId = regionService.getById(record.getRegionId()); record.setRegionName(byId.getProvinceName()+"-"+byId.getName()); TTechnicalTitle byId1 = tTechnicalTitleService.getById(record.getTechnicalId()); TTitleMajor byId2 = majorService.getById(record.getMajorId()); record.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName()); if (cacheSet!=null){ if (cacheSet.contains(record.getId())){ record.setIsCollect(1); }else { record.setIsCollect(0); } }else { record.setIsCollect(0); } } return R.ok(page); } @ApiOperation(value = "检查当前用户是否购买资料",tags = {"web资料查询"}) @PostMapping(value = "/check") public R check(@RequestParam Long id) { Long userId = tokenService.getLoginUser().getUserId(); List list = orderService.lambdaQuery().eq(TOrder::getGoodType, 2).eq(TOrder::getPaymentStatus,2).eq(TOrder::getGoodId,id).eq(TOrder::getUserId, userId).list(); if (list.isEmpty()){ return R.ok(false); }else { return R.ok(true); } } @ApiOperation(value = "下载累计次数",tags = {"web资料查询"}) @PostMapping(value = "/downland") public R downland(@RequestParam Long id) { TInformation byId = informationService.getById(id); byId.setDownlandNum(byId.getDownlandNum()+1); informationService.updateById(byId); return R.ok(); } //新增咨询 @ApiOperation(value = "详情",tags = {"后台-资料管理","web资料查询"}) @PostMapping(value = "/detail") public R detail(@RequestParam Long id) { Long userId = tokenService.getLoginUser().getUserId(); TInformation record = informationService.getById(id); Set cacheSet = redisCache.getCacheSet("INFORMATION:" + userId); TRegion byId = regionService.getById(record.getRegionId()); record.setRegionName(byId.getProvinceName()+"-"+byId.getName()); TTechnicalTitle byId1 = tTechnicalTitleService.getById(record.getTechnicalId()); TTitleMajor byId2 = majorService.getById(record.getMajorId()); record.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName()); if (cacheSet!=null){ if (cacheSet.contains(record.getId())){ record.setIsCollect(1); }else { record.setIsCollect(0); } }else { record.setIsCollect(0); } return R.ok(record); } @ApiOperation(value = "生成订单",tags = {"web资料查询"}) @PostMapping(value = "/create") public R buy( @RequestParam Long id) throws AlipayApiException { Long userId = tokenService.getLoginUser().getUserId(); TInformation byId = informationService.getById(id); String code = "ZL" + WeChatUtil.generateTradeNumber(); TOrder order = new TOrder(); order.setCode(code); order.setUserId(userId); order.setGoodType(2); order.setGoodId(id); order.setOrderAmount(byId.getInformationPrice()); order.setPaymentAmount(byId.getInformationPrice()); if (byId.getInformationPrice().compareTo(new BigDecimal(0))==0){ order.setPaymentStatus(2); } order.setPaymentStatus(1); orderService.save(order); return R.ok(order.getId()); } @ApiOperation(value = "购买资料",tags = {"web资料查询"}) @PostMapping(value = "/buy") public R buy(@RequestParam Integer type, @RequestParam Long orderId) throws AlipayApiException { TOrder byId = orderService.getById(orderId); int i = byId.getPaymentAmount().multiply(BigDecimal.valueOf(100)).intValue(); TOrder byId1 = orderService.getById(orderId); if (type == 1) { Long userId = tokenService.getLoginUser().getUserId(); com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest prepayRequest = new com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest(); prepayRequest.setAppid(weChatConfig.appId); prepayRequest.setMchid(weChatConfig.merchantId); prepayRequest.setOutTradeNo(byId1.getCode()); prepayRequest.setDescription("购买资料"); prepayRequest.setNotifyUrl("http://www.zhipingwang.com.cn:8081/call-back/buy"); com.wechat.pay.java.service.payments.nativepay.model.Amount amount = new com.wechat.pay.java.service.payments.nativepay.model.Amount(); amount.setTotal(i); prepayRequest.setAmount(amount); // 调用下单方法,得到应答 PrepayResponse response; try { com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse prepay = nativePayService.prepay(prepayRequest); //预支付成功,创建预支付订单 PayDto payDto = new PayDto(); payDto.setOrderId(orderId); payDto.setQrCode(prepay.getCodeUrl()); return R.ok(payDto); } catch (HttpException e) { // 发送HTTP请求失败 // log.error("发送HTTP请求失败: {}", e.getHttpRequest()); } catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500 // log.error("服务返回状态异常: {}", e.getResponseBody()); } catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败 // log.error("返回体类型不合法: {}", e.getMessage()); } catch (Exception e) { // log.error("预下单异常: {}", e.getMessage()); } return null; } else { String qrCode = AlipayTradePagePay.pay("购买资料",byId1.getCode(),byId.getPaymentAmount().toString()); PayDto payDto = new PayDto(); payDto.setOrderId(orderId); payDto.setQrCode(qrCode); new Thread(new Runnable() { @Override public void run() { try { int num = 1; int wait = 0; while (num <= 30) { int min = 2000; Thread.sleep(min); Boolean check = AlipayTradeQuery.check(byId.getCode()); if (check){ byId.setPaymentStatus(2); byId.setPaymentType(2); byId.setPayTime(LocalDateTime.now()); orderService.updateById(byId); }else { num++; } } } catch (Exception e) { e.printStackTrace(); } } }).start(); return R.ok(payDto); } } @ApiOperation(value = "收藏资料",tags = {"web-个人中心"}) @PostMapping(value = "/info/list") public R> infolist(@RequestBody BasePage basePage) { Long userId = tokenService.getLoginUser().getUserId(); Set cacheSet = redisCache.getCacheSet("INFORMATION:" + userId); if (!cacheSet.isEmpty()) { Page page = informationService.lambdaQuery().in(TInformation::getId, cacheSet).page(Page.of(basePage.getPageNum(), basePage.getPageSize())); return R.ok(page); }else { return R.ok(new Page<>()); } } @ApiOperation(value = "判断资料",tags = {"web-个人中心"}) @PostMapping(value = "/collect/is") public R collectis(@RequestParam Long informationId) { Long userId = tokenService.getLoginUser().getUserId(); Set cacheSet = redisCache.getCacheSet("INFORMATION:" + userId); if (cacheSet.contains(informationId)){ return R.ok(true); }else { return R.ok(false); } } }