
--学院
CREATE TABLE IF NOT EXISTS college_table (
  college_id SERIAL PRIMARY KEY,
  college_name VARCHAR(255) DEFAULT NULL
);
--专业
CREATE TABLE IF NOT EXISTS major_table (
  major_table_id SERIAL PRIMARY KEY,
  major_table_name VARCHAR(255) DEFAULT NULL,
  college_id INT DEFAULT NULL
);
--用户表
CREATE TABLE IF NOT EXISTS user_table (
  user_id SERIAL PRIMARY KEY,
  user_name VARCHAR(50) NOT NULL,
  s_t_id VARCHAR(20) NOT NULL,
  user_password VARCHAR(255) NOT NULL,
  user_privileges INT NOT NULL DEFAULT 1,
  email VARCHAR(50) NOT NULL,
  college_id INT NOT NULL,
  major_table_id INT DEFAULT NULL,
  phone VARCHAR(20) DEFAULT NULL,
  teacher_title VARCHAR(50) DEFAULT NULL,
  birthdate DATE DEFAULT NULL,
  gender VARCHAR(10) DEFAULT NULL CHECK (gender IN ('男', '女')),
  user_status INT DEFAULT 0
);
--竞赛表
CREATE TABLE IF NOT EXISTS competition_table (
  competition_id SERIAL PRIMARY KEY,
  competition_name VARCHAR(50) NOT NULL,
  user_id INT NOT NULL,
  registration_start_time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  registration_end_time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  announcement_link VARCHAR(255) DEFAULT '',
  competition_status INT DEFAULT 0
);
--资料表
CREATE TABLE IF NOT EXISTS information_table (
    profile_id SERIAL PRIMARY KEY,
    user_id INT NOT NULL,
    competition_id INT NOT NULL,
    personal_info TEXT,
    related_data TEXT,
    CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES user_table (user_id),
    CONSTRAINT fk_competition_id FOREIGN KEY (competition_id) REFERENCES competition_table (competition_id),
);
--报名表
CREATE TABLE IF NOT EXISTS registration_table (
  registration_id SERIAL PRIMARY KEY,
  student_id INT NOT NULL,
  competition_id INT NOT NULL,
  team_leader_id INT DEFAULT NULL,
  competition_type VARCHAR(50) NOT NULL DEFAULT '校级' CHECK (competition_type IN ('国家级', '省级', '市/区级', '校级', '院级', '其他')),
  award_level VARCHAR(50) DEFAULT NULL CHECK (award_level IN ('未获奖', '一等奖', '二等奖', '三等奖')),
  registration_time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  registration_status VARCHAR(50) NOT NULL DEFAULT '等待学校审核' CHECK (registration_status IN ('等待学校审核', '等待主办方审核', '成功报名', '已取消')),
  certificate_path VARCHAR(255) DEFAULT '',
  additional_info VARCHAR(255) DEFAULT ''
);
--文章表
CREATE TABLE IF NOT EXISTS article_table (
  article_id SERIAL PRIMARY KEY,
  user_id INT NOT NULL,
  article_title TEXT NOT NULL,
  brief_content TEXT,
  article_content TEXT NOT NULL,
  article_type VARCHAR(50) NOT NULL CHECK (article_type IN ('公告', '活动', '讨论')),
  publish_time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  competition_id INT
);
--评论表
CREATE TABLE IF NOT EXISTS comment_table (
  comment_id SERIAL PRIMARY KEY,
  user_id INT NOT NULL,
  article_id INT NOT NULL,
  comment_content TEXT NOT NULL,
  publish_time TIMESTAMP WITHOUT TIME ZONE NOT NULL
);
--设置外键约束
ALTER TABLE major_table ADD CONSTRAINT fk_college_id FOREIGN KEY (college_id) REFERENCES college_table (college_id);
ALTER TABLE user_table ADD CONSTRAINT fk_college_id FOREIGN KEY (college_id) REFERENCES college_table (college_id);
ALTER TABLE user_table ADD CONSTRAINT fk_major_table_id FOREIGN KEY (major_table_id) REFERENCES major_table (major_table_id);
ALTER TABLE competition_table ADD CONSTRAINT competition_fk_user_id FOREIGN KEY (user_id) REFERENCES user_table (user_id);
ALTER TABLE registration_table ADD CONSTRAINT fk_competition_id FOREIGN KEY (competition_id) REFERENCES competition_table (competition_id);
ALTER TABLE registration_table ADD CONSTRAINT registration_fk_student_id FOREIGN KEY (student_id) REFERENCES user_table (user_id);
ALTER TABLE registration_table ADD CONSTRAINT registration_fk_team_leader_id FOREIGN KEY (team_leader_id) REFERENCES user_table (user_id);
ALTER TABLE article_table ADD CONSTRAINT article_fk_user_id FOREIGN KEY (user_id) REFERENCES user_table (user_id);
ALTER TABLE comment_table ADD CONSTRAINT comment_fk_user_id FOREIGN KEY (user_id) REFERENCES user_table (user_id);
ALTER TABLE comment_table ADD CONSTRAINT fk_article_id FOREIGN KEY (article_id) REFERENCES article_table (article_id);
ALTER TABLE article_table
    ADD CONSTRAINT fk_article_competition_id
        FOREIGN KEY (competition_id)
            REFERENCES competition_table (competition_id)
            ON DELETE SET NULL;


--插入数据
INSERT INTO college_table (college_name) VALUES
  (NULL),
	('会计学院'),
	('经济贸易学院'),
	('电信工程学院'),
	('服装与技术贸易学院'),
	('医药工程学院'),
	('人文与艺术学院'),
	('计算级学院'),
	('国际教育学院'),
	('镐京书院'),
	('外语教育部'),
	('思政部'),
	('体育部');
INSERT INTO major_table (major_table_name, college_id) VALUES
  (NULL, 1),
	('税收学', 2),
	('会计学', 2),
	('审计学', 2),
	('金融工程', 2),
	('财务管理', 2),
	('国际经济与贸易', 3),
	('市场营销', 3),
	('工商管理', 3),
	('人力资源管理', 3),
	('行政管理', 3),
	('电子商务', 3),
	('电气工程及其自动化', 4),
	('电子信息工程', 4),
	('服装设计', 5),
	('管理运营', 5),
	('服装科技', 5),
	('健康服务与管理', 7),
	('药物制剂', 7),
	('计算机科学与技术', 8),
	('网络工程', 8),
	('物联网工程', 8),
	('智能科学与技术', 8);
INSERT INTO user_table (user_name, s_t_id, user_password, user_privileges, email, college_id, major_table_id, phone, teacher_title, birthdate, gender, user_status) VALUES
	('史旺', 'Admin', '1234', 0, '1919093209@qq.com', 7, 1, '', '', NULL, NULL, NULL),
	('史旺', '213010248', '0248', 1, '1919093209@qq.com', 7, 20, '17342351231', '', '2002-04-04', '男', NULL),
	('刘舒光', '213010127', '0127', 2, 'qqsyun@gmail.com', 7, 20, '18509144920', '教授', NULL, NULL, 0);
INSERT INTO competition_table (competition_name, user_id, registration_start_time, registration_end_time, announcement_link, competition_status) VALUES
	('中国大学生计算机设计大赛', 3, '2023-04-01 00:00:00', '2023-05-01 00:00:00', 'https://jsjds.blcu.edu.cn/', 0);
INSERT INTO registration_table (student_id, competition_id, team_leader_id, competition_type, award_level, registration_time, registration_status, certificate_path, additional_info) VALUES
	(2, 1, 2, '校级', '未获奖', '2023-04-02 00:00:00', '成功报名', NULL, '');
INSERT INTO article_table (user_id, article_title, brief_content, article_content, article_type, publish_time) VALUES
	(3, '全国大学生计算级设计大赛报名通道开启！', '第17届全国大学生计算级设计大赛报名通道开启，请大家踊跃报名！', '中国大学生计算机设计大赛是非营利的、公益性的、科技型的群众活动。大赛的生命线与遵从的原则是“三公”，即公开、公平、公正。公平、公正是灵魂和基础，公开是公平、公正的保障。', '公告', '2023-03-01 08:00:00'),
	(2, '关于开展科研讨论会的通知', '', '科研讨论会', '活动', '2023-03-05 09:00:00');
INSERT INTO comment_table (user_id, article_id, comment_content, publish_time) VALUES
	(2, 1, '非常期待！', '2023-03-01 09:00:00');
	

