#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()<abort(); m_numchk->abort(); } void AutoSyncData::setAccountInfo(QMap 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()<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