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;


namespace WebApi
{

    [RoutePrefix("api/ZSavePosition")] //定义路由前缀
    public class ZSavePositionController : ApiController
    {
        /// <summary>
        /// 插入数据库
        /// </summary>
        /// <param name="jObject"></param>
        /// <returns></returns>
        [HttpPost]
        public void Insert(JObject jObject)
        {
            string pp = "";
            try
            {
                var z_Save_Position = JsonConvert.DeserializeObject<Z_Save_Position>(JsonConvert.SerializeObject(jObject));

                //z_Save_Position.OpName = "a1";
                //z_Save_Position.Value = "asd";
                //z_Save_Position.OperationTime = "2022/06/29 16:40:43";
                //z_Save_Position.ProjectCode = "123";

                string sql = "INSERT INTO z_save_position(OpName, Value, OperationTime, ProjectCode) VALUES('" + z_Save_Position.OpName + "', '" + z_Save_Position.Value + "', '" + z_Save_Position.OperationTime + "', '" + z_Save_Position.ProjectCode + "');\n";
                var res = GlobalVar.dbClient.ExecNonQuery(sql);

                pp = res.ToString();

            }
            catch (Exception err)
            {
                GlobalVar.log.Error(err.Message);
                // Console.WriteLine(err.Message);
            }
        }

        [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_position " +
                            "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();

            }
            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
            {
                var OpName = jobj["OpName"].ToString();
                var StartTime = jobj["StartTime"].ToString();
                var EndTime = jobj["EndTime"].ToString();

                string sql = "SELECT * FROM z_save_position " +
                            "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;
        }
    }
}
