diff --git a/trading_assistant_api/docker-compose-dev.yml b/trading_assistant_api/docker-compose-dev.yml deleted file mode 100644 index 0a6b066..0000000 --- a/trading_assistant_api/docker-compose-dev.yml +++ /dev/null @@ -1,63 +0,0 @@ -version: '3.8' -services: - # 公共PostgresSQL - postgres: - image: postgres:18.1-alpine - container_name: trading-assistant-postgres - environment: - POSTGRES_USER: ${PG_USER:-root} - POSTGRES_PASSWORD: ${PG_PWD:-123456} - POSTGRES_DB: ${PG_DB:-monorepo} - ports: - - "${PG_PORT:-5432}:5432" - volumes: - - ./deploy/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql - - pg_data:/var/lib/postgresql/data - networks: - - trading-assistant-net - restart: always - - # 公共Redis - redis: - image: redis:8.4.-alpine - container_name: trading-assistant-redis - ports: - - "${REDIS_PORT:-6379}:6379" - volumes: - - ./deploy/redis/redis.conf:/etc/redis/redis.conf - - redis_data:/data - command: redis-server /etc/redis/redis.conf - networks: - - trading-assistant-net - restart: always - - # 【用户】 - user: - build: - context: ./services/user - dockerfile: Dockerfile - container_name: trading-assistant-user - environment: - - GIN_MODE=release - - PG_ADDR=postgres:5432 - - REDIS_ADDR=redis:6379 - ports: - - "8080:8080" - volumes: - - ./logs/user:/app/logs - networks: - - trading-assistant-net - restart: always - depends_on: - - postgres - - redis - -# 全局网络 -networks: - trading-assistant-net: - driver: bridge - -# 全局数据卷 -volumes: - pg_data: - redis_data: diff --git a/trading_assistant_api/docker-compose.yml b/trading_assistant_api/docker-compose.yml deleted file mode 100644 index 35add92..0000000 --- a/trading_assistant_api/docker-compose.yml +++ /dev/null @@ -1,68 +0,0 @@ -version: '3.8' -services: - # 公共PostgresSQL - postgres: - image: postgres:18.1-alpine - container_name: trading-assistant-postgres - environment: - POSTGRES_USER: ${PG_USER:-root} - POSTGRES_PASSWORD: ${PG_PWD:-123456} - POSTGRES_DB: ${PG_DB:-monorepo} - ports: - - "${PG_PORT:-5432}:5432" - volumes: - - ./deploy/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql - - pg_data:/var/lib/postgresql/data - networks: - - trading-assistant-net - restart: always - - # 公共Redis - redis: - image: redis:8.4.-alpine - container_name: trading-assistant-redis - ports: - - "${REDIS_PORT:-6379}:6379" - volumes: - - ./deploy/redis/redis.conf:/etc/redis/redis.conf - - redis_data:/data - command: redis-server /etc/redis/redis.conf - networks: - - trading-assistant-net - restart: always - - # 【业务服务模板】新增业务时复制以下块修改 - # user: - # build: - # context: ./services/user - # dockerfile: Dockerfile - # container_name: trading-assistant-user - # environment: - # - GIN_MODE=release - # - PG_ADDR=postgres:5432 - # - REDIS_ADDR=redis:6379 - # ports: - # - "8080:8080" - # volumes: - # - ./logs/user:/app/logs - # networks: - # - trading-assistant-net - # restart: always - # depends_on: - # - postgres - # - redis - # deploy: - # resources: - # limits: - # cpus: "0.5" - # memory: "512M" - -# 全局网络 -networks: - trading-assistant-net: - driver: bridge - -# 全局数据卷 -volumes: - pg_data: - redis_data: diff --git a/trading_assistant_api/services/country/api/user_api.go b/trading_assistant_api/services/country/api/country_api.go similarity index 100% rename from trading_assistant_api/services/country/api/user_api.go rename to trading_assistant_api/services/country/api/country_api.go diff --git a/trading_assistant_api/services/country/dao/user_dao.go b/trading_assistant_api/services/country/dao/country_dao.go similarity index 100% rename from trading_assistant_api/services/country/dao/user_dao.go rename to trading_assistant_api/services/country/dao/country_dao.go diff --git a/trading_assistant_api/services/country/deploy/postgres/init.sql b/trading_assistant_api/services/country/deploy/postgres/init.sql index 602418a..2413da1 100644 --- a/trading_assistant_api/services/country/deploy/postgres/init.sql +++ b/trading_assistant_api/services/country/deploy/postgres/init.sql @@ -1,4 +1,113 @@ --- PostgresSQL全局初始化脚本 --- 所有业务公共建库/建表语句写在这里,单独业务表建议在各自业务中处理 -CREATE DATABASE IF NOT EXISTS monorepo; -\c monorepo; +---------------------------------- 国家主表 ---------------------------------- + +-- 创建国家ID记录表(主表,存储基础主键+通用字段) +CREATE TABLE IF NOT EXISTS countries ( + country_id UUID PRIMARY KEY DEFAULT uuidv7(), + deleted boolean DEFAULT false, + create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +-- 表注释 +COMMENT ON TABLE countries IS '国家主表,仅存储国家基础主键及通用公共字段,关联国家/地区子表'; +-- 字段注释 +COMMENT ON COLUMN countries.country_id IS '国家主键ID,PG18原生UUIDv7,作为所有子表的关联外键'; +COMMENT ON COLUMN countries.deleted IS '逻辑删除标识,false-未删除,true-已删除,默认未删除'; +COMMENT ON COLUMN countries.create_time IS '记录创建时间,默认当前系统时间,不可手动修改'; +COMMENT ON COLUMN countries.update_time IS '记录最后更新时间,默认当前系统时间'; + +-- 创建update_time自动刷新触发器函数(所有表复用) +CREATE OR REPLACE FUNCTION update_table_modify_time() +RETURNS TRIGGER AS $$ +BEGIN + NEW.update_time = CURRENT_TIMESTAMP; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; +-- 绑定触发器到国家主表 +CREATE TRIGGER tr_countries_update_time +BEFORE UPDATE ON countries +FOR EACH ROW +EXECUTE FUNCTION update_table_modify_time(); + + + +---------------------------------- 国家核心信息表 ---------------------------------- + +-- 创建国家核心信息记录表(子表,存储国家编码/名称等核心信息) +CREATE TABLE IF NOT EXISTS country_core_infos ( + core_id UUID PRIMARY KEY DEFAULT uuidv7(), + country_id UUID NOT NULL, + country_code VARCHAR(5) NOT NULL, -- 如CN(中国)/US(美国),遵循ISO 3166-1 + country_cn_short_name VARCHAR(50) NOT NULL, -- 如中国/美国,中文简称 + deleted boolean DEFAULT false, + create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + -- 外键约束:关联国家主表,保证数据一致性(删除主表时级联置空/禁止删除,按需选择) + CONSTRAINT fk_country_core_infos_country_id + FOREIGN KEY (country_id) REFERENCES countries (country_id) + ON DELETE RESTRICT -- 主表国家未删除时,禁止删除子表信息(推荐生产使用) + -- ON DELETE SET NULL -- 可选:主表删除时,子表country_id置空(适合软删除场景) +); + +-- 表注释 +COMMENT ON TABLE country_core_infos IS '国家核心信息子表,存储国家编码、中文名称等专属信息,与国家主表一对一关联'; +-- 字段注释 +COMMENT ON COLUMN country_core_infos.core_id IS '子表主键ID,PG18原生UUIDv7'; +COMMENT ON COLUMN country_core_infos.country_id IS '关联国家主表countries.country_id,外键约束'; +COMMENT ON COLUMN country_core_infos.country_code IS '国家二位编码,遵循ISO 3166-1,如CN(中国)/US(美国),唯一标识国家'; +COMMENT ON COLUMN country_core_infos.country_cn_short_name IS '国家中文简称,如中国/美国,业务展示专用'; +COMMENT ON COLUMN country_core_infos.deleted IS '逻辑删除标识,false-未删除,true-已删除,默认未删除'; +COMMENT ON COLUMN country_core_infos.create_time IS '记录创建时间,默认当前系统时间,不可手动修改'; +COMMENT ON COLUMN country_core_infos.update_time IS '记录最后更新时间,更新时触发器自动刷新'; + +-- 索引:优化高频业务查询(核心) +CREATE UNIQUE INDEX uk_country_core_infos_code ON country_core_infos (country_code) WHERE deleted = false; -- 唯一索引:国家编码不重复(未删除数据) +CREATE INDEX idx_country_core_infos_country_id ON country_core_infos (country_id, deleted); -- 联合索引:按国家ID关联查询+过滤删除数据 +CREATE INDEX idx_country_core_infos_name ON country_core_infos (country_cn_short_name, deleted); -- 联合索引:按国家名称模糊查询+过滤删除数据 + +-- 绑定触发器:更新时自动刷新update_time +CREATE TRIGGER tr_country_core_infos_update_time +BEFORE UPDATE ON country_core_infos +FOR EACH ROW +EXECUTE FUNCTION update_table_modify_time(); + + +---------------------------------- 国家地区核心信息表 ---------------------------------- + +-- 创建国家地区信息记录表(子表,存储国家下属地区编码/名称等信息,如中国香港/美国纽约) +CREATE TABLE IF NOT EXISTS country_region_core_infos ( + region_id UUID PRIMARY KEY DEFAULT uuidv7(), + country_id UUID NOT NULL, + region_code VARCHAR(5) NOT NULL, -- 如HK(香港)/TW(台湾)/NYC(纽约),建议按 国家码-地区码 设计如CN-HK + region_cn_short_name VARCHAR(50) NOT NULL, -- 如香港/台湾/纽约,中文简称 + deleted boolean DEFAULT false, + create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + -- 外键约束:关联国家主表,保证数据一致性 + CONSTRAINT fk_country_region_core_infos_country_id + FOREIGN KEY (country_id) REFERENCES countries (country_id) + ON DELETE RESTRICT -- 主表国家未删除时,禁止删除子表地区信息(推荐生产使用) +); + +-- 表注释 +COMMENT ON TABLE country_region_core_infos IS '国家地区核心信息子表,存储国家下属地区编码、中文名称等信息,与国家主表一对多关联,支持区分中国香港/澳门/台湾等地区'; +-- 字段注释 +COMMENT ON COLUMN country_region_core_infos.region_id IS '子表主键ID,PG18原生UUIDv7'; +COMMENT ON COLUMN country_region_core_infos.country_id IS '关联国家主表countries.country_id,外键约束,标识地区所属国家'; +COMMENT ON COLUMN country_region_core_infos.region_code IS '地区编码,如HK(香港)/TW(台湾)/NYC(纽约),建议遵循ISO 3166-2设计为CN-HK/CN-TW'; +COMMENT ON COLUMN country_region_core_infos.region_cn_short_name IS '地区中文简称,如香港/台湾/纽约,业务展示专用'; +COMMENT ON COLUMN country_region_core_infos.deleted IS '逻辑删除标识,false-未删除,true-已删除,默认未删除'; +COMMENT ON COLUMN country_region_core_infos.create_time IS '记录创建时间,默认当前系统时间,不可手动修改'; +COMMENT ON COLUMN country_region_core_infos.update_time IS '记录最后更新时间,更新时触发器自动刷新'; + +-- 索引:优化高频业务查询(核心) +CREATE UNIQUE INDEX uk_country_region_core_infos_country_region ON country_region_core_infos (country_id, region_code) WHERE deleted = false; -- 唯一索引:同一国家下地区编码不重复(未删除数据) +CREATE INDEX idx_country_region_core_infos_country_id ON country_region_core_infos (country_id, deleted); -- 联合索引:按国家ID查询下属所有地区+过滤删除数据 +CREATE INDEX idx_country_region_core_infos_name ON country_region_core_infos (region_cn_short_name, deleted); -- 联合索引:按地区名称模糊查询+过滤删除数据 + +-- 绑定触发器:更新时自动刷新update_time +CREATE TRIGGER tr_country_region_core_infos_update_time +BEFORE UPDATE ON country_region_core_infos +FOR EACH ROW +EXECUTE FUNCTION update_table_modify_time(); \ No newline at end of file diff --git a/trading_assistant_api/services/country/go.mod b/trading_assistant_api/services/country/go.mod new file mode 100644 index 0000000..905c1b0 --- /dev/null +++ b/trading_assistant_api/services/country/go.mod @@ -0,0 +1,3 @@ +module country + +go 1.25.7