原型
1.新建,上传报告可多选部门
2.查询,可多选部门
数据库(Postgresql)
方式一 新增字段Jsonb:
CREATE TABLE public.admin_report (admin_report_uuid uuid DEFAULT gen_random_uuid() NOT NULL,admin_report_title text NULL,admin_report_theme_id int4 NULL,admin_report_link text NULL,create_time timestamptz DEFAULT now() NULL,update_time timestamptz DEFAULT now() NULL,admin_report_type_id int4 NULL,admin_report_id text NOT NULL,departments jsonb NULL,CONSTRAINT admin_report_admin_report_id_key UNIQUE (admin_report_id),CONSTRAINT admin_report_pkey PRIMARY KEY (admin_report_uuid),CONSTRAINT admin_report_to_admin_theme_id FOREIGN KEY (admin_report_theme_id) REFERENCES public.admin_theme(admin_theme_id) ON DELETE SET NULL ON UPDATE CASCADE,CONSTRAINT admin_report_to_report_type_id FOREIGN KEY (admin_report_type_id) REFERENCES public.admin_report_type(admin_report_type_id) ON DELETE SET NULL ON UPDATE CASCADE
);
class AdminReport(Base):__tablename__ = "admin_report"admin_report_uuid = sa.Column(UUID, nullable=False, server_default=func.gen_random_uuid(), primary_key=True)admin_report_title = sa.Column(sa.Text())admin_report_theme_id = sa.Column(sa.Integer(),sa.ForeignKey("admin_theme.admin_theme_id", onupdate="CASCADE", ondelete="SET NULL"),)admin_report_link = sa.Column(sa.Text())create_time = sa.Column(sa.TIMESTAMP(timezone="UTC"), server_default=func.now())update_time = sa.Column(sa.TIMESTAMP(timezone="UTC"), server_default=func.now())admin_report_type_id = sa.Column(sa.Integer(),sa.ForeignKey("admin_report_type.admin_report_type_id",onupdate="CASCADE",ondelete="SET NULL",),)admin_report_id = sa.Column(sa.Text(), nullable=False, unique=True)departments = sa.Column(sa.JSON)
func.jsonb_contains(models.AdminReport.departments, query.department_ids)
SELECT admin_report.admin_report_uuid, admin_report.admin_report_title, admin_report.admin_report_theme_id, admin_report.admin_report_link, admin_report.create_time, admin_report.update_time, admin_report.admin_report_type_id, admin_report.admin_report_id, admin_report.departments
FROM admin_report JOIN admin_theme ON admin_theme.admin_theme_id = admin_report.admin_report_theme_id
WHERE jsonb_contains(admin_report.departments, '7') or jsonb_contains(admin_report.departments, '6') ORDER BY admin_report.create_time DESC
方式二 新建表,然后关联:
CREATE TABLE public.admin_report_department_r (admin_report_department_uuid uuid DEFAULT gen_random_uuid() NOT NULL,admin_report_id int4 NULL,department_id int4 NULL,create_time timestamptz DEFAULT now() NULL,update_time timestamptz DEFAULT now() NULL,del_flag int4 DEFAULT 0 NOT NULL
);
class AdminReportDepartmentR(Base):__tablename__ = "admin_report_department_r"__table_args__ = (sa.UniqueConstraint("admin_report_id", "department_id"),)admin_report_department_uuid = sa.Column(UUID, nullable=False, server_default=func.gen_random_uuid(), primary_key=True)admin_report_id = sa.Column(sa.String(64), nullable=False)department_id = sa.Column(sa.Integer(), nullable=False)create_time = sa.Column(sa.TIMESTAMP(timezone="UTC"), server_default=func.now())update_time = sa.Column(sa.TIMESTAMP(timezone="UTC"), server_default=func.now())del_flag = sa.Column(sa.Integer(), server_default="0", nullable=False)
sub_expressions = []sub_expressions.append(models.AdminReportDepartmentR.del_flag == 0)if query.department_ids:admin_report_ids = db_session.execute(select(models.AdminReportDepartmentR.admin_report_id).distinct().filter(and_(models.AdminReportDepartmentR.department_id.in_(query.department_ids),models.AdminReportDepartmentR.del_flag == 0,))).all()admin_report_id_list = []if admin_report_ids is not None:admin_report_id_list = set([v for v, in admin_report_ids])sub_expressions.append(models.AdminReportDepartmentR.admin_report_id.in_(admin_report_id_list))sub_stmt = (select(models.AdminReportDepartmentR.admin_report_id,func.string_agg(sa.cast(models.AdminReportDepartmentR.department_id, sa.String),literal_column("','"),).label("department_list"),).select_from(models.AdminReportDepartmentR).filter(and_(True, *sub_expressions)).group_by(models.AdminReportDepartmentR.admin_report_id)).subquery()stmt = (select(models.AdminReport,sub_stmt.c.department_list,).join(models.AdminTheme).join(sub_stmt,sub_stmt.c.admin_report_id == models.AdminReport.admin_report_id,).filter(and_(True, *conditions)))
SELECT admin_report.admin_report_uuid, admin_report.admin_report_title, admin_report.admin_report_theme_id, admin_report.admin_report_link, admin_report.create_time, admin_report.update_time, admin_report.admin_report_type_id, admin_report.admin_report_id, anon_1.department_list
FROM admin_report JOIN admin_theme ON admin_theme.admin_theme_id = admin_report.admin_report_theme_id JOIN (SELECT admin_report_department_r.admin_report_id AS admin_report_id, string_agg(CAST(admin_report_department_r.department_id AS VARCHAR), ',') AS department_list
FROM admin_report_department_r
WHERE admin_report_department_r.del_flag = 0 AND admin_report_department_r.admin_report_id IN ('B20240708171602_6740', 'B20240708171827_7231') GROUP BY admin_report_department_r.admin_report_id) AS anon_1 ON anon_1.admin_report_id = admin_report.admin_report_idWHERE admin_report.create_time >= :create_time_1 AND admin_report.create_time <= :create_time_2 ORDER BY admin_report.create_time DESCLIMIT :param_1 OFFSET :param_2 SELECT admin_report_department_r.admin_report_id AS admin_report_id, string_agg(CAST(admin_report_department_r.department_id AS VARCHAR), ',') AS department_list
FROM admin_report_department_r
WHERE admin_report_department_r.del_flag = 0 AND admin_report_department_r.admin_report_id IN ('B20240708171602_6740', 'B20240708171827_7231') GROUP BY admin_report_department_r.admin_report_id