Commit 2f081428 by guanghui.cui

JSON字段转换

parent cd4aa8be
...@@ -17,6 +17,13 @@ add_compile_options(-std=c++11) ...@@ -17,6 +17,13 @@ add_compile_options(-std=c++11)
#设置执行文件输出目录 #设置执行文件输出目录
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#设置编译后的可执行程序优先调用本地库(和可执行程序在同一个文件夹下的库)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
SET(CMAKE_SKIP_BUILD_RPATH TRUE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
SET(CMAKE_INSTALL_RPATH ".")
SET(CMAKE_LIBRARY_PATH ".")
# 查找目录下的所有源文件,并将名称保存到 DIR_ 变量中 # 查找目录下的所有源文件,并将名称保存到 DIR_ 变量中
aux_source_directory(./3rdParty/easylogging/ DIR_LOGS) aux_source_directory(./3rdParty/easylogging/ DIR_LOGS)
......
...@@ -66,7 +66,7 @@ JsonModule::~JsonModule() ...@@ -66,7 +66,7 @@ JsonModule::~JsonModule()
} }
void JsonModule::getPushOrders(IN const char* json,OUT std::vector<orderObj> &vecOrders) bool JsonModule::getPushOrders(IN const char* json,OUT orderObj &order)
{ {
rapidjson::Document document; // 定义一个Document对象 rapidjson::Document document; // 定义一个Document对象
document.Parse(json); // 解析,Parse()无返回值,也不会抛异常 document.Parse(json); // 解析,Parse()无返回值,也不会抛异常
...@@ -77,103 +77,85 @@ void JsonModule::getPushOrders(IN const char* json,OUT std::vector<orderObj> &ve ...@@ -77,103 +77,85 @@ void JsonModule::getPushOrders(IN const char* json,OUT std::vector<orderObj> &ve
// 使用函数rapidjson::GetParseError_En()得到错误码的字符串说明,这里的En为English简写 // 使用函数rapidjson::GetParseError_En()得到错误码的字符串说明,这里的En为English简写
// 函数GetErrorOffset()返回出错发生的位置 // 函数GetErrorOffset()返回出错发生的位置
LOG(ERROR)<<"JSON parse error:"<<document.GetParseError()<<":"<<document.GetErrorOffset(); LOG(ERROR)<<"JSON parse error:"<<document.GetParseError()<<":"<<document.GetErrorOffset();
return false;
} }
else else
{ {
rapidjson::Value& vCode = document["code"]; rapidjson::Value& channel = documentdocument["channel"];
rapidjson::Value& vMsg = document["message"];
rapidjson::Value& result_obj = document["result"];
std::string strCode = vCode.GetString();
if(strCode.compare("100")!=0)
return;
std::string strMsg = vMsg.GetString();
if(result_obj.IsObject()){
rapidjson::Value& orders_array = result_obj["orders"];
if(orders_array.IsArray()){
for(unsigned int i=0;i<orders_array.Size();i++){
orderObj order;
rapidjson::Value& objValue = orders_array[i];
rapidjson::Value& channel = objValue["channel"];
order.channel=channel.GetString(); order.channel=channel.GetString();
rapidjson::Value& order_id = objValue["order_id"]; rapidjson::Value& order_id = document["order_id"];
order.order_id=order_id.GetString(); order.order_id=order_id.GetString();
rapidjson::Value& create_time = objValue["create_time"]; rapidjson::Value& create_time = document["create_time"];
order.create_time=create_time.GetString(); order.create_time=create_time.GetString();
rapidjson::Value& delivery_time = objValue["delivery_time"]; rapidjson::Value& delivery_time = document["delivery_time"];
order.delivery_time=delivery_time.GetString(); order.delivery_time=delivery_time.GetString();
rapidjson::Value& status = objValue["status"]; rapidjson::Value& status = document["status"];
order.status=_getPOSOrderStatus(status.GetInt()); order.status=_getPOSOrderStatus(status.GetInt());
rapidjson::Value& status_desc = objValue["status_desc"]; rapidjson::Value& status_desc = document["status_desc"];
order.status_desc=status_desc.GetString(); order.status_desc=status_desc.GetString();
rapidjson::Value& total_fee = objValue["total_fee"]; rapidjson::Value& total_fee = document["total_fee"];
order.total_price=total_fee.GetInt(); order.total_price=total_fee.GetInt();
rapidjson::Value& send_fee = objValue["send_fee"]; rapidjson::Value& send_fee = document["send_fee"];
order.delivery_price=send_fee.GetInt(); order.delivery_price=send_fee.GetInt();
rapidjson::Value& discount_fee = objValue["discount_fee"]; rapidjson::Value& discount_fee = document["discount_fee"];
order.reduced_price=discount_fee.GetInt(); order.reduced_price=discount_fee.GetInt();
//配送 //配送
rapidjson::Value& delivery_type = objValue["delivery_type"]; rapidjson::Value& delivery_type = document["delivery_type"];
order.deliveryInfo.type=_getDeliveryTypeString(delivery_type.GetInt()); order.deliveryInfo.type=_getDeliveryTypeString(delivery_type.GetInt());
rapidjson::Value& delivery_status = objValue["delivery_status"]; rapidjson::Value& delivery_status = document["delivery_status"];
order.deliveryInfo.status=delivery_status.GetInt(); order.deliveryInfo.status=delivery_status.GetInt();
rapidjson::Value& courier_name = objValue["courier_name"]; rapidjson::Value& courier_name = document["courier_name"];
order.deliveryInfo.driver_name=courier_name.GetString(); order.deliveryInfo.driver_name=courier_name.GetString();
rapidjson::Value& courier_phone = objValue["courier_phone"]; rapidjson::Value& courier_phone = document["courier_phone"];
order.deliveryInfo.driver_phone=courier_phone.GetString(); order.deliveryInfo.driver_phone=courier_phone.GetString();
//顾客信息 //顾客信息
rapidjson::Value& customer = objValue["customer"]; rapidjson::Value& customer = document["customer"];
order.customerInfo.name=customer.GetString(); order.customerInfo.name=customer.GetString();
rapidjson::Value& phone = objValue["phone"]; rapidjson::Value& phone = document["phone"];
order.customerInfo.phone=phone.GetString(); order.customerInfo.phone=phone.GetString();
rapidjson::Value& address = objValue["address"]; rapidjson::Value& address = document["address"];
order.customerInfo.address=address.GetString(); order.customerInfo.address=address.GetString();
//商品列表 //商品列表
rapidjson::Value& products_array = objValue["products"]; rapidjson::Value& products_array = document["products"];
if(products_array.IsArray()){ if(products_array.IsArray()){
for(unsigned int j=0;j<products_array.Size();j++){ for(unsigned int i=0;i<products_array.Size();i++){
productAttr proAttr; productAttr proAttr;
rapidjson::Value& objProductValue = products_array[j]; rapidjson::Value& objProductValue = products_array[i];
rapidjson::Value& name = objValue["name"]; rapidjson::Value& name = objProductValue["name"];
proAttr.pro.name=name.GetString(); proAttr.pro.name=name.GetString();
rapidjson::Value& pid = objValue["pid"]; rapidjson::Value& pid = objProductValue["pid"];
proAttr.pro.sku=pid.GetString(); proAttr.pro.sku=pid.GetString();
rapidjson::Value& price = objValue["price"]; rapidjson::Value& price = objProductValue["price"];
proAttr.pro.price=price.GetInt(); proAttr.pro.price=price.GetInt();
rapidjson::Value& productAmount = objValue["productAmount"]; rapidjson::Value& productAmount = objProductValue["productAmount"];
proAttr.pro.qty=atoi(productAmount.GetString()); proAttr.pro.qty=atoi(productAmount.GetString());
order.vecProducts.push_back(proAttr); order.vecProducts.push_back(proAttr);
} }
} }
vecOrders.push_back(order);
}
}
}
} }
return true;
} }
bool JsonModule::isInitData(const std::string &data) bool JsonModule::isInitData(const std::string &data)
...@@ -331,7 +313,23 @@ bool JsonModule::getOdsResponseData(const std::string &posResponse, const std::s ...@@ -331,7 +313,23 @@ bool JsonModule::getOdsResponseData(const std::string &posResponse, const std::s
bool JsonModule::convertDataPos2Ods(const std::string &data, std::string &result) bool JsonModule::convertDataPos2Ods(const std::string &data, std::string &result)
{ {
bool rlt=true;
orderObj order;
rlt=getPushOrders(data.data(),order);
if(ORDERSTATUS_NEW==order.status){
result = _convertToNewOrderJson(order);
}
else if(ORDERSTATUS_CONFIRMED==order_status||ORDERSTATUS_CANCELED==order_status||ORDERSTATUS_SHIPPING==order_status||ORDERSTATUS_COMPLETE==order_status||ORDERSTATUS_REFUND_COMPLETE){
orderStatusObj statusObj;
_getStatusObj(order,statusObj);
result = _convertToOrderStatusJson(statusObj);
}
else if(ORDERSTATUS_REFUND_FULL==order_status||ORDERSTATUS_REFUND_PART==order_status){
refundObj refund;
_getRefundObj(order,refund);
result = _convertToRefundJson(refund);
}
return rlt;
} }
bool JsonModule::convertDataOds2Pos(const std::string &data, std::string &result) bool JsonModule::convertDataOds2Pos(const std::string &data, std::string &result)
...@@ -352,7 +350,7 @@ bool JsonModule::convertDataOds2Pos(const std::string &data, std::string &result ...@@ -352,7 +350,7 @@ bool JsonModule::convertDataOds2Pos(const std::string &data, std::string &result
} }
} }
std::string JsonModule::convertToNewOrderJson(orderObj &obj) std::string JsonModule::_convertToNewOrderJson(orderObj &obj)
{ {
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
...@@ -634,7 +632,7 @@ std::string JsonModule::convertToNewOrderJson(orderObj &obj) ...@@ -634,7 +632,7 @@ std::string JsonModule::convertToNewOrderJson(orderObj &obj)
return buffer.GetString(); return buffer.GetString();
} }
std::string JsonModule::convertToOrderStatusJson(orderStatusObj &obj) std::string JsonModule::_convertToOrderStatusJson(orderStatusObj &obj)
{ {
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
...@@ -657,7 +655,7 @@ std::string JsonModule::convertToOrderStatusJson(orderStatusObj &obj) ...@@ -657,7 +655,7 @@ std::string JsonModule::convertToOrderStatusJson(orderStatusObj &obj)
return buffer.GetString(); return buffer.GetString();
} }
std::string JsonModule::convertToRefundJson(refundObj &obj) std::string JsonModule::_convertToRefundJson(refundObj &obj)
{ {
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
...@@ -747,7 +745,7 @@ std::string JsonModule::convertToRefundJson(refundObj &obj) ...@@ -747,7 +745,7 @@ std::string JsonModule::convertToRefundJson(refundObj &obj)
return buffer.GetString(); return buffer.GetString();
} }
std::string JsonModule::convertToStockWarnJson(stockWarnObj &obj) std::string JsonModule::_convertToStockWarnJson(stockWarnObj &obj)
{ {
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
...@@ -862,14 +860,14 @@ int JsonModule::_getPOSOrderStatus(int status) ...@@ -862,14 +860,14 @@ int JsonModule::_getPOSOrderStatus(int status)
return rltStatus; return rltStatus;
} }
void JsonModule::getStatusObj(IN orderObj &order_obj,OUT orderStatusObj &status_obj) void JsonModule::_getStatusObj(IN orderObj &order_obj,OUT orderStatusObj &status_obj)
{ {
status_obj.order_id=order_obj.order_id; status_obj.order_id=order_obj.order_id;
status_obj.order_status=order_obj.status; status_obj.order_status=order_obj.status;
status_obj.delivery_status=order_obj.deliveryInfo.status; status_obj.delivery_status=order_obj.deliveryInfo.status;
} }
void JsonModule::getRefundObj(IN orderObj &order_obj,OUT refundObj &refund_obj) void JsonModule::_getRefundObj(IN orderObj &order_obj,OUT refundObj &refund_obj)
{ {
refund_obj.order_id=order_obj.order_id; refund_obj.order_id=order_obj.order_id;
refund_obj.channel=order_obj.channel; refund_obj.channel=order_obj.channel;
......
...@@ -12,7 +12,7 @@ public: ...@@ -12,7 +12,7 @@ public:
JsonModule(); JsonModule();
~JsonModule(); ~JsonModule();
void getPushOrders(IN const char* json,OUT std::vector<orderObj> &vecOrders); bool getPushOrders(IN const char* json,OUT orderObj &order);
/* 功能:判断是否是初始化数据 /* 功能:判断是否是初始化数据
* 参数:[1]待判断数据 * 参数:[1]待判断数据
...@@ -53,17 +53,19 @@ public: ...@@ -53,17 +53,19 @@ public:
* */ * */
bool convertDataOds2Pos(IN const std::string& data, OUT std::string& result); bool convertDataOds2Pos(IN const std::string& data, OUT std::string& result);
std::string convertToNewOrderJson(orderObj &obj);
std::string convertToOrderStatusJson(orderStatusObj &obj);
std::string convertToRefundJson(refundObj &obj);
std::string convertToStockWarnJson(stockWarnObj &obj);
void getStatusObj(IN orderObj &order_obj,OUT orderStatusObj &status_obj);
void getRefundObj(IN orderObj &order_obj,OUT refundObj &refund_obj);
private: private:
std::string _getDeliveryTypeString(int type); std::string _getDeliveryTypeString(int type);
//订单状态转换(转换为POS对应的状态) //订单状态转换(转换为POS对应的状态)
int _getPOSOrderStatus(int status); int _getPOSOrderStatus(int status);
void _getStatusObj(IN orderObj &order_obj,OUT orderStatusObj &status_obj);
void _getRefundObj(IN orderObj &order_obj,OUT refundObj &refund_obj);
std::string _convertToNewOrderJson(orderObj &obj);
std::string _convertToOrderStatusJson(orderStatusObj &obj);
std::string _convertToRefundJson(refundObj &obj);
std::string _convertToStockWarnJson(stockWarnObj &obj);
}; };
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment