PreviewController.java 2.33 KB
package com.xly.web;

import com.xly.agent.AgentIdentity;
import com.xly.service.AuthzService;
import com.xly.service.PreviewService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import java.util.Map;

/**
 * 预览卡按钮端点(**确定性、不经过 LLM**)—— 用户在预览卡上点【保存/审核/作废/…】后调用。
 *
 * <p>{@link PreviewService#save} 按 previewId 取回服务端暂存的解析产物,**重读记录重校验**
 * (所见即所写:记录被他人改过/状态已变 → 拒绝),通过后写一行 ai_op_queue(AI 侧唯一写入口)。
 * **xlyAi 到此为止**:是否/何时执行由 ERP 侧负责。
 *
 * <p>安全:需登录(token 服务端内省 fail-closed);预览归属本人(previewId 服务端绑定 userId);
 * 写队列前重查表单权限——三层都在 PreviewService.save 内完成。
 */
@RestController
@RequestMapping("/api/agent/preview")
public class PreviewController {

    private final PreviewService previews;
    private final AuthzService authz;

    public PreviewController(PreviewService previews, AuthzService authz) {
        this.previews = previews;
        this.authz = authz;
    }

    public static class SaveReq {
        /** 可编辑预览(update)上用户最终确认的 字段中文名->值;状态类操作可空。 */
        public Map<String, String> fields;
    }

    @PostMapping("/{id}/save")
    public Map<String, Object> save(@PathVariable("id") String id,
                                    @RequestBody(required = false) SaveReq req,
                                    @RequestHeader(value = "Authorization", required = false) String auth) {
        AgentIdentity me = authz.resolveIdentity(auth);
        if (me == null) {
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "登录已过期或未登录");
        }
        return previews.save(me, id, req == null ? null : req.fields);
    }
}