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
package com.talentcard.server.business.portal;
 
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
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 AdminApprovalPortalService {
 
    private final JdbcTemplate jdbc;
 
    public AdminApprovalPortalService(JdbcTemplate jdbc) {
        this.jdbc = jdbc;
    }
 
    @Transactional(readOnly = true)
    public Map<String, Object> detail(Long serviceId) {
        approvalService(serviceId);
        Map<String, Object> benefit = benefit();
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("benefitId", benefit.get("id"));
        result.put("serviceId", serviceId);
        result.put("title", benefit.get("title"));
        result.put("freeLabel", benefit.get("freeLabel"));
        result.put("heroImage", benefit.get("heroImage"));
        result.put("available", number(benefit.get("enabled")) == 1);
        result.put("unavailableReason", text(benefit.get("unavailableReason"), "该权益暂未开放"));
        result.put("organizations", organizations(((Number) benefit.get("id")).longValue()));
        return result;
    }
 
    private List<Map<String, Object>> organizations(Long benefitId) {
        return query("""
                select id, organization_name, address, latitude, longitude, rights_text, consultation_phone, office_hours
                from admin_approval_organization
                where benefit_id = ? and enabled = 1 and deleted = 0
                order by sort_no asc, id asc
                """, benefitId).stream().map(row -> {
            Long organizationId = ((Number) row.get("id")).longValue();
            List<Map<String, Object>> windows = query("""
                    select id, window_name, location_text
                    from admin_approval_window
                    where organization_id = ? and enabled = 1 and deleted = 0
                    order by sort_no asc, id asc
                    """, organizationId).stream().map(window -> Map.<String, Object>of(
                    "id", window.get("id"), "name", window.get("windowName"), "location", text(window.get("locationText"))
            )).toList();
            Map<String, Object> organization = new LinkedHashMap<>();
            organization.put("id", organizationId);
            organization.put("name", row.get("organizationName"));
            organization.put("address", text(row.get("address")));
            organization.put("latitude", row.get("latitude"));
            organization.put("longitude", row.get("longitude"));
            organization.put("description", row.get("rightsText"));
            organization.put("phone", text(row.get("consultationPhone")));
            organization.put("officeHours", text(row.get("officeHours")));
            organization.put("windows", windows);
            organization.put("hasWindowInfo", !windows.isEmpty());
            return organization;
        }).toList();
    }
 
    private Map<String, Object> approvalService(Long serviceId) {
        if (serviceId == null || serviceId <= 0) throw new BusinessException(ErrorCode.BAD_REQUEST, "行政审批服务信息不能为空");
        List<Map<String, Object>> rows = query("""
                select m.id
                from merchant m
                where m.id = ? and m.deleted = 0 and m.merchant_type_code = 'approval'
                """, serviceId);
        if (rows.isEmpty()) throw new BusinessException(ErrorCode.NOT_FOUND, "行政审批服务不存在");
        return rows.get(0);
    }
 
    private Map<String, Object> benefit() {
        List<Map<String, Object>> rows = query("""
                select id, title, free_label, hero_image, enabled, unavailable_reason
                from admin_approval_benefit where deleted = 0
                order by id asc limit 1
                """);
        if (rows.isEmpty()) throw new BusinessException(ErrorCode.NOT_FOUND, "行政审批权益暂未配置");
        return rows.get(0);
    }
 
    private List<Map<String, Object>> query(String sql, Object... args) {
        return DatabaseRows.normalize(jdbc.queryForList(sql, args));
    }
 
    private int number(Object value) {
        return value instanceof Number number ? number.intValue() : Integer.parseInt(String.valueOf(value));
    }
 
    private String text(Object value) {
        return value == null ? "" : String.valueOf(value);
    }
 
    private String text(Object value, String fallback) {
        String valueText = text(value).trim();
        return valueText.isBlank() ? fallback : valueText;
    }
}