version: '3.8'

services:
  # ============================================================
  # 基础设施服务
  # ============================================================

  # MySQL数据库
  mysql:
    image: mysql:8.0
    container_name: erp-mysql
    environment:
      MYSQL_ROOT_PASSWORD: root123456
      MYSQL_DATABASE: erp_java
      MYSQL_USER: erp_user
      MYSQL_PASSWORD: erp123456
    ports:
      - "3307:3306"
    volumes:
      - mysql_data:/var/lib/mysql
      - ./infrastructure/mysql/init.sql:/docker-entrypoint-initdb.d/init.sql
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --max_connections=1000
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-proot123456"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s

  # Redis缓存
  redis:
    image: redis:7-alpine
    container_name: erp-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes --requirepass redis123456 --maxmemory 512mb --maxmemory-policy allkeys-lru
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "redis123456", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Nacos服务注册与配置中心
  nacos:
    image: nacos/nacos-server:v2.2.3
    container_name: erp-nacos
    environment:
      MODE: standalone
      SPRING_DATASOURCE_PLATFORM: mysql
      MYSQL_SERVICE_HOST: mysql
      MYSQL_SERVICE_DB_NAME: nacos_config
      MYSQL_SERVICE_PORT: 3306
      MYSQL_SERVICE_USER: root
      MYSQL_SERVICE_PASSWORD: root123456
      NACOS_AUTH_ENABLE: "true"
      NACOS_AUTH_TOKEN: "SecretKey012345678901234567890123456789012345678901234567890123456789"
      NACOS_AUTH_IDENTITY_KEY: "serverIdentity"
      NACOS_AUTH_IDENTITY_VALUE: "security"
    ports:
      - "8848:8848"
      - "9848:9848"
      - "9849:9849"
    volumes:
      - nacos_logs:/home/nacos/logs
      - nacos_data:/home/nacos/data
    depends_on:
      mysql:
        condition: service_healthy
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8848/nacos/"]
      interval: 15s
      timeout: 10s
      retries: 5
      start_period: 60s

  # RocketMQ
  rocketmq:
    image: apache/rocketmq:5.1.4
    container_name: erp-rocketmq
    ports:
      - "9876:9876"
      - "10909:10909"
      - "10911:10911"
      - "10912:10912"
    environment:
      NAMESRV_ADDR: "localhost:9876"
      JAVA_OPTS: "-Xms1g -Xmx1g"
    volumes:
      - rocketmq_logs:/home/rocketmq/logs
      - rocketmq_store:/home/rocketmq/store
    networks:
      - erp-network
    command: sh mqnamesrv
    healthcheck:
      test: ["CMD", "sh", "/usr/sbin/rocketmq-ce", "namesrv", "-n", "localhost:9876"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s

  # RocketMQ Broker
  rocketmq-broker:
    image: apache/rocketmq:5.1.4
    container_name: erp-rocketmq-broker
    ports:
      - "10911:10911"
    environment:
      NAMESRV_ADDR: "rocketmq:9876"
      JAVA_OPTS: "-Xms1g -Xmx1g -Xmn512m"
    volumes:
      - rocketmq_store:/home/rocketmq/store
    depends_on:
      - rocketmq
    networks:
      - erp-network
    command: sh mqbroker -n rocketmq:9876 -c ../conf/broker.conf

  # RocketMQ控制台
  rocketmq-console:
    image: apacherocketmq/rocketmq-dashboard:latest
    container_name: erp-rocketmq-console
    ports:
      - "8080:8080"
    environment:
      JAVA_OPTS: "-Drocketmq.namesrv.addr=rocketmq:9876 -Dserver.port=8080"
    depends_on:
      - rocketmq
    networks:
      - erp-network

  # Seata分布式事务
  seata:
    image: seataio/seata-server:1.7.1
    container_name: erp-seata
    ports:
      - "8091:8091"
      - "7091:7091"
    environment:
      SEATA_IP: seata
      SEATA_PORT: "8091"
      STORE_MODE: db
      SERVER_NODE: "1"
    volumes:
      - ./infrastructure/seata/registry.conf:/seata-server/resources/registry.conf
      - ./infrastructure/seata/file.conf:/seata-server/resources/file.conf
    depends_on:
      mysql:
        condition: service_healthy
      nacos:
        condition: service_started
    networks:
      - erp-network

  # SkyWalking OAP
  skywalking-oap:
    image: apache/skywalking-oap-server:9.7.0
    container_name: erp-skywalking-oap
    ports:
      - "11800:11800"
      - "12800:12800"
    environment:
      SW_STORAGE: elasticsearch7
      SW_STORAGE_ES_CLUSTER_NODES: elasticsearch:9200
    depends_on:
      elasticsearch:
        condition: service_healthy
    networks:
      - erp-network

  # SkyWalking UI
  skywalking-ui:
    image: apache/skywalking-ui:9.7.0
    container_name: erp-skywalking-ui
    ports:
      - "8081:8080"
    environment:
      SW_OAP_ADDRESS: skywalking-oap:12800
    depends_on:
      - skywalking-oap
    networks:
      - erp-network

  # Elasticsearch
  elasticsearch:
    image: elasticsearch:7.17.16
    container_name: erp-elasticsearch
    environment:
      discovery.type: single-node
      ES_JAVA_OPTS: -Xms512m -Xmx512m
      xpack.security.enabled: "false"
    ports:
      - "9200:9200"
      - "9300:9300"
    volumes:
      - elasticsearch_data:/usr/share/elasticsearch/data
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9200/_cluster/health"]
      interval: 15s
      timeout: 10s
      retries: 5
      start_period: 60s

  # MinIO对象存储
  minio:
    image: minio/minio:latest
    container_name: erp-minio
    ports:
      - "9000:9000"
      - "9001:9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin123
    volumes:
      - minio_data:/data
    command: server /data --console-address ":9001"
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 15s
      timeout: 10s
      retries: 3
      start_period: 30s

  # ============================================================
  # API网关
  # ============================================================
  gateway:
    build:
      context: ./gateway
      dockerfile: docker/Dockerfile
    container_name: erp-gateway
    ports:
      - "8080:8080"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - NACOS_SERVER_ADDR=nacos:8848
      - NACOS_NAMESPACE=${NACOS_NAMESPACE:-public}
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=redis123456
      - JWT_SECRET=your-256-bit-secret-key-for-jwt-token-generation-erp
      - JAVA_OPTS=-Xms256m -Xmx512m -XX:+UseG1GC
    volumes:
      - gateway_logs:/app/logs
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    depends_on:
      nacos:
        condition: service_healthy
      redis:
        condition: service_healthy

  # ============================================================
  # 业务微服务
  # ============================================================

  user-service:
    build:
      context: .
      dockerfile: services/user-service/Dockerfile
    container_name: erp-user-service
    ports:
      - "8082:8082"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - NACOS_SERVER_ADDR=nacos:8848
      - NACOS_NAMESPACE=${NACOS_NAMESPACE:-public}
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_NAME=erp_java
      - DB_USERNAME=erp_user
      - DB_PASSWORD=erp123456
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=redis123456
      - JAVA_OPTS=-Xms512m -Xmx1024m -XX:+UseG1GC
    volumes:
      - user_logs:/app/logs
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8082/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      nacos:
        condition: service_healthy

  admin-service:
    build:
      context: .
      dockerfile: services/admin-service/Dockerfile
    container_name: erp-admin-service
    ports:
      - "8081:8081"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - NACOS_SERVER_ADDR=nacos:8848
      - NACOS_NAMESPACE=${NACOS_NAMESPACE:-public}
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_NAME=erp_java
      - DB_USERNAME=erp_user
      - DB_PASSWORD=erp123456
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=redis123456
      - JAVA_OPTS=-Xms512m -Xmx1024m -XX:+UseG1GC
    volumes:
      - admin_logs:/app/logs
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8081/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      nacos:
        condition: service_healthy

  product-service:
    build:
      context: .
      dockerfile: services/product-service/Dockerfile
    container_name: erp-product-service
    ports:
      - "8083:8083"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - NACOS_SERVER_ADDR=nacos:8848
      - NACOS_NAMESPACE=${NACOS_NAMESPACE:-public}
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_NAME=erp_java
      - DB_USERNAME=erp_user
      - DB_PASSWORD=erp123456
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=redis123456
      - JAVA_OPTS=-Xms512m -Xmx1024m -XX:+UseG1GC
    volumes:
      - product_logs:/app/logs
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8083/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      nacos:
        condition: service_healthy

  order-service:
    build:
      context: .
      dockerfile: services/order-service/Dockerfile
    container_name: erp-order-service
    ports:
      - "8084:8082"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - NACOS_SERVER_ADDR=nacos:8848
      - NACOS_NAMESPACE=${NACOS_NAMESPACE:-public}
      - ORDER_SERVICE_PORT=8082
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_NAME=erp_java
      - DB_USERNAME=erp_user
      - DB_PASSWORD=erp123456
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=redis123456
      - JAVA_OPTS=-Xms512m -Xmx1024m -XX:+UseG1GC
    volumes:
      - order_logs:/app/logs
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8082/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      nacos:
        condition: service_healthy

  inventory-service:
    build:
      context: .
      dockerfile: services/inventory-service/Dockerfile
    container_name: erp-inventory-service
    ports:
      - "8085:8084"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - NACOS_SERVER_ADDR=nacos:8848
      - NACOS_NAMESPACE=${NACOS_NAMESPACE:-public}
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_NAME=erp_java
      - DB_USERNAME=erp_user
      - DB_PASSWORD=erp123456
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=redis123456
      - JAVA_OPTS=-Xms512m -Xmx1024m -XX:+UseG1GC
    volumes:
      - inventory_logs:/app/logs
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8084/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      nacos:
        condition: service_healthy

  tenant-service:
    build:
      context: .
      dockerfile: services/tenant-service/Dockerfile
    container_name: erp-tenant-service
    ports:
      - "8086:8083"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - NACOS_SERVER_ADDR=nacos:8848
      - NACOS_NAMESPACE=${NACOS_NAMESPACE:-public}
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_NAME=erp_java
      - DB_USERNAME=erp_user
      - DB_PASSWORD=erp123456
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=redis123456
      - JAVA_OPTS=-Xms512m -Xmx1024m -XX:+UseG1GC
    volumes:
      - tenant_logs:/app/logs
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8083/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      nacos:
        condition: service_healthy

  file-service:
    build:
      context: .
      dockerfile: services/file-service/Dockerfile
    container_name: erp-file-service
    ports:
      - "8090:8082"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - NACOS_SERVER_ADDR=nacos:8848
      - NACOS_NAMESPACE=${NACOS_NAMESPACE:-public}
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_NAME=erp_java
      - DB_USERNAME=erp_user
      - DB_PASSWORD=erp123456
      - MINIO_ENDPOINT=http://minio:9000
      - MINIO_ACCESS_KEY=minioadmin
      - MINIO_SECRET_KEY=minioadmin123
      - JAVA_OPTS=-Xms512m -Xmx1024m -XX:+UseG1GC
    volumes:
      - file_logs:/app/logs
      - file_storage:/app/storage
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8082/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      minio:
        condition: service_healthy
      nacos:
        condition: service_healthy

  scheduled-task-service:
    build:
      context: .
      dockerfile: services/scheduled-task-service/Dockerfile
    container_name: erp-scheduled-task-service
    ports:
      - "8091:8088"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - NACOS_SERVER_ADDR=nacos:8848
      - NACOS_NAMESPACE=${NACOS_NAMESPACE:-public}
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_NAME=erp_java
      - DB_USERNAME=erp_user
      - DB_PASSWORD=erp123456
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=redis123456
      - JAVA_OPTS=-Xms512m -Xmx1024m -XX:+UseG1GC
    volumes:
      - task_logs:/app/logs
    networks:
      - erp-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8088/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      nacos:
        condition: service_healthy

  # ============================================================
  # 可选服务（按需启用，注释掉不需要的服务以节省资源）
  # ============================================================

  # report-service:
  #   build:
  #     context: .
  #     dockerfile: services/report-service/Dockerfile
  #   container_name: erp-report-service
  #   ports:
  #     - "8092:8084"
  #   environment:
  #     - SPRING_PROFILES_ACTIVE=docker
  #     - NACOS_SERVER_ADDR=nacos:8848
  #     - DB_HOST=mysql
  #     - DB_PORT=3306
  #     - DB_NAME=erp_java
  #     - DB_USERNAME=erp_user
  #     - DB_PASSWORD=erp123456
  #     - REDIS_HOST=redis
  #     - REDIS_PORT=6379
  #     - REDIS_PASSWORD=redis123456
  #   networks:
  #     - erp-network
  #   restart: unless-stopped
  #   depends_on:
  #     mysql:
  #       condition: service_healthy
  #     redis:
  #       condition: service_healthy

# ============================================================
# 网络定义
# ============================================================
networks:
  erp-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16

# ============================================================
# 持久化卷
# ============================================================
volumes:
  mysql_data:
  redis_data:
  nacos_logs:
  nacos_data:
  rocketmq_logs:
  rocketmq_store:
  elasticsearch_data:
  minio_data:
  gateway_logs:
  user_logs:
  admin_logs:
  product_logs:
  order_logs:
  inventory_logs:
  tenant_logs:
  file_logs:
  file_storage:
  task_logs:
