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(@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 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 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 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 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 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> expireCheck() { LocalDate today = LocalDate.now(); List all = repo.findByStatus("有效"); List 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> check(@RequestParam String grantee, @RequestParam String authType, @RequestParam(defaultValue = "0") Double amountCheck) { List valid = repo.findByGrantee(grantee).stream() .filter(a -> "有效".equals(a.getStatus()) && authType.equals(a.getAuthType())) .toList(); BigDecimal amount = Money.of(amountCheck); Map 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> stats() { List all = repo.findAll(); Map 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 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)); } }