| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- package com.bskj.example;
- import com.bskj.framework.license.annotation.LicenseRequired;
- import com.bskj.framework.license.service.LicenseService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.event.EventListener;
- import org.springframework.stereotype.Component;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * 医疗管理系统示例应用
- * 展示如何集成和使用Medical License组件
- *
- * @author Medical License Team
- * @since 1.0.0
- */
- @SpringBootApplication
- public class MedicalApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(MedicalApplication.class, args);
- }
- }
- /**
- * 患者管理控制器
- * 展示模块级权限控制
- */
- @RestController
- @RequestMapping("/api/patients")
- class PatientController {
-
- @Autowired
- private LicenseService licenseService;
-
- /**
- * 获取患者列表 - 需要患者管理模块权限
- */
- @LicenseRequired(modules = {"patient_management"})
- @GetMapping
- public List<Map<String, Object>> getPatients() {
- // 模拟返回患者数据
- return List.of(
- Map.of("id", 1, "name", "张三", "age", 35),
- Map.of("id", 2, "name", "李四", "age", 42)
- );
- }
-
- /**
- * 创建患者 - 需要患者管理模块权限,并检查用户数限制
- */
- @LicenseRequired(
- modules = {"patient_management"},
- checkUserLimit = true,
- message = "创建患者需要患者管理模块授权且不能超过用户数限制"
- )
- @PostMapping
- public Map<String, Object> createPatient(@RequestBody Map<String, Object> patient) {
- // 模拟创建患者
- patient.put("id", System.currentTimeMillis());
- patient.put("createdTime", System.currentTimeMillis());
- return patient;
- }
-
- /**
- * 删除患者 - 严格模式,任何license问题都不允许执行
- */
- @LicenseRequired(
- modules = {"patient_management"},
- strict = true,
- message = "删除患者操作需要有效的license授权"
- )
- @DeleteMapping("/{id}")
- public Map<String, String> deletePatient(@PathVariable Long id) {
- // 模拟删除患者
- return Map.of("message", "患者 " + id + " 已删除");
- }
- }
- /**
- * 计费管理控制器
- * 展示不同模块的权限控制
- */
- @RestController
- @RequestMapping("/api/billing")
- class BillingController {
-
- /**
- * 获取账单列表 - 需要计费模块权限
- */
- @LicenseRequired(modules = {"billing"})
- @GetMapping
- public List<Map<String, Object>> getBills() {
- return List.of(
- Map.of("id", 1, "patientId", 1, "amount", 1500.00, "status", "已支付"),
- Map.of("id", 2, "patientId", 2, "amount", 2300.00, "status", "待支付")
- );
- }
-
- /**
- * 生成账单 - 需要计费模块权限,license过期时仍允许执行(紧急情况)
- */
- @LicenseRequired(
- modules = {"billing"},
- allowExpired = true,
- message = "生成账单需要计费模块授权"
- )
- @PostMapping
- public Map<String, Object> createBill(@RequestBody Map<String, Object> bill) {
- bill.put("id", System.currentTimeMillis());
- bill.put("createdTime", System.currentTimeMillis());
- bill.put("status", "待支付");
- return bill;
- }
- }
- /**
- * 报表管理控制器
- * 展示高级功能的权限控制
- */
- @RestController
- @RequestMapping("/api/reports")
- class ReportController {
-
- /**
- * 生成基础报表 - 需要报表模块权限
- */
- @LicenseRequired(modules = {"reporting"})
- @GetMapping("/basic")
- public Map<String, Object> getBasicReport() {
- return Map.of(
- "type", "基础报表",
- "patientCount", 150,
- "todayVisits", 45,
- "generatedTime", System.currentTimeMillis()
- );
- }
-
- /**
- * 生成高级分析报表 - 需要高级分析模块权限
- */
- @LicenseRequired(
- modules = {"advanced_analytics"},
- strict = true,
- message = "高级分析功能需要专门的模块授权"
- )
- @GetMapping("/advanced")
- public Map<String, Object> getAdvancedReport() {
- return Map.of(
- "type", "高级分析报表",
- "trends", List.of("患者增长趋势", "收入分析", "疾病统计"),
- "predictions", Map.of("nextMonthPatients", 180, "revenue", 450000),
- "generatedTime", System.currentTimeMillis()
- );
- }
- }
- /**
- * 系统管理控制器
- * 展示编程式license检查
- */
- @RestController
- @RequestMapping("/api/system")
- class SystemController {
-
- @Autowired
- private LicenseService licenseService;
-
- /**
- * 获取license状态信息
- */
- @GetMapping("/license-status")
- public Map<String, Object> getLicenseStatus() {
- Map<String, Object> status = new HashMap<>();
- status.put("valid", licenseService.isLicenseValid());
- status.put("status", licenseService.getLicenseStatus().name());
- status.put("remainingDays", licenseService.getRemainingDays());
-
- if (licenseService.getLicenseInfo() != null) {
- status.put("customerName", licenseService.getLicenseInfo().getCustomerName());
- status.put("productName", licenseService.getLicenseInfo().getProductName());
- status.put("endTime", licenseService.getLicenseInfo().getEndTime());
- status.put("maxUsers", licenseService.getLicenseInfo().getMaxUsers());
- }
-
- String warning = licenseService.getLicenseWarningMessage();
- if (warning != null) {
- status.put("warning", warning);
- }
-
- return status;
- }
-
- /**
- * 检查特定模块权限
- */
- @GetMapping("/check-module/{moduleName}")
- public Map<String, Object> checkModulePermission(@PathVariable String moduleName) {
- boolean permitted = licenseService.isModulePermitted(moduleName);
- return Map.of(
- "module", moduleName,
- "permitted", permitted,
- "message", permitted ? "模块已授权" : "模块未授权或license无效"
- );
- }
-
- /**
- * 检查用户数限制
- */
- @GetMapping("/check-user-limit/{userCount}")
- public Map<String, Object> checkUserLimit(@PathVariable int userCount) {
- boolean withinLimit = licenseService.isUserCountWithinLimit(userCount);
- return Map.of(
- "currentUserCount", userCount,
- "withinLimit", withinLimit,
- "maxUsers", licenseService.getLicenseInfo() != null ?
- licenseService.getLicenseInfo().getMaxUsers() : "未知",
- "message", withinLimit ? "用户数在限制范围内" : "用户数超过license限制"
- );
- }
-
- /**
- * 手动重新加载license
- */
- @PostMapping("/reload-license")
- public Map<String, String> reloadLicense() {
- try {
- licenseService.reloadLicense();
- return Map.of("message", "License重新加载成功");
- } catch (Exception e) {
- return Map.of("error", "License重新加载失败: " + e.getMessage());
- }
- }
- }
- /**
- * License事件监听器
- * 展示如何监听license相关事件
- */
- @Component
- class LicenseEventListener {
-
- /**
- * 监听license状态变化事件
- */
- @EventListener
- public void handleLicenseStatusChange(Object event) {
- // 这里可以处理license状态变化
- // 例如:发送邮件通知、记录审计日志、触发业务逻辑等
- System.out.println("License状态发生变化: " + event);
- }
-
- /**
- * 监听license通知事件
- */
- @EventListener
- public void handleLicenseNotification(Object event) {
- // 处理license通知
- // 例如:显示用户提示、发送系统通知等
- System.out.println("License通知: " + event);
- }
-
- /**
- * 监听服务停机事件
- */
- @EventListener
- public void handleServiceShutdown(Object event) {
- // 处理服务停机事件
- // 例如:保存数据、清理资源、通知用户等
- System.out.println("服务即将停机: " + event);
- }
- }
- /**
- * 业务服务示例
- * 展示在服务层使用license检查
- */
- @Component
- class PatientService {
-
- @Autowired
- private LicenseService licenseService;
-
- /**
- * 执行复杂业务操作前的license检查
- */
- public void performComplexOperation() {
- // 检查license是否有效
- if (!licenseService.isLicenseValid()) {
- throw new RuntimeException("License无效,无法执行操作");
- }
-
- // 检查特定模块权限
- if (!licenseService.isModulePermitted("advanced_features")) {
- throw new RuntimeException("高级功能模块未授权");
- }
-
- // 检查用户数限制
- int currentUsers = getCurrentActiveUsers();
- if (!licenseService.isUserCountWithinLimit(currentUsers)) {
- throw new RuntimeException("当前用户数(" + currentUsers + ")超过license限制");
- }
-
- // 执行业务逻辑
- doComplexBusinessLogic();
- }
-
- private int getCurrentActiveUsers() {
- // 模拟获取当前活跃用户数
- return 85;
- }
-
- private void doComplexBusinessLogic() {
- // 模拟复杂业务逻辑
- System.out.println("执行复杂业务操作...");
- }
- }
|