using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Collections.Concurrent;
using Newtonsoft.Json.Linq;
using System.Threading;

namespace WebApi
{

    [RoutePrefix("api/WC")] //定义路由前缀
    public class WCController : ApiController
    {

        /// <summary>
        /// 插入数据库
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage ReportUp()
        {
            string pp = @"{\""Result\"":\""OK\""}";
            try
            {
                //  pp = jObject.ToString();
            }
            catch (Exception err)
            {
            }
            return new HttpResponseMessage { Content = new StringContent(pp, Encoding.GetEncoding("UTF-8"), "application/json") };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage ReportUpCancel()
        {
            string pp = @"{\""Result\"":\""别试了，就不好用，试了也不好用\""}";
            try
            {
                //  pp = jObject.ToString();
            }
            catch (Exception err)
            {
            }
            return new HttpResponseMessage { Content = new StringContent(pp, Encoding.GetEncoding("UTF-8"), "application/json") };
        }




        [HttpPost]
        public HttpResponseMessage SelectPage([FromBody]JObject jobj)
        {
            string pp = "";
            
            try
            {
                var OpName = jobj["OpName"].ToString();
                var StartTime = jobj["StartTime"].ToString();
                var EndTime = jobj["EndTime"].ToString();
                var PageCurrent = jobj["PageCurrent"].ToString();
                var PageSize = jobj["PageSize"].ToString();


                string sql = "SELECT * FROM z_save_tag " +
                            "WHERE(OperationTime BETWEEN '" + StartTime + "' AND '" + EndTime + "') AND OpName like '" + OpName + "%' " +
                            "ORDER BY OperationTime;\n";
               // var res = GlobalVar.dbClient.ExecQuery(sql);

              //  var zSavePersonList = Z_Save_Position.DataTableToClass(res);

              //  var newZSaveList = DatabaseClient.DBClient.SplitePage<Z_Save_Position>(zSavePersonList, Convert.ToInt32(PageSize), Convert.ToInt32(PageCurrent));
                //var tableData = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(newZSaveList));
                //JObject resjobj = new JObject() { 
                //    new JProperty("ItemCount", zSavePersonList.Count.ToString()),
                //    new JProperty("TableData",tableData)
                //};

                //pp = resjobj.ToString()
                //    //.Replace("\r\n","")
                //    ;

            }
            catch (Exception err)
            {
               //GlobalVar.log.Error(err.Message);
                // Console.WriteLine(err.Message);
            }
            HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(pp, Encoding.GetEncoding("UTF-8"), "application/json") };
            return result;
        }

        [HttpPost]
        public HttpResponseMessage Select([FromBody] JObject jobj)
        {
            string pp = "";
            try
            {
                string OpName = jobj["OpName"].ToString();
                string StartTime = jobj["StartTime"].ToString();
                string EndTime = jobj["EndTime"].ToString();

                string sql = "SELECT * FROM z_save_tag " +
                            "WHERE(OperationTime BETWEEN '"+StartTime+"' AND '"+EndTime+"') AND OpName like '"+OpName+"%' " +
                            "ORDER BY OperationTime;\n";
               // var res = GlobalVar.dbClient.ExecQuery(sql);

                //var zSavePersonList = Z_Save_Position.DataTableToClass(res);

                //var zSavePersonListStr = JsonConvert.SerializeObject(zSavePersonList);

                //pp = zSavePersonListStr.ToString();
            }
            catch (Exception err)
            {
               // GlobalVar.log.Error(err.Message);
                // Console.WriteLine(err.Message);
            }
            HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(pp, Encoding.GetEncoding("UTF-8"), "application/json") };
            return result;
        }
    }
}
