
#include    "ResultUtils.h"
#include    "goahead.h"
#include    "cJSON.h"
#include 	<stdio.h>

static void commResult(Webs *wp, int msgCode, int httpStatus, int isSuccess) ;

void HelloWord(Webs *wp)
{
	printf("---> hello ResultUtils!\n");

}

void successResult(Webs *wp) {
     commResult(wp, 100001, 200, 1);
}


void errorResult(Webs *wp) {

    commResult(wp, 100002, 500, 0);
}



static void commResult(Webs *wp, int msgCode, int httpStatus, int isSuccess) {
    char *pStr;
    cJSON * root =  cJSON_CreateObject();
    cJSON * data =  cJSON_CreateObject();
    // cJSON * next =  cJSON_CreateObject();

    cJSON_AddItemToObject(root, "data", data);//根节点下添加
 //   cJSON_AddItemToObject(root, "msg", "");
    cJSON_AddItemToObject(root, "code", cJSON_CreateNumber(msgCode));
    cJSON_AddItemToObject(root, "isSuccess", cJSON_CreateNumber(isSuccess));
    
    pStr = cJSON_PrintUnformatted(root);
    

    // memcpy(cjson_str, pStr, strlen(pStr));
    logmsg(2, "-----------------------jso1n----------------------%s", pStr);
    websResponse(wp, httpStatus, pStr);
    cJSON_Delete(root);//free memery
    wfree(pStr);

}




//add by lusa 
void getErrorJson(char *cjson_str, int cjson_code)
{
    logmsg(2, "-----------------------json----------------------");
    char *pStr;
    cJSON * root =  cJSON_CreateObject();
    cJSON * data =  cJSON_CreateObject();
    // cJSON * next =  cJSON_CreateObject();

    cJSON_AddItemToObject(root, "data", data);//根节点下添加
    cJSON_AddItemToObject(root, "msg", cJSON_CreateString(websErrorMsg(cjson_code)));
    cJSON_AddItemToObject(root, "code", cJSON_CreateNumber(cjson_code));
    

    pStr = cJSON_Print(root);
     logmsg(2, "-----------------------jso1n----------------------%s", pStr);
    //cjson_str = sfmt("%s", pStr);
    // printf("---> pStr: %s\n", pStr);
    
    memcpy(cjson_str, pStr, strlen(pStr));
    logmsg(2, "-----------------------json-2------ %d ---------------%s", strlen(pStr), cjson_str);

}