
以下是从零搭建Java 养老代办服务系统预约、护理、陪诊一体化的完整方案涵盖需求分析、技术选型、数据库设计、核心接口实现及部署流程。一、系统需求分析1. 核心功能模块用户管理老人、家属、护工、管理员多角色注册/登录/权限控制。服务预约护理服务如洗澡、喂饭、康复训练。陪诊服务医院挂号、陪同就医、取药。服务调度根据护工技能、位置、时间智能匹配订单。实时跟踪家属通过小程序查看护工位置、服务进度。评价系统老人/家属对护工服务评分形成信用体系。支付集成支持微信/支付宝支付服务费用。2. 非功能需求高并发支持1000老人同时预约服务。实时性护工位置更新延迟 3秒。安全性用户数据加密存储敏感操作需短信验证。二、技术选型组件技术栈理由后端框架Spring Boot 3.0 Spring Cloud快速开发微服务支持高并发数据库MySQL 8.0主库 Redis 7.0MySQL存储结构化数据Redis缓存热点数据如护工位置消息队列RocketMQ 5.0异步处理订单状态变更、通知推送实时通信Netty WebSocket护工位置实时推送至家属端定位服务高德地图API获取护工实时位置支付微信支付SDK 支付宝沙箱环境对接主流支付渠道前端Vue 3 Uniapp一套代码多端发布小程序/H5/APP部署Docker Kubernetes容器化部署支持弹性伸缩三、数据库设计1. 用户表usersqlCREATE TABLE user ( user_id BIGINT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(100) NOT NULL, -- BCrypt加密 role ENUM(elderly, family, nurse, admin) NOT NULL, real_name VARCHAR(50), phone VARCHAR(20) NOT NULL UNIQUE, id_card VARCHAR(18) UNIQUE, avatar VARCHAR(255), status ENUM(active, frozen) DEFAULT active, create_time DATETIME DEFAULT CURRENT_TIMESTAMP );2. 护工信息表nurse_profilesqlCREATE TABLE nurse_profile ( nurse_id BIGINT PRIMARY KEY, skills JSON NOT NULL, -- 存储技能标签数组如[洗澡,喂饭] experience INT DEFAULT 0, -- 工作经验年 certification VARCHAR(255), -- 证书图片URL rating DECIMAL(2,1) DEFAULT 0, -- 平均评分 online_status BOOLEAN DEFAULT FALSE, -- 是否在线 last_location POINT, -- 存储经纬度需MySQL空间扩展 FOREIGN KEY (nurse_id) REFERENCES user(user_id) );3. 服务订单表service_ordersqlCREATE TABLE service_order ( order_id BIGINT PRIMARY KEY AUTO_INCREMENT, elderly_id BIGINT NOT NULL, nurse_id BIGINT, service_type ENUM(nursing, accompany_doctor) NOT NULL, service_time DATETIME NOT NULL, duration INT NOT NULL, -- 服务时长分钟 status ENUM(pending, assigned, in_progress, completed, cancelled) DEFAULT pending, price DECIMAL(10,2) NOT NULL, address VARCHAR(255) NOT NULL, -- 服务地址 create_time DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (elderly_id) REFERENCES user(user_id), FOREIGN KEY (nurse_id) REFERENCES user(user_id) );4. 实时位置表nurse_locationsqlCREATE TABLE nurse_location ( id BIGINT PRIMARY KEY AUTO_INCREMENT, nurse_id BIGINT NOT NULL, longitude DECIMAL(10,6) NOT NULL, -- 经度 latitude DECIMAL(10,6) NOT NULL, -- 纬度 update_time DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (nurse_id) REFERENCES user(user_id), INDEX idx_nurse_id (nurse_id), INDEX idx_update_time (update_time) );四、核心接口实现1. 护工注册接口javaRestController RequestMapping(/api/nurse) public class NurseController { Autowired private UserService userService; Autowired private NurseProfileService nurseProfileService; PostMapping(/register) public Result registerNurse(RequestBody NurseRegisterDTO dto) { // 1. 注册用户基础信息 User user new User(); user.setUsername(dto.getPhone()); user.setPassword(BCrypt.hashpw(dto.getPassword(), BCrypt.gensalt())); user.setRole(nurse); user.setPhone(dto.getPhone()); Long userId userService.saveUser(user); // 2. 保存护工扩展信息 NurseProfile profile new NurseProfile(); profile.setNurseId(userId); profile.setSkills(dto.getSkills()); // JSON数组转字符串 profile.setCertification(dto.getCertificationUrl()); nurseProfileService.saveProfile(profile); return Result.success(护工注册成功, userId); } }2. 服务预约接口javaRestController RequestMapping(/api/order) public class OrderController { Autowired private OrderService orderService; Autowired private RocketMQTemplate rocketMQTemplate; PostMapping(/create) public Result createOrder(RequestBody OrderCreateDTO dto) { // 1. 校验护工是否在线且有空 boolean isAvailable nurseProfileService.checkNurseAvailability( dto.getNurseId(), dto.getServiceTime(), dto.getDuration() ); if (!isAvailable) { return Result.fail(护工当前不可用); } // 2. 创建订单 Order order new Order(); order.setElderlyId(dto.getElderlyId()); order.setNurseId(dto.getNurseId()); order.setServiceType(dto.getServiceType()); order.setServiceTime(dto.getServiceTime()); order.setDuration(dto.getDuration()); order.setPrice(calculatePrice(dto.getServiceType(), dto.getDuration())); Long orderId orderService.saveOrder(order); // 3. 发送订单分配消息到MQ护工端监听 rocketMQTemplate.convertAndSend(ORDER_ASSIGNED, orderId); return Result.success(订单创建成功, orderId); } private BigDecimal calculatePrice(String serviceType, int duration) { // 示例护理服务50元/小时陪诊服务80元/小时 BigDecimal basePrice nursing.equals(serviceType) ? new BigDecimal(50) : new BigDecimal(80); return basePrice.multiply(new BigDecimal(duration / 60.0)); } }3. 护工位置上报接口javaRestController RequestMapping(/api/nurse/location) public class NurseLocationController { Autowired private NurseLocationService locationService; PostMapping(/update) public Result updateLocation(RequestBody LocationUpdateDTO dto) { // 1. 校验护工身份 User nurse userService.getById(dto.getNurseId()); if (nurse null || !nurse.equals(nurse.getRole())) { return Result.fail(无效的护工ID); } // 2. 保存位置到MySQL NurseLocation location new NurseLocation(); location.setNurseId(dto.getNurseId()); location.setLongitude(dto.getLongitude()); location.setLatitude(dto.getLatitude()); locationService.saveLocation(location); // 3. 更新Redis缓存用于实时查询 String redisKey nurse:location: dto.getNurseId(); redisTemplate.opsForValue().set(redisKey, location, 10, TimeUnit.SECONDS); return Result.success(位置更新成功); } }4. 家属端获取护工位置接口javaRestController RequestMapping(/api/family/nurse) public class FamilyNurseController { Autowired private RedisTemplateString, NurseLocation redisTemplate; GetMapping(/location/{nurseId}) public Result getNurseLocation(PathVariable Long nurseId) { String redisKey nurse:location: nurseId; NurseLocation location redisTemplate.opsForValue().get(redisKey); if (location null) { return Result.fail(护工位置未上报); } return Result.success(location); } }五、部署方案1. 容器化部署Dockerdockerfile# 后端服务Dockerfile示例 FROM openjdk:17-jdk-slim VOLUME /tmp ARG JAR_FILEtarget/elderly-care-0.0.1-SNAPSHOT.jar COPY ${JAR_FILE} app.jar ENTRYPOINT [java,-jar,/app.jar]2. Kubernetes编排yaml# deployment.yaml示例 apiVersion: apps/v1 kind: Deployment metadata: name: elderly-care-service spec: replicas: 3 selector: matchLabels: app: elderly-care template: metadata: labels: app: elderly-care spec: containers: - name: elderly-care image: registry.example.com/elderly-care:latest ports: - containerPort: 8080 env: - name: SPRING_DATASOURCE_URL value: jdbc:mysql://mysql-service:3306/elderly_care六、扩展功能建议AI健康预警集成血压/心率异常检测模型。电子围栏通过GPS定位防止老人走失。区块链存证服务记录上链确保不可篡改。通过以上方案可快速搭建一个高可用、可扩展的养老代办服务系统覆盖预约、护理、陪诊全流程。实际开发中需根据业务规模调整数据库分库分表策略并完善异常处理机制如护工爽约补偿逻辑。