以下是从零搭建 Java 养老代办服务系统(预约、护理、陪诊一体化)的完整方案,涵盖需求分析、技术选型、数据库设计、核心接口实现及部署流程。


一、系统需求分析

1. 核心功能模块

  • 用户管理:老人、家属、护工、管理员多角色注册/登录/权限控制。
  • 服务预约
    • 护理服务(如洗澡、喂饭、康复训练)。
    • 陪诊服务(医院挂号、陪同就医、取药)。
  • 服务调度:根据护工技能、位置、时间智能匹配订单。
  • 实时跟踪:家属通过小程序查看护工位置、服务进度。
  • 评价系统:老人/家属对护工服务评分,形成信用体系。
  • 支付集成:支持微信/支付宝支付服务费用。

2. 非功能需求

  • 高并发:支持1000+老人同时预约服务。
  • 实时性:护工位置更新延迟 < 3秒。
  • 安全性:用户数据加密存储,敏感操作需短信验证。

二、技术选型

组件 技术栈 理由
后端框架 Spring Boot 3.0 + Spring Cloud 快速开发微服务,支持高并发
数据库 MySQL 8.0(主库) + Redis 7.0 MySQL存储结构化数据,Redis缓存热点数据(如护工位置)
消息队列 RocketMQ 5.0 异步处理订单状态变更、通知推送
实时通信 Netty + WebSocket 护工位置实时推送至家属端
定位服务 高德地图API 获取护工实时位置
支付 微信支付SDK + 支付宝沙箱环境 对接主流支付渠道
前端 Vue 3 + Uniapp 一套代码多端发布(小程序/H5/APP)
部署 Docker + Kubernetes 容器化部署,支持弹性伸缩

三、数据库设计

1. 用户表(user


sql

CREATE 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_profile


sql

CREATE 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_order


sql

CREATE 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_location


sql

CREATE 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. 护工注册接口


java

@RestController
@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. 服务预约接口


java

@RestController
@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. 护工位置上报接口


java

@RestController
@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. 家属端获取护工位置接口


java

@RestController
@RequestMapping("/api/family/nurse")
public class FamilyNurseController {

    @Autowired
    private RedisTemplate<String, 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. 容器化部署(Docker)


dockerfile

# 后端服务Dockerfile示例
FROM openjdk:17-jdk-slim
VOLUME /tmp
ARG JAR_FILE=target/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"

六、扩展功能建议

  1. AI健康预警:集成血压/心率异常检测模型。
  2. 电子围栏:通过GPS定位防止老人走失。
  3. 区块链存证:服务记录上链,确保不可篡改。

通过以上方案,可快速搭建一个高可用、可扩展的养老代办服务系统,覆盖预约、护理、陪诊全流程。实际开发中需根据业务规模调整数据库分库分表策略,并完善异常处理机制(如护工爽约补偿逻辑)。

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐