Commit 27811ff8 by guanghui.cui

库存预警完善

parent c7a4adb9
......@@ -664,17 +664,18 @@ bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, std
return true;
}
bool JsonModule::getOdsResponseData(const std::string &posResponse, const std::string &orderData, std::string &result)
{
if( posResponse.empty() || orderData.empty() ){
bool JsonModule::getOdsResponseData(IN const std::string& posResponse, IN const std::string& orderData,IN const std::string& odsData, OUT std::string& result)
{
if( posResponse.empty() || orderData.empty() || odsData.empty()){
LOG(INFO)<<"getOdsResponseData empty input";
return false;
}
rapidjson::Document document, document1;
rapidjson::Document document, document1,document2;
document.Parse(posResponse.c_str());
document1.Parse(orderData.c_str());
if (document.HasParseError() || document1.HasParseError())
document2.Parse(odsData.c_str());
if (document.HasParseError() || document1.HasParseError() || document2.HasParseError())
{
LOG(INFO)<<"getOdsResponseData posResponse JSON parse error:"<<document.GetParseError()<<":"<<document.GetErrorOffset();
return false;
......@@ -693,11 +694,11 @@ bool JsonModule::getOdsResponseData(const std::string &posResponse, const std::s
rlt=_getOrderResponseJson(posResponse,orderData,result);
}
else if(fm_cmd==REQUEST_TYPE_STOCK_WARN){
rlt=_getStockWarnResponseJson(posResponse,orderData,result);
rlt=_getStockWarnResponseJson(posResponse,orderData,odsData,result);
}
return rlt;
}
}
bool JsonModule::convertDataPos2Ods(const std::string &data, std::string &result)
{
......@@ -1498,14 +1499,15 @@ bool JsonModule::_getOrderResponseJson(IN const std::string& posResponse, IN con
return true;
}
bool JsonModule::_getStockWarnResponseJson(IN const std::string& posResponse, IN const std::string& orderData, OUT std::string& result)
bool JsonModule::_getStockWarnResponseJson(IN const std::string& posResponse, IN const std::string& orderData,IN const std::string& odsData, OUT std::string& result)
{
//待接口确定
rapidjson::Document document, document1;
rapidjson::Document document, document1,document2;
document.Parse(posResponse.c_str());
document1.Parse(orderData.c_str());
document2.Parse(odsData.c_str());
if( !document.HasMember("status_code") || !document1.HasMember("channel") || !document1.HasMember("alertTime") )
if( !document.HasMember("status_code") || !document2.HasMember("channel") || !document2.HasMember("alertTime")|| !document2.HasMember("storeId"))
{
LOG(INFO)<<"Don't have needed parames";
return false;
......@@ -1515,8 +1517,9 @@ bool JsonModule::_getStockWarnResponseJson(IN const std::string& posResponse, IN
std::string msg = document["msg"].GetString();
std::string channel = document1["channel"].GetString();
std::string alertTime = document1["alertTime"].GetString();
std::string channel = document2["channel"].GetString();
int64_t alertTime = document2["alertTime"].GetInt64();
std::string storeId = document2["storeId"].GetString();
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
......@@ -1528,15 +1531,15 @@ bool JsonModule::_getStockWarnResponseJson(IN const std::string& posResponse, IN
writer.Key("channel");
writer.String(channel.c_str());
writer.Key("alertTime");
writer.String(alertTime.c_str());
writer.Key("store_id");
writer.String(storeId.c_str());
writer.Key("alert_time");
writer.Int64(alertTime);
writer.Key("status_code");
writer.Int(status_code);
writer.Key("msg");
writer.String(msg.c_str());
writer.EndObject();
result = buffer.GetString();
......
......@@ -51,10 +51,11 @@ public:
/* 功能:获取ODS推送的返回数据
* 参数:[1]状态码[2]消息[3]转换后数据
* 重载:[1]POS返回数据[2]订单数据[3]转换后数据
* 重载:[1]POS返回数据[2]订单数据[3]ods发送数据[4]转换后数据
* 返回:是否转换成功
* */
bool getOdsResponseData(IN int status_code, IN const std::string& msg, OUT std::string& result);
bool getOdsResponseData(IN const std::string& posResponse, IN const std::string& orderData, OUT std::string& result);
bool getOdsResponseData(IN const std::string& posResponse, IN const std::string& orderData,IN const std::string& odsData, OUT std::string& result);
/* 功能:转换POS数据格式到中台数据格式
* 参数:[1]POS数据格式[2]转换后数据
......@@ -97,7 +98,7 @@ private:
std::string _convertToStockWarnJson(stockWarnObj &obj);
bool _getOrderResponseJson(IN const std::string& posResponse, IN const std::string& orderData, OUT std::string& result);
bool _getStockWarnResponseJson(IN const std::string& posResponse, IN const std::string& orderData, OUT std::string& result);
bool _getStockWarnResponseJson(IN const std::string& posResponse, IN const std::string& orderData, IN const std::string& odsData,OUT std::string& result);
};
#endif
......@@ -27,7 +27,7 @@ bool bInitDone=false; //初始化完成
SQLite sqlite;
//函数,订单信息发送给pos
bool order_send_to_pos(IN std::string &order_json,OUT std::string &back_json);
bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT std::string &back_json);
//失败订单写入vector及sqlite
void write_failed_order(IN std::string &order_json);
......@@ -152,21 +152,27 @@ void* listen_pos_func(void* arg)
void* retry_send_pos_func(void* arg)
{
bRetryThreadRunning=true;
JsonModule jsonTool;
std::vector<orderSendFailedObj>::iterator Iter;
for(Iter = vecFailedOrders.begin(); Iter != vecFailedOrders.end();) {
Iter = vecFailedOrders.begin(); //一直发送第一个,直到发送成功
std::string back_json;
if(order_send_to_pos((*Iter).order_json,back_json)){
char lpSql[200] = {0};
sprintf(lpSql, "delete from fmOrderFailed where id = %lld",(*Iter).timestamp);
LOG(INFO)<<"sql:"<<lpSql;
std::string ods_json=(*Iter).order_json;
std::string order_json;
if( jsonTool.convertDataOds2Pos(ods_json, order_json) ){
if(order_send_to_pos(order_json,ods_json,back_json)){
char lpSql[200] = {0};
sprintf(lpSql, "delete from fmOrderFailed where id = %lld",(*Iter).timestamp);
LOG(INFO)<<"sql:"<<lpSql;
if(!sqlite.remove(lpSql)){
LOG(ERROR) << "remove failed:"<<lpSql;
}
if(!sqlite.remove(lpSql)){
LOG(ERROR) << "remove failed:"<<lpSql;
vecFailedOrders.erase(Iter); //Iter为删除元素的下一个元素的迭代器
}
vecFailedOrders.erase(Iter); //Iter为删除元素的下一个元素的迭代器
}
sleep(1); //等待1s,再次尝试发送
}
LOG(INFO)<<"retry_send_pos_func thread done";
......@@ -302,8 +308,8 @@ int main()
{
LOG(INFO) << "********convert data to pos************";
LOG(INFO) << pushPosData;
if(!order_send_to_pos(pushPosData,responseData)){
write_failed_order(pushPosData);
if(!order_send_to_pos(pushPosData,odsPushData,responseData)){
write_failed_order(odsPushData);
if(!bRetryThreadRunning){ //如果线程没有在运行
pthread_t retry_thread_id;
/*创建 retry send 线程*/
......@@ -337,7 +343,7 @@ int main()
return 0;
}
bool order_send_to_pos(IN std::string &order_json,OUT std::string &back_json)
bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT std::string &back_json)
{
bool rlt=true;
TCPClient pos;
......@@ -370,7 +376,8 @@ bool order_send_to_pos(IN std::string &order_json,OUT std::string &back_json)
{
rlt=false;
}
jsonTool.getOdsResponseData(tmp, order_json, back_json);
jsonTool.getOdsResponseData(tmp, order_json,ods_json, back_json);
return rlt;
}
......
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