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/ZSaveTag")] //定义路由前缀
    public class ZSaveTagController : ApiController
    {
        
        /// <summary>
        /// 插入数据库
        /// </summary>
        /// <param name="jObject"></param>
        /// <returns></returns>
        [HttpPost]
        public void Insert(JObject jObject)
        {
            string pp = "";
            try
            {
                var z_Save_Tag = JsonConvert.DeserializeObject<Z_Save_Tag>(JsonConvert.SerializeObject(jObject));

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

                pp = res.ToString();

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

        [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;
        }
    }
}
