yeg
2 天以前 3773bc4f2d966fb248f96c7e5cedce58c91df689
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.talentcard.server.business.application;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
 
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 com.talentcard.server.request.ParkingPlateStatusUpdateRequest;
import com.talentcard.server.service.ParkingPlateSubmissionAdminService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
public class ServiceApplicationService {
 
    private final JdbcTemplate jdbcTemplate;
    private final ParkingPlateSubmissionAdminService parkingPlateSubmissionAdminService;
 
    public ServiceApplicationService(
            JdbcTemplate jdbcTemplate,
            ParkingPlateSubmissionAdminService parkingPlateSubmissionAdminService
    ) {
        this.jdbcTemplate = jdbcTemplate;
        this.parkingPlateSubmissionAdminService = parkingPlateSubmissionAdminService;
    }
 
    @Transactional(readOnly = true)
    public PageResult<Map<String, Object>> page(
            String applicationType,
            String talentKeyword,
            String merchantName,
            String hospitalName,
            String parkingLot,
            String cardLevel,
            String status,
            String submissionType,
            String subsidy,
            String submittedStart,
            String submittedEnd,
            int page,
            int size
    ) {
        int safePage = Math.max(page, 1);
        int safeSize = Math.min(Math.max(size, 1), 100);
        if (isParkingPlateApplication(applicationType)) {
            return parkingPlateSubmissionAdminService.page(
                    null,
                    null,
                    talentKeyword,
                    null,
                    null,
                    null,
                    firstNonBlank(merchantName, parkingLot, hospitalName),
                    cardLevel,
                    status,
                    submissionType,
                    submittedStart,
                    submittedEnd,
                    safePage,
                    safeSize
            );
        }
        StringBuilder where = new StringBuilder(" where deleted = 0");
        List<Object> args = new ArrayList<>();
        if (applicationType != null && !applicationType.isBlank()) {
            where.append(" and application_type = ?");
            args.add(applicationType);
        }
        if (talentKeyword != null && !talentKeyword.isBlank()) {
            where.append("""
                     and (talent_name like ? or talent_mobile like ? or card_no like ? or application_no like ?
                          or applicant_name like ? or applicant_mobile like ? or plate_no like ?
                          or spouse_name like ? or spouse_mobile like ? or alipay_mobile like ?)
                    """);
            String like = "%" + talentKeyword.trim() + "%";
            for (int i = 0; i < 10; i++) {
                args.add(like);
            }
        }
        if (merchantName != null && !merchantName.isBlank()) {
            where.append(" and merchant_name like ?");
            args.add("%" + merchantName.trim() + "%");
        }
        if (hospitalName != null && !hospitalName.isBlank()) {
            where.append(" and hospital_name like ?");
            args.add("%" + hospitalName.trim() + "%");
        }
        if (parkingLot != null && !parkingLot.isBlank()) {
            where.append(" and parking_lot like ?");
            args.add("%" + parkingLot.trim() + "%");
        }
        if (cardLevel != null && !cardLevel.isBlank()) {
            where.append(" and talent_level = ?");
            args.add(cardLevel);
        }
        if (status != null && !status.isBlank()) {
            where.append(" and status = ?");
            args.add(status);
        }
        if (submissionType != null && !submissionType.isBlank()) {
            where.append(" and submission_type = ?");
            args.add(submissionType);
        }
        if (subsidy != null && !subsidy.isBlank()) {
            where.append(" and subsidy = ?");
            args.add(subsidy);
        }
        if (submittedStart != null && !submittedStart.isBlank()) {
            where.append(" and date(submitted_at) >= ?");
            args.add(submittedStart);
        }
        if (submittedEnd != null && !submittedEnd.isBlank()) {
            where.append(" and date(submitted_at) <= ?");
            args.add(submittedEnd);
        }
 
        List<Object> pageArgs = new ArrayList<>(args);
        pageArgs.add(safeSize);
        pageArgs.add((safePage - 1) * safeSize);
        List<Map<String, Object>> records = query("""
                select *
                from service_application
                """ + where + " order by submitted_at desc, id desc limit ? offset ?", pageArgs.toArray());
        Long total = jdbcTemplate.queryForObject("""
                select count(*)
                from service_application
                """ + where, Long.class, args.toArray());
        return new PageResult<>(records, safePage, safeSize, total == null ? 0 : total);
    }
 
    @Transactional(readOnly = true)
    public Map<String, Object> detail(Long id) {
        return detail(id, null);
    }
 
    @Transactional(readOnly = true)
    public Map<String, Object> detail(Long id, String applicationType) {
        if (isParkingPlateApplication(applicationType)) {
            return parkingPlateSubmissionAdminService.detail(id);
        }
        List<Map<String, Object>> rows = query("""
                select *
                from service_application
                where deleted = 0 and id = ?
                """, id);
        if (rows.isEmpty()) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "申请单不存在");
        }
        return rows.get(0);
    }
 
    @Transactional
    public Map<String, Object> updateStatus(Long id, ServiceApplicationStatusRequest request) {
        return updateStatus(id, request, null);
    }
 
    @Transactional
    public Map<String, Object> updateStatus(Long id, ServiceApplicationStatusRequest request, String applicationType) {
        if (isParkingPlateApplication(applicationType)) {
            return parkingPlateSubmissionAdminService.updateStatus(
                    id,
                    new ParkingPlateStatusUpdateRequest(request.status(), request.rejectReason()),
                    null
            );
        }
        Map<String, Object> current = detail(id);
        String status = request.status();
        if (!List.of("待处理", "已处理", "已驳回", "已取消").contains(status)) {
            throw new BusinessException(ErrorCode.BAD_REQUEST, "申请单状态不正确");
        }
        String handlerName = request.handlerName() == null || request.handlerName().isBlank()
                ? "管理员"
                : request.handlerName();
        Object handledAt = "待处理".equals(status) ? null : java.time.LocalDateTime.now();
        int updated = jdbcTemplate.update("""
                update service_application
                set status = ?, handled_at = ?, handler_name = ?, reject_reason = ?
                where deleted = 0 and id = ?
                """,
                status,
                handledAt,
                "待处理".equals(status) ? null : handlerName,
                request.rejectReason(),
                current.get("id"));
        if (updated == 0) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "申请单不存在");
        }
        return detail(id);
    }
 
    private boolean isParkingPlateApplication(String applicationType) {
        String value = applicationType == null ? "" : applicationType.trim().toUpperCase(Locale.ROOT).replace("-", "_");
        return switch (value) {
            case "PARKING_PLATE", "PARKING_PLATE_REGISTRATION", "PARKING_PLATE_SUBMISSION", "停车车牌", "停车车牌登记" -> true;
            default -> false;
        };
    }
 
    private String firstNonBlank(String... values) {
        for (String value : values) {
            if (value != null && !value.isBlank()) {
                return value.trim();
            }
        }
        return null;
    }
 
    private List<Map<String, Object>> query(String sql, Object... args) {
        return DatabaseRows.normalize(jdbcTemplate.queryForList(sql, args));
    }
}