MedicalApplication.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package com.bskj.example;
  2. import com.bskj.framework.license.annotation.LicenseRequired;
  3. import com.bskj.framework.license.service.LicenseService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.context.event.EventListener;
  8. import org.springframework.stereotype.Component;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.web.bind.annotation.*;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. /**
  15. * 医疗管理系统示例应用
  16. * 展示如何集成和使用Medical License组件
  17. *
  18. * @author Medical License Team
  19. * @since 1.0.0
  20. */
  21. @SpringBootApplication
  22. public class MedicalApplication {
  23. public static void main(String[] args) {
  24. SpringApplication.run(MedicalApplication.class, args);
  25. }
  26. }
  27. /**
  28. * 患者管理控制器
  29. * 展示模块级权限控制
  30. */
  31. @RestController
  32. @RequestMapping("/api/patients")
  33. class PatientController {
  34. @Autowired
  35. private LicenseService licenseService;
  36. /**
  37. * 获取患者列表 - 需要患者管理模块权限
  38. */
  39. @LicenseRequired(modules = {"patient_management"})
  40. @GetMapping
  41. public List<Map<String, Object>> getPatients() {
  42. // 模拟返回患者数据
  43. return List.of(
  44. Map.of("id", 1, "name", "张三", "age", 35),
  45. Map.of("id", 2, "name", "李四", "age", 42)
  46. );
  47. }
  48. /**
  49. * 创建患者 - 需要患者管理模块权限,并检查用户数限制
  50. */
  51. @LicenseRequired(
  52. modules = {"patient_management"},
  53. checkUserLimit = true,
  54. message = "创建患者需要患者管理模块授权且不能超过用户数限制"
  55. )
  56. @PostMapping
  57. public Map<String, Object> createPatient(@RequestBody Map<String, Object> patient) {
  58. // 模拟创建患者
  59. patient.put("id", System.currentTimeMillis());
  60. patient.put("createdTime", System.currentTimeMillis());
  61. return patient;
  62. }
  63. /**
  64. * 删除患者 - 严格模式,任何license问题都不允许执行
  65. */
  66. @LicenseRequired(
  67. modules = {"patient_management"},
  68. strict = true,
  69. message = "删除患者操作需要有效的license授权"
  70. )
  71. @DeleteMapping("/{id}")
  72. public Map<String, String> deletePatient(@PathVariable Long id) {
  73. // 模拟删除患者
  74. return Map.of("message", "患者 " + id + " 已删除");
  75. }
  76. }
  77. /**
  78. * 计费管理控制器
  79. * 展示不同模块的权限控制
  80. */
  81. @RestController
  82. @RequestMapping("/api/billing")
  83. class BillingController {
  84. /**
  85. * 获取账单列表 - 需要计费模块权限
  86. */
  87. @LicenseRequired(modules = {"billing"})
  88. @GetMapping
  89. public List<Map<String, Object>> getBills() {
  90. return List.of(
  91. Map.of("id", 1, "patientId", 1, "amount", 1500.00, "status", "已支付"),
  92. Map.of("id", 2, "patientId", 2, "amount", 2300.00, "status", "待支付")
  93. );
  94. }
  95. /**
  96. * 生成账单 - 需要计费模块权限,license过期时仍允许执行(紧急情况)
  97. */
  98. @LicenseRequired(
  99. modules = {"billing"},
  100. allowExpired = true,
  101. message = "生成账单需要计费模块授权"
  102. )
  103. @PostMapping
  104. public Map<String, Object> createBill(@RequestBody Map<String, Object> bill) {
  105. bill.put("id", System.currentTimeMillis());
  106. bill.put("createdTime", System.currentTimeMillis());
  107. bill.put("status", "待支付");
  108. return bill;
  109. }
  110. }
  111. /**
  112. * 报表管理控制器
  113. * 展示高级功能的权限控制
  114. */
  115. @RestController
  116. @RequestMapping("/api/reports")
  117. class ReportController {
  118. /**
  119. * 生成基础报表 - 需要报表模块权限
  120. */
  121. @LicenseRequired(modules = {"reporting"})
  122. @GetMapping("/basic")
  123. public Map<String, Object> getBasicReport() {
  124. return Map.of(
  125. "type", "基础报表",
  126. "patientCount", 150,
  127. "todayVisits", 45,
  128. "generatedTime", System.currentTimeMillis()
  129. );
  130. }
  131. /**
  132. * 生成高级分析报表 - 需要高级分析模块权限
  133. */
  134. @LicenseRequired(
  135. modules = {"advanced_analytics"},
  136. strict = true,
  137. message = "高级分析功能需要专门的模块授权"
  138. )
  139. @GetMapping("/advanced")
  140. public Map<String, Object> getAdvancedReport() {
  141. return Map.of(
  142. "type", "高级分析报表",
  143. "trends", List.of("患者增长趋势", "收入分析", "疾病统计"),
  144. "predictions", Map.of("nextMonthPatients", 180, "revenue", 450000),
  145. "generatedTime", System.currentTimeMillis()
  146. );
  147. }
  148. }
  149. /**
  150. * 系统管理控制器
  151. * 展示编程式license检查
  152. */
  153. @RestController
  154. @RequestMapping("/api/system")
  155. class SystemController {
  156. @Autowired
  157. private LicenseService licenseService;
  158. /**
  159. * 获取license状态信息
  160. */
  161. @GetMapping("/license-status")
  162. public Map<String, Object> getLicenseStatus() {
  163. Map<String, Object> status = new HashMap<>();
  164. status.put("valid", licenseService.isLicenseValid());
  165. status.put("status", licenseService.getLicenseStatus().name());
  166. status.put("remainingDays", licenseService.getRemainingDays());
  167. if (licenseService.getLicenseInfo() != null) {
  168. status.put("customerName", licenseService.getLicenseInfo().getCustomerName());
  169. status.put("productName", licenseService.getLicenseInfo().getProductName());
  170. status.put("endTime", licenseService.getLicenseInfo().getEndTime());
  171. status.put("maxUsers", licenseService.getLicenseInfo().getMaxUsers());
  172. }
  173. String warning = licenseService.getLicenseWarningMessage();
  174. if (warning != null) {
  175. status.put("warning", warning);
  176. }
  177. return status;
  178. }
  179. /**
  180. * 检查特定模块权限
  181. */
  182. @GetMapping("/check-module/{moduleName}")
  183. public Map<String, Object> checkModulePermission(@PathVariable String moduleName) {
  184. boolean permitted = licenseService.isModulePermitted(moduleName);
  185. return Map.of(
  186. "module", moduleName,
  187. "permitted", permitted,
  188. "message", permitted ? "模块已授权" : "模块未授权或license无效"
  189. );
  190. }
  191. /**
  192. * 检查用户数限制
  193. */
  194. @GetMapping("/check-user-limit/{userCount}")
  195. public Map<String, Object> checkUserLimit(@PathVariable int userCount) {
  196. boolean withinLimit = licenseService.isUserCountWithinLimit(userCount);
  197. return Map.of(
  198. "currentUserCount", userCount,
  199. "withinLimit", withinLimit,
  200. "maxUsers", licenseService.getLicenseInfo() != null ?
  201. licenseService.getLicenseInfo().getMaxUsers() : "未知",
  202. "message", withinLimit ? "用户数在限制范围内" : "用户数超过license限制"
  203. );
  204. }
  205. /**
  206. * 手动重新加载license
  207. */
  208. @PostMapping("/reload-license")
  209. public Map<String, String> reloadLicense() {
  210. try {
  211. licenseService.reloadLicense();
  212. return Map.of("message", "License重新加载成功");
  213. } catch (Exception e) {
  214. return Map.of("error", "License重新加载失败: " + e.getMessage());
  215. }
  216. }
  217. }
  218. /**
  219. * License事件监听器
  220. * 展示如何监听license相关事件
  221. */
  222. @Component
  223. class LicenseEventListener {
  224. /**
  225. * 监听license状态变化事件
  226. */
  227. @EventListener
  228. public void handleLicenseStatusChange(Object event) {
  229. // 这里可以处理license状态变化
  230. // 例如:发送邮件通知、记录审计日志、触发业务逻辑等
  231. System.out.println("License状态发生变化: " + event);
  232. }
  233. /**
  234. * 监听license通知事件
  235. */
  236. @EventListener
  237. public void handleLicenseNotification(Object event) {
  238. // 处理license通知
  239. // 例如:显示用户提示、发送系统通知等
  240. System.out.println("License通知: " + event);
  241. }
  242. /**
  243. * 监听服务停机事件
  244. */
  245. @EventListener
  246. public void handleServiceShutdown(Object event) {
  247. // 处理服务停机事件
  248. // 例如:保存数据、清理资源、通知用户等
  249. System.out.println("服务即将停机: " + event);
  250. }
  251. }
  252. /**
  253. * 业务服务示例
  254. * 展示在服务层使用license检查
  255. */
  256. @Component
  257. class PatientService {
  258. @Autowired
  259. private LicenseService licenseService;
  260. /**
  261. * 执行复杂业务操作前的license检查
  262. */
  263. public void performComplexOperation() {
  264. // 检查license是否有效
  265. if (!licenseService.isLicenseValid()) {
  266. throw new RuntimeException("License无效,无法执行操作");
  267. }
  268. // 检查特定模块权限
  269. if (!licenseService.isModulePermitted("advanced_features")) {
  270. throw new RuntimeException("高级功能模块未授权");
  271. }
  272. // 检查用户数限制
  273. int currentUsers = getCurrentActiveUsers();
  274. if (!licenseService.isUserCountWithinLimit(currentUsers)) {
  275. throw new RuntimeException("当前用户数(" + currentUsers + ")超过license限制");
  276. }
  277. // 执行业务逻辑
  278. doComplexBusinessLogic();
  279. }
  280. private int getCurrentActiveUsers() {
  281. // 模拟获取当前活跃用户数
  282. return 85;
  283. }
  284. private void doComplexBusinessLogic() {
  285. // 模拟复杂业务逻辑
  286. System.out.println("执行复杂业务操作...");
  287. }
  288. }