liusheng
2 天以前 eb3195e397b023cfae1ee4dea7fbc29f1271504f
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
package cn.lihu.jh.module.ecg.controller.admin.external;
 
import cn.lihu.jh.framework.common.pojo.CommonResult;
import cn.lihu.jh.module.ecg.enums.ActionTypeEnum;
import cn.lihu.jh.module.ecg.service.appointment.AppointmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.function.Consumer;
 
import static cn.lihu.jh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.lihu.jh.framework.common.pojo.CommonResult.success;
import static cn.lihu.jh.module.ecg.enums.ErrorCodeConstants.APPOINTMENT_CREATE_FAIL;
 
@Tag(name = "供第三方调用接口")
@RestController
@RequestMapping("/ecg/external")
@Validated
@Slf4j
public class ExternalController {
 
    @Resource
    private AppointmentService appointmentService;
 
    /**
     * 通用接口
     * <p>
     * 支持以下action类型:
     * - S0201ECG: 预约创建
     * - S0202ECG: 预约更新
     *
     * @param dataMap 请求数据
     * @param headers 请求头,必须包含action字段
     * @return 处理结果
     */
    @PermitAll
    @Operation(summary = "通用接口")
    @PostMapping("/generalInterface")
    public CommonResult<Boolean> generalInterface(
            @RequestBody @NotNull(message = "请求数据不能为空") Map<String, Object> dataMap,
            @RequestHeader @NotEmpty(message = "请求头不能为空") Map<String, String> headers) {
        log.info("[generalInterface][开始处理请求 action({}) dataMap({})]", headers.get("action"), dataMap);
 
        String actionType = headers.get("action");
        ActionTypeEnum action = ActionTypeEnum.getByType(actionType);
        if (action == null) {
            log.warn("[generalInterface][未知的action类型 action({})]", actionType);
            throw exception(APPOINTMENT_CREATE_FAIL);
        }
 
        try {
            // 使用策略模式处理不同的action
            Map<ActionTypeEnum, Consumer<Map<String, Object>>> actionHandlers = Map.of(
                    ActionTypeEnum.S0201ECG, appointmentService::handleAppointmentCreate,
                    ActionTypeEnum.S0202ECG, appointmentService::handleAppointmentUpdate,
                    ActionTypeEnum.S040501HIS, appointmentService::handleAppointmentStateUpdate,
                    ActionTypeEnum.S050401, appointmentService::handleCheckAppointmentUpdate,
                    ActionTypeEnum.S050501, appointmentService::handleCheckAppointmentUpdate
            );
 
            Consumer<Map<String, Object>> handler = actionHandlers.get(action);
            if (handler == null) {
                throw exception(APPOINTMENT_CREATE_FAIL);
            }
 
            handler.accept(dataMap);
            return success(true);
        } catch (Exception e) {
            throw exception(APPOINTMENT_CREATE_FAIL);
        }
    }
}