bool CAccessPersonBTCard::getCompareResult(const Json::Value& cardList, Json::Value& results) { debugf("getCompareResult cardList = %s\n", cardList.toStyledString().c_str());

if (!cardList.isMember("BTCardInfoList") || !cardList["BTCardInfoList"].isArray())
{
    Dahua::Infra::setLastError(businessCommonErrorInvalidArgs);
    errorf_ReDFXLOG("request param CardList is error\n");
    return false;
}

const uint32_t cardSize = cardList["BTCardInfoList"].size();
if (cardSize > AccessControl::MAX_INSERT_CARD_RATE)
{
    Dahua::Infra::setLastError(accessControlErrorExceedMaxInsertRate);
    errorf_ReDFXLOG("The request cardSize exceeds the MaxInsertRate\n");
    return false;
}

Json::Value condition = Json::nullValue;
condition["UserID"] = cardList["UserID"].asCString();
Json::Value conResult = Json::nullValue;
CACPlatformHelper::instance()->findTableDB(AccessPersonnalBTCard, condition, conResult);

const int32_t oldSize = conResult.size();
const int32_t newSize = cardSize;
if (oldSize > 0 && newSize == 0)
{
    Json::Value removeCard = Json::nullValue;
    for (const auto& card : conResult)
    {
        removeCard["BTCardInfoList"].append(card);
    }
    results["RemoveList"] = removeCard["BTCardInfoList"].asString();
    debugf("results = %s\n", results.toStyledString().c_str());
    return true;
}
else if (oldSize == 0 && newSize == 0)
{
    return true;
}
else if (oldSize == 0 && newSize > 0)
{
    results["InsertList"] = cardList["BTCardInfoList"].asString();
    debugf("results = %s\n", results.toStyledString().c_str());
    return true;
}
else if (oldSize > 0 && newSize > 0)
{
    Json::Value insertCard = Json::nullValue;
    Json::Value updateCard = Json::nullValue;
    Json::Value removeCard = Json::nullValue;
    for (const auto& conCard : conResult)
    {
        bool find = false;
        for (const auto& card : cardList["BTCardInfoList"])
        {
            if (conCard["AKID"].asUInt64() == card["AKID"].asUInt64())
            {
                find = true;
                updateCard["BTCardInfoList"].append(card);
                break;
            }
        }
        if (!find)
        {
            removeCard["BTCardInfoList"].append(conCard);
            debugf("conCard = %s\n", conCard.toStyledString().c_str());
        }
    }
    results["RemoveList"] = removeCard;

    for (const auto& card : cardList["BTCardInfoList"])
    {
        bool find = false;
        for (const auto& conCard : conResult)
        {
            if (conCard["AKID"].asUInt64() == card["AKID"].asUInt64())
            {
                find = true;
                break;
            }
        }
        if (!find)
        {
            insertCard["Info"]["BTCardInfoList"].append(card);
            debugf("cardList[BTCardInfoList][m] = %s\n", card.toStyledString().c_str());
        }
    }
    results["InsertList"] = insertCard;

    tracef("results=%s\n", results.toStyledString().c_str());
}

return true;

}

优化代码:nbool-CAccessPersonBTCardgetCompareResultconst-JsonValue&-cardList-JsonValue&-resultsnntdebugfgetCompareResult-cardList-=-sncardListtoStyledStringc_str;-ntif-!cardListisMemberBTCardInfoList--!cardListBTCardInfoListisArrayntntt-错误应该是协议组头文件定好这里先按照

原文地址: https://www.cveoy.top/t/topic/quy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录