112 lines
4.3 KiB
Python
112 lines
4.3 KiB
Python
import os
|
|
import uuid
|
|
from appPublic.uniqueID import getID
|
|
from datetime import datetime
|
|
from typing import List, Dict, Optional
|
|
from appPublic.jsonConfig import getConfig
|
|
# Worker removed - not available
|
|
from sqlor.dbpools import DBPools
|
|
|
|
class ContractAttachmentManager:
|
|
def __init__(self):
|
|
self.config = getConfig()
|
|
pass # Worker removed
|
|
self.upload_dir = self.config.get('contract_upload_dir', '/var/uploads/contracts')
|
|
|
|
async def get_db_connection(self, org_id: str):
|
|
"""获取数据库连接"""
|
|
dbp = await getDBP(org_id)
|
|
return dbp
|
|
|
|
async def ensure_upload_dir(self, org_id: str):
|
|
"""确保存储目录存在"""
|
|
org_dir = os.path.join(self.upload_dir, org_id)
|
|
os.makedirs(org_dir, exist_ok=True)
|
|
return org_dir
|
|
|
|
async def upload_contract_attachment(self, contract_id: str, file_data: bytes,
|
|
file_name: str, file_type: str,
|
|
user_id: str, org_id: str,
|
|
description: Optional[str] = None) -> str:
|
|
"""上传合同附件"""
|
|
attachment_id = getID()
|
|
dbp = await self.get_db_connection(org_id)
|
|
org_dir = await self.ensure_upload_dir(org_id)
|
|
|
|
# 生成文件路径
|
|
file_ext = os.path.splitext(file_name)[1] if '.' in file_name else ''
|
|
safe_filename = f"{attachment_id}{file_ext}"
|
|
file_path = os.path.join(org_dir, safe_filename)
|
|
|
|
# 保存文件
|
|
with open(file_path, 'wb') as f:
|
|
f.write(file_data)
|
|
|
|
# 获取文件大小
|
|
file_size = len(file_data)
|
|
|
|
# 检查是否已有同名文件的版本
|
|
check_sql = """
|
|
SELECT MAX(version) as max_version FROM contract_attachment
|
|
WHERE contract_id = %(contract_id)s AND file_name = %(file_name)s AND org_id = %(org_id)s
|
|
"""
|
|
check_result = await sor.doQuery(check_sql, {
|
|
'contract_id': contract_id,
|
|
'file_name': file_name,
|
|
'org_id': org_id
|
|
})
|
|
current_version = check_result[0]['max_version'] if check_result and check_result[0]['max_version'] else 0
|
|
new_version = current_version + 1
|
|
|
|
# 插入附件记录
|
|
sql = """
|
|
INSERT INTO contract_attachment (
|
|
id, contract_id, file_name, file_path, file_size, file_type,
|
|
version, description, uploaded_by, org_id, created_at
|
|
) VALUES (
|
|
%(id)s, %(contract_id)s, %(file_name)s, %(file_path)s, %(file_size)s, %(file_type)s,
|
|
%(version)s, %(description)s, %(uploaded_by)s, %(org_id)s, NOW()
|
|
)
|
|
"""
|
|
|
|
params = {
|
|
'id': attachment_id,
|
|
'contract_id': contract_id,
|
|
'file_name': file_name,
|
|
'file_path': file_path,
|
|
'file_size': file_size,
|
|
'file_type': file_type,
|
|
'version': new_version,
|
|
'description': description,
|
|
'uploaded_by': user_id,
|
|
'org_id': org_id
|
|
}
|
|
|
|
await sor.doTransaction([{'sql': sql, 'params': params}])
|
|
return attachment_id
|
|
|
|
async def get_contract_attachments(self, contract_id: str, org_id: str) -> List[Dict]:
|
|
"""获取合同的所有附件"""
|
|
dbp = await self.get_db_connection(org_id)
|
|
sql = """
|
|
SELECT * FROM contract_attachment
|
|
WHERE contract_id = %(contract_id)s AND org_id = %(org_id)s
|
|
ORDER BY version DESC, created_at DESC
|
|
"""
|
|
result = await sor.doQuery(sql, {'contract_id': contract_id, 'org_id': org_id})
|
|
return result
|
|
|
|
# 全局实例
|
|
attachment_manager = ContractAttachmentManager()
|
|
|
|
# 导出函数
|
|
async def upload_contract_attachment(contract_id: str, file_data: bytes,
|
|
file_name: str, file_type: str,
|
|
user_id: str, org_id: str,
|
|
description: Optional[str] = None) -> str:
|
|
return await attachment_manager.upload_contract_attachment(
|
|
contract_id, file_data, file_name, file_type, user_id, org_id, description
|
|
)
|
|
|
|
async def get_contract_attachments(contract_id: str, org_id: str) -> List[Dict]:
|
|
return await attachment_manager.get_contract_attachments(contract_id, org_id) |