Commit 84c033f6 by 章家宝

feat:1 小程序埋点接口获取用户名 2 优惠券使用实时模板数据来抵扣

1 parent 269c5020
...@@ -2,22 +2,20 @@ package com.glsx.usedcar.web.api.event; ...@@ -2,22 +2,20 @@ package com.glsx.usedcar.web.api.event;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.glsx.usedcar.enums.event.EventSourceEnum; import com.glsx.usedcar.enums.event.EventSourceEnum;
import com.glsx.usedcar.module.account.entity.Account;
import com.glsx.usedcar.module.event.service.EventTrackingService; import com.glsx.usedcar.module.event.service.EventTrackingService;
import com.glsx.usedcar.module.event.vo.EventTrackingBatchSaveVO; import com.glsx.usedcar.module.event.vo.EventTrackingBatchSaveVO;
import com.glsx.usedcar.module.security.entity.User;
import com.glsx.usedcar.resp.BaseResp; import com.glsx.usedcar.resp.BaseResp;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.summerframework.core.exception.BusinessCodeException; import org.summerframework.core.exception.BusinessCodeException;
import static com.glsx.usedcar.web.WebConstants.LOGIN_ACCOUNT;
/** /**
* 埋点事件控制器 * 埋点事件控制器
* *
...@@ -27,7 +25,7 @@ import org.summerframework.core.exception.BusinessCodeException; ...@@ -27,7 +25,7 @@ import org.summerframework.core.exception.BusinessCodeException;
*/ */
@Slf4j @Slf4j
@RestController @RestController
@RequestMapping("/rest/eventTracking") @RequestMapping("/api/eventTracking")
@RequiredArgsConstructor @RequiredArgsConstructor
@Tag(name = "埋点事件控制器", description = "埋点事件控制器") @Tag(name = "埋点事件控制器", description = "埋点事件控制器")
public class EventTrackingController { public class EventTrackingController {
...@@ -38,30 +36,28 @@ public class EventTrackingController { ...@@ -38,30 +36,28 @@ public class EventTrackingController {
* 批量保存埋点事件 * 批量保存埋点事件
* *
* @param vo 请求体(包含事件列表、业务主键、扩展参数) * @param vo 请求体(包含事件列表、业务主键、扩展参数)
* @param authentication 当前登录用户
* @return 操作结果 * @return 操作结果
*/ */
@PostMapping("/batchSave") @PostMapping("/batchSave")
@Operation(summary = "批量保存埋点事件", description = "前端批量上报用户行为埋点事件,支持同时携带业务主键和扩展参数") @Operation(summary = "批量保存埋点事件", description = "前端批量上报用户行为埋点事件,支持同时携带业务主键和扩展参数")
public BaseResp<Void> batchSave(@Valid @RequestBody EventTrackingBatchSaveVO vo, Authentication authentication) { public BaseResp<Void> batchSave(@Valid @RequestBody EventTrackingBatchSaveVO vo, @RequestAttribute(LOGIN_ACCOUNT) Account account) {
log.info("【接口请求】批量保存埋点事件,操作人={},事件数量={}", log.info("【接口请求】批量保存埋点事件,操作人={},事件数量={}",
getUserName(authentication), vo.getEvents() == null ? 0 : vo.getEvents().size()); getUserName(account), vo.getEvents() == null ? 0 : vo.getEvents().size());
try { try {
User user = (User) authentication.getPrincipal(); eventTrackingService.batchSave(vo, String.valueOf(account.getAccountId()), account.getAccountName(), EventSourceEnum.API.getSource());
eventTrackingService.batchSave(vo, String.valueOf(user.getUserId()), user.getUserName(), EventSourceEnum.API.getSource());
return BaseResp.success(); return BaseResp.success();
} catch (BusinessCodeException be) { } catch (BusinessCodeException be) {
log.error("【接口响应】批量保存埋点事件失败!操作人={},业务异常!", getUserName(authentication), be); log.error("【接口响应】批量保存埋点事件失败!操作人={},业务异常!", getUserName(account), be);
return BaseResp.fail(StrUtil.toString(be.getCode()), be.getMessage()); return BaseResp.fail(StrUtil.toString(be.getCode()), be.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("【接口响应】批量保存埋点事件失败!操作人={},系统异常!", getUserName(authentication), e); log.error("【接口响应】批量保存埋点事件失败!操作人={},系统异常!", getUserName(account), e);
return BaseResp.fail("系统异常: " + e.getMessage()); return BaseResp.fail("系统异常: " + e.getMessage());
} }
} }
private String getUserName(Authentication authentication) { private String getUserName(Account account) {
try { try {
return authentication != null ? ((User) authentication.getPrincipal()).getUserName() : "未知用户"; return account != null ? account.getAccountName() : "未知用户";
} catch (Exception e) { } catch (Exception e) {
return "未知用户"; return "未知用户";
} }
......
...@@ -64,7 +64,6 @@ public class CouponVerificationService { ...@@ -64,7 +64,6 @@ public class CouponVerificationService {
*/ */
private Map<Long, CouponTemplate> loadTemplateMapForTiered(List<CouponAccount> coupons) { private Map<Long, CouponTemplate> loadTemplateMapForTiered(List<CouponAccount> coupons) {
Set<Long> templateIds = coupons.stream() Set<Long> templateIds = coupons.stream()
.filter(c -> CouponTypeEnum.TIERED_AMOUNT.getCode().equals(c.getCouponType()))
.map(CouponAccount::getTemplateId) .map(CouponAccount::getTemplateId)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
...@@ -100,25 +99,26 @@ public class CouponVerificationService { ...@@ -100,25 +99,26 @@ public class CouponVerificationService {
* @param orderPrice 订单金额(车款),用于门槛校验 * @param orderPrice 订单金额(车款),用于门槛校验
* @param targetAmount 目标抵扣金额(车款或佣金),用于限制最大抵扣金额 * @param targetAmount 目标抵扣金额(车款或佣金),用于限制最大抵扣金额
* @param templateMap 模板缓存(可为空,非阶梯券不需要) * @param templateMap 模板缓存(可为空,非阶梯券不需要)
* @param tierRuleMap 阶梯规则缓存(可为空,非阶梯券不需要)
* @return 实际抵扣金额(不超过目标抵扣金额) * @return 实际抵扣金额(不超过目标抵扣金额)
*/ */
private BigDecimal calculateActualDeduction(CouponAccount coupon, BigDecimal orderPrice, private BigDecimal calculateActualDeduction(CouponAccount coupon, BigDecimal orderPrice,
BigDecimal targetAmount, BigDecimal targetAmount,
Map<Long, CouponTemplate> templateMap, Map<Long, CouponTemplate> templateMap,
Map<String, List<CouponTierRule>> tierRuleMap) { Map<String, List<CouponTierRule>> tierRuleMap) {
BigDecimal faceAmount = coupon.getFaceAmount();
BigDecimal rawDeduction;
CouponTemplate template = templateMap.get(coupon.getTemplateId());
if (template == null) {
log.info("优惠券模板不存在,使用面额:couponId={}", coupon.getCouponId());
return BigDecimal.ZERO;
}
BigDecimal rawDeduction;
BigDecimal faceAmount = template.getFaceAmount();
if (!CouponTypeEnum.TIERED_AMOUNT.getCode().equals(coupon.getCouponType())) { if (!CouponTypeEnum.TIERED_AMOUNT.getCode().equals(coupon.getCouponType())) {
// 固定面额券:直接使用面额 // 固定面额券:直接使用面额
rawDeduction = faceAmount; rawDeduction = template.getFaceAmount();
log.info("使用固定面额券,面额:couponId={}, faceAmount={}", coupon.getCouponId(), template.getFaceAmount());
} else { } else {
// 阶梯券:从缓存 Map 取模板和阶梯规则,门槛校验只根据订单金额(orderPrice) // 阶梯券:从缓存 Map 取模板和阶梯规则,门槛校验只根据订单金额(orderPrice)
CouponTemplate template = templateMap.get(coupon.getTemplateId());
if (template == null) {
throw new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在");
}
List<CouponTierRule> tierRules = tierRuleMap.get(template.getTemplateId()); List<CouponTierRule> tierRules = tierRuleMap.get(template.getTemplateId());
if (tierRules == null || tierRules.isEmpty()) { if (tierRules == null || tierRules.isEmpty()) {
log.warn("阶梯券未配置阶梯规则,使用面额:templateId={}", coupon.getTemplateId()); log.warn("阶梯券未配置阶梯规则,使用面额:templateId={}", coupon.getTemplateId());
...@@ -445,7 +445,7 @@ public class CouponVerificationService { ...@@ -445,7 +445,7 @@ public class CouponVerificationService {
String unavailableReason = checkCouponUnavailableReason( String unavailableReason = checkCouponUnavailableReason(
coupon, orderPrice, commissionPrice, now, usageMap, actualDeduction); coupon, orderPrice, commissionPrice, now, usageMap, actualDeduction);
return convertToOrderCouponVO(coupon, maxVehicleDeduction, maxCommissionDeduction, return convertToOrderCouponVO(coupon, maxVehicleDeduction, maxCommissionDeduction,
unavailableReason, actualDeduction); unavailableReason, actualDeduction, templateMap);
}) })
.toList(); .toList();
...@@ -522,13 +522,19 @@ public class CouponVerificationService { ...@@ -522,13 +522,19 @@ public class CouponVerificationService {
BigDecimal maxVehicleDeduction, BigDecimal maxVehicleDeduction,
BigDecimal maxCommissionDeduction, BigDecimal maxCommissionDeduction,
String unavailableReason, String unavailableReason,
BigDecimal actualDeduction) { BigDecimal actualDeduction,
Map<Long, CouponTemplate> templateMap) {
OrderCouponRespVO vo = new OrderCouponRespVO(); OrderCouponRespVO vo = new OrderCouponRespVO();
vo.setCouponId(coupon.getCouponId()); vo.setCouponId(coupon.getCouponId());
vo.setTemplateName(coupon.getTemplateName()); vo.setTemplateName(coupon.getTemplateName());
vo.setCouponType(coupon.getCouponType()); vo.setCouponType(coupon.getCouponType());
vo.setDeductionType(coupon.getDeductionType()); vo.setDeductionType(coupon.getDeductionType());
CouponTemplate couponTemplate = templateMap.get(coupon.getTemplateId());
if (couponTemplate != null) {
vo.setFaceAmount(couponTemplate.getFaceAmount());
} else {
vo.setFaceAmount(coupon.getFaceAmount()); vo.setFaceAmount(coupon.getFaceAmount());
}
vo.setThresholdAmount(coupon.getThresholdAmount()); vo.setThresholdAmount(coupon.getThresholdAmount());
vo.setValidStartTime(coupon.getValidStartTime()); vo.setValidStartTime(coupon.getValidStartTime());
vo.setValidEndTime(coupon.getValidEndTime()); vo.setValidEndTime(coupon.getValidEndTime());
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!