package com.talentcard.server.business.portal;
|
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import com.talentcard.server.business.BusinessIdGenerator;
|
import com.talentcard.server.business.DatabaseRows;
|
import com.talentcard.server.common.BusinessException;
|
import com.talentcard.server.common.ErrorCode;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
@Service
|
public class FuelMerchantPortalService {
|
private final JdbcTemplate jdbc;
|
|
public FuelMerchantPortalService(JdbcTemplate jdbc) {
|
this.jdbc = jdbc;
|
}
|
|
@Transactional(readOnly = true)
|
public Map<String, Object> detail(Long merchantId) {
|
Map<String, Object> merchant = fuelMerchant(merchantId);
|
Map<String, Object> benefit = one("""
|
select id, talent_levels, discount_text, oil_types, single_limit_text, annual_limit_text,
|
available_time, stock, valid_end, usage_instructions, rule_text, status
|
from transport_fuel_benefit
|
where merchant_id = ? and deleted = 0
|
order by id asc limit 1
|
""", merchantId);
|
boolean available = number(merchant.get("status")) == 1 && number(benefit.get("status")) == 1;
|
Map<String, Object> result = new LinkedHashMap<>();
|
result.put("merchantId", merchant.get("id"));
|
result.put("merchantName", merchant.get("merchantName"));
|
result.put("merchantType", "fuel");
|
result.put("coverUrl", merchant.get("coverUrl"));
|
result.put("address", merchant.get("address"));
|
result.put("contactMobile", merchant.get("contactMobile"));
|
result.put("latitude", merchant.get("latitude"));
|
result.put("longitude", merchant.get("longitude"));
|
result.put("available", available);
|
result.put("serviceLabel", text(merchant.get("serviceLabel"), "请咨询商户"));
|
result.put("discountText", text(benefit.get("discountText"), "请咨询商户"));
|
result.put("applicableCards", text(benefit.get("talentLevels"), "请咨询商户"));
|
result.put("groupBuyingEnabled", false);
|
result.put("usageInstructions", text(benefit.get("usageInstructions"), "请咨询商户"));
|
result.put("rightsText", text(benefit.get("ruleText"), "请咨询商户"));
|
result.put("benefit", benefitView(benefit));
|
result.put("heroImages", query("""
|
select id, image_url, sort_no
|
from transport_fuel_hero_image
|
where merchant_id = ? and deleted = 0 and enabled = 1
|
order by sort_no asc, id asc
|
""", merchantId));
|
result.put("applicableStores", query("""
|
select id, store_name, address, latitude, longitude, sort_no,
|
case when latitude is not null and longitude is not null and latitude <> 0 and longitude <> 0 then 1 else 0 end as can_navigate
|
from transport_fuel_store
|
where merchant_id = ? and deleted = 0 and enabled = 1
|
order by sort_no asc, id asc
|
""", merchantId));
|
return result;
|
}
|
|
@Transactional
|
public Map<String, Object> registerMobile(Long merchantId, FuelMobileRegistrationRequest request, String idempotencyKey) {
|
if (idempotencyKey == null || idempotencyKey.isBlank() || idempotencyKey.length() > 128) {
|
throw new BusinessException(ErrorCode.BAD_REQUEST, "提交凭证不能为空");
|
}
|
List<Map<String, Object>> existing = query("""
|
select id, registration_no, status
|
from transport_fuel_mobile_registration
|
where idempotency_key = ? and deleted = 0
|
""", idempotencyKey);
|
if (!existing.isEmpty()) return registrationView(existing.get(0));
|
|
Map<String, Object> merchant = fuelMerchant(merchantId);
|
if (number(merchant.get("status")) != 1) throw new BusinessException(ErrorCode.CONFLICT, "加油站当前不可用");
|
Map<String, Object> benefit = one("""
|
select id, status from transport_fuel_benefit
|
where id = ? and merchant_id = ? and deleted = 0
|
""", request.benefitId(), merchantId);
|
if (number(benefit.get("status")) != 1) throw new BusinessException(ErrorCode.CONFLICT, "加油权益当前不可用");
|
Integer talentCount = jdbc.queryForObject("select count(*) from talent_user where id = ? and deleted = 0", Integer.class, request.talentId());
|
if (talentCount == null || talentCount == 0) throw new BusinessException(ErrorCode.NOT_FOUND, "人才用户不存在");
|
|
String registrationNo = BusinessIdGenerator.next("FR");
|
jdbc.update("""
|
insert into transport_fuel_mobile_registration(registration_no, merchant_id, benefit_id, talent_id, mobile, status, idempotency_key)
|
values (?, ?, ?, ?, ?, 'SUBMITTED', ?)
|
""", registrationNo, merchantId, request.benefitId(), request.talentId(), request.mobile(), idempotencyKey);
|
return registrationView(one("""
|
select id, registration_no, status
|
from transport_fuel_mobile_registration
|
where idempotency_key = ? and deleted = 0
|
""", idempotencyKey));
|
}
|
|
private Map<String, Object> fuelMerchant(Long merchantId) {
|
return one("""
|
select id, merchant_name, merchant_type_code as merchant_type, contact_mobile, address, cover_url, service_label, status, latitude, longitude
|
from merchant
|
where id = ? and merchant_type_code = 'fuel' and deleted = 0
|
""", merchantId);
|
}
|
|
private Map<String, Object> benefitView(Map<String, Object> benefit) {
|
Map<String, Object> result = new LinkedHashMap<>();
|
result.put("benefitId", benefit.get("id"));
|
result.put("oilTypes", text(benefit.get("oilTypes"), "请咨询商户"));
|
result.put("discount", text(benefit.get("discountText"), "请咨询商户"));
|
result.put("singleLimit", text(benefit.get("singleLimitText"), "请咨询商户"));
|
result.put("annualLimit", text(benefit.get("annualLimitText"), "请咨询商户"));
|
result.put("availableTime", text(benefit.get("availableTime"), "请咨询商户"));
|
result.put("stock", benefit.get("stock"));
|
result.put("validEnd", benefit.get("validEnd"));
|
return result;
|
}
|
|
private Map<String, Object> registrationView(Map<String, Object> row) {
|
Map<String, Object> result = new LinkedHashMap<>();
|
result.put("registrationId", row.get("id"));
|
result.put("registrationNo", row.get("registrationNo"));
|
result.put("status", row.get("status"));
|
return result;
|
}
|
|
private List<Map<String, Object>> query(String sql, Object... args) {
|
return jdbc.queryForList(sql, args).stream().map(DatabaseRows::normalize).toList();
|
}
|
|
private Map<String, Object> one(String sql, Object... args) {
|
List<Map<String, Object>> rows = query(sql, args);
|
if (rows.isEmpty()) throw new BusinessException(ErrorCode.NOT_FOUND, "加油站权益配置不存在");
|
return rows.get(0);
|
}
|
|
private int number(Object value) {
|
return value instanceof Number number ? number.intValue() : 0;
|
}
|
|
private String text(Object value, String fallback) {
|
if (value == null) return fallback;
|
String text = String.valueOf(value).trim();
|
return text.isEmpty() ? fallback : text;
|
}
|
}
|