Files
nuttyreading-java/src/main/java/com/peanut/modules/common/controller/CourseRelearnController.java
2025-06-13 17:58:54 +08:00

274 lines
14 KiB
Java

package com.peanut.modules.common.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.peanut.common.utils.R;
import com.peanut.common.utils.ShiroUtils;
import com.peanut.config.Constants;
import com.peanut.config.DelayQueueConfig;
import com.peanut.modules.book.service.TransactionDetailsService;
import com.peanut.modules.common.dao.CourseToMedicineDao;
import com.peanut.modules.common.entity.*;
import com.peanut.modules.common.service.BuyOrderService;
import com.peanut.modules.common.service.CourseToPsycheService;
import com.peanut.modules.common.service.JfTransactionDetailsService;
import com.peanut.modules.common.service.MyUserService;
import com.peanut.modules.master.service.*;
import com.peanut.modules.medical.service.CourseService;
import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo;
import com.peanut.modules.pay.weChatPay.service.WxpayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController("commonCourseRelearn")
@RequestMapping("common/courseRelearn")
public class CourseRelearnController {
@Autowired
private UserCourseBuyService userCourseBuyService;
@Autowired
private CourseService courseService;
@Autowired
private CourseCatalogueService courseCatalogueService;
@Autowired
private CourseToMedicalService courseToMedicalService;
@Autowired
private CourseToSociologyService courseToSociologyService;
@Autowired
private CourseToPsycheService courseToPsycheService;
@Autowired
private ShopProductService shopProductService;
@Autowired
private BuyOrderService buyOrderService;
@Autowired
private MyUserService userService;
@Autowired
private WxpayService wxpayService;
@Autowired
private TransactionDetailsService transactionDetailsService;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private JfTransactionDetailsService jfTransactionDetailsService;
//复读订单列表
@RequestMapping("/orderListRelearn")
public R orderListRelearn(@RequestBody Map<String,Object> params) {
MPJLambdaWrapper<BuyOrder> wrapper = new MPJLambdaWrapper<>();
wrapper.selectAll(BuyOrder.class);
wrapper.leftJoin(MyUserEntity.class, MyUserEntity::getId,BuyOrder::getUserId);
wrapper.eq(BuyOrder::getOrderType,"relearn");
if (StringUtils.isNotEmpty(params.get("orderSn").toString())) {
wrapper.eq(BuyOrder::getOrderSn,params.get("orderSn"));
}
if (StringUtils.isNotEmpty(params.get("userInfo").toString())) {
wrapper.and(t->t.like(MyUserEntity::getName,params.get("userInfo")).or().like(MyUserEntity::getNickname,params.get("userInfo"))
.or().like(MyUserEntity::getTel,params.get("userInfo")).or().like(MyUserEntity::getEmail,params.get("userInfo")));
}
wrapper.orderByDesc(BuyOrder::getOrderId);
Page<BuyOrder> buyOrderPage = buyOrderService.page(new Page<BuyOrder>(
Long.parseLong(params.get("page").toString()), Long.parseLong(params.get("limit").toString())), wrapper);
for (BuyOrder buyOrder : buyOrderPage.getRecords()){
buyOrder.setUser(userService.getById(buyOrder.getUserId()));
}
return R.ok().put("buyOrderPage",buyOrderPage);
}
//课程目录是否可复读(已购买并过期)
@RequestMapping("/courseCatalogueCanRelearn")
public R courseCatalogueCanRelearn(@RequestBody Map<String,Object> params){
boolean flag = false;
boolean displayFlag = false;
CourseCatalogueEntity cc = courseCatalogueService.getById(params.get("courseCatalogueId").toString());
if (cc != null) {
CourseEntity courseEntity = courseService.getById(cc.getCourseId());
if (courseEntity != null) {
int mCount = courseToMedicalService.count(new LambdaQueryWrapper<CourseToMedicine>().eq(CourseToMedicine::getCourseId,courseEntity.getId()));
int sCount = courseToSociologyService.count(new LambdaQueryWrapper<CourseToSociologyEntity>().eq(CourseToSociologyEntity::getCourseId,courseEntity.getId()));
int pCount = courseToPsycheService.count(new LambdaQueryWrapper<CourseToPsyche>().eq(CourseToPsyche::getCourseId,courseEntity.getId()));
if (mCount>0||sCount > 0||pCount > 0){
List<UserCourseBuyEntity> list = userCourseBuyService.list(new MPJLambdaWrapper<UserCourseBuyEntity>()
.disableLogicDel()//查询出已删除数据
.eq(UserCourseBuyEntity::getUserId, ShiroUtils.getUId())
.eq(UserCourseBuyEntity::getCatalogueId,params.get("courseCatalogueId")));
if (list.size() > 0) {
flag = true;
for (UserCourseBuyEntity userCourseBuyEntity : list) {
if (userCourseBuyEntity.getDelFlag()==0) {
flag = false;
}
}
}
}else {
displayFlag = true;
}
}
}
return R.ok().put("canRelearn",flag).put("displayFlag",displayFlag);
}
//课程目录复读列表
@RequestMapping("/relearnShopProductList")
public R relearnShopProductList(@RequestBody Map<String,Object> params) {
CourseCatalogueEntity cc = courseCatalogueService.getById(params.get("catalogueId").toString());
if (cc != null) {
CourseEntity courseEntity = courseService.getById(cc.getCourseId());
if (courseEntity != null){
int mCount = courseToMedicalService.count(new LambdaQueryWrapper<CourseToMedicine>().eq(CourseToMedicine::getCourseId,courseEntity.getId()));
int sCount = courseToSociologyService.count(new LambdaQueryWrapper<CourseToSociologyEntity>().eq(CourseToSociologyEntity::getCourseId,courseEntity.getId()));
int pCount = courseToPsycheService.count(new LambdaQueryWrapper<CourseToPsyche>().eq(CourseToPsyche::getCourseId,courseEntity.getId()));
if (mCount>0||sCount > 0||pCount > 0){
MPJLambdaWrapper<ShopProduct> wrapper = new MPJLambdaWrapper<>();
wrapper.leftJoin(ShopProductCourseEntity.class,ShopProductCourseEntity::getProductId,ShopProduct::getProductId);
wrapper.selectAll(ShopProduct.class);
wrapper.eq(ShopProductCourseEntity::getCatalogueId,params.get("catalogueId"));
wrapper.eq(ShopProductCourseEntity::getDays,365);
wrapper.orderByAsc(ShopProduct::getPrice);
List<ShopProduct> shopProducts = shopProductService.list(wrapper);
if (shopProducts.size()>0) {
ShopProduct shopProduct = shopProducts.get(0);
ShopProduct sp1 = new ShopProduct();
sp1.setProductId(shopProduct.getProductId());
sp1.setProductName(shopProduct.getProductName().replace("一年","一月"));
sp1.setProductImages(shopProduct.getProductImages());
BigDecimal sp1Price = shopProduct.getPrice().divide(new BigDecimal(12),0, RoundingMode.DOWN);
sp1.setPrice(sp1Price);
ShopProduct sp2 = new ShopProduct();
sp2.setProductId(shopProduct.getProductId());
sp2.setProductName(shopProduct.getProductName().replace("一年","三月"));
sp2.setProductImages(shopProduct.getProductImages());
BigDecimal sp2Price = shopProduct.getPrice().divide(new BigDecimal(4),0, RoundingMode.DOWN);
sp2Price = sp2Price.multiply(new BigDecimal(0.6));
sp2Price = sp2Price.setScale(0, RoundingMode.DOWN);
sp2.setPrice(sp2Price);
List<ShopProduct> res = new ArrayList<>();
res.add(sp1);
res.add(sp2);
return R.ok().put("productList",res);
}else {
return R.error("未找到一年期商品");
}
}
}
}
return R.ok().put("productList",new ArrayList<>());
}
@RequestMapping("/relearnSave")
@Transactional
public R relearnSave(@RequestBody BuyOrder buyOrder){
int uid = ShiroUtils.getUId();
buyOrder.setOrderStatus("0");
buyOrder.setOrderType("relearn");
String timeId = IdWorker.getTimeId().substring(0, 32);
buyOrder.setOrderSn(timeId);
buyOrder.setUserId(uid);
buyOrderService.save(buyOrder);
BigDecimal totalPrice = buyOrder.getRealMoney();
//虚拟币支付
if (Constants.PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) {
buyOrder.setOrderStatus(Constants.ORDER_STATUS_TO_BE_SHIPPED);
MyUserEntity user = userService.getById(buyOrder.getUserId());
if (usePeanutCoin(user, totalPrice)) {
//处理抵扣积分
if(buyOrder.getJfDeduction().compareTo(BigDecimal.ZERO)>0){
userCoinJf(buyOrder);
}
buyOrder.setSuccessTime(new Date());
// 更新订单状态
buyOrderService.updateOrderStatus(user.getId(), buyOrder.getOrderSn(), "2");
//记录用户虚拟币消费
if(totalPrice.compareTo(BigDecimal.ZERO)>0){
transactionDetailsService.recordTransaction(buyOrder, user, totalPrice);
}
//插入复读记录
userCourseBuyService.addUserCourseBuyRelearn(buyOrder,"虚拟币购买:"+buyOrder.getOrderSn());
} else {
return R.error(500, "余额不足!");
}
}
//微信支付预付款订单
if(Constants.PAYMENT_METHOD_WECHAT_PAY.equals(buyOrder.getPaymentMethod())){
BuyOrder buyOrderEntity = buyOrderService.getBaseMapper().selectOne(new LambdaQueryWrapper<BuyOrder>()
.eq(BuyOrder::getOrderSn, timeId));
WechatPaymentInfo paymentInfo = new WechatPaymentInfo();
paymentInfo.setOrderSn(buyOrderEntity.getOrderSn());
paymentInfo.setBuyOrderId(buyOrder.getOrderId());
paymentInfo.setTotalAmount(buyOrderEntity.getRealMoney());
if (buyOrder.getCome()==2){
paymentInfo.setAppName("wumen");
} else if (buyOrder.getCome()==1) {
paymentInfo.setAppName("zmzm");
} else if (buyOrder.getCome()==3) {
paymentInfo.setAppName("xlkj");
} else if (buyOrder.getCome()==4) {
paymentInfo.setAppName("thyy");
}else {
paymentInfo.setAppName(buyOrder.getAppName());
}
wxpayService.prepay(paymentInfo);
}
rabbitTemplate.convertAndSend(
DelayQueueConfig.ORDER_TO_BE_PAY_EXCHANGE,
DelayQueueConfig.ORDER_TO_BE_PAY_ROUTING_KEY,
buyOrder.getOrderId(),
messagePostProcessor()
);
return R.ok().put("orderSn", timeId).put("money", totalPrice);
}
private boolean usePeanutCoin(MyUserEntity user, BigDecimal totalPrice) {
if (user.getPeanutCoin().compareTo(totalPrice) >= 0) {
if(totalPrice.compareTo(BigDecimal.ZERO)==0){//纯积分支付,虚拟币不用支付
return true;
}
user.setPeanutCoin(user.getPeanutCoin().subtract(totalPrice));
userService.updateById(user);
return true;
}
return false;
}
private MessagePostProcessor messagePostProcessor() {
return message -> {
//设置有效期30分钟
message.getMessageProperties().setExpiration(String.valueOf(30 * 60 * 1000));
return message;
};
}
private void userCoinJf(BuyOrder order){
MyUserEntity userEntity = userService.getById(order.getUserId());
userEntity.setJf(userEntity.getJf().subtract(order.getJfDeduction()));
userService.updateById(userEntity);
JfTransactionDetails jfTransactionDetails = new JfTransactionDetails();
jfTransactionDetails.setUserId(userEntity.getId());
jfTransactionDetails.setChangeAmount(order.getJfDeduction().negate());
jfTransactionDetails.setActType(1);
jfTransactionDetails.setUserBalance(userEntity.getJf());
jfTransactionDetails.setRelationId(order.getOrderId());
jfTransactionDetails.setRemark("消费积分抵扣:"+order.getJfDeduction().toString()+",订单号:"+order.getOrderSn());
jfTransactionDetailsService.save(jfTransactionDetails);
}
}