syscommugroupthread.cpp 5.09 KB
Newer Older
‘oliver.hui’'s avatar
‘oliver.hui’ committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#include "syscommugroupthread.h"
#include <tbaseutil/tdataparse.h>
#include <tbaseutil/tdataresponse.h>
#include <tdatabaseutil/tsqlconnectionpoolv2.h>
#include <tdatabaseutil/tsqlqueryv2.h>
#include <tdatabaseutil/tsqlselectorv2.h>

SysCommuGroupThread::SysCommuGroupThread(QObject *iParent) :
    TopClassThreadAbs(iParent)
{

}

SysCommuGroupThread::~SysCommuGroupThread()
{

}

void SysCommuGroupThread::run()
{
    if (invokeName() == "LOAD_DATA") {
        loadData(invokeParameter().toInt());
    } else if (invokeName() == "SAVE_DATA") {
        saveData(invokeParameter().toMap());
    }
}

void SysCommuGroupThread::loadData(int iIdInt)
{
    TDataResponse dataRes;

    TSqlQueryV2 sqlQuery(T_SQLCNT_POOL->getSqlDatabase());
    sqlQuery.begin();
    try {
        TSqlSelectorV2 sqlSelector;
        sqlSelector.setTable("sys_commu_grp");
        sqlSelector.setField("*");
        sqlSelector.setFieldFormat(QVariantMap{{"attr_data", "json"}});
        sqlSelector.setWhere(QString(" id = %1 ").arg(iIdInt));
        QVariantMap dataMap = sqlQuery.selectMap(sqlSelector);
        if (sqlQuery.lastError().isValid()) {
            throw sqlQuery.lastError();
        }

        sqlSelector.clear();
        sqlSelector.setTable("sys_commu_grp_detail");
        sqlSelector.setField("*");
        sqlSelector.setWhere(QString(" commu_grp_id = %1 ").arg(QString::number(iIdInt)));
        QVariantList linkLst = sqlQuery.selectArrayMap(sqlSelector);
        if (sqlQuery.lastError().isValid()) {
            throw sqlQuery.lastError();
        }

        dataMap.insert("MAILINFO_LIST", linkLst);

        sqlQuery.commit();

        dataRes.setData(dataMap);
        setInvokeResult(dataRes.toVariantMap());
        return;
    } catch (const TError &err){
        dataRes.setError(err);
    } catch(...) {
        dataRes.setErrText(ttr("Unknow Error!"));
    }
    sqlQuery.rollback();
    setInvokeResult(dataRes.toVariantMap());
}

void SysCommuGroupThread::saveData(const QVariantMap &iDataMap)
{
    TDataResponse dataRes;
    TSqlQueryV2 sqlQuery(T_SQLCNT_POOL->getSqlDatabase());
    sqlQuery.begin();
    try {
        TSqlSelectorV2 sqlSelector;
        sqlSelector.setTable("sys_commu_grp");
        sqlSelector.setField("COUNT(1)");
        if (iDataMap.value("sys_name").toString().trimmed().isEmpty()) {
            throw TError(ttr("Name must not be empty!"),"ERROR","ALREADY_EXISTS");
        }
        sqlSelector.addWhere("sys_name", iDataMap.value("sys_name"));
        if (!iDataMap.value("id").toString().trimmed().isEmpty()) {
            sqlSelector.addWhere(QString(" id != %1 ").arg(iDataMap.value("id").toString()));
        }
        int count = sqlQuery.selectCount(sqlSelector);
        if (count > 0) {
            throw TError(ttr("'%1' already exists!").arg(iDataMap.value("sys_name").toString()), "ERROR", "ALREADY_EXISTS");
        }

        QStringList fieldLst = iDataMap.keys();
        fieldLst.removeOne("updatePolicy");
        fieldLst.removeOne("MAILINFO_LIST");
        fieldLst.removeOne("id");

        TSqlInserterV2 sqlInserter;
        sqlInserter.setTable("sys_commu_grp");
        sqlInserter.setData(iDataMap);
        sqlInserter.setField(fieldLst);
        sqlInserter.setUpdatePolicy(iDataMap.value("updatePolicy").toMap());
        sqlInserter.setUniqueField("id");
        sqlInserter.setAutoIncrementField("id");

        QVariant mailGroupId = sqlQuery.replaceRow(sqlInserter);
        if (sqlQuery.lastError().isValid()) {
            throw sqlQuery.lastError();
        }

        TSqlDeleterV2 sqlDeleter;
        sqlDeleter.setTable("sys_commu_grp_detail");
        sqlDeleter.setWhere(QString(" commu_grp_id = %1 ").arg(mailGroupId.toInt()));
        sqlQuery.deleteRow(sqlDeleter);
        if (sqlQuery.lastError().isValid()) {
            throw sqlQuery.lastError();
        }

        TSqlInserterV2 linkInserter;
        linkInserter.setTable("sys_commu_grp_detail");
        linkInserter.fieldRef() << "commu_grp_id" << "name" << "address" << "mode" << "src_type" << "src_uid";
        for (QVariant link: iDataMap.value("MAILINFO_LIST").toList()) {
            QVariantMap linkMap = link.toMap();
            QVariantMap insertMap;
            insertMap.insert("commu_grp_id", mailGroupId.toString());
            insertMap.insert("name", linkMap.value("name").toString());
            insertMap.insert("address", linkMap.value("address"));
            insertMap.insert("mode", linkMap.value("mode"));
            insertMap.insert("src_type", linkMap.value("src_type"));
            insertMap.insert("src_uid", linkMap.value("src_uid"));
            //插入to_cc和src的值
            linkInserter.setData(insertMap);
            sqlQuery.insertRow(linkInserter);
            if (sqlQuery.lastError().isValid()) {
                throw sqlQuery.lastError();
            }
        }

        sqlQuery.commit();

        dataRes.setData(mailGroupId);
        setInvokeResult(dataRes.toVariantMap());
        return;
    } catch (const TError &err) {
        dataRes.setError(err);
    } catch (...) {
        dataRes.setErrText(ttr("Unknow Error!"));
    }
    sqlQuery.rollback();
    setInvokeResult(dataRes.toVariantMap());
}