sysuserthread.cpp 8.31 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
#include "sysuserthread.h"
#include <topcore/topclassabs.h>
#include <tbaseutil/tdataparse.h>
#include <tbaseutil/tdataresponse.h>
#include <tdatabaseutil/tsqlconnectionpoolv2.h>
#include <tdatabaseutil/tsqlqueryv2.h>
#include <tdatabaseutil/tsqlselectorv2.h>

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

}

SysUserThread::~SysUserThread()
{

}

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

void SysUserThread::loadData(int iIdInt)
{
    TDataResponse dataRes;
    TSqlQueryV2 sqlQuery(T_SQLCNT_POOL->getSqlDatabase());
    sqlQuery.begin();
    try {
        TSqlSelectorV2 sqlSelector;
        sqlSelector.setTable("sys_user AS U LEFT JOIN pub_contacts AS C ON U.contcat_id = C.id");
        sqlSelector.setField("U.*, C.mail");
        sqlSelector.setWhere("U.id", QVariant(iIdInt));
        sqlSelector.setFieldFormat("product_category", "array");
        sqlSelector.setFieldFormat("attr_data", "json");
        QVariantMap dataMap = sqlQuery.selectMap(sqlSelector);
        if (sqlQuery.lastError().isValid()) {
            throw sqlQuery.lastError();
        }

        sqlSelector.clear();
        sqlSelector.setTable("sys_role_map_user AS UR LEFT JOIN sys_role AS R ON UR.role_id = R.id");
        sqlSelector.fieldRef() << "R.id" << "R.name" << "R.sys_name" << "R.tags" << "R.description";
        sqlSelector.setWhere("UR.user_id", iIdInt);
        sqlSelector.setOrder("R.name", Qt::AscendingOrder);
        QVariantList roleList = sqlQuery.selectArrayMap(sqlSelector);
        if (sqlQuery.lastError().isValid()) {
            throw sqlQuery.lastError();
        }
        dataMap.insert("ROLE_LIST", roleList);

        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 SysUserThread::saveData(const QVariantMap &iDataMap)
{
    TDataResponse dataRes;

    TSqlQueryV2 sqlQuery(T_SQLCNT_POOL->getSqlDatabase());
    sqlQuery.begin();
    try {
        TSqlSelectorV2 sqlSelector;
        sqlSelector.setTable("sys_user");
        sqlSelector.setField("COUNT(1)");
        sqlSelector.addWhere("username", iDataMap.value("username"));
        sqlSelector.addWhere("id", iDataMap.value("id"), "!=");
        int count = sqlQuery.selectCount(sqlSelector);
        if (count > 0) {
            throw TError(ttr("'%1' already exists!").arg(iDataMap.value("username").toString()), "ERROR", "ALREADY_EXISTS");
        }

        // 更新pub_contacts表的mail
        QVariantMap contactMap;
        contactMap.insert("mail", iDataMap.value("mail"));
        QString username = iDataMap.value("username").toString();
        QString fullname = iDataMap.value("fullname").toString();
        QString name;
        if (!fullname.isEmpty() && (username != fullname)) {
            name = QString("%1(%2)").arg(fullname).arg(username);
        } else {
            name = username;
        }
        contactMap.insert("name", name);
        contactMap.insert("id", iDataMap.value("contcat_id").toInt());
        contactMap.insert("source", "company");
        contactMap.insert("status", iDataMap.value("status").toString() == "active" ? "active" : "inactive");
        TopClassAbs *parentModule = static_cast<TopClassAbs*>(this->parent());
        if (parentModule) {
            if (parentModule->isHookExists("handleContactData")) {
                contactMap = parentModule->callHooksQuick("handleContactData", QVariantList()<<contactMap<<iDataMap).toVariant().toMap();
            }
        }
        QStringList contactFields = contactMap.keys();
        contactFields.removeOne("id");
        TSqlInserterV2 sqlInserter;
        sqlInserter.setTable("pub_contacts");
        sqlInserter.setData(contactMap);
        sqlInserter.setField(contactFields);
        sqlInserter.setUniqueField("id");
        sqlInserter.setAutoIncrementField("id");

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

        // 更新sys_commu_grp_detail
        TSqlUpdaterV2 sqlUpdater;
        sqlUpdater.setTable("sys_commu_grp_detail");
        sqlUpdater.setField(QStringList() << "address" << "name");
        QVariantMap commuGrpDetailDataMap;
        commuGrpDetailDataMap.insert("address", iDataMap.value("mail"));
        commuGrpDetailDataMap.insert("name", name);
        sqlUpdater.setData(commuGrpDetailDataMap);
        TSqlWhereCompsiteV2 commuGrpDetailWhere;
        commuGrpDetailWhere.append("src_type", "pub_contacts");
        commuGrpDetailWhere.append("src_uid", iDataMap.value("contcat_id").toString());
        commuGrpDetailWhere.setLogic(TSqlWhereCompsiteV2::Logic_And);
        sqlUpdater.setWhere(commuGrpDetailWhere);
        sqlQuery.updateRow(sqlUpdater);
        if (sqlQuery.lastError().isValid()) {
            throw sqlQuery.lastError();
        }

        // 更新sys_user表
        QVariantMap dataMap = iDataMap;
        QStringList fieldLst = dataMap.keys();
        dataMap.insert("contcat_id", contactId);

        QStringList productCategory = dataMap.value("product_category").toStringList();
        // 只要产品分类列表中包含有*, 就只存*
        if (productCategory.contains("*")) {
            productCategory = (QStringList() << "*");
        }
        dataMap.insert("product_category", productCategory);

        if (iDataMap.value("id").toInt() == 0) {
            // 新建时插入默认密码
            QString password = iDataMap.value("password").toString();
            if (password.isEmpty()) {
                password = QStringLiteral("123456");
            }
            dataMap.insert("password", TDataParse::getVariantMd5(password));
        } else {
            fieldLst.removeOne("password");
        }
        fieldLst.removeOne("ROLE_LIST");
        fieldLst.removeOne("id");
        fieldLst.removeOne("mail");
        QVariantMap attrDataMap = dataMap.value("attr_data").toMap();
        if (attrDataMap.value("password_validity_control") == 0) {
            attrDataMap.insert("effective_date", QString(""));
            attrDataMap.insert("validity_period", QString(""));
            dataMap.insert("attr_data", attrDataMap);
        }
        qDebug().noquote().nospace()<<TDataParse::variant2JsonStr(dataMap);
        sqlInserter.clear();
        sqlInserter.setTable("sys_user");
        sqlInserter.setData(dataMap);
        sqlInserter.setField(fieldLst);
        sqlInserter.setUpdatePolicy("attr_data", "json_merge");
        sqlInserter.setUniqueField("id");
        sqlInserter.setAutoIncrementField("id");

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

        // 更新sys_role_map_user表
        TSqlDeleterV2 sqlDeleter;
        sqlDeleter.setTable("sys_role_map_user");
        sqlDeleter.setWhere("user_id", userId);
        sqlQuery.deleteRow(sqlDeleter);
        if (sqlQuery.lastError().isValid()) {
            throw sqlQuery.lastError();
        }

        QVariantList roleMapUserLst;
        for (QVariant role : iDataMap.value("ROLE_LIST").toList()) {
            QVariantMap roleMap = role.toMap();
            QVariantMap tempMap;
            tempMap.insert("user_id", userId.toString());
            tempMap.insert("role_id", roleMap.value("id").toString());
            roleMapUserLst << tempMap;
        }
        if (!roleMapUserLst.isEmpty()) {
            sqlQuery.batchInsert("sys_role_map_user",
                                 QStringList() << "user_id" << "role_id",
                                 roleMapUserLst);
            if (sqlQuery.lastError().isValid()) {
                throw sqlQuery.lastError();
            }
        }

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