﻿// Copyrigth Xtooltech @ 2023- All rights reserved.(LXH)

#include "PrintResultItem.hpp"
#include "MyStringUtils.h"
#include "cJSON.h"

namespace otx {
	namespace jidu {
		PrintResultItem::PrintResultItem() {
			this->mTestBlockResult = "OK";
		}
		std::string PrintResultItem::getTestBlockName() {
			return this->mTestBlockName;
		}
		void PrintResultItem::setTestBlockName(const std::string& v) { this->mTestBlockName = v; }

		std::string PrintResultItem::getTestBlockId() { return this->mTestBlockId; }
		void PrintResultItem::setTestBlockId(const std::string& v) { this->mTestBlockId = v; }

		std::string PrintResultItem::getTestBlockResult() { return this->mTestBlockResult; }
		void PrintResultItem::setTestBlockResult(const std::string& v) { this->mTestBlockResult = v; }

		std::vector<std::string>* PrintResultItem::getFailReason() { return &this->mFailReason; }
		void PrintResultItem::addFailReason(const std::string& v) { this->mFailReason.push_back(v); }

		std::string PrintResultItem::toJsonString() {
			std::string result;

			cJSON* obj = cJSON_CreateObject();

			if (this->mTestBlockName.size() > 0) {
				cJSON_AddStringToObject(obj, "testBlockName", this->mTestBlockName.c_str());
			}

			if (this->mTestBlockId.size() > 0) {
				cJSON_AddStringToObject(obj, "testBlockId", this->mTestBlockId.c_str());
			}

			if (this->mTestBlockResult.empty()) {
				mTestBlockResult = "OK";
			}
			cJSON_AddStringToObject(obj, "testBlockResult", this->mTestBlockResult.c_str());

			int size = this->mFailReason.size();
			if (size > 0) {
				cJSON* arrObj = cJSON_CreateArray();
				for (int i = 0; i < size; i++) {
					int blockId = MyStringUtils::toLong(this->mTestBlockId, 16);
					if (blockId == 0x0C || blockId == 0x33 || blockId == 0x75) {
						if (this->mFailReason[i].find("请求") == string::npos && this->mFailReason[i].find("响应") == string::npos) {
							cJSON_AddItemToArray(arrObj, cJSON_CreateString(this->mFailReason[i].c_str()));
						}
					}
					else {
						cJSON_AddItemToArray(arrObj, cJSON_CreateString(this->mFailReason[i].c_str()));
					}
				}
				cJSON_AddItemToObject(obj, "failReason", arrObj);
			}

			char* tepm = cJSON_PrintUnformatted(obj);
			cJSON_Delete(obj);
			result = tepm;
			cJSON_free(tepm);
			return result;
		}

	} // namespace jidu
} // namespace otx
