/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-02-16 15:25:46
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-07-21 16:23:13
 */
#ifndef __EXPELDEVICE_H_
#define __EXPELDEVICE_H_

#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>

#include "Util/SqlPool.h"

#include "deviceBase.h"

using namespace toolkit;
using namespace std;

enum DeviceType { 
    DeviceGasV1 = 0,
    DeviceGasV2 = 1,
    DeviceUlt = 2,
    DevicePlayer = 3,
    DeviceGasV3 = 4
};

class ExpelDevice
{
public:
    int Id;
    std::string DeviceId;
    enum DeviceToken deviceToken;
    enum DeviceType deviceType;
    std::string UpdateTime;

    std::string ServerIp;
    std::string ServerCname;
    std::string ServerCpwd;

public:
    ExpelDevice(){}
    ~ExpelDevice(){}

    /**
     * @description: json转换为对象
     * @param {string&} json
     * @return {*}
     */    
    bool JsonToObject(std::string& json){
        rapidjson::Document doc;
        if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
            return false;
        }
        // get members
        const auto end = doc.MemberEnd();
        // json_type
        if(end == doc.FindMember("DeviceId") || !doc["DeviceId"].IsString()) {
            return false;
        }else{
            DeviceId = doc["DeviceId"].GetString();
        }

        // json_type
        if(end == doc.FindMember("DeviceToken") || !doc["DeviceToken"].IsString()) {
            return false;
        }else{
            deviceToken = (enum DeviceToken)atoi(doc["DeviceToken"].GetString());
        }

        if (end == doc.FindMember("DeviceType") || !doc["DeviceType"].IsString()) {
            return false;
        } else {
            deviceType = (enum DeviceType)atoi(doc["DeviceType"].GetString());
        }

        if (end == doc.FindMember("ServerIp") || !doc["ServerIp"].IsString()) {
            return false;
        } else {
            ServerIp = doc["ServerIp"].GetString();
        }

        if (end == doc.FindMember("ServerCname") || !doc["ServerCname"].IsString()) {
            return false;
        } else {
            ServerCname = doc["ServerCname"].GetString();
        }
        
        if (end == doc.FindMember("ServerCpwd") || !doc["ServerCpwd"].IsString()) {
            return false;
        } else {
            ServerCpwd = doc["ServerCpwd"].GetString();
        }

        return true;
    }

    /**
     * @description: object转换为sql
     * @param {*}
     * @return {*}
     */    
    std::string ObjectToSql(){
        SqlStream sqlStream("insert into gsdDB.`DeviceList` (Id, DeviceId, DeviceToken, DeviceType, ServerIp, ServerCname, ServerCpwd, UpdateTime,ISDELETE)\
            values  ('?','?','?','?','?','?','?','?','0')\
            on duplicate key update Id=VALUES(Id), DeviceId=VALUES(DeviceId),\
            DeviceToken=VALUES(DeviceToken), DeviceType=VALUES(DeviceType), \
            ServerIp=VALUES(ServerIp), ServerCname=VALUES(ServerCname), \
            ServerCpwd=VALUES(ServerCpwd), UpdateTime=VALUES(UpdateTime),\
            ISDELETE=VALUES(ISDELETE)");

        sqlStream << Id << DeviceId << deviceToken << deviceType << ServerIp << ServerCname << ServerCpwd << UpdateTime;
        return (std::string)sqlStream;
    }
};




#endif