table_list.py 5.1 KB
#author:        4N
#createtime:    2021/1/27
#email:         nheweijun@sina.com


from ..models import Table,Catalog
from app.modules.auth.models import User
from sqlalchemy import or_,and_
from sqlalchemy.orm import Query
from app.util.component.ApiTemplate import ApiTemplate
from app.util.component.UserAlias import UserAlias
from app.util.component.ModelVisitor import ModelVisitor

class Api(ApiTemplate):
    api_name = "表列表"
    def process(self):
    
        res = {}
        try:

            page_index = int(self.para.get("page_index", "0"))
            page_size = int(self.para.get("page_size", "10"))
    
            alias = self.para.get("alias")
            name = self.para.get("name")
    
    
            database_guid = self.para.get("database_guid")
            catalog_guid = self.para.get("catalog_guid")
            table_type = self.para.get("table_type")
    
            start_time = self.para.get("start_time")
            end_time =  self.para.get("end_time")
            is_for_partition = self.para.get("is_for_partition")
    
    
            recursion = int(self.para.get("recursion","0"))
    
            sort = int(self.para.get("sort","1"))
            if sort.__eq__(1):
                tables:Query = Table.query.order_by(Table.update_time.desc(),Table.name)
            else:
                tables = Table.query.order_by(Table.name)


            #过滤spatial_ref_sys
            tables = tables.filter(Table.name.notin_(["spatial_ref_sys"]))

            if database_guid:
                tables = tables.filter_by(database_guid=database_guid)
            if is_for_partition:
                tables = tables.filter_by(is_for_partition=int(is_for_partition))
            if catalog_guid:
    
                if recursion.__eq__(0):
                    tables = tables.filter_by(catalog_guid=catalog_guid)
                else:
                    #所有子目录id
                    catalog_guids = [c.guid for c in  Catalog.query.filter(Catalog.path.like("%" + catalog_guid + "%")).all()]
                    tables = tables.filter(Table.catalog_guid.in_(catalog_guids))
    
            if table_type:
                tables = tables.filter_by(table_type=table_type)
            if start_time and end_time:
                tables = tables.filter(Table.create_time.between(start_time, end_time))
    
            # 并集
            if alias and name:
                tables = tables.filter(or_(Table.alias.like("%" + alias + "%") , Table.name.like("%" + name + "%")))
            else:
                if alias:
                    tables = tables.filter(Table.alias.like("%" + alias + "%"))
                if name:
                    tables = tables.filter(Table.name.like("%" + name + "%"))
            res["data"]={}
            res["data"]["count"] = tables.count()
            tables = tables.limit(page_size).offset(page_index * page_size).all()
            res["data"]["list"]=[]


            user_alias = UserAlias()
            for t in tables:
                table_json =  ModelVisitor.table_to_json(t)
                table_json["display_name"] = user_alias.get_alias(table_json["creator"])
                res["data"]["list"].append(table_json)

            res["result"] = True
        except Exception as e:
            raise e
        return res

    
    api_doc={
    "tags":["管理接口"],
    "parameters":[
        {"name": "page_index",
         "in": "formData",
         "type": "int",
         "description": "页"},
        {"name": "page_size",
         "in": "formData",
         "type": "int",
         "description": "页大小"},
        {"name": "alias",
         "in": "formData",
         "type": "string",
         "description": "表别名"},
        {"name": "name",
         "in": "formData",
         "type": "string",
         "description": "表名"},
        {"name": "table_type",
         "in": "formData",
         "type": "int",
         "description": "点线面123","enum":[1,2,3]},
        {"name": "is_for_partition",
         "in": "formData",
         "type": "int",
         "description": "是否用于分层分级", "enum": [0, 1]},
        {"name": "database_guid",
         "in": "formData",
         "type": "string",
         "description": "数据库guid"},
        {"name": "catalog_guid",
         "in": "formData",
         "type": "string",
         "description": "目录guid"},
        {"name": "recursion",
         "in": "formData",
         "type": "int",
         "description": "是否递归目录,0不递归,1递归","enum":[0,1]},
        {"name": "start_time",
         "in": "formData",
         "type": "string",
         "description": "开始时间 2020-12-12 00:00:00"},
        {"name": "end_time",
         "in": "formData",
         "type": "string",
         "description": "结束时间"},
        {"name": "sort",
         "in": "formData",
         "type": "int",
         "description": "排序","enum":[1,2]},
    ],
    "responses":{
        200:{
            "schema":{
                "properties":{
                }
            }
            }
        }
    }