|
|
# -*- coding: utf-8 -*-
|
|
|
import json
|
|
|
import copy
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
from sqlalchemy import Column, Integer, String, DateTime, func
|
|
|
from typing import Any, Dict, List, Optional, Tuple, Union
|
|
|
|
|
|
from website.db_mysql import get_session
|
|
|
from website.util import shortuuid
|
|
|
from website.db.enterprise_entity.enterprise_entity import EnterpriseEntityRepository
|
|
|
from website.db.enterprise_node import enterprise_node as DB_Node
|
|
|
from website.db.alg_model import alg_model as DB_alg_model
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
"""
|
|
|
CREATE TABLE `enterprise_busi_model` (
|
|
|
`id` int NOT NULL AUTO_INCREMENT,
|
|
|
`suid` varchar(10) NOT NULL DEFAULT '' COMMENT 'short uuid',
|
|
|
`entity_id` int NOT NULL COMMENT '企业id',
|
|
|
`entity_suid` varchar(10) NOT NULL COMMENT '企业uuid',
|
|
|
`name` varchar(255) NOT NULL,
|
|
|
`comment` varchar(255) DEFAULT '',
|
|
|
`basemodel_ids` varchar(255) NOT NULL COMMENT '关联模型,json list, [{"id":123,"suid":"xxx"},...]',
|
|
|
`business_logic` varchar(32) DEFAULT NULL COMMENT '业务代码压缩包的md5',
|
|
|
`business_conf_file` varchar(32) DEFAULT NULL COMMENT '业务配置参数压缩包的文件md5',
|
|
|
`business_conf_param` varchar(255) DEFAULT NULL COMMENT '业务配置的参数,json字符串,eg: ''{"a":1, "b":2}''',
|
|
|
`delete` tinyint(1) DEFAULT '0',
|
|
|
`create_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
PRIMARY KEY (`id`)
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='企业业务模型表';
|
|
|
|
|
|
CREATE TABLE `enterprise_busi_model_node` (
|
|
|
`id` int NOT NULL AUTO_INCREMENT,
|
|
|
`suid` varchar(10) NOT NULL,
|
|
|
`entity_suid` varchar(10) DEFAULT NULL COMMENT '企业suid',
|
|
|
`busi_model_id` int DEFAULT NULL,
|
|
|
`busi_model_suid` varchar(10) DEFAULT NULL,
|
|
|
`node_id` int DEFAULT NULL,
|
|
|
`node_suid` varchar(10) DEFAULT NULL,
|
|
|
`create_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
PRIMARY KEY (`id`)
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
|
"""
|
|
|
|
|
|
|
|
|
class EnterpriseBusiModel(Base):
|
|
|
__tablename__ = 'enterprise_busi_model'
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
suid = Column(String(10), nullable=False, default='')
|
|
|
entity_id = Column(Integer, nullable=False)
|
|
|
entity_suid = Column(String(10), nullable=False)
|
|
|
name = Column(String(255), nullable=False)
|
|
|
comment = Column(String(255))
|
|
|
basemodel_ids = Column(String(255), nullable=False)
|
|
|
business_logic = Column(String(32))
|
|
|
business_conf_file = Column(String(32))
|
|
|
business_conf_param = Column(String(255))
|
|
|
delete = Column(Integer, default=0)
|
|
|
create_time = Column(DateTime, default=func.current_timestamp())
|
|
|
update_time = Column(DateTime, default=func.current_timestamp(), onupdate=func.current_timestamp())
|
|
|
|
|
|
def __repr__(self):
|
|
|
return f'<EnterpriseBusiModel(id={self.id}, suid={self.suid})>'
|
|
|
|
|
|
class EnterpriseBusiModelNode(Base):
|
|
|
__tablename__ = 'enterprise_busi_model_node'
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
suid = Column(String(10), nullable=False, default='')
|
|
|
entity_suid = Column(String(10))
|
|
|
busi_model_id = Column(Integer)
|
|
|
busi_model_suid = Column(String(10))
|
|
|
node_id = Column(Integer)
|
|
|
node_suid = Column(String(10))
|
|
|
create_time = Column(DateTime, default=func.current_timestamp())
|
|
|
def __repr__(self):
|
|
|
return f'<EnterpriseBusiModelNode(id={self.id}, suid={self.suid})>'
|
|
|
|
|
|
|
|
|
class EnterpriseBusiModelRepository(object):
|
|
|
|
|
|
def get_by_id(self, id: int) -> Optional[EnterpriseBusiModel]:
|
|
|
return self.db.query(EnterpriseBusiModel).filter(EnterpriseBusiModel.id == id).first()
|
|
|
|
|
|
def insert_busi_model(self, data: Dict):
|
|
|
entity_suid = EnterpriseEntityRepository().get_entity_suid(data['entity_id'])
|
|
|
data['suid'] = shortuuid.ShortUUID().random(10)
|
|
|
data['entity_suid'] = entity_suid
|
|
|
|
|
|
base_model_ids = [int(model_id) for model_id in data['basemodel_ids'].split(',')]
|
|
|
base_model_db = DB_alg_model.ModelRepositry()
|
|
|
base_model = []
|
|
|
for base_model_id in base_model_ids:
|
|
|
base_model_suid = base_model_db.get_suid(base_model_id)
|
|
|
base_model.append({
|
|
|
'id': base_model_id,
|
|
|
'suid': base_model_suid
|
|
|
})
|
|
|
data['basemodel_ids'] = json.dumps(base_model)
|
|
|
new_data = copy.copy(data)
|
|
|
|
|
|
with get_session() as session:
|
|
|
model = EnterpriseBusiModel(**new_data)
|
|
|
session.add(model)
|
|
|
session.commit()
|
|
|
|
|
|
return model.id, model.suid
|
|
|
|
|
|
|
|
|
def get_busi_model_by_id(self, id: int) -> Optional[EnterpriseBusiModel]:
|
|
|
with get_session() as session:
|
|
|
model = session.query(EnterpriseBusiModel).filter(EnterpriseBusiModel.id == id).first()
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
class EnterpriseBusiModelNodeRepository(object):
|
|
|
# def get_by_id(self, id: int) -> Optional[EnterpriseBusiModelNode]:
|
|
|
# return self.db.query(EnterpriseBusiModelNode).filter(EnterpriseBusiModelNode.id == id).first()
|
|
|
|
|
|
def insert_busi_model_nodes(self, data: Dict):
|
|
|
data['suid'] = shortuuid.ShortUUID().random(10)
|
|
|
link_node_ids = [int(node_id) for node_id in data['node_ids'].split(',')]
|
|
|
with get_session() as session:
|
|
|
for node_id in link_node_ids:
|
|
|
node_db = DB_Node.EnterpriseNodeRepository()
|
|
|
node = node_db.get_node_by_id(node_id)
|
|
|
node_suid = node["suid"]
|
|
|
model_node = EnterpriseBusiModelNode(
|
|
|
suid=shortuuid.ShortUUID().random(10),
|
|
|
entity_suid=data['entity_suid'],
|
|
|
busi_model_id=data['busi_model_id'],
|
|
|
busi_model_suid=data['busi_model_suid'],
|
|
|
node_id=node_id,
|
|
|
node_suid=node_suid,
|
|
|
)
|
|
|
session.add(model_node)
|
|
|
session.commit()
|
|
|
return
|
|
|
|
|
|
|