error 6 лет назад
Родитель
Сommit
a585f13ffc

+ 2 - 0
JxcClient.pro

@@ -20,6 +20,7 @@ include($$PWD/pqQtlib/pqQtlib.pri)
 SOURCES += \
     configinfo.cpp \
     globalinfo.cpp \
+    handle/autosyncpackinfo.cpp \
     handle/printer/boxtemlateone.cpp \
     handle/printer/code39.cpp \
     handle/httprequest.cpp \
@@ -53,6 +54,7 @@ SOURCES += \
 HEADERS += \
     configinfo.h \
     globalinfo.h \
+    handle/autosyncpackinfo.h \
     handle/printer/boxtemlateone.h \
     handle/printer/code39.h \
     handle/httprequest.h \

+ 9 - 0
globalinfo.cpp

@@ -67,6 +67,9 @@ void GlobalInfo::initInfo()
     _projectmanger = new ProjectInfoManger(packmanger,this);
     connect(_projectmanger,&ProjectInfoManger::inited,this,&GlobalInfo::syncProject);
     connect(_projectmanger,&ProjectInfoManger::synced,this,&GlobalInfo::syncProject);
+    _syncPack = new AutoSyncPackInfo();
+    connect(this,&GlobalInfo::doInited,_syncPack,&AutoSyncPackInfo::start);
+    connect(this,&GlobalInfo::logOuted,_syncPack,&AutoSyncPackInfo::loadOut);
 }
 
 void GlobalInfo::printerBox(QSharedPointer<BoxInfo> info)
@@ -76,6 +79,12 @@ void GlobalInfo::printerBox(QSharedPointer<BoxInfo> info)
     }
 }
 
+void GlobalInfo::doSync(QDate & tm)
+{
+    if(_syncPack == nullptr || isNoLine()) return;
+    _syncPack->sync(tm);
+}
+
 void GlobalInfo::printerPack(QSharedPointer<PackInfo> info)
 {
     if(_printer){

+ 8 - 0
globalinfo.h

@@ -11,6 +11,7 @@
 #include "handle/serialthreadhandle.h"
 #include "handle/remotepackconfig.h"
 #include "handle/projectinfomanger.h"
+#include "handle/autosyncpackinfo.h"
 
 class GlobalInfo : public QObject
 {
@@ -52,10 +53,16 @@ public:
 
     void printerBox(QSharedPointer<BoxInfo> info);
     void printerPack(QSharedPointer<PackInfo> info);
+
+    // 同步调用
+    void doSync(QDate & tm);
+    //推出帐号别忘调用
+    void logOut();
 signals:
     void dbConfigUpdated();
     void dengJiUped();
     void doInited();
+    void logOuted();
 public slots:
     void start(UserInfo & user,bool online);
 
@@ -84,6 +91,7 @@ private:
     SerialThreadHandle * _serial;
     RemotePackConfig * _remoteConfig;
     ProjectInfoManger * _projectmanger;
+    AutoSyncPackInfo * _syncPack;
 };
 
 

+ 90 - 0
handle/autosyncpackinfo.cpp

@@ -0,0 +1,90 @@
+#include "autosyncpackinfo.h"
+#include "globalinfo.h"
+#include <QTimer>
+
+AutoSyncPackInfo::AutoSyncPackInfo(QObject *parent) : QObject(parent),_server(nullptr)
+{
+    _manger = nullptr;
+    inQueue = false;
+}
+
+
+void AutoSyncPackInfo::start()
+{
+    if(th == nullptr){
+        th = new QThread(this);
+        this->moveToThread(th);
+        connect(th,&QThread::started,this,&AutoSyncPackInfo::doSend);
+        th->start();
+    } else {
+        QTimer::singleShot(20,this,&AutoSyncPackInfo::doSend);
+    }
+}
+
+void AutoSyncPackInfo::sync(const QDate & dt)
+{
+    if(GlobalInfo::this_()->isNoLine()) return;
+    mutex.lock();
+    _dateQueue.append(dt);
+    mutex.unlock();
+    QTimer::singleShot(20,this,&AutoSyncPackInfo::doQueue);
+}
+
+void AutoSyncPackInfo::doQueue()
+{
+    if(inQueue) return;
+    if(_manger == nullptr)
+        _manger = GlobalInfo::this_()->packInfoManger();
+    if(_server == nullptr) {
+        _server = new PackInfoSeed2Server(_manger,this,false);
+        connect(_server,&PackInfoSeed2Server::syncEnd,this,&AutoSyncPackInfo::syncedOne);
+    }
+    if(_dateQueue.isEmpty()) return;
+    QDate n = _dateQueue.first();
+    auto list =  _manger->getPackInfo(n,true);
+    QList<QSharedPointer<PackInfo>> infos;
+    for(int i = 0; i < list.size(); ++i){
+        infos.append(list.at(i).packInfo);
+    }
+    _server->sendList(infos);
+}
+
+void AutoSyncPackInfo::loadOut()
+{
+    mutex.lock();
+    _dateQueue.clear();
+    mutex.unlock();
+}
+
+void AutoSyncPackInfo::doSend()
+{
+    if(GlobalInfo::this_()->isNoLine()) return;
+    if(_manger == nullptr)
+        _manger = GlobalInfo::this_()->packInfoManger();
+    QDate dt = QDate::currentDate();
+    int td = -15;
+    while(true){
+        auto tdd = dt.addDays(td);
+        auto list = _manger->getPackInfo(tdd,true);
+        if(!list.isEmpty()) {
+            td -= 7;
+        } else {
+            break;
+        }
+    }
+    mutex.lock();
+    for(int i = td; i < 0; ++i){
+        _dateQueue.append(dt.addDays(td));
+    }
+//    _dateQueue.append(dt);
+    mutex.unlock();
+}
+
+void AutoSyncPackInfo::syncedOne()
+{
+    if(_dateQueue.isEmpty()) return;
+    mutex.lock();
+    _dateQueue.dequeue();
+    mutex.unlock();
+    QTimer::singleShot(20,this,&AutoSyncPackInfo::doQueue);
+}

+ 39 - 0
handle/autosyncpackinfo.h

@@ -0,0 +1,39 @@
+#ifndef AUTOSYNCPACKINFO_H
+#define AUTOSYNCPACKINFO_H
+
+#include <QObject>
+#include "packinfoseed2server.h"
+#include <QDate>
+#include <QThread>
+#include <QMutex>
+
+class AutoSyncPackInfo : public QObject
+{
+    Q_OBJECT
+public:
+    explicit AutoSyncPackInfo(QObject *parent = nullptr);
+
+signals:
+
+public slots:
+    void start();
+
+    void sync(const QDate & dt);
+
+    void loadOut();
+protected slots:
+    void doSend();
+    void syncedOne();
+
+    void doQueue();
+private:
+    PackInfoSeed2Server * _server;
+    QQueue<QDate> _dateQueue;
+    PackInfoManger * _manger;
+    QThread * th;
+    bool inQueue;
+    QMutex mutex;
+};
+
+
+#endif // AUTOSYNCPACKINFO_H

+ 12 - 1
handle/packinfoseed2server.cpp

@@ -23,7 +23,11 @@ void PackInfoSeed2Server::send(QSharedPointer<PackInfo> & info)
 
 void PackInfoSeed2Server::doSend()
 {
-    if(_current.isNull() && !_queue.isEmpty()){
+    if(_current.isNull()){
+        if(_queue.isEmpty()) {
+            emit syncEnd();
+            return;
+        }
         _current = _queue.first();
         //docs: http://doc.vanlai.net:3001/web/#/1?page_id=64
         QString url = GlobalInfo::this_()->config()->baseUrl+"/v1/product/pending";
@@ -32,6 +36,13 @@ void PackInfoSeed2Server::doSend()
     }
 }
 
+void PackInfoSeed2Server::sendList(QList<QSharedPointer<PackInfo>> & infos)
+{
+    if(_manger == nullptr) return;
+    _queue.append(infos);
+    QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
+}
+
 void PackInfoSeed2Server::result(int code, const QJsonObject & body)
 {
     qDebug()<<body;

+ 2 - 1
handle/packinfoseed2server.h

@@ -15,10 +15,11 @@ public:
     explicit PackInfoSeed2Server(PackInfoManger * manger,QObject *parent = nullptr,bool autoSync = false);
 
 signals:
-
+    void syncEnd();
 public slots:
     void send(QSharedPointer<PackInfo> & info);
 
+    void sendList(QList<QSharedPointer<PackInfo>> & infos);
 private slots:
     void doSend();
     void result(int code, const QJsonObject & body);

+ 8 - 0
mainwindow.cpp

@@ -111,3 +111,11 @@ void MainWindow::on_budingButton_clicked()
     ui->stackedWidget_2->setCurrentWidget(ui->buding);
     ui->buding->init();
 }
+
+
+void MainWindow::closeEvent(QCloseEvent *event)
+{
+    ui->dingzhong->save();
+//    ui->buding->save();
+    QMainWindow::closeEvent(event);
+}

+ 2 - 0
mainwindow.h

@@ -45,6 +45,8 @@ private:
     void changeToPackDetail();
 //    RtuPort rtuport;
 
+protected:
+    void closeEvent(QCloseEvent *event); // 关闭时调用
 private:
     Ui::MainWindow *ui;
 };

+ 5 - 4
widget/fixedweightpackform.cpp

@@ -50,14 +50,15 @@ void FixedWeightPackForm::calcData()
     }
 }
 
-
-void FixedWeightPackForm::on_pushBack_clicked()
+void FixedWeightPackForm::save()
 {
-    emit back();
     QStringList list;
     list << ui->taitou->text() <<  ui->taiPhone->text();
     PQ::CacheFile::writeFile("dingzhongInfo.cache",list.join("[,]").toUtf8());
 
+    if(!packinfo.isNull()){
+        Manger->savePackInfo(packinfo);
+    }
 }
 
 void FixedWeightPackForm::on_addPackList_clicked()
@@ -178,7 +179,7 @@ void FixedWeightPackForm::init()
 {
     if(packServer == nullptr){
         Manger = GlobalInfo::this_()->packInfoManger();
-        packServer = new PackInfoSeed2Server(Manger);
+        packServer = new PackInfoSeed2Server(Manger,this,true);
 
     }
     ui->pizhong->setCurrentText(QString(_info->box_weight));

+ 4 - 2
widget/fixedweightpackform.h

@@ -47,12 +47,14 @@ public slots:
     void upconfig(/*const DaBaoConfig & config*/);
     void upDengjiInfo(/*const QList<DengJiConfigItem> & dengJis*/);
     void changeSpecs();
+
+    void save(); // 程序关闭的时候调用,保存当前未完成的码单
+private:
+
     void xuanZe_clicked(QString batchno);
     void del_clicked(int row);
     void Makeup_clicked(int row);
     void edit_clicked(int row);
-
-private:
     void init();
     void checkPackInfo();
     void addTableWidget(QSharedPointer<BoxInfo> box, int row);