using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pub_Class;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebAPI.Models;
namespace WebAPI.Controllers
{
    public class CellWidthController : ApiController
    {
        private json objJsonResult = new json(); 
        public DataSet ds = new DataSet();
        public WebServer webserver = new WebServer();
        #region 单据保存列宽
        /// 
        /// 单据列表保存
        /// 
        /// 
        [Route("tb_CellWidth/GetMouldScrapInHouseCellWidth")]
        [HttpPost]
        public object GetMouldScrapInHouseCellWidth()
        {
            string Item = HttpContext.Current.Request["myMap"];
            string Name = HttpContext.Current.Request["Name"];
            string[] sArray = Item.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg3 = sArray[0].ToString();
            //按 },{来拆分数组 //去掉【和】
            msg3 = msg3.Substring(1, msg3.Length - 2);
            msg3 = msg3.Replace("\\", "");
            msg3 = msg3.Replace("\n", "");  //\n
            string[] myMap = msg3.Split(','); //字符串转数组
            SaveGridView(myMap, Name, HttpContext.Current.Server.MapPath("~/bin"));
            try
            {
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "成功!";
                objJsonResult.data = null;
            }
            catch (Exception ex)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = ex.ToString();
                objJsonResult.data = null;
            }
            return objJsonResult;
        }
        #endregion
        #region 列宽保存
        public object SaveGridView(string[] _value, string KeyItem, string AppPath)
        {
            string text = "";
            try
            {
                for (int i = 0; i <= _value.Length - 1; i++)
                {
                    bool flag = i == 0;
                    if (flag)
                    {
                        text = _value[i].ToString();
                    }
                    else
                    {
                        text = text + "," + _value[i].ToString();
                    }
                }
                ClsIni clsIni = new ClsIni();
                bool flag2 = ClsIni.WriteIni("GridSave", KeyItem, text, AppPath + "\\grdWidth.wyini");
                if (flag2)
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "操作成功!";
                    objJsonResult.data = null;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "操作失败!";
                    objJsonResult.data = null;
                }
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = e.Message;
                objJsonResult.data = null;
            }
            return objJsonResult;
        }
        #endregion
        #region 列宽查询
        [Route("tb_CellWidth/GetMouldScrapInHouseCellWidthList")]
        [HttpGet]
        public object GetGridView(int colNum, string Name)
        {
            try
            {
                char c = Convert.ToChar(",");
                string text = ClsIni.ReadIni("GridSave", Name, HttpContext.Current.Server.MapPath("~/bin") + "\\grdWidth.wyini");
                bool flag = text != "没有找到!";
                if (flag)
                {
                    string[] array = text.Split(new char[]
                    {
                        c
                    });
                    bool flag2 = colNum > array.Length;
                    int num;
                    if (flag2)
                    {
                        num = array.Length;
                    }
                    else
                    {
                        num = colNum;
                    }
                    for (int i = 0; i < num; i++)
                    {
                        bool flag3 = ClsPub.isInt(array[i]) > 500;
                        if (flag3)
                        {
                            array[i] = ClsPub.isInt(array[i]).ToString();
                        }
                        else
                        {
                            array[i] = ClsPub.isInt(array[i]).ToString();
                        }
                    }
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "获取列宽成功!";
                    objJsonResult.data = array;
                }
                else
                {
                    objJsonResult.code = "-1";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无设置!";
                    objJsonResult.data = null;
                }
            }
            catch (Exception ex)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "设置列宽失败!" + ex.Message;
                objJsonResult.data = null;
            }
            return objJsonResult;
        }
        #endregion
    }
}