diff --git a/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppVerifyController.java b/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppVerifyController.java index b9face6..977a3e2 100644 --- a/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppVerifyController.java +++ b/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppVerifyController.java @@ -36,6 +36,7 @@ import javax.annotation.Resource; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.*; +import java.util.stream.Collectors; /** * app接口(需要登录校验的) @@ -1302,17 +1303,36 @@ public class AppVerifyController extends BaseController } /** - * 我的骑行卡(未使用) + * 我的骑行卡(未使用和已过期) */ @GetMapping("/getCouponListByUserId") public AjaxResult getCouponListByUserId(Long userId) { logger.info("根据用户搜索优惠券:【userId="+userId+"】"); - EtCouponUserLog etCouponUserLog = new EtCouponUserLog(); - etCouponUserLog.setUserId(userId); - etCouponUserLog.setStatus(ServiceConstants.COUPON_STATUS_UNUSED); - etCouponUserLog.setStatusList(new String[]{ServiceConstants.COUPON_STATUS_UNUSED, ServiceConstants.COUPON_STATUS_EXPIRED}); - List list = etCouponClaimLogService.selectEtCouponClaimLogList(etCouponUserLog); + List list = etCouponClaimLogService.selectCouponListByUserId(userId); +// // 判空处理,确保list不为null +// if (list != null && !list.isEmpty()) { +// // 将 couponType 为 1 和 2 的记录按类型分组,并各保留一条记录 +// Map> type12Map = list.stream() +// .filter(log -> "1".equals(log.getCouponType()) || "2".equals(log.getCouponType())) +// .collect(Collectors.groupingBy(EtCouponUserLog::getCouponType)); +// +// // 创建结果列表,并添加每个类型的第一条记录 +// List resultList = new ArrayList<>(); +// type12Map.values().forEach(logs -> { +// if (logs != null && !logs.isEmpty()) { +// resultList.add(logs.get(0)); +// } +// }); +// +// // 添加其他类型的记录 +// list.stream() +// .filter(log -> !"1".equals(log.getCouponType()) && !"2".equals(log.getCouponType())) +// .forEach(resultList::add); +// +// // 将结果列表赋值回原列表 +// list = resultList; +// } return success(list); } diff --git a/electripper-system/src/main/java/com/ruoyi/system/domain/EtCouponUserLog.java b/electripper-system/src/main/java/com/ruoyi/system/domain/EtCouponUserLog.java index c7c118e..5ad7338 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/domain/EtCouponUserLog.java +++ b/electripper-system/src/main/java/com/ruoyi/system/domain/EtCouponUserLog.java @@ -43,6 +43,10 @@ public class EtCouponUserLog extends BaseEntity @Excel(name = "优惠券") private Long couponId; + /** 优惠券类型 */ + @Excel(name = "优惠券类型") + private String couponType; + /** 优惠券名称 */ @Excel(name = "优惠券名称") private String couponName; @@ -68,4 +72,8 @@ public class EtCouponUserLog extends BaseEntity @Excel(name = "有效时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss") private Date expirationTime; + /** 可使用次数:0-无限制;其他数字直接显示次数 */ + @Excel(name = "可使用次数") + private int limitNum; + } diff --git a/electripper-system/src/main/java/com/ruoyi/system/mapper/EtCouponClaimLogMapper.java b/electripper-system/src/main/java/com/ruoyi/system/mapper/EtCouponClaimLogMapper.java index f088396..1be2b6f 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/mapper/EtCouponClaimLogMapper.java +++ b/electripper-system/src/main/java/com/ruoyi/system/mapper/EtCouponClaimLogMapper.java @@ -58,4 +58,14 @@ public interface EtCouponClaimLogMapper * @return 结果 */ public int deleteEtCouponClaimLogByLogIds(Long[] claimIds); + + /** + * 我的骑行卡(未使用和已过期) + */ + List selectCouponListByUserId(Long userId); + + /** + * 扣除一次使用次数 + */ + void deductLimitNum(Long logId); } diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/IEtCouponClaimLogService.java b/electripper-system/src/main/java/com/ruoyi/system/service/IEtCouponClaimLogService.java index c87b693..ffa93f2 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/IEtCouponClaimLogService.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/IEtCouponClaimLogService.java @@ -58,4 +58,9 @@ public interface IEtCouponClaimLogService * @return 结果 */ public int deleteEtCouponClaimLogByLogId(Long claimId); + + /** + * 我的骑行卡(未使用和已过期) + */ + List selectCouponListByUserId(Long userId); } diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java index 15843a0..1c997de 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java @@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.annotation.DataScope; import com.ruoyi.common.constant.*; +import com.ruoyi.common.core.domain.entity.AsUser; import com.ruoyi.common.core.domain.entity.SysDept; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.exception.ServiceException; @@ -22,10 +23,7 @@ import com.ruoyi.common.utils.uuid.IdUtils; import com.ruoyi.system.domain.*; import com.ruoyi.system.domain.response.OrderResponse; import com.ruoyi.system.domain.vo.*; -import com.ruoyi.system.mapper.AsDeviceMapper; -import com.ruoyi.system.mapper.EtCommandLogMapper; -import com.ruoyi.system.mapper.EtLocationLogMapper; -import com.ruoyi.system.mapper.EtOrderMapper; +import com.ruoyi.system.mapper.*; import com.ruoyi.system.service.*; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -116,6 +114,16 @@ public class AsDeviceServiceImpl extends ServiceImpl i @Resource private EtLocationLogMapper etLocationLogMapper; + @Resource + private EtCouponMapper etCouponMapper; + + @Resource + private EtCouponClaimLogMapper etCouponClaimLogMapper; + + @Resource + private AsUserMapper asUserMapper; + + @Value(value = "${iot.iotUrl}") private String iotUrl; @@ -191,10 +199,6 @@ public class AsDeviceServiceImpl extends ServiceImpl i EtOperatingArea etOperatingArea; if (ObjectUtil.isNotNull(areaId) && areaId!=0) { etOperatingArea = etOperatingAreaService.selectEtOperatingAreaByAreaId(areaId); - }else{ - throw new ServiceException("【selectAsDeviceBySn】区域信息不存在"); - } - if(ObjectUtil.isNotNull(etOperatingArea)){ asDevice.setAreaName(etOperatingArea.getAreaName()); } EtModel model = etModelService.selectEtModelByModelId(asDevice.getModelId()); @@ -1789,6 +1793,11 @@ public class AsDeviceServiceImpl extends ServiceImpl i } /** 3. 计算订单费用,保存订单总金额*/ order = calculateOrderFee(order,isInParkingArea); + /** 4. 优惠券计算价格 + * order中有couponId则有使用优惠券 */ + if(ObjectUtil.isNotNull(order.getCouponId())){ + order = calculateCoupon(order); + } /** 6.计算行车距离*/ String tripRouteStr = order.getTripRouteStr(); if(StrUtil.isNotBlank(tripRouteStr)){ @@ -1904,6 +1913,74 @@ public class AsDeviceServiceImpl extends ServiceImpl i //// } // } + /** + * 使用优惠券后的价格 + * 1. 先判断是否过期?并且是否是未使用的状态 + * 2. 再判断是哪种类型的优惠券, + * 如果是 + * 时间卡:骑行价格直接改成0 + * 贵宾卡:所有价格改成0 + * 折扣卡:总金额 * 折扣比例 次数减一 + * 抵用券:总金额 - 抵用金额 次数减一 + */ + private EtOrder calculateCoupon(EtOrder order) { + Long couponId = order.getCouponId(); + EtCoupon etCoupon = etCouponMapper.selectEtCouponByCouponId(couponId); + Long userId = order.getUserId(); + AsUser asUser = asUserMapper.selectUserById(userId); + + EtCouponUserLog etCouponUserLog = new EtCouponUserLog(); + etCouponUserLog.setUserId(userId); + etCouponUserLog.setCouponId(couponId); + etCouponUserLog.setStatus(ServiceConstants.COUPON_STATUS_UNUSED); + List etCouponUserLogs = etCouponClaimLogMapper.selectEtCouponClaimLogList(etCouponUserLog); + EtCouponUserLog etCouponUserLog1 = null; + if(etCouponUserLogs !=null && etCouponUserLogs.size()>0){ + etCouponUserLog1 = etCouponUserLogs.get(0); + } + + if(etCoupon.getType().equals(asUser.getVipType()) || asUser.getExpirationTime().compareTo(new Date()) < 0){ + throw new ServiceException("会员已过期"); + } + + if((etCoupon.getType().equals(ServiceConstants.COUPON_TYPE_DISCOUNT_CARD) || etCoupon.getType().equals(ServiceConstants.COUPON_TYPE_VOUCHER)) + && isCouponExpired(etCouponUserLog1)){ + throw new ServiceException("优惠券已过期"); + } + + if(etCouponUserLog1.getLimitNum() == 0){ + throw new ServiceException("可用次数不足"); + } + + if(etCoupon.getType().equals(ServiceConstants.COUPON_TYPE_TIME_CARD)){ + order.setPayFee(order.getTotalFee().subtract(order.getRidingFee()));//会员 + order.setRidingFee(BigDecimal.ZERO); + }else if(etCoupon.getType().equals(ServiceConstants.COUPON_TYPE_VIP_CARD)){//贵宾卡 + order.setPayFee(BigDecimal.ZERO); + }else if(etCoupon.getType().equals(ServiceConstants.COUPON_TYPE_DISCOUNT_CARD)){//折扣卡 + order.setPayFee(order.getTotalFee().multiply(etCoupon.getDiscountPercent()).setScale(2, RoundingMode.HALF_UP)); + }else if(etCoupon.getType().equals(ServiceConstants.COUPON_TYPE_VOUCHER)){//抵用券 + order.setPayFee(order.getTotalFee().subtract(etCoupon.getDiscountAmount())); + }else{ + throw new ServiceException("优惠券类型错误"); + } + + // 扣除一次使用次数 + if(etCoupon.getType().equals(ServiceConstants.COUPON_TYPE_DISCOUNT_CARD) || etCoupon.getType().equals(ServiceConstants.COUPON_TYPE_VOUCHER) && etCouponUserLog1.getLimitNum() > 0){ + etCouponClaimLogMapper.deductLimitNum(etCouponUserLog1.getLogId()); + } + return order; + } + + /** 判断优惠券是否过期 */ + private boolean isCouponExpired(EtCouponUserLog etCouponUserLog) { + if(etCouponUserLog!=null){ + return etCouponUserLog.getExpirationTime().compareTo(new Date()) < 0; + }else{ + return true; + } + } + /** * 计算订单费用 */ diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/CallbackServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/CallbackServiceImpl.java index 6612336..10ed840 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/CallbackServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/CallbackServiceImpl.java @@ -325,6 +325,7 @@ public class CallbackServiceImpl implements CallbackService { etCouponUserLog.setGainMethod(ServiceConstants.COUPON_GAIN_METHOD_BUY); etCouponUserLog.setAreaId(etCoupon.getAreaId()); etCouponUserLog.setAreaName(etCoupon.getAreaName()); + etCouponUserLog.setLimitNum(Integer.parseInt(etCoupon.getLimitNum())); if(ObjectUtil.isNull(etCoupon.getValidityUnit())){ throw new ServiceException("优惠券【有效期单位--validityUnit】为空"); } diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtCouponClaimLogServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtCouponClaimLogServiceImpl.java index 46a65ee..585a87f 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtCouponClaimLogServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtCouponClaimLogServiceImpl.java @@ -92,4 +92,12 @@ public class EtCouponClaimLogServiceImpl implements IEtCouponClaimLogService { return etCouponClaimLogMapper.deleteEtCouponClaimLogByLogId(claimId); } + + /** + * 我的骑行卡(未使用和已过期) + */ + @Override + public List selectCouponListByUserId(Long userId) { + return etCouponClaimLogMapper.selectCouponListByUserId(userId); + } } diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtCouponServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtCouponServiceImpl.java index 1999776..3a8d9a6 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtCouponServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtCouponServiceImpl.java @@ -173,6 +173,9 @@ public class EtCouponServiceImpl implements IEtCouponService if(ObjectUtil.isNull(etCoupon)){ throw new ServiceException("优惠券【"+couponId+"】不存在"); } + if(asUser.getVipType().equals(ServiceConstants.COUPON_TYPE_VIP_CARD) && etCoupon.getType().equals(ServiceConstants.COUPON_TYPE_TIME_CARD)){ + throw new ServiceException("用户【"+asUser.getUserName()+"】已经是贵宾卡,无需添加会员卡"); + } EtCouponUserLog etCouponUserLog = new EtCouponUserLog(); etCouponUserLog.setCouponId(couponId); etCouponUserLog.setUserId(userId); @@ -181,6 +184,7 @@ public class EtCouponServiceImpl implements IEtCouponService etCouponUserLog.setGainMethod(ServiceConstants.COUPON_GAIN_METHOD_ISSUE); etCouponUserLog.setAreaId(etCoupon.getAreaId()); etCouponUserLog.setAreaName(etCoupon.getAreaName()); + etCouponUserLog.setLimitNum(Integer.parseInt(etCoupon.getLimitNum())); if(ObjectUtil.isNull(etCoupon.getValidityUnit())){ throw new ServiceException("优惠券【有效期单位--validityUnit】为空"); } diff --git a/electripper-system/src/main/resources/mapper/system/EtCouponClaimLogMapper.xml b/electripper-system/src/main/resources/mapper/system/EtCouponClaimLogMapper.xml index e6d7d9f..24b85db 100644 --- a/electripper-system/src/main/resources/mapper/system/EtCouponClaimLogMapper.xml +++ b/electripper-system/src/main/resources/mapper/system/EtCouponClaimLogMapper.xml @@ -54,6 +54,35 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where log_id = #{logId} + + insert into et_coupon_user_log @@ -90,6 +119,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where log_id = #{logId} + + update et_coupon + set limit_num = limit_num - 1 + where log_id = #{logId} and limit_num > 0 + + delete from et_coupon_user_log where log_id = #{logId}