177 lines
7.2 KiB
Java
177 lines
7.2 KiB
Java
package com.peanut.modules.taihumed.controller;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
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.entity.*;
|
|
import com.peanut.modules.common.service.*;
|
|
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 javax.transaction.Transactional;
|
|
import java.math.BigDecimal;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
@Slf4j
|
|
@RestController("taihumedAiVip")
|
|
@RequestMapping("taihumed/aiVip")
|
|
public class AiVipController {
|
|
|
|
@Autowired
|
|
private BuyOrderService buyOrderService;
|
|
@Autowired
|
|
private MyUserService myUserService;
|
|
@Autowired
|
|
private TransactionDetailsService transactionDetailsService;
|
|
@Autowired
|
|
private JfTransactionDetailsService jfTransactionDetailsService;
|
|
@Autowired
|
|
private RabbitTemplate rabbitTemplate;
|
|
@Autowired
|
|
private WxpayService wxpayService;
|
|
@Autowired
|
|
private AiChatContentService aiChatContentService;
|
|
@Autowired
|
|
private AiVipLogService aiVipLogService;
|
|
@Autowired
|
|
private AiBuyConfigService aiBuyConfigService;
|
|
|
|
//用户开通aivip信息
|
|
@RequestMapping("/getUserAiVip")
|
|
public R getUserAiVip(){
|
|
int freeCount = 0;
|
|
int flag = 0;//不是vip
|
|
AiVipLog aiVipLog = aiVipLogService.getOne(new LambdaQueryWrapper<AiVipLog>()
|
|
.eq(AiVipLog::getUserId,ShiroUtils.getUId())
|
|
.eq(AiVipLog::getState,0));
|
|
if (aiVipLog!=null){
|
|
List<AiBuyConfig> aiBuyConfig = aiBuyConfigService.list(new LambdaQueryWrapper<AiBuyConfig>()
|
|
.eq(AiBuyConfig::getType,aiVipLog.getType())
|
|
.orderByDesc(AiBuyConfig::getCount));
|
|
//能否升级
|
|
if (aiBuyConfig.size() > 0){
|
|
if (aiVipLog.getCount()<aiBuyConfig.get(0).getCount()){
|
|
flag = 1;//是vip可升级
|
|
}else if (aiVipLog.getCount()==aiBuyConfig.get(0).getCount()){
|
|
flag = 2;//是vip不可升级
|
|
}
|
|
}
|
|
}
|
|
List<AiChatContent> quankeContentList = aiChatContentService.list(new LambdaQueryWrapper<AiChatContent>()
|
|
.eq(AiChatContent::getUserId,ShiroUtils.getUId())
|
|
.like(AiChatContent::getChatAssistantName,"全科")
|
|
.orderByAsc(AiChatContent::getCreateTime)
|
|
.groupBy(AiChatContent::getChatId));
|
|
if (quankeContentList.size()<3){
|
|
freeCount = 3-quankeContentList.size();
|
|
}
|
|
return R.ok().put("aiVipLog",aiVipLog)
|
|
.put("flag",flag)
|
|
.put("freeCount",freeCount);
|
|
}
|
|
|
|
//aivip下单
|
|
@RequestMapping("/placeAiVipOrder")
|
|
@Transactional
|
|
public R placeVipOrder(@RequestBody BuyOrder buyOrder){
|
|
int uid = ShiroUtils.getUId();
|
|
buyOrder.setOrderStatus("0");
|
|
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 = myUserService.getById(buyOrder.getUserId());
|
|
if (usePeanutCoin(user, totalPrice)&&useJfCoin(user,buyOrder.getJfDeduction())) {
|
|
// 更新订单状态
|
|
buyOrderService.updateOrderStatus(user.getId(), buyOrder.getOrderSn(), "2");
|
|
//记录用户虚拟币消费
|
|
if(totalPrice.compareTo(BigDecimal.ZERO)>0){
|
|
transactionDetailsService.recordTransaction(buyOrder, user, totalPrice);
|
|
}
|
|
//记录用户积分消费情况
|
|
if(buyOrder.getJfDeduction()!=null&&buyOrder.getJfDeduction().compareTo(BigDecimal.ZERO) > 0){
|
|
jfTransactionDetailsService.recordJfTransaction(buyOrder, user, buyOrder.getJfDeduction());
|
|
}
|
|
//开通用户aiVip
|
|
aiVipLogService.openAiVip(buyOrder);
|
|
} else {
|
|
rabbitTemplate.convertAndSend(
|
|
DelayQueueConfig.ORDER_TO_BE_PAY_EXCHANGE,
|
|
DelayQueueConfig.ORDER_TO_BE_PAY_ROUTING_KEY,
|
|
buyOrder.getOrderId(),
|
|
messagePostProcessor()
|
|
);
|
|
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(buyOrderEntity.getOrderId());
|
|
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).put("money",buyOrder.getRealMoney());
|
|
}
|
|
|
|
|
|
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));
|
|
myUserService.updateById(user);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private boolean useJfCoin(MyUserEntity user,BigDecimal jf){
|
|
if(jf==null){
|
|
return true;
|
|
}
|
|
if(user.getJf().compareTo(jf)>=0){
|
|
user.setJf(user.getJf().subtract(jf));
|
|
myUserService.updateById(user);
|
|
return true;
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private MessagePostProcessor messagePostProcessor() {
|
|
return message -> {
|
|
//设置有效期30分钟
|
|
message.getMessageProperties().setExpiration(String.valueOf(30 * 60 * 1000));
|
|
return message;
|
|
};
|
|
}
|
|
|
|
}
|