Files
ERP/oa-backend/src/main/java/com/kaidi/oa/web/LegalAuthLimitController.java
T
QiufengandClaude Opus 4.8 5e51dc3f56 SNAPSHOT W7 已部署稳定态 — 凯迪ERP+OA一体化平台 (MET 73.3%)
恢复点(restore point)。别人改崩后可 git reset --hard 回到此提交。

== 此快照内容 ==
- 后端 oa-backend: 734 控制器 / 711 实体 (Spring Boot 3.2.5 + SQLite, 端口8091)
- 前端 modern-ui/app: Vue3+Vite, 约700页 (构建产物已在 oa-backend/src/main/resources/static)
- 数据库 oa-backend/data/oa.db: 含全部演示数据 (强制入库, 6.6MB)
- 交接文档 go.md + go-code-reference/endpoints/entities/database.md
- 多代理建设脚本 .claude/wf-*.js

== 状态 ==
- 对 凯迪科技ERP_20260507.xlsx 合规 MET ~73.3% (PARTIAL 75: 34可建+6种子/bug+35外部硬天花板)
- 安全: 5轮红队+5轮复检, default-deny分级鉴权, 连续零可利用
- W3~W7 累计补完436缺口; W8末轮(40缺口)为半成品(源码树可编译但未集成)
- 运行: cd oa-backend; java -jar build/libs/oa-backend-0.1.0.jar --server.port=8091; admin/123456

== 排除(gitignore, 可再生) ==
node_modules / oa-backend/build / .jdks / *.log / Backup-ERP-* / 弃用的OFBiz核心(只保留modern-ui)
完整文件夹备份见同目录 Backup-ERP-20260615-191517/ (含上述全部, 仅缺 node_modules)

时间戳: 20260615-191517

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-15 19:19:15 +08:00

238 lines
10 KiB
Java

package com.kaidi.oa.web;
import com.kaidi.oa.common.ApiException;
import com.kaidi.oa.common.ApiResp;
import com.kaidi.oa.common.Money;
import com.kaidi.oa.common.NotFoundException;
import com.kaidi.oa.domain.LegalAuthLimit;
import com.kaidi.oa.repository.LegalAuthLimitRepository;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Year;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 授权管理台账(内控部 / 法务风险部 · 制度与授权管理)。
*
* 记录各级管理人员、业务岗位的授权范围(合同签署限额、费用审批限额、投标授权等)。
* 核心功能:
* - CRUD(授权登记/更新/撤销);
* - revoke:撤销授权(有效 → 已撤销);
* - expire-check:扫描到期授权,自动更新状态为已过期;
* - check:按被授权人+授权类型查询有效授权,可用于业务流程超授权预警。
*
* 读口含管理人员权限配置(内部敏感),已登记进 SENSITIVE_READ_PREFIXES;写口受 default-deny 保护。
*/
@RestController
@RequestMapping("/api/oa/legal-auth-limits")
public class LegalAuthLimitController {
private final LegalAuthLimitRepository repo;
public LegalAuthLimitController(LegalAuthLimitRepository repo) {
this.repo = repo;
}
@GetMapping
public ApiResp<List<LegalAuthLimit>> list(@RequestParam(required = false) String status,
@RequestParam(required = false) String authType,
@RequestParam(required = false) String dept,
@RequestParam(required = false) String grantee) {
if (grantee != null && !grantee.isBlank()) return ApiResp.ok(repo.findByGrantee(grantee));
if (authType != null && !authType.isBlank()) return ApiResp.ok(repo.findByAuthType(authType));
if (dept != null && !dept.isBlank()) return ApiResp.ok(repo.findByGranteeDept(dept));
if (status != null && !status.isBlank()) return ApiResp.ok(repo.findByStatus(status));
return ApiResp.ok(repo.findAll());
}
@GetMapping("/{id}")
public ApiResp<LegalAuthLimit> get(@PathVariable Long id) {
return ApiResp.ok(load(id));
}
public record AuthLimitRequest(
String grantee, String granteePost, String granteeDept,
String authType, Double amountLimit, String effectiveDate, String expireDate,
String grantor, String remark) {
}
@PostMapping
@Transactional
public ApiResp<LegalAuthLimit> create(@RequestBody AuthLimitRequest req) {
if (req.grantee() == null || req.grantee().isBlank()) {
throw new ApiException(400, "被授权人(grantee) 不能为空");
}
if (req.authType() == null || req.authType().isBlank()) {
throw new ApiException(400, "授权类型(authType) 不能为空");
}
LegalAuthLimit a = new LegalAuthLimit();
a.setAuthNo("SQ-" + Year.now().getValue() + "-" + String.format("%03d", repo.count() + 1));
a.setGrantee(req.grantee());
a.setGranteePost(req.granteePost());
a.setGranteeDept(req.granteeDept());
a.setAuthType(req.authType());
a.setAmountLimit(req.amountLimit() != null ? Money.of(req.amountLimit()) : BigDecimal.ZERO);
a.setEffectiveDate(req.effectiveDate() == null ? LocalDate.now().toString() : req.effectiveDate());
a.setExpireDate(req.expireDate());
a.setGrantor(req.grantor());
a.setRemark(req.remark());
a.setStatus("有效");
a.setCreatedAt(Instant.now());
return ApiResp.ok(repo.save(a));
}
@PatchMapping("/{id}")
@Transactional
public ApiResp<LegalAuthLimit> update(@PathVariable Long id, @RequestBody AuthLimitRequest req) {
LegalAuthLimit a = load(id);
if ("已撤销".equals(a.getStatus())) {
throw new ApiException(409, "已撤销的授权不可修改");
}
if (req.grantee() != null && !req.grantee().isBlank()) a.setGrantee(req.grantee());
if (req.granteePost() != null) a.setGranteePost(req.granteePost());
if (req.granteeDept() != null) a.setGranteeDept(req.granteeDept());
if (req.authType() != null && !req.authType().isBlank()) a.setAuthType(req.authType());
if (req.amountLimit() != null) a.setAmountLimit(Money.of(req.amountLimit()));
if (req.effectiveDate() != null) a.setEffectiveDate(req.effectiveDate());
if (req.expireDate() != null) a.setExpireDate(req.expireDate());
if (req.grantor() != null) a.setGrantor(req.grantor());
if (req.remark() != null) a.setRemark(req.remark());
return ApiResp.ok(repo.save(a));
}
@DeleteMapping("/{id}")
@Transactional
public ApiResp<Void> delete(@PathVariable Long id) {
if (!repo.existsById(id)) throw new NotFoundException("授权记录不存在: " + id);
repo.deleteById(id);
return ApiResp.ok(null);
}
/** 撤销授权:有效 → 已撤销。 */
@PostMapping("/{id}/revoke")
@Transactional
public ApiResp<LegalAuthLimit> revoke(@PathVariable Long id, @RequestBody RevokeRequest req) {
LegalAuthLimit a = load(id);
if (!"有效".equals(a.getStatus())) {
throw new ApiException(409, "当前状态 [" + a.getStatus() + "] 不允许撤销");
}
a.setStatus("已撤销");
if (req.remark() != null) {
a.setRemark("撤销:" + req.remark());
}
return ApiResp.ok(repo.save(a));
}
public record RevokeRequest(String remark) {}
/**
* 到期扫描:检测 expireDate < 今日 且状态为「有效」的授权,自动更新为「已过期」。
* 返回本次更新的过期记录列表。
*/
@PostMapping("/expire-check")
@Transactional
public ApiResp<List<LegalAuthLimit>> expireCheck() {
LocalDate today = LocalDate.now();
List<LegalAuthLimit> all = repo.findByStatus("有效");
List<LegalAuthLimit> expired = new java.util.ArrayList<>();
for (LegalAuthLimit a : all) {
if (a.getExpireDate() == null || a.getExpireDate().isBlank()) continue;
try {
LocalDate exp = LocalDate.parse(a.getExpireDate());
if (exp.isBefore(today)) {
a.setStatus("已过期");
repo.save(a);
expired.add(a);
}
} catch (Exception ignore) {}
}
return ApiResp.ok(expired);
}
/**
* 超授权预警查询:给定被授权人、授权类型、拟操作金额,
* 返回该人有效授权中金额上限 < 拟操作金额的告警信息(可用于业务流程接入检查)。
* amountCheck = 0 时仅查询是否有该类型的有效授权。
*/
@GetMapping("/check")
public ApiResp<Map<String, Object>> check(@RequestParam String grantee,
@RequestParam String authType,
@RequestParam(defaultValue = "0") Double amountCheck) {
List<LegalAuthLimit> valid = repo.findByGrantee(grantee).stream()
.filter(a -> "有效".equals(a.getStatus()) && authType.equals(a.getAuthType()))
.toList();
BigDecimal amount = Money.of(amountCheck);
Map<String, Object> m = new LinkedHashMap<>();
m.put("grantee", grantee);
m.put("authType", authType);
m.put("hasAuth", !valid.isEmpty());
if (valid.isEmpty()) {
m.put("warning", "被授权人 [" + grantee + "] 无 [" + authType + "] 类型有效授权");
} else {
BigDecimal maxLimit = valid.stream()
.map(LegalAuthLimit::getAmountLimit)
.max(BigDecimal::compareTo)
.orElse(BigDecimal.ZERO);
m.put("maxAmountLimit", maxLimit);
boolean overAuth = maxLimit.compareTo(BigDecimal.ZERO) > 0
&& amount.compareTo(maxLimit) > 0;
m.put("overAuth", overAuth);
if (overAuth) {
m.put("warning", "拟操作金额 " + amount + " 超出授权上限 " + maxLimit);
}
}
return ApiResp.ok(m);
}
/** 统计:有效/过期/撤销授权数量。 */
@GetMapping("/stats/overview")
public ApiResp<Map<String, Object>> stats() {
List<LegalAuthLimit> all = repo.findAll();
Map<String, Object> m = new LinkedHashMap<>();
m.put("total", all.size());
m.put("valid", all.stream().filter(a -> "有效".equals(a.getStatus())).count());
m.put("expired", all.stream().filter(a -> "已过期".equals(a.getStatus())).count());
m.put("revoked", all.stream().filter(a -> "已撤销".equals(a.getStatus())).count());
return ApiResp.ok(m);
}
/**
* 调度任务:每天 02:00 自动扫描过期授权并更新状态为「已过期」。
* 超授权自动拦截的基础:业务流程调用 /check 接口时,此任务确保过期授权不被视为有效。
*/
@Scheduled(cron = "0 0 2 * * *")
@Transactional
public void autoExpireScheduled() {
LocalDate today = LocalDate.now();
List<LegalAuthLimit> active = repo.findByStatus("有效");
for (LegalAuthLimit a : active) {
if (a.getExpireDate() == null || a.getExpireDate().isBlank()) continue;
try {
if (LocalDate.parse(a.getExpireDate()).isBefore(today)) {
a.setStatus("已过期");
repo.save(a);
}
} catch (Exception ignored) {}
}
}
private LegalAuthLimit load(Long id) {
return repo.findById(id).orElseThrow(() -> new NotFoundException("授权记录不存在: " + id));
}
}