| | |
| | | result.put("customerName", displayName(string(source.get("talentName")), number(source.get("gender")))); |
| | | result.put("customerMobile", maskMobile(string(source.get("talentMobile")))); |
| | | result.put("cardLevel", string(source.get("cardLevel"))); |
| | | result.put("benefitText", benefitText(principal.merchantId(), string(source.get("cardLevel")))); |
| | | result.put("rules", rules(principal.merchantId(), string(source.get("cardLevel")))); |
| | | List<Map<String, Object>> matchedRules = rules(principal.merchantId(), string(source.get("cardLevel")), string(source.get("sourceAreaType"))); |
| | | if (matchedRules.isEmpty()) throw new BusinessException(ErrorCode.CONFLICT, "当前商户未配置适用于该人才卡的到店消费权益"); |
| | | result.put("benefitText", benefitText(principal.merchantId(), string(source.get("cardLevel")), string(source.get("sourceAreaType")))); |
| | | result.put("rules", matchedRules); |
| | | result.put("coupons", coupons(principal.merchantId(), number(source.get("talentId")), string(source.get("cardLevel")))); |
| | | result.put("expiresAt", source.get("expiresAt")); |
| | | return result; |
| | |
| | | public Map<String, Object> quote(MerchantPrincipal principal, ManualConsumptionCreateRequest request) { |
| | | Map<String, Object> source = source(principal, request.verificationId(), false); |
| | | ensurePending(source); |
| | | Map<String, Object> rule = rule(principal.merchantId(), string(source.get("cardLevel")), request.ruleId()); |
| | | Map<String, Object> rule = rule(principal.merchantId(), string(source.get("cardLevel")), string(source.get("sourceAreaType")), request.ruleId()); |
| | | Map<String, Object> coupon = usableCoupon(principal.merchantId(), number(source.get("talentId")), string(source.get("cardLevel")), request.couponId(), discountedAmount(rule, request.amount())); |
| | | return quote(rule, coupon, request.amount()); |
| | | } |
| | |
| | | return createdOrder(existingOrderId); |
| | | } |
| | | ensurePending(source); |
| | | Map<String, Object> rule = rule(principal.merchantId(), string(source.get("cardLevel")), request.ruleId()); |
| | | Map<String, Object> rule = rule(principal.merchantId(), string(source.get("cardLevel")), string(source.get("sourceAreaType")), request.ruleId()); |
| | | Map<String, Object> coupon = usableCoupon(principal.merchantId(), number(source.get("talentId")), string(source.get("cardLevel")), request.couponId(), discountedAmount(rule, request.amount())); |
| | | Map<String, Object> amounts = quote(rule, coupon, request.amount()); |
| | | Long dynamicCodeId = number(source.get("dynamicCodeId")); |
| | |
| | | List<Map<String, Object>> sources = rows(""" |
| | | select a.id attempt_id, a.dynamic_code_id, a.status attempt_status, a.expires_at, a.manual_order_id, |
| | | d.talent_id, d.card_id, c.card_level, |
| | | t.name talent_name, t.mobile talent_mobile, t.gender |
| | | t.name talent_name, t.mobile talent_mobile, t.gender, t.source_area_type |
| | | from merchant_verification_attempt a |
| | | join talent_dynamic_code d on d.id = a.dynamic_code_id |
| | | join talent_card c on c.id = d.card_id and c.deleted = 0 |
| | |
| | | } |
| | | } |
| | | |
| | | private List<Map<String, Object>> rules(Long merchantId, String cardLevel) { |
| | | List<Map<String, Object>> configured = configuredRules(merchantId, cardLevel); |
| | | if (!configured.isEmpty()) return configured; |
| | | private List<Map<String, Object>> rules(Long merchantId, String cardLevel, String sourceAreaType) { |
| | | List<Map<String, Object>> configured = configuredRules(merchantId, cardLevel, sourceAreaType); |
| | | if (!configured.isEmpty() || hasStructuredRightsConfig(merchantId)) return configured; |
| | | return rows(""" |
| | | select id, card_level, rule_name, discount_rate, subsidy_enabled |
| | | from merchant_store_consume_rule |
| | |
| | | }).toList(); |
| | | } |
| | | |
| | | private List<Map<String, Object>> configuredRules(Long merchantId, String cardLevel) { |
| | | boolean hasApplicableRule(Long merchantId, String cardLevel, String sourceAreaType) { |
| | | return !rules(merchantId, cardLevel, sourceAreaType).isEmpty(); |
| | | } |
| | | |
| | | private boolean hasStructuredRightsConfig(Long merchantId) { |
| | | List<Map<String, Object>> merchants = rows("select merchant_rights_config from merchant where id = ? and deleted = 0 limit 1", merchantId); |
| | | if (merchants.isEmpty()) return false; |
| | | Object raw = merchants.get(0).get("merchantRightsConfig"); |
| | | return raw != null && !string(raw).isBlank(); |
| | | } |
| | | |
| | | private List<Map<String, Object>> configuredRules(Long merchantId, String cardLevel, String sourceAreaType) { |
| | | List<Map<String, Object>> merchants = rows("select merchant_rights_config from merchant where id = ? and deleted = 0 limit 1", merchantId); |
| | | if (merchants.isEmpty()) return List.of(); |
| | | Object raw = merchants.get(0).get("merchantRightsConfig"); |
| | |
| | | } catch (Exception ignored) { |
| | | return List.of(); |
| | | } |
| | | return configs.stream().filter(right -> booleanValue(right.get("enabled")) && booleanValue(right.get("consumeEnabled")) && booleanValue(right.get("storeConsume")) && matchesCardLevel(string(right.get("level")), cardLevel)).map(this::configuredRule).filter(rule -> decimal(rule.get("discountRate")).signum() > 0).toList(); |
| | | return configs.stream() |
| | | .filter(right -> booleanValue(right.get("enabled")) |
| | | && configuredStoreConsumptionEnabled(right) |
| | | && matchesConfiguredLevel(string(right.get("level")), cardLevel, sourceAreaType)) |
| | | .map(this::configuredRule) |
| | | .filter(rule -> decimal(rule.get("discountRate")).signum() > 0 || decimal(rule.get("fixedReduction")).signum() > 0) |
| | | .toList(); |
| | | } |
| | | |
| | | private Map<String, Object> configuredRule(Map<String, Object> right) { |
| | | boolean configuredStoreConsumptionEnabled(Map<String, Object> right) { |
| | | if (!booleanValue(right.get("storeConsume"))) return false; |
| | | return booleanValue(right.get("consumeEnabled")) || "paid".equalsIgnoreCase(string(right.get("parkingType"))); |
| | | } |
| | | |
| | | Map<String, Object> configuredRule(Map<String, Object> right) { |
| | | String level = normalizedCardLevel(string(right.get("level"))); |
| | | Map<String, Object> result = new LinkedHashMap<>(); |
| | | result.put("id", configRuleId(level)); |
| | | result.put("cardLevel", level); |
| | | result.put("ruleName", string(right.get("tag")).isBlank() ? "到店消费优惠" : right.get("tag")); |
| | | result.put("name", result.get("ruleName")); |
| | | result.put("discountRate", configuredDiscountRate(right.get("discount"))); |
| | | result.put("subsidyEnabled", booleanValue(right.get("governmentSubsidy")) ? 1 : 0); |
| | | result.put("discountRate", configuredDiscountRate(right)); |
| | | result.put("fixedReduction", configuredFixedReduction(right)); |
| | | result.put("subsidyEnabled", configuredSubsidyEnabled(right) ? 1 : 0); |
| | | result.put("merchantDetail", right.get("detail")); |
| | | result.put("consumeDesc", right.get("consumeDesc")); |
| | | result.put("benefitText", configuredBenefitText(right)); |
| | | return result; |
| | | } |
| | | |
| | | private Map<String, Object> rule(Long merchantId, String cardLevel, Long ruleId) { |
| | | List<Map<String, Object>> configured = configuredRules(merchantId, cardLevel); |
| | | private Map<String, Object> rule(Long merchantId, String cardLevel, String sourceAreaType, Long ruleId) { |
| | | List<Map<String, Object>> configured = configuredRules(merchantId, cardLevel, sourceAreaType); |
| | | for (Map<String, Object> candidate : configured) if (String.valueOf(candidate.get("id")).equals(String.valueOf(ruleId))) return candidate; |
| | | List<Map<String, Object>> found = rows(""" |
| | | select id, card_level, rule_name, discount_rate, subsidy_enabled |
| | |
| | | return switch (level) { case "EXCELLENT" -> -1L; case "OUTSTANDING" -> -2L; case "ELITE" -> -3L; default -> -9L; }; |
| | | } |
| | | |
| | | private BigDecimal configuredDiscountRate(Object value) { |
| | | private BigDecimal configuredDiscountRate(Map<String, Object> right) { |
| | | try { |
| | | Object value = "折扣".equals(string(right.get("consumeType"))) ? right.get("consumeDiscount") : right.get("discount"); |
| | | BigDecimal discount = decimal(string(value)); |
| | | return discount.signum() > 0 && discount.compareTo(BigDecimal.TEN) <= 0 ? discount.divide(BigDecimal.TEN, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; |
| | | } catch (Exception ignored) { |
| | | return BigDecimal.ZERO; |
| | | } |
| | | } |
| | | |
| | | private BigDecimal configuredFixedReduction(Map<String, Object> right) { |
| | | if (!"优惠".equals(string(right.get("consumeType")))) return BigDecimal.ZERO; |
| | | try { |
| | | BigDecimal reduction = decimal(string(right.get("consumeCoupon"))); |
| | | return reduction.signum() > 0 ? reduction : BigDecimal.ZERO; |
| | | } catch (Exception ignored) { |
| | | return BigDecimal.ZERO; |
| | | } |
| | | } |
| | | |
| | | private boolean configuredSubsidyEnabled(Map<String, Object> right) { |
| | | String consumeType = string(right.get("consumeType")); |
| | | if ("折扣".equals(consumeType)) return booleanValue(right.get("consumeDiscountSubsidy")); |
| | | if ("优惠".equals(consumeType)) return booleanValue(right.get("consumeCouponSubsidy")); |
| | | return booleanValue(right.get("governmentSubsidy")); |
| | | } |
| | | |
| | | private boolean booleanValue(Object value) { |
| | |
| | | return "true".equals(text) || "1".equals(text) || "yes".equals(text); |
| | | } |
| | | |
| | | private Map<String, Object> quote(Map<String, Object> rule, Map<String,Object> coupon, BigDecimal inputAmount) { |
| | | Map<String, Object> quote(Map<String, Object> rule, Map<String,Object> coupon, BigDecimal inputAmount) { |
| | | long originalAmount = inputAmount.movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValueExact(); |
| | | long payAmount = discountedAmountCents(rule, originalAmount); |
| | | long basePayAmount = payAmount; |
| | | long couponAmount = coupon == null ? 0 : Math.min(decimal(coupon.get("couponAmount")).movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValue(), basePayAmount); |
| | | payAmount = basePayAmount - couponAmount; |
| | | long discountAmount = originalAmount - payAmount; |
| | | long subsidyAmount = originalAmount - payAmount; |
| | | long subsidyAmount = booleanValue(rule.get("subsidyEnabled")) ? discountAmount : 0; |
| | | Map<String, Object> result = new LinkedHashMap<>(); |
| | | result.put("originalAmount", originalAmount); |
| | | result.put("discountAmount", discountAmount); |
| | |
| | | } |
| | | |
| | | private long discountedAmountCents(Map<String, Object> rule, long originalAmount) { |
| | | BigDecimal fixedReduction = rule.get("fixedReduction") == null ? BigDecimal.ZERO : decimal(rule.get("fixedReduction")); |
| | | if (fixedReduction.signum() > 0) { |
| | | long reductionCents = fixedReduction.movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValueExact(); |
| | | return Math.max(0, originalAmount - reductionCents); |
| | | } |
| | | BigDecimal rate = decimal(rule.get("discountRate")); |
| | | if (rate.signum() <= 0 || rate.compareTo(BigDecimal.ONE) > 0) throw new BusinessException(ErrorCode.CONFLICT, "商户优惠规则配置无效"); |
| | | return BigDecimal.valueOf(originalAmount).multiply(rate).setScale(0, RoundingMode.HALF_UP).longValueExact(); |
| | |
| | | return result; |
| | | } |
| | | |
| | | private String benefitText(Long merchantId, String cardLevel) { |
| | | private String benefitText(Long merchantId, String cardLevel, String sourceAreaType) { |
| | | List<Map<String, Object>> benefits = rows(""" |
| | | select card_level, benefit_text from merchant_card_benefit |
| | | where merchant_id = ? and status = 1 order by sort_no desc, id desc |
| | | """, merchantId); |
| | | String configuredDescription = configuredRules(merchantId, cardLevel).stream() |
| | | String configuredDescription = configuredRules(merchantId, cardLevel, sourceAreaType).stream() |
| | | .map(rule -> string(rule.get("consumeDesc"))) |
| | | .filter(value -> !value.isBlank()) |
| | | .findFirst() |
| | | .orElse(""); |
| | | if (configuredDescription.isBlank()) { |
| | | configuredDescription = configuredRules(merchantId, cardLevel).stream() |
| | | configuredDescription = configuredRules(merchantId, cardLevel, sourceAreaType).stream() |
| | | .map(rule -> string(rule.get("merchantDetail"))) |
| | | .filter(value -> !value.isBlank()) |
| | | .findFirst() |
| | | .orElse(""); |
| | | } |
| | | if (configuredDescription.isBlank()) { |
| | | configuredDescription = configuredRules(merchantId, cardLevel).stream() |
| | | configuredDescription = configuredRules(merchantId, cardLevel, sourceAreaType).stream() |
| | | .map(rule -> string(rule.get("benefitText"))) |
| | | .filter(value -> !value.isBlank()) |
| | | .findFirst() |
| | |
| | | } |
| | | |
| | | String benefitTextForMerchant(Long merchantId, String cardLevel) { |
| | | return benefitText(merchantId, cardLevel); |
| | | return benefitText(merchantId, cardLevel, ""); |
| | | } |
| | | |
| | | boolean matchesConfiguredLevel(String configured, String actual, String sourceAreaType) { |
| | | if (!"EXCELLENT".equals(normalizedCardLevel(actual))) return matchesCardLevel(configured, actual); |
| | | String configuredUpper = configured.trim().toUpperCase(); |
| | | String areaUpper = sourceAreaType.trim().toUpperCase(); |
| | | if (configuredUpper.contains("本地") || configuredUpper.contains("LOCAL")) { |
| | | return areaUpper.contains("LOCAL") || areaUpper.contains("本地"); |
| | | } |
| | | if (configuredUpper.contains("外地") || configuredUpper.contains("OUTSIDE")) { |
| | | return areaUpper.contains("OUTSIDE") || areaUpper.contains("外地"); |
| | | } |
| | | return matchesCardLevel(configured, actual); |
| | | } |
| | | |
| | | private boolean matchesCardLevel(String configured, String actual) { |