|
|
|
|
@@ -1,13 +1,30 @@
|
|
|
|
|
package com.peanut.modules.common.controller;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
|
|
import com.peanut.common.utils.R;
|
|
|
|
|
import com.peanut.modules.common.entity.UserVip;
|
|
|
|
|
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.JfTransactionDetailsDao;
|
|
|
|
|
import com.peanut.modules.common.dao.MyUserDao;
|
|
|
|
|
import com.peanut.modules.common.dao.VipBuyConfigDao;
|
|
|
|
|
import com.peanut.modules.common.entity.*;
|
|
|
|
|
import com.peanut.modules.common.service.BuyOrderService;
|
|
|
|
|
import com.peanut.modules.common.service.UserVipService;
|
|
|
|
|
import com.peanut.modules.common.to.VipOrderDto;
|
|
|
|
|
import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo;
|
|
|
|
|
import com.peanut.modules.pay.weChatPay.service.WxpayService;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.amqp.core.MessagePostProcessor;
|
|
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -19,7 +36,21 @@ import java.util.List;
|
|
|
|
|
public class UserVipController {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserVipService vipService;
|
|
|
|
|
private UserVipService userVipService;
|
|
|
|
|
@Autowired
|
|
|
|
|
private BuyOrderService buyOrderService;
|
|
|
|
|
@Autowired
|
|
|
|
|
private WxpayService wxpayService;
|
|
|
|
|
@Autowired
|
|
|
|
|
private RabbitTemplate rabbitTemplate;
|
|
|
|
|
@Autowired
|
|
|
|
|
private MyUserDao myUserDao;
|
|
|
|
|
@Autowired
|
|
|
|
|
private VipBuyConfigDao vipBuyConfigDao;
|
|
|
|
|
@Autowired
|
|
|
|
|
private TransactionDetailsService transactionDetailsService;
|
|
|
|
|
@Autowired
|
|
|
|
|
private JfTransactionDetailsDao jfTransactionDetailsDao;
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/getMyVipHistory")
|
|
|
|
|
public R getMyVipHistory(String userId) {
|
|
|
|
|
@@ -27,8 +58,122 @@ public class UserVipController {
|
|
|
|
|
wrapper.eq(UserVip::getUserId,userId);
|
|
|
|
|
wrapper.orderByAsc(UserVip::getState);
|
|
|
|
|
wrapper.orderByAsc(UserVip::getEndTime);
|
|
|
|
|
List<UserVip> userVips = vipService.list(wrapper);
|
|
|
|
|
List<UserVip> userVips = userVipService.list(wrapper);
|
|
|
|
|
return R.ok().put("result", userVips);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public R placeVipOrder(@RequestBody BuyOrder buyOrder){
|
|
|
|
|
int uid = ShiroUtils.getUId();
|
|
|
|
|
|
|
|
|
|
buyOrder.setOrderStatus("0");
|
|
|
|
|
buyOrder.setOrderType("vip");
|
|
|
|
|
String timeId = IdWorker.getTimeId().substring(0, 32);
|
|
|
|
|
buyOrder.setOrderSn(timeId);
|
|
|
|
|
buyOrder.setUserId(uid);
|
|
|
|
|
if(buyOrder.getCome()==1){
|
|
|
|
|
buyOrder.setAppName("zmzm");
|
|
|
|
|
}
|
|
|
|
|
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 = myUserDao.selectById(buyOrder.getUserId());
|
|
|
|
|
if (usePeanutCoin(user, totalPrice)&&useJfCoin(user,buyOrder.getJfDeduction())) {
|
|
|
|
|
// 更新订单状态
|
|
|
|
|
buyOrderService.updateOrderStatus(user.getId(), buyOrder.getOrderSn(), "2");
|
|
|
|
|
//记录用户虚拟币消费
|
|
|
|
|
if(totalPrice.compareTo(BigDecimal.ZERO)>0){
|
|
|
|
|
recordTransaction(buyOrder, user, totalPrice);
|
|
|
|
|
}
|
|
|
|
|
//记录用户积分消费情况
|
|
|
|
|
if(buyOrder.getJfDeduction()!=null&&buyOrder.getJfDeduction().compareTo(BigDecimal.ZERO) > 0){
|
|
|
|
|
recordJfTransaction(buyOrder, user, buyOrder.getJfDeduction());
|
|
|
|
|
}
|
|
|
|
|
openVipForUser(buyOrder);
|
|
|
|
|
} 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(Integer.valueOf(buyOrderEntity.getProductId()));
|
|
|
|
|
paymentInfo.setTotalAmount(buyOrderEntity.getRealMoney());
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
myUserDao.updateById(user);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void openVipForUser(BuyOrder buyOrder){
|
|
|
|
|
VipBuyConfigEntity vipBuyConfigEntity = vipBuyConfigDao.selectById(buyOrder.getVipBuyConfigId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void recordJfTransaction(BuyOrder buyOrder, MyUserEntity user, BigDecimal jf){
|
|
|
|
|
JfTransactionDetails jfTransactionDetails = new JfTransactionDetails();
|
|
|
|
|
jfTransactionDetails.setUserId(user.getId());
|
|
|
|
|
jfTransactionDetails.setChangeAmount(jf);
|
|
|
|
|
jfTransactionDetails.setActType(0);
|
|
|
|
|
jfTransactionDetails.setUserBalance(user.getJf());
|
|
|
|
|
jfTransactionDetails.setRelationId(buyOrder.getOrderId());
|
|
|
|
|
jfTransactionDetails.setRemark("消费积分抵扣:"+jf.toString()+",订单号:"+buyOrder.getOrderSn());
|
|
|
|
|
jfTransactionDetailsDao.insert(jfTransactionDetails);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean useJfCoin(MyUserEntity user,BigDecimal jf){
|
|
|
|
|
if(jf==null){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if(user.getJf().compareTo(jf)>=0){
|
|
|
|
|
user.setJf(user.getJf().subtract(jf));
|
|
|
|
|
myUserDao.updateById(user);
|
|
|
|
|
return true;
|
|
|
|
|
}else{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void recordTransaction(BuyOrder buyOrder, MyUserEntity user, BigDecimal totalPrice) {
|
|
|
|
|
TransactionDetailsEntity transactionDetailsEntity = new TransactionDetailsEntity();
|
|
|
|
|
transactionDetailsEntity.setRemark("订单编号为 - " + buyOrder.getOrderSn());
|
|
|
|
|
transactionDetailsEntity.setUserId(user.getId());
|
|
|
|
|
transactionDetailsEntity.setUserName(user.getNickname());
|
|
|
|
|
transactionDetailsEntity.setChangeAmount(totalPrice.negate());
|
|
|
|
|
transactionDetailsEntity.setUserBalance(user.getPeanutCoin());
|
|
|
|
|
transactionDetailsEntity.setTel(user.getTel());
|
|
|
|
|
transactionDetailsEntity.setOrderType("购买商品");
|
|
|
|
|
transactionDetailsService.save(transactionDetailsEntity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private MessagePostProcessor messagePostProcessor() {
|
|
|
|
|
return message -> {
|
|
|
|
|
//设置有效期30分钟
|
|
|
|
|
message.getMessageProperties().setExpiration(String.valueOf(30 * 60 * 1000));
|
|
|
|
|
return message;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|