shinnosuke
2026-08-21 bc2efbdb52190fe2e7be9920bbed6902d4ee28aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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;
    }
}