| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- #include "autosyncdata.h"
- #include "respgetrecommendseg.h"
- #include "sncdatabase.h"
- #include "unzip.h"
- #include "respgetsegdyinfo.h"
- #include "respdldyseginfo.h"
- AutoSyncData::AutoSyncData()
- {
- m_stop = false;
- m_skip = false;
- m_cubeWs = new WSCube();
- m_numchk = new WSNumChk();
- m_first = true;
- m_clientID = "";
- m_moduleID = "";
- }
- AutoSyncData::~AutoSyncData()
- {
- m_cubeWs->abort();
- m_numchk->abort();
- delete m_cubeWs;
- delete m_numchk;
- qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
- <<"FormRec.AutoSyncData Destroyed.";
- }
- void AutoSyncData::setSkip(bool skip)
- {
- m_skip = skip;
- }
- void AutoSyncData::abort()
- {
- m_stop = true;
- m_cubeWs->abort();
- m_numchk->abort();
- }
- void AutoSyncData::setAccountInfo(QMap<QString, QString> accountInfo)
- {
- m_account = accountInfo;
- m_first = true;
- }
- void AutoSyncData::sync()
- {
- QString ssoHash = m_account.value("ssohash");
- QString userCode = m_account.value("usercode");
- if(ssoHash.isEmpty() || userCode.isEmpty())
- return ;
- if(!checkSynCondition())
- return;
- QTime t;
- t.start();
- updateSegRecInfo();
- emit dataSync();
- }
- bool AutoSyncData::checkSynCondition()
- {
- //从服务器同步的条件
- //1、用户还未同步过一次
- if(m_first)
- {
- m_first = false;
- return true;
- }
- return false;
- }
- void AutoSyncData::run()
- {
- if(m_skip)
- return ;
- m_stop = false;
- sync();
- }
- QString AutoSyncData::dlSegRecInfo()
- {
- QString mcode = SNCDataBase::getAppHash();
- QString clientVer = SNCDataBase::getAppVersion();
- QString userhash = m_account.value("userhash");
- QString json = m_cubeWs->getRecommendSeg(clientVer,mcode,userhash,"","");
- QString zipFile;
- RespGetRecommendSeg response(json);
- if(response.getRtnCode()>=0)
- {
- zipFile = QDir::tempPath()+"/snc_recseg.zip";
- QString segRecInfo = response.getSegRecInfo();
- QByteArray arrayBase = QByteArray::fromBase64(segRecInfo.toAscii());
- QFile file(zipFile);
- file.open(QIODevice::WriteOnly);
- file.write(arrayBase);
- file.close();
- }
- return zipFile;
- }
- bool AutoSyncData::updateSegRecInfo()
- {
- QString zipFile = dlSegRecInfo();
- if(zipFile.isEmpty())
- return false;
- QString csvFile = extractZipFile(zipFile);
- if(csvFile.isEmpty())
- return false;
- SNCDataBase::updateSegRecInfo(csvFile);
- QFile::remove(zipFile);
- QFile::remove(csvFile);
- return true;
- }
- QString AutoSyncData::dlSegDynamicInfoByAreaCode(QString areacode)
- {
- QString json = m_numchk->dlDySegInfo(areacode);
- RespDlDySegInfo response(json);
- QString zipFile;
- if(response.getRtnCode()>0)
- {
- zipFile = QDir::tempPath()+"/snc_dynamicseg.zip";
- QString segChkInfo = response.getSegChkInfo();
- QByteArray arrayBase = QByteArray::fromBase64(segChkInfo.toAscii());
- QFile file(zipFile);
- file.open(QIODevice::WriteOnly);
- file.write(arrayBase);
- file.close();
- }
- return zipFile;
- }
- bool AutoSyncData::updateSegDynamicInfoByAreaCode(QString areacode)
- {
- QString zipFile = dlSegDynamicInfoByAreaCode(areacode);
- if(zipFile.isEmpty())
- return false;
- QString csvFile = extractZipFile(zipFile);
- if(csvFile.isEmpty())
- return false;
- //qDebug()<<zipFile;
- QString today = QDate::currentDate().toString("yyyy-MM-dd");
- SNCDataBase::updateDynamicSegInfo(today,areacode,csvFile);
- //qDebug()<<csvFile;
- //QFile::remove(QDir::tempPath()+"/snc_recseg.zip");
- //QFile::remove(QDir::tempPath()+"/speednumcube/recseg.csv");
- return true;
- }
- QString AutoSyncData::dlSegDynamicInfoBySeg(QString seg)
- {
- QString json = m_numchk->getDySegInfo(seg);
- RespGetSegDyInfo response(json);
- QString segChkInfo;
- if(response.getRtnCode()==1)
- {
- segChkInfo = response.getSegChkInfo();
- }
- return segChkInfo;
- }
- bool AutoSyncData::updateSegDynamicInfoBySeg(QString seg)
- {
- QString chkinfo = dlSegDynamicInfoBySeg(seg);
- if(chkinfo.isEmpty())
- return false;
- QStringList list = chkinfo.split(",");
- if(list.size()!=8)
- return false;
- QString today = QDate::currentDate().toString("yyyy-MM-dd");
- SNCDataBase::updateDynamicSegInfo(today,list);
- return true;
- }
- QString AutoSyncData::extractZipFile(QString zipFile)
- {
- //准备临时目录并删除该目录下的所有文件
- QDir dir;
- if(!dir.exists(QDir::tempPath()+"/snctemp/rec/"))
- {
- dir.mkpath(QDir::tempPath()+"/snctemp/rec/");
- }
- dir.cd(QDir::tempPath()+"/snctemp/rec/");
- QFileInfoList list = dir.entryInfoList();
- for(int i=0;i<list.size();i++)
- {
- if(list.at(i).isFile())
- {
- //qDebug()<<"AutoSyncData::extractZipFile delete file :"<<list.at(i).absoluteFilePath();
- QFile::remove(list.at(i).absoluteFilePath());
- }
- }
- //解压缩zip文件
- UnZip::ErrorCode ec;
- UnZip uz;
- ec = uz.openArchive(zipFile);
- if (ec != UnZip::Ok) {
- qDebug() << "AutoSyncData::extractZipFile Failed to open archive: " << uz.formatError(ec).toAscii().data() << endl << endl;
- return "" ;
- }
- ec = uz.extractAll(QDir::tempPath()+"/snctemp/rec/");
- if (ec != UnZip::Ok) {
- qDebug() << "AutoSyncData::extractZipFile Extraction failed: " << uz.formatError(ec).toAscii().data() << endl << endl;
- uz.closeArchive();
- return "" ;
- }
- //返回解压缩后的文件,找到第一个csv格式即返回
- dir.refresh();
- list = dir.entryInfoList();
- QString csvFile;
- for(int i=0;i<list.size();i++)
- {
- if(list.at(i).isFile())
- {
- if(list.at(i).suffix().toLower()=="csv")
- {
- csvFile = list.at(i).absoluteFilePath();
- break;
- }
- }
- }
- return csvFile;
- }
|