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.business.PageResult;
|
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 ParkingMerchantPortalService {
|
private final JdbcTemplate jdbc;
|
|
public ParkingMerchantPortalService(JdbcTemplate jdbc) {
|
this.jdbc = jdbc;
|
}
|
|
@Transactional(readOnly = true)
|
public Map<String, Object> detail(Long merchantId) {
|
Map<String, Object> merchant = parkingMerchant(merchantId);
|
Map<String, Object> benefit = one("""
|
select id, talent_levels, benefit_text, action_mode, plate_form_mode, usage_instructions,
|
rule_text, valid_end, unavailable_reason, status
|
from transport_parking_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", "parking");
|
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("unavailableReason", available ? "" : text(benefit.get("unavailableReason"), "停车权益当前不可用"));
|
result.put("serviceLabel", text(merchant.get("serviceLabel"), "停车服务"));
|
result.put("benefitText", text(benefit.get("benefitText"), "免费停车"));
|
result.put("applicableCards", text(benefit.get("talentLevels"), "请咨询商户"));
|
result.put("usageInstructions", text(benefit.get("usageInstructions"), "请咨询商户"));
|
result.put("rightsText", text(benefit.get("ruleText"), "请咨询商户"));
|
result.put("actionMode", text(benefit.get("actionMode"), "plate"));
|
result.put("plateFormMode", text(benefit.get("plateFormMode"), "simple"));
|
result.put("benefit", benefitView(benefit));
|
result.put("applicableStores", query("""
|
select id, parking_lot_name as 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_parking_lot
|
where merchant_id = ? and deleted = 0 and enabled = 1
|
order by sort_no asc, id asc
|
""", merchantId));
|
return result;
|
}
|
|
@Transactional(readOnly = true)
|
public PageResult<Map<String, Object>> parkingLots(Long merchantId, String keyword, int page, int size) {
|
parkingMerchant(merchantId);
|
int safePage = Math.max(1, page);
|
int safeSize = Math.min(50, Math.max(1, size));
|
String pattern = "%" + (keyword == null ? "" : keyword.trim()) + "%";
|
long total = jdbc.queryForObject("""
|
select count(*) from transport_parking_lot
|
where merchant_id = ? and deleted = 0 and enabled = 1
|
and (parking_lot_name like ? or address like ?)
|
""", Long.class, merchantId, pattern, pattern);
|
List<Map<String, Object>> records = query("""
|
select id, parking_lot_name, address, latitude, longitude, sort_no
|
from transport_parking_lot
|
where merchant_id = ? and deleted = 0 and enabled = 1
|
and (parking_lot_name like ? or address like ?)
|
order by sort_no asc, id asc
|
limit ? offset ?
|
""", merchantId, pattern, pattern, safeSize, (safePage - 1) * safeSize);
|
return new PageResult<>(records, safePage, safeSize, total);
|
}
|
|
@Transactional
|
public Map<String, Object> registerPlate(Long merchantId, ParkingPlateRegistrationRequest 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_parking_plate_registration
|
where idempotency_key = ? and deleted = 0
|
""", idempotencyKey);
|
if (!existing.isEmpty()) return registrationView(existing.get(0));
|
|
Map<String, Object> merchant = parkingMerchant(merchantId);
|
if (number(merchant.get("status")) != 1) throw new BusinessException(ErrorCode.CONFLICT, "停车服务当前不可用");
|
Map<String, Object> benefit = one("""
|
select id, action_mode, plate_form_mode, status
|
from transport_parking_benefit
|
where id = ? and merchant_id = ? and deleted = 0
|
""", request.benefitId(), merchantId);
|
if (number(benefit.get("status")) != 1) throw new BusinessException(ErrorCode.CONFLICT, "停车权益当前不可用");
|
if (!"plate".equals(benefit.get("actionMode"))) throw new BusinessException(ErrorCode.CONFLICT, "当前停车权益无需登记车牌");
|
if ("withParking".equals(benefit.get("plateFormMode"))) {
|
if (request.parkingLotId() == null) throw new BusinessException(ErrorCode.BAD_REQUEST, "请选择停车场");
|
Integer lotCount = jdbc.queryForObject("select count(*) from transport_parking_lot where id = ? and merchant_id = ? and enabled = 1 and deleted = 0", Integer.class, request.parkingLotId(), merchantId);
|
if (lotCount == null || lotCount == 0) throw new BusinessException(ErrorCode.BAD_REQUEST, "停车场不可用");
|
}
|
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, "人才用户不存在");
|
Integer duplicateCount = jdbc.queryForObject("""
|
select count(*) from transport_parking_plate_registration
|
where merchant_id = ? and talent_id = ? and plate_number = ? and deleted = 0
|
and status in ('SUBMITTED', 'ACTIVE')
|
""", Integer.class, merchantId, request.talentId(), request.plateNumber());
|
if (duplicateCount != null && duplicateCount > 0) throw new BusinessException(ErrorCode.CONFLICT, "该车牌已登记,如需变更请联系停车服务商");
|
|
String registrationNo = BusinessIdGenerator.next("PR");
|
jdbc.update("""
|
insert into transport_parking_plate_registration(registration_no, merchant_id, benefit_id, parking_lot_id, talent_id, plate_number, status, idempotency_key)
|
values (?, ?, ?, ?, ?, ?, 'SUBMITTED', ?)
|
""", registrationNo, merchantId, request.benefitId(), request.parkingLotId(), request.talentId(), request.plateNumber(), idempotencyKey);
|
return registrationView(one("""
|
select id, registration_no, status
|
from transport_parking_plate_registration
|
where idempotency_key = ? and deleted = 0
|
""", idempotencyKey));
|
}
|
|
private Map<String, Object> parkingMerchant(Long merchantId) {
|
return one("""
|
select id, merchant_name, contact_mobile, address, cover_url, service_label, status, latitude, longitude
|
from merchant
|
where id = ? and merchant_type in ('parking', '停车服务') 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("actionMode", benefit.get("actionMode"));
|
result.put("plateFormMode", benefit.get("plateFormMode"));
|
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;
|
}
|
}
|