2025-06-23 10:41:33 +08:00
# include "SysManager.h"
# include "DictItem.h"
# include "M_DictItemDAO.h"
# include "DictData.h"
# include <QCoreApplication>
# include <filesystem>
# include "errno.h"
# include <string.h>
namespace fs = std : : filesystem ;
SysManager & GetSysManager ( )
{
static SysManager sysMgr ;
return sysMgr ;
}
SysManager : : SysManager ( )
{
2025-06-23 18:01:09 +08:00
//配置文件路径
2025-06-23 10:41:33 +08:00
QString dir = QCoreApplication : : applicationDirPath ( ) ;
_profilePath = dir + " /profile.json " ;
2025-06-23 18:01:09 +08:00
//数据库文件路径
2025-06-23 10:41:33 +08:00
QString curPath = QCoreApplication : : applicationDirPath ( ) ;
//QString curPath = QDir::currentPath();
_sysDBPath = curPath + " /DBPConfig.dbp " ;
}
SysManager : : ~ SysManager ( )
{
qDeleteAll ( mapConnection ) ;
}
void SysManager : : setProfileData ( json & data )
{
data [ " sysDBPath " ] = CommonHelper : : qstringToUtf8 ( _sysDBPath ) ;
}
void SysManager : : getSettingData ( json & data )
{
if ( ! data [ " sysDBPath " ] . is_null ( ) )
{
_sysDBPath = CommonHelper : : utf8ToQString ( data [ " sysDBPath " ] ) ;
}
if ( ! data [ " author " ] . is_null ( ) )
{
_defaultAuthor = CommonHelper : : utf8ToQString ( data [ " author " ] ) ;
}
if ( ! data [ " codeGeneratePath " ] . is_null ( ) )
{
_codeGeneratePath = CommonHelper : : utf8ToQString ( data [ " codeGeneratePath " ] ) ;
}
if ( ! data [ " connection " ] . is_null ( ) )
{
for ( auto x : data [ " connection " ] )
{
Connection * pNewCon = new Connection ( ) ;
int id = x [ " id " ] ;
pNewCon - > setID ( id ) ;
IDTool . use_id ( id ) ;
pNewCon - > setName ( CommonHelper : : utf8ToQString ( x [ " conName " ] ) ) ;
pNewCon - > setPath ( CommonHelper : : utf8ToQString ( x [ " path " ] ) ) ;
pNewCon - > setStatus ( false ) ;
pNewCon - > setTime ( CommonHelper : : utf8ToQString ( x [ " time " ] ) ) ;
addConnection ( id , pNewCon ) ;
}
}
if ( ! data [ " xmlFilePath " ] . is_null ( ) )
{
_xmlfilePath = CommonHelper : : utf8ToQString ( data [ " xmlFilePath " ] ) ;
}
}
void SysManager : : getConnectionJson ( json & jsonobj )
{
jsonobj = json : : array ( ) ;
for ( QMap < int , Connection * > : : iterator it = mapConnection . begin ( ) ; it ! = mapConnection . end ( ) ; + + it )
{
json c ;
( it . value ( ) ) - > toJson ( c ) ;
jsonobj . push_back ( c ) ;
}
}
void SysManager : : save ( json & profile )
{
// auto data = profile["data"];
// auto data = profile["data"];
std : : fstream fp ;
fs : : path path = CommonHelper : : qstringToStdString ( _profilePath ) ;
fp . open ( path , std : : fstream : : out | std : : fstream : : trunc ) ;
if ( fp . is_open ( ) ) {
fp < < profile . dump ( 6 ) < < std : : endl ;
fp . close ( ) ;
}
else
2025-06-23 21:06:10 +08:00
{
char err_msg [ 256 ] ; // 确保足够大(通常 256 字节足够)
LOG ( INFO ) < < CommonHelper : : utf8ToStdString ( " can't open file " ) < < CommonHelper : : qstringToStdString ( _profilePath ) < < " .error: " < < strerror_s ( err_msg , sizeof ( err_msg ) , errno ) < < std : : endl ;
}
2025-06-23 10:41:33 +08:00
}
DPData * SysManager : : findDictData ( int id )
{
QMap < int , DictItem * > : : iterator it = _dictItemMap . begin ( ) ;
QMap < int , DictData * > : : iterator it_dict ;
while ( it ! = _dictItemMap . end ( ) )
{
it_dict = ( * it ) - > getDictDataMap ( ) . begin ( ) ;
while ( it_dict ! = ( * it ) - > getDictDataMap ( ) . end ( ) )
{
if ( id = = ( * it_dict ) - > _id )
{
return ( * it_dict ) ;
}
it_dict + + ;
}
it + + ;
}
return nullptr ;
}
DPData * SysManager : : findDictItem ( int id )
{
if ( _dictItemMap . contains ( id ) )
{
return _dictItemMap . value ( id ) ;
}
return nullptr ;
}
bool SysManager : : getAllDictItem ( )
{
using namespace DBPlatformSpace ;
ResultMsg rm ;
list < M_DictItemDAO * > dictItenList ;
rm = M_DictItemDAO : : FindAll ( dictItenList ) ;
if ( rm . rCode = = 0 )
{
LOG ( INFO ) < < CommonHelper : : utf8ToStdString ( " M_DictItemDAO::FindAll success " ) ;
for ( list < M_DictItemDAO * > : : iterator it = dictItenList . begin ( ) ; it ! = dictItenList . end ( ) ; it + + )
{
DictItem * pDictItem ;
if ( _dictItemMap . contains ( ( * it ) - > _ID ) )
{
2025-06-23 18:01:09 +08:00
//从数据库覆盖
2025-06-23 10:41:33 +08:00
pDictItem = _dictItemMap . value ( ( * it ) - > _ID ) ;
pDictItem - > loadData ( * it ) ;
}
else
{
pDictItem = new DictItem ( ) ;
pDictItem - > loadData ( * it ) ;
_dictItemMap . insert ( pDictItem - > _id , pDictItem ) ;
}
}
return true ;
}
else
{
LOG ( INFO ) < < CommonHelper : : utf8ToStdString ( " M_DictItemDAO::FindAll failed " ) ;
LOG ( INFO ) < < rm . rMsg ;
return false ;
}
}
bool SysManager : : getAllDictChildren ( )
{
using namespace DBPlatformSpace ;
ResultMsg rm ;
QMap < int , DictItem * > : : iterator it = _dictItemMap . begin ( ) ;
while ( it ! = _dictItemMap . end ( ) )
{
2025-06-23 18:01:09 +08:00
//获取当前字典项的字典数据
2025-06-23 10:41:33 +08:00
// DictItem* pDictItem = *it;
( * it ) - > getAllChildren ( ) ;
it + + ;
}
return true ;
}
void SysManager : : getDictJson ( json & jsonobj )
{
QMap < int , DictItem * > : : iterator it = getDictItemMap ( ) . begin ( ) ;
while ( it ! = getDictItemMap ( ) . end ( ) )
{
json j ;
( * it ) - > toJson ( j , true ) ;
jsonobj [ " response " ] . push_back ( j ) ;
it + + ;
}
}
void SysManager : : clearAllDictData ( )
{
qDeleteAll ( getDictItemMap ( ) ) ;
getDictItemMap ( ) . clear ( ) ;
}
2025-06-23 18:01:09 +08:00
//连接相关操作
2025-06-23 10:41:33 +08:00
bool SysManager : : addConnection ( int id , Connection * con )
{
if ( con & & id > 0 )
{
mapConnection . insert ( id , con ) ;
return true ;
}
2025-06-23 18:01:09 +08:00
//连接信息为空
2025-06-23 10:41:33 +08:00
return false ;
}
Connection * SysManager : : findConnection ( int id )
{
QMap < int , Connection * > : : iterator it ;
if ( mapConnection . contains ( id ) )
{
it = mapConnection . find ( id ) ;
return it . value ( ) ;
}
return nullptr ;
}
bool SysManager : : delConnection ( int id )
{
if ( mapConnection . contains ( id ) )
{
mapConnection . remove ( id ) ;
return true ;
}
return false ;
}