dushibaiyu 6 лет назад
Родитель
Сommit
cda7cc3401

+ 6 - 0
JxcClient.pro

@@ -26,6 +26,9 @@ SOURCES += \
     handle/database.cpp \
     handle/httprequest.cpp \
     handle//seriport/logserialport.cpp \
+    handle/packinfomanger.cpp \
+    handle/packinfoseed2server.cpp \
+    handle/projectinfomanger.cpp \
     handle/remotepackconfig.cpp \
     handle/rtuport.cpp \
     handle/selectspecs.cpp \
@@ -52,6 +55,9 @@ HEADERS += \
     handle/danjumuban.h \
     handle/database.h \
     handle/httprequest.h \
+    handle/packinfomanger.h \
+    handle/packinfoseed2server.h \
+    handle/projectinfomanger.h \
     handle/selectspecs.h \
     handle/seriport/logserialport.h \
     handle/remotepackconfig.h \

+ 119 - 0
handle/packinfomanger.cpp

@@ -0,0 +1,119 @@
+#include "packinfomanger.h"
+#include <pqQtlib/core/pqapppath.h>
+#include <pqQtlib/utils/pqfileutils.h>
+#include <QDateTime>
+#include <QJsonDocument>
+#include <QJsonArray>
+
+using namespace PQ;
+
+PackInfoManger::PackInfoManger(QObject *parent) : QObject(parent)
+{
+    _dataDir = AppPath::this_()->dataDir();
+    if(!_dataDir.exists("packinfo")){
+        _dataDir.mkdir("packinfo");
+    }
+    _dataDir.cd("packinfo");
+}
+
+
+QString PackInfoManger::savePackInfo(const QSharedPointer<PackInfo> & info)
+{
+    if(info.isNull()) return "";
+    auto tm = QDateTime::fromSecsSinceEpoch(info->time());
+    auto dir = getDir(tm.date(),true);
+    QString name = info->codeSingle();
+    name += "_sync.json";
+    QString ret = dir.absoluteFilePath(name);
+    if(writeAbsoluteFile(ret,QJsonDocument(info->toObject()).toJson()))
+        return ret;
+    return "";
+}
+
+QString PackInfoManger::setPackInfoSync(const QString & code, qint64 time, bool autoSync)
+{
+    QDateTime tm;
+    if(time < 0){
+        tm = QDateTime::currentDateTime();
+    } else {
+        tm = QDateTime::fromSecsSinceEpoch(time);
+    }
+    auto dir = getDir(tm.date());
+    QString name = code +  "_sync.json";
+    QString newName = code + ".json";
+    if(QFile::rename(name,newName) && autoSync){
+        writeAbsoluteFile(_dataDir.absoluteFilePath("auto_sync.time"),tm.toString("yyyy-MM-dd").toUtf8());
+        return newName;
+    }
+    return "";
+}
+
+QDate PackInfoManger::lastSyncDate()
+{
+    QByteArray dt = readAbsoluteFile(_dataDir.absoluteFilePath("sync.time"));
+    if(dt.isEmpty()) {
+        QDate ret = QDate::currentDate().addDays(-30);
+        writeAbsoluteFile(_dataDir.absoluteFilePath("auto_sync.time"),ret.toString("yyyy-MM-dd").toUtf8());
+    }
+    return QDate::fromString(QString::fromUtf8(dt),"yyyy-MM-dd");
+}
+
+
+QDir PackInfoManger::getDir(const QDate & tm, bool create)
+{
+    QString dirName = tm.toString("yyyy-MM-dd");
+    if(create && !_dataDir.exists(dirName)){
+        _dataDir.mkdir(dirName);
+    }
+    return QDir(_dataDir.absoluteFilePath(dirName));
+}
+
+
+QList<PackSaveInfo> PackInfoManger::getPackInfo(const QDate & tm,bool noSync)
+{
+    auto dir = getDir(tm);
+    auto list  = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
+    QList<PackSaveInfo> ret;
+    for(int i = 0; i < list.size(); ++i){
+        auto & finfo = list.at(i);
+        if(noSync && finfo.fileName().indexOf("_sync") > 0) continue;
+        auto obj = QJsonDocument::fromJson(readAbsoluteFile(finfo.absoluteFilePath())).object();
+        auto info = PackInfo::fromObject(obj);
+        if(!info.isNull()){
+            PackSaveInfo pinfo;
+            pinfo.absPath = finfo.absoluteFilePath();
+            pinfo.packInfo = info;
+            ret.append(pinfo);
+        }
+    }
+    return ret;
+}
+
+
+QList<QSharedPointer<ProjectInfo>>  PackInfoManger::getLocalProjectInfo()
+{
+    QList<QSharedPointer<ProjectInfo>> list;
+    auto dt = readAbsoluteFile(_dataDir.absoluteFilePath("project.json"));
+    if(!dt.isEmpty()) {
+        QJsonArray ary = QJsonDocument::fromJson(dt).array();
+        for(int i = 0; i < ary.size(); ++i){
+            auto obj = ary.at(i).toObject();
+            auto info = ProjectInfo::fromObject(obj);
+            if(!info.isNull())
+                list.append(info);
+        }
+    }
+    return list;
+}
+
+
+bool PackInfoManger::syncLocalProjectInfo(QList<QSharedPointer<ProjectInfo> > &projects)
+{
+    QJsonArray ary;
+    for(int i = 0; i < projects.size(); ++i)
+    {
+        ary.append(projects.at(i)->toObject());
+    }
+    QByteArray dt = QJsonDocument(ary).toJson();
+    return writeAbsoluteFile(_dataDir.absoluteFilePath("project.json"),dt);
+}

+ 42 - 0
handle/packinfomanger.h

@@ -0,0 +1,42 @@
+#ifndef PACKINFOMANGER_H
+#define PACKINFOMANGER_H
+
+#include <QObject>
+#include <QDir>
+#include <QList>
+#include <QDate>
+#include "struct_/packinfo.h"
+// 管理本地码单(箱单) 的类
+// 管理本地批号的类
+
+struct PackSaveInfo
+{
+    QSharedPointer<PackInfo> packInfo;
+    QString absPath;
+};
+
+class PackInfoManger : public QObject
+{
+    Q_OBJECT
+public:
+    explicit PackInfoManger(QObject *parent = nullptr);
+
+    QList<PackSaveInfo> getPackInfo(const QDate & tm,bool noSync = false);
+    QDate lastSyncDate();
+
+    QList<QSharedPointer<ProjectInfo>>  getLocalProjectInfo();
+signals:
+
+public slots:
+    QString savePackInfo(const QSharedPointer<PackInfo> & info);
+
+    QString setPackInfoSync(const QString & code, qint64 time = -1,bool autoSync = false);
+
+    bool syncLocalProjectInfo(QList<QSharedPointer<ProjectInfo>> & projects);
+private:
+    QDir getDir(const QDate & tm,bool create = false);
+private:
+    QDir _dataDir;
+};
+
+#endif // PACKINFOMANGER_H

+ 46 - 0
handle/packinfoseed2server.cpp

@@ -0,0 +1,46 @@
+#include "packinfoseed2server.h"
+#include <QTimer>
+
+PackInfoSeed2Server::PackInfoSeed2Server(PackInfoManger * manger, QObject *parent, bool autoSync) : QObject(parent),
+    _manger(manger),_autoSync(autoSync)
+{
+    connect(&_requst,&HttpRequest::request,this,&PackInfoSeed2Server::_requst);
+    _failedCount = 0;
+}
+
+
+void PackInfoSeed2Server::send(QSharedPointer<PackInfo> & info)
+{
+    if(_manger == nullptr) return;
+    _queue.append(info);
+    QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
+}
+
+
+void PackInfoSeed2Server::doSend()
+{
+    if(_current.isNull() && !_queue.isEmpty()){
+        _current = _queue.first();
+        // TODO:小宋, 发送 _current 到服务器
+    }
+}
+
+void PackInfoSeed2Server::result(int code, const QJsonObject & body)
+{
+    if(code != 200 || body.isEmpty()){
+        _current.clear();
+        QTimer::singleShot(500,this,&PackInfoSeed2Server::doSend);
+        _failedCount ++;
+        return;
+    }
+    auto error = body.value("code").toInt();
+    if(error == 200){
+        _queue.dequeue();
+        _manger->setPackInfoSync(_current->codeSingle(),_current->time(),_autoSync);
+        _current.clear();
+        QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
+        _failedCount = 0;
+    } else {
+        // TODO: 判断错误,根据错误去处理下一步,例如重复了,就next的
+    }
+}

+ 33 - 0
handle/packinfoseed2server.h

@@ -0,0 +1,33 @@
+#ifndef PACKINFOSEED2SERVER_H
+#define PACKINFOSEED2SERVER_H
+
+#include <QObject>
+#include "httprequest.h"
+#include "struct_/packinfo.h"
+#include <QQueue>
+#include "packinfomanger.h"
+// 码单的上传
+class PackInfoSeed2Server : public QObject
+{
+    Q_OBJECT
+public:
+    explicit PackInfoSeed2Server(PackInfoManger * manger,QObject *parent = nullptr,bool autoSync = false);
+
+signals:
+
+public slots:
+    void send(QSharedPointer<PackInfo> & info);
+
+private slots:
+    void doSend();
+    void result(int code, const QJsonObject & body);
+private:
+    uint _failedCount;
+    QQueue<QSharedPointer<PackInfo>> _queue;
+    QSharedPointer<PackInfo> _current;
+    PackInfoManger * _manger;
+    HttpRequest _requst;
+    bool _autoSync;
+};
+
+#endif // PACKINFOSEED2SERVER_H

+ 6 - 0
handle/projectinfomanger.cpp

@@ -0,0 +1,6 @@
+#include "projectinfomanger.h"
+
+ProjectInfoManger::ProjectInfoManger(QObject *parent) : QObject(parent)
+{
+
+}

+ 33 - 0
handle/projectinfomanger.h

@@ -0,0 +1,33 @@
+#ifndef PROJECTINFOMANGER_H
+#define PROJECTINFOMANGER_H
+
+#include <QObject>
+#include "httprequest.h"
+// 处理批号的管理和同步
+
+class ProjectInfoManger : public QObject
+{
+    Q_OBJECT
+public:
+    explicit ProjectInfoManger(QObject *parent = nullptr);
+
+    enum ReqStatus
+    {
+        IDIL = 0,
+        Load_Server_List = 1,
+        Sync_Local_List = 2
+    };
+signals:
+
+public slots:
+    void loadServerList();
+
+    void syncLocalList();
+private slots:
+    void loadLocalList();
+
+private:
+    ReqStatus _status;
+};
+
+#endif // PROJECTINFOMANGER_H