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.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; /** *

* 咨询列表 前端控制器 *

* * @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 TLevelService levelService; @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; if(StringUtils.isEmpty(informationQuery.getProvinceName())){ if (informationQuery.getSortType()==1){ 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::getCreateTime) .page(Page.of(informationQuery.getPageNum(), informationQuery.getPageSize())); }else { 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())); } }else { List list; // 查询省份下的市 if(informationQuery.getProvinceName().equals("直辖市")){ List cityList = new ArrayList<>(); cityList.add("北京市"); cityList.add("天津市"); cityList.add("重庆市"); cityList.add("上海市"); list = regionService.lambdaQuery().in(TRegion::getProvinceName, cityList).list(); }else { list = regionService.lambdaQuery().eq(TRegion::getProvinceName, informationQuery.getProvinceName()).list(); } List ids = list.stream().map(TRegion::getId).collect(Collectors.toList()); if (informationQuery.getSortType()==1){ page = informationService.lambdaQuery() .like(!StringUtils.isEmpty(informationQuery.getInformationName()), TInformation::getInformationName, informationQuery.getInformationName()) .in( TInformation::getRegionId, ids) .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::getCreateTime) .page(Page.of(informationQuery.getPageNum(), informationQuery.getPageSize())); }else { page = informationService.lambdaQuery() .like(!StringUtils.isEmpty(informationQuery.getInformationName()), TInformation::getInformationName, informationQuery.getInformationName()) .in( TInformation::getRegionId, ids) .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); List regions = regionService.lambdaQuery().list(); List tTechnicalTitles = tTechnicalTitleService.lambdaQuery().list(); List tTitleMajors = majorService.lambdaQuery().list(); for (TInformation record : page.getRecords()) { TRegion region = regions.stream().filter(e -> e.getId().equals(record.getRegionId())).findFirst().orElse(null); TTechnicalTitle tTechnicalTitle = tTechnicalTitles.stream().filter(e -> e.getId().equals(record.getTechnicalId())).findFirst().orElse(null); TTitleMajor tTitleMajor = tTitleMajors.stream().filter(e -> e.getId().equals(record.getMajorId())).findFirst().orElse(null); record.setRegionName(region.getProvinceName()+"-"+region.getName()); record.setTechnicalName(tTechnicalTitle.getTitileName()+"-"+tTitleMajor.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 information = informationService.getById(id); information.setDownlandNum(information.getDownlandNum()+1); informationService.updateById(information); 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 region = regionService.getById(record.getRegionId()); record.setRegionName(region.getProvinceName()+"-"+region.getName()); TTechnicalTitle technicalTitle = tTechnicalTitleService.getById(record.getTechnicalId()); TTitleMajor titleMajor = majorService.getById(record.getMajorId()); record.setTechnicalName(technicalTitle.getTitileName()+"-"+titleMajor.getMajorName()+"-"+record.getLevel()); 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 information = 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(information.getInformationPrice()); order.setPaymentAmount(information.getInformationPrice()); if (information.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 order = orderService.getById(orderId); int i = order.getPaymentAmount().multiply(BigDecimal.valueOf(100)).intValue(); TOrder tOrder = 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(tOrder.getCode()); prepayRequest.setDescription("购买资料"); prepayRequest.setNotifyUrl("https://0ifzoxq2516g.guyubao.com/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("购买资料",tOrder.getCode(),order.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(order.getCode()); if (check){ order.setPaymentStatus(2); order.setPaymentType(2); order.setPayTime(LocalDateTime.now()); orderService.updateById(order); }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); } } }