using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RecData;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLMTEST
{
   public class Re
    {


        public static DataTable Analysis_DataObject(string jsonStr)
        {
            /*
{
"list": [
{
    "errcode": 0,
    "errmsg": null,
    "objId": "01_18AC913FCA4A4DA99BDF01A99C232219",
    "objNo": "0100045006",
    "fname": null,
    "suffix": null,
    "hasAffine": null,
    "name": "油泵轴油封",
    "fsize": null,
    "fsizeStr": "未知",
    "type": "D",
    "tablename": "MPART",
    "smemo": null,
    "ctimestr": "2017-07-11 16:03",
    "mtimestr": "2020-12-21 15:36",
    "creator": "5914洪杰",
    "modifier": "11458戚亚克",
    "ver": "1",
    "stimestr": null,
    "etimestr": null,
    "extra": null,//这里加了俩字段：PTYPE（小类）、DTYPE（大类）
    "searchText": null
}
],
"errcode": 0,
"errmsg": null,
"total": 1
}
             */

            DataTable dt = new DataTable();
            dt.Columns.Add("errcode");
            dt.Columns.Add("errmsg");
            dt.Columns.Add("objId");
            dt.Columns.Add("objNo");
            dt.Columns.Add("fname");
            dt.Columns.Add("suffix");
            dt.Columns.Add("hasAffine");
            dt.Columns.Add("name");
            dt.Columns.Add("fsize");
            dt.Columns.Add("fsizeStr");
            dt.Columns.Add("type");
            dt.Columns.Add("tablename");
            dt.Columns.Add("smemo");
            dt.Columns.Add("ctimestr");
            dt.Columns.Add("mtimestr");
            dt.Columns.Add("creator");
            dt.Columns.Add("modifier");
            dt.Columns.Add("ver");
            dt.Columns.Add("stimestr");
            dt.Columns.Add("etimestr");
            dt.Columns.Add("extra");
            dt.Columns.Add("PTYPE");
            dt.Columns.Add("DTYPE");
            dt.Columns.Add("searchText");



            var json = JsonConvert.DeserializeObject<JObject>(jsonStr);
            var errcode = json["errcode"].ToString();
            var errmsg = json["errmsg"].ToString();
           // var total = json["total"].ToString();
            if (errcode != "0")
            {
                dt = new DataTable();
                dt.Columns.Add("errcode");
                dt.Columns.Add("errmsg");
               // dt.Columns.Add("total");
                var dr = dt.NewRow();
                dr["errcode"] = errcode;
                dr["errmsg"] = errmsg;
                //dr["total"] = total;
                dt.Rows.Add(dr);
                return dt;
            }
            var array = json["list"];
            foreach (var a in array)
            {
                var _errcode = a["errcode"].ToString();
                var _errmsg = a["errmsg"].ToString();
                var objId = a["objId"].ToString();
                var objNo = a["objNo"].ToString();
                var fname = a["fname"].ToString();
                var suffix = a["suffix"].ToString();
                var hasAffine = a["hasAffine"].ToString();
                var name = a["name"].ToString();
                var fsize = a["fsize"].ToString();
                var fsizeStr = a["fsizeStr"].ToString();
                var type = a["type"].ToString();
                var tablename = a["tablename"].ToString();
                var smemo = a["smemo"].ToString();
                var ctimestr = a["ctimestr"].ToString();
                var mtimestr = a["mtimestr"].ToString();
                var creator = a["creator"].ToString();
                var modifier = a["modifier"].ToString();
                var ver = a["ver"].ToString();
                var stimestr = a["stimestr"].ToString();
                var etimestr = a["etimestr"].ToString();
                var extra_O = a["extra"];
                var extra = "-1";
                if (a["extra"] != null)
                {
                    extra = a["extra"].ToString();
                }
                var PTYPE = GetVal.JToken_Value(extra_O, "PTYPE");
                var DTYPE = GetVal.JToken_Value(extra_O, "DTYPE");
               
                var searchText = a["searchText"].ToString();

                var dr = dt.NewRow();
                dr["errcode"] = _errcode;
                dr["errmsg"] = _errmsg;
                dr["objId"] = objId;
                dr["objNo"] = objNo;
                dr["fname"] = fname;
                dr["suffix"] = suffix;
                dr["hasAffine"] = hasAffine;
                dr["name"] = name;
                dr["fsize"] = fsize;
                dr["fsizeStr"] = fsizeStr;
                dr["type"] = type;
                dr["tablename"] = tablename;
                dr["smemo"] = smemo;

                dr["ctimestr"] = ctimestr;
                dr["mtimestr"] = mtimestr;
                dr["creator"] = creator;
                dr["modifier"] = modifier;
                dr["ver"] = ver;

                dr["stimestr"] = stimestr;
                dr["etimestr"] = etimestr;
                dr["extra"] = extra;
                dr["PTYPE"] = PTYPE;
                dr["DTYPE"] = DTYPE;
                dr["searchText"] = searchText;

                dt.Rows.Add(dr);
            }

            return dt;
        }



     



    }
}
