vip购买相关
This commit is contained in:
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -163,6 +163,8 @@ public class BuyOrder implements Serializable {
|
||||
*/
|
||||
private String isSend;
|
||||
|
||||
private Integer vipBuyConfigId;
|
||||
|
||||
/**
|
||||
* 地址id
|
||||
*/
|
||||
|
||||
@@ -19,4 +19,6 @@ public interface BuyOrderService extends IService<BuyOrder> {
|
||||
Map<String,Object> commonOrderDetail(Map params);
|
||||
|
||||
ExpressQueryResponse commonExpressDetail(Map params);
|
||||
|
||||
void updateOrderStatus(Integer userId, String orderSn, String type);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -144,6 +146,31 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateOrderStatus(Integer userId, String orderSn, String type) {
|
||||
|
||||
// 支付成功 0
|
||||
|
||||
// 发货成功 1
|
||||
|
||||
// 交易成功 2
|
||||
|
||||
// 交易失败 9
|
||||
BuyOrder orderEntity = this.getOne(new QueryWrapper<BuyOrder>().eq("user_id", userId)
|
||||
.eq("order_sn", orderSn));
|
||||
if (type.equals("0")) {
|
||||
orderEntity.setPaymentDate(new Date());
|
||||
orderEntity.setOrderStatus("1");
|
||||
} else if (type.equals("9")) {
|
||||
orderEntity.setOrderStatus("4");
|
||||
} else if (type.equals("1")) {
|
||||
orderEntity.setOrderStatus("2");
|
||||
} else if (type.equals("2")) {
|
||||
orderEntity.setOrderStatus("3");
|
||||
}
|
||||
updateById(orderEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressQueryResponse commonExpressDetail(Map params) {
|
||||
LambdaQueryWrapper<ExpressOrder> expressOrderWrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.peanut.modules.common.to;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class VipOrderDto {
|
||||
|
||||
// private Integer
|
||||
}
|
||||
@@ -66,7 +66,7 @@ public class CourseCatalogueChapterVideoServiceImpl extends ServiceImpl<CourseCa
|
||||
Integer uId = ShiroUtils.getUId();
|
||||
MyUserEntity userEntity = userDao.selectById(uId);
|
||||
//审查课程观看权限
|
||||
if(!courseCheckVip(userEntity,courseCatalogueEntity.getCourseId())&&!courseCheckBuy(userEntity,courseCatalogueEntity)){
|
||||
if(courseCatalogueChapterEntity.getIsAudition()==0&&!courseCheckVip(userEntity,courseCatalogueEntity.getCourseId())&&!courseCheckBuy(userEntity,courseCatalogueEntity)){
|
||||
return R.error(505,"鉴权失败!");
|
||||
}
|
||||
//处理课程开始计时的逻辑
|
||||
@@ -186,7 +186,7 @@ public class CourseCatalogueChapterVideoServiceImpl extends ServiceImpl<CourseCa
|
||||
LambdaQueryWrapper<UserCourseBuyEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(UserCourseBuyEntity::getUserId,u.getId());
|
||||
wrapper.eq(UserCourseBuyEntity::getCatalogueId,courseCatalogueEntity.getId());
|
||||
wrapper.and(r->r.eq(UserCourseBuyEntity::getEndTime,null).or().lt(UserCourseBuyEntity::getEndTime,new Date()));
|
||||
wrapper.and(r->r.eq(UserCourseBuyEntity::getEndTime,null).or().gt(UserCourseBuyEntity::getEndTime,new Date()));
|
||||
Integer integer = userCourseBuyDao.selectCount(wrapper);
|
||||
return integer>0;
|
||||
}
|
||||
|
||||
@@ -59,6 +59,8 @@ public class ShopProductServiceImpl extends ServiceImpl<ShopProductDao, ShopProd
|
||||
private ShopProductCourseDao shopProductCourseDao;
|
||||
@Autowired
|
||||
private CourseCatalogueDao courseCatalogueDao;
|
||||
@Autowired
|
||||
private BuyOrderDao buyOrderDao;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getProductDetail(Integer productId) {
|
||||
@@ -277,6 +279,14 @@ public class ShopProductServiceImpl extends ServiceImpl<ShopProductDao, ShopProd
|
||||
|
||||
@Override
|
||||
public R delShopProduct(int productId){
|
||||
//确定是否可以删除
|
||||
LambdaQueryWrapper<BuyOrder> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(BuyOrder::getProductId,productId);
|
||||
wrapper.in(BuyOrder::getOrderType,Arrays.asList(0,1,2));
|
||||
Integer integer = buyOrderDao.selectCount(wrapper);
|
||||
if(integer>0){
|
||||
return R.error(505,"删除失败,有人下单,且订单未完成");
|
||||
}
|
||||
//清除连带绑定关系
|
||||
shopProductToMedicineLabelDao.delete(new LambdaQueryWrapper<ShopProductToMedicineLabel>().eq(ShopProductToMedicineLabel::getProductId,productId));
|
||||
shopProductToMedicineMarketDao.delete(new LambdaQueryWrapper<ShopProductToMedicineMarket>().eq(ShopProductToMedicineMarket::getProductId,productId));
|
||||
|
||||
Reference in New Issue
Block a user