using NFine.Application.WMS;
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Mvc;
using NFine.Code;
using System.Data.SqlClient;
using NFine.Data.Extensions;

using ICS.Application.Entity;

namespace NFine.Web.Areas.WMS.Controllers
{
    public class BasicSettingsController : ControllerBase
    {

        BasicSettingsApp App = new BasicSettingsApp();
        /// <summary>
        /// 加载仓库
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Warehouse()
        {
            return View();
        }
        /// <summary>
        /// 加载库位
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSStackWMS()
        {
            return View();
        }

        /// <summary>
        /// 物料档案
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSINVENTORY()
        {
            return View();
        }
        /// <summary>
        /// 不良代码组
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSECG()
        {
            return View();
        }
        
      [HttpGet]
        public ActionResult ICSSDN()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ICSSDNToSAP()
        {
            return View();
        }
        /// <summary>
        /// 不良代码维护
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSBadCode()
        {
            return View();
        }
        /// <summary>
        /// 新增库位信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSStackWMSAdd()
        {
            return View();
        }
        [HttpGet]
        /// <summary>
        /// 标签模板维护
        /// </summary>
        /// <returns></returns>

        public ActionResult ICSLableManage()
        {
            return View();
        }
        public ActionResult ICSLableManageAdd()
        {
            return View();
        }
        
        /// <summary>
        /// 新增不良代码
        /// </summary>
        /// <returns></returns>
        public ActionResult ICSBadCodeAdd()
        {
            return View();
        }

        /// <summary>
        /// 类型维护
        /// </summary>
        /// <returns></returns>
        public ActionResult ICSTypeMaintain()
        {
            return View();
        }
        /// <summary>
        /// 站点维护
        /// </summary>
        /// <returns></returns>
        public ActionResult Sys_WorkPoint()
        {
            return View();
        }
        /// <summary>
        /// 新增站点
        /// </summary>
        /// <returns></returns>
        public ActionResult Sys_WorkPointAdd()
        {
            return View();
        }



        /// 新增类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSTypeMaintainAdd()
        {
            return View();
        }
        /// <summary>
        /// 配置参数维护
        /// </summary>
        /// <returns></returns>
        public ActionResult ICSConfiguration()
        {
            return View();
        }

       
        /// <summary>
        /// 启用配置
        /// </summary>
        /// <returns></returns>
        public ActionResult ICSExtensionEnable()
        {
            return View();
        }
        /// <summary>
        /// 新增启用配置
        /// </summary>
        /// <returns></returns>
        public ActionResult ICSExtensionEnableAdd()
        {
            return View();
        }

        /// 新增类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSConfigurationAdd()
        {
            return View();
        }
       
        /// 不良原因组
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSBadReasonGroup()
        {
            return View();
        }
        /// 新增不良原因组
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSBadReasonGroupAdd()
        {
            return View();
        }

        
        /// 新增不良原因组
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSInventoryBadGroupAdd()
        {
            return View();
        }

        
        //包装容器绑定
        [HttpGet]
        public ActionResult ICSContainerBinding()
        {
            return View();
        }
        //维护条码过期时间
        [HttpGet]
        public ActionResult MaintainBarcodeTime()
        {
            return View();
        }
        //维护条码过期时间
        [HttpGet]
        public ActionResult MaintainBarcodeTimeUpdate()
        {
            return View();
        }

        //多语言
        
        [HttpGet]
        public ActionResult ICSlanguage()
        {
            return View();
        }

        

        [HttpGet]
        public ActionResult ICSlanguageAdd()
        {
            return View();
        }


        [HttpGet]

        //不良代码
        public ActionResult GetICSBadCodeGroup(Pagination pagination,string queryJson)
        {
            DataTable ListData = App.GetICSBadCodeGroup(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //不良原因()
        public ActionResult GetICSBadReasonGroup(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSBadReasonGroup(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        

        //不良代码子表
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetICSBadCodeGroupdetil(string ID, Pagination pagination)
        {
            DataTable ListData = App.GetICSBadCodeGroupdetil(ID, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        public ActionResult GetICSBadCodeGroupdetil2(string ID)
        {
            var data = App.GetICSBadCodeGroupdetil2(ID);
            return Content(data.ToJson());
        }


        //不良原因子表
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetICSBadReason(string BRGCode, Pagination pagination)
        {
            DataTable ListData = App.GetICSBadReason(BRGCode, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        public ActionResult GetICSBadReason2(string BRGCode)
        {
            var data = App.GetICSBadReason2(BRGCode);
            return Content(data.ToJson());
        }


        [HttpGet]
        public ActionResult ICSInspectionGroup()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ICSInspectionListAdd()
        {
            return View();
        }
        
       [HttpGet]
        public ActionResult ICSINVEBTORYAdd()
        {
            return View();
        }

        
       [HttpGet]
        public ActionResult ICSInventoryBatchEnable()
        {
            return View();
        }
        
       [HttpGet]
        public ActionResult ICSInventoryBatchEnableAdd()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ICSInventoryBatchEnableUpadte()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ICSInventoryLocation()
        {
            return View();
        }
        [HttpGet]
        public ActionResult ICSInventoryLocationAdd()
        {
            return View();
        }
        [HttpGet]
        public ActionResult ICSInventoryLocationUpdate()
        {
            return View();
        }
        
        [HttpGet]
        public ActionResult ICSContainer()
        {
            return View();
        }
        [HttpGet]
        public ActionResult ICSContainerAdd()
        {
            return View();
        }
        [HttpGet]
        public ActionResult ICSContainerLot()
        {
            return View();
        }
        [HttpGet]
        public ActionResult ICSContainerLotAdd()
        {
            return View();
        }
        [HttpGet]
        public ActionResult ICSInventoryBadGroup()
        {
            return View();
        }
        
        [HttpGet]
        public ActionResult ICSInventoryBadGroupUpdate()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ICSLabelTemplate()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ICSLabelTemplateAdd()
        {
            return View();
        }
        
       [HttpGet]
        public ActionResult ICSContainerLotBinding()
        {
            return View();
        }

        //料品检验项目组
        [HttpGet]
        public ActionResult ICSInventoryInspectionGroup()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ICSInventoryInspectionGroupAdd()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ICSSearchInventory()
        {
            return View();
        }

        //    public ActionResult DownloadExcl(int day)
        //    {
        //  try
        //  {
        //    //int showDay = NVelocityBLL.DAY_AGO * -1;
        //    //var list = NVelocityBLL.Instance.DataList;
        //    //NVelocityBLL.Instance.Refresh();
        //    //if (day != 0)
        //    //{
        //      //showDay = day;
        //      //list = NVelocityBLL.Instance.GetDataByTotalDay(day);
        //    //}

        //    string sWebRootFolder = _hostingEnvironment.WebRootPath;
        //    string sFileName = $"Subscription/{Guid.NewGuid()}.xlsx";
        //    DeleteDir($"{sWebRootFolder}\\Subscription");

        //    FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
        //    using (ExcelPackage package = new ExcelPackage(file))
        //    {
        //      ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("aspnetcore");
        //      worksheet.Cells[1, 1].Value = "date/Step";
        //      worksheet.DefaultColWidth = 25;
        //      for (int i = 1; i <= showDay; i++)
        //      {
        //        // The number of now days.Used as a column header.
        //        int nowDay = -(i - 1);
        //        //Col index
        //        int colIndex = i + 1;

        //        //DateTime nowTime = NVelocityBLL.Instance.GetChinaByUtcDate(DateTime.UtcNow).AddDays(nowDay);
        //        string utcTime = DateTime.UtcNow.AddDays(nowDay).ToString("yyyy-MM-dd");
        //        //Insert the column header
        //        worksheet.Cells[1, colIndex].Value = nowTime.ToString("yyyy-MM-dd");
        //        //Insert the row
        //        InsertExeclRow(list, worksheet, colIndex, nowTime);
        //      }
        //      package.Save();
        //    }
        //    return File(sFileName, "applicationnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //  }
        //  catch (Exception ex)
        //  {
        //    FxLogger.DEFAULT.Error($"DownloadExcl:", ex);
        //    return SiteBLL.ErrorResponseData(-2, ex.Message);
        //  }
        //}
        /// <summary>
        /// 删除库位
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteStack(string keyValue)
        {
            string msg = App.DeleteStack(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }
        /// <summary>
        /// 删除不良原因
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteBadCode(string keyValue)
        {
            string msg = App.DeleteBadCode(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertStack(string keyValue)
        {
            string msg = App.InsertStack(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertBadCode(string keyValue)
        {
            string msg = App.InsertBadCode(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateBadCode(string keyValue)
        {
            string msg = App.UpdateBadCode(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateStack(string keyValue)
        {
            string msg = App.UpdateStack(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }

        /// <summary>
        /// 获取库位信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWarehouse()
        {
            DataTable dt = App.GetWarehouse();
            return Content(dt.ToJson());
        }

        /// <summary>
        /// 新增类型
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertICSType(string keyValue)
        {
            string msg = App.InsertICSType(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }
        
        /// <summary>
        /// 修改类型
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateICSType(string keyValue)
        {
            string msg = App.UpdateICSType(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }
        /// <summary>
        /// 删除类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSType(string keyValue)
        {
            string msg = App.DeleteICSType(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }
        /// <summary>
        /// 修改配置参数
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateICSConfiguration(string keyValue)
        {
            string msg = App.UpdateICSConfiguration(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }

        
        /// <summary>
        /// 新增参数配置
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertICSConfiguration(string keyValue)
        {
            string msg = App.InsertICSConfiguration(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }

        
        /// <summary>
        /// 删除参数配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSConfiguration(string keyValue)
        {
            string msg = App.DeleteICSConfiguration(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        /// <summary>
        /// 修改启用配置
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateICSExtensionEnable(string keyValue)
        {
            string msg = App.UpdateICSExtensionEnable(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }


        /// <summary>
        /// 新增启用配置
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertICSExtensionEnable(string keyValue)
        {
            string msg = App.InsertICSExtensionEnable(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }
        
        /// <summary>
        /// 删除启用配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSExtensionEnable(string keyValue)
        {
            string msg = App.DeleteICSExtensionEnable(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }


        [HttpGet]
        public ActionResult DeleteBCItem(string ID)
        {
            try
            {
                App.DeleteBCItem(ID);
                return Success("删除成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }

        }

        [HttpGet]
        public ActionResult DeleteINvItem(string ID)
        {
            try
            {
                App.DeleteItem(ID);
                return Success("删除成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }

        }


        //不良代码组新增
        [HttpPost]
        public ActionResult SubICSBadCodeGroupAdd(ICSBadReason main, string BRGCode, string BRGDesc)
        {
            try
            {
                App.SubICSBadCodeGroupAdd(main, BRGCode, BRGDesc);
                return Success("保存成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        //不良原因组新增
        [HttpPost]
        public ActionResult SubICSBadReasonGroupAdd(ICSBadReason main,string BRGCode, string BRGDesc)
        {
            try
            {
                App.SubICSBadReasonGroupAdd(main, BRGCode, BRGDesc);
                return Success("保存成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }


        //不良代码组修改
        [HttpPost]
        public ActionResult UpdateICSBadCodeGroup(ICSBadReason main, string BID, string BRGCode, string BRGDesc)
        {
            try
            {
                App.UpdateICSBadCodeGroup(main, BID, BRGCode, BRGDesc);
                return Success("修改成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        //不良原因组修改
        [HttpPost]
        public ActionResult UpdateICSBadReasonGroup(ICSBadReason main,string BID ,string BRGCode, string BRGDesc)
        {
            try
            {
                App.UpdateICSBadReasonGroup(main, BID, BRGCode, BRGDesc);
                return Success("修改成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }


        
        /// <summary>
        /// 删除不良代码组
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSBadCodeGroup(string keyValue)
        {
            string msg = App.DeleteICSBadCodeGroup(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }
        /// <summary>
        /// 删除不良原因组
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSBadReasonGroup(string keyValue)
        {
            string msg = App.DeleteICSBadReasonGroup(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        [HttpGet]
        public ActionResult GetICSInspectionGroup(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInspectionGroup(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult ICSInspectionList(string GroupCode, Pagination pagination)
        {
            DataTable ListData = App.ICSInspectionList(GroupCode, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //删除自动表
        [HttpGet]
        public ActionResult DeleteList(string ID)
        {
            try
            {
                App.DeleteList(ID);
                return Success("删除成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }

        }

        //检查项目组组修改
        [HttpPost]
        public ActionResult UpdateICSInspectionGroup(ICSInspectionList main, string BID, string GroupCode, string GroupName, string Enable)
        {
            try
            {
                App.UpdateICSInspectionGroup(main, BID, GroupCode, GroupName, Enable);
                return Success("修改成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        //检查项目组新增
        [HttpPost]
        public ActionResult ICSInspectionGroupAdd(ICSInspectionList main, string GroupCode, string GroupName,string Enable)
        {
            try
            {
                App.ICSInspectionGroupAdd(main, GroupCode, GroupName, Enable);
                return Success("保存成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        /// <summary>
        /// 动态子表修改加载页面
        /// </summary>
        /// <param name="GroupCode"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ICSInspectionList2(string GroupCode)
        {
            var data = App.ICSInspectionList2(GroupCode);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 删除不良原因组
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSInspectionGroup(string keyValue)
        {
            string msg = App.DeleteICSInspectionGroup(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }


        /// <summary>
        /// 新增料品属性
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertICSInventory(string keyValue)
        {
            string msg = App.InsertICSInventory(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }

        /// <summary>
        /// 修改料品属性
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateICSInventory(string keyValue)
        {
            string msg = App.UpdateICSInventory(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }


        /// <summary>
        /// 获取料品属性修改文本框值
        /// </summary>
        /// <param name="POCode"></param>
        /// <param name="PORow"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult GetInputValue(string ID)
        {
            DataTable ListData = App.GetInputValue(ID);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 删除料品属性
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSInventory(string keyValue)
        {
            string msg = App.DeleteICSInventory(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        /// <summary>
        /// 获取料品信息
        /// </summary>
        /// <param name="pagination"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetICSInventoryInfo(string queryJson, Pagination pagination)
        {
            DataTable ListData = App.GetICSInventoryInfo(queryJson,ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// 获取仓库信息
        /// </summary>
        /// <param name="pagination"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetICSWarehouseInfo(string queryJson, Pagination pagination)
        {
            DataTable ListData = App.GetICSWarehouseInfo(queryJson,ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 新增料品仓库批次
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertICSInventoryBatchEnable(string keyValue,string keyValue2)
        {
            string msg = App.InsertICSInventoryBatchEnable(keyValue, keyValue2);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }

        /// <summary>
        /// 修改料品仓库批次关系
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateICSInventoryBatchEnable(string keyValue)
        {
            string msg = App.UpdateICSInventoryBatchEnable(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }

        /// <summary>
        /// 删除料品仓库批次关系
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSInventoryBatchEnable(string keyValue)
        {
            string msg = App.DeleteICSInventoryBatchEnable(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        /// 获取库位信息
        /// </summary>
        /// <param name="pagination"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetICSLocation(string queryJson, Pagination pagination)
        {
            DataTable ListData = App.GetICSLocation(queryJson,ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        /// <summary>
        /// 新增料品库位绑定关系
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertICSInventoryLocation(string keyValue, string keyValue2)
        {
            string msg = App.InsertICSInventoryLocation(keyValue, keyValue2);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }


        /// <summary>
        /// 修改料品库位绑定关系
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateICSInventoryLocation(string keyValue)
        {
            string msg = App.UpdateICSInventoryLocation(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }

        /// <summary>
        /// 删除料品仓库批次关系
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSInventoryLocation(string keyValue)
        {
            string msg = App.DeleteICSInventoryLocation(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        /// <summary>
        /// 新增包装、容器
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertICSContainer(string keyValue)
        {
            string msg = App.InsertICSContainer(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }

        /// <summary>
        /// 修改包装、容器
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateICSContainer(string keyValue)
        {
            string msg = App.UpdateICSContainer(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }

        /// <summary>
        /// 删除包装、容器
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSContainer(string keyValue)
        {
            string msg = App.DeleteICSContainer(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        /// <summary>
        /// 新增包装、容器
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertICSContainerLot(string keyValue)
        {
            string msg = App.InsertICSContainerLot(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }

        /// <summary>
        /// 修改包装、容器
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateICSContainerLot(string keyValue)
        {
            string msg = App.UpdateICSContainerLot(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }
        /// <summary>
        /// 删除包装、容器关联
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSContainerLot(string keyValue)
        {
            string msg = App.DeleteICSContainerLot(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetInvCode()
        {
            DataTable dt = App.GetInvCode();
            return Content(dt.ToJson());
        }
        
        /// <summary>
        /// 获取不良代码信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetBCGroup()
        {
            DataTable dt = App.GetBCGroup();
            return Content(dt.ToJson());
        }
        
        /// <summary>
        /// 获取不良原因信息
        /// </summary>
        /// <returns></returns>
        public ActionResult BRGroupID()
        {
            DataTable dt = App.BRGroupID();
            return Content(dt.ToJson());
        }


        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertInventoryBadGroup(string keyValue)
        {
            string msg = App.InsertInventoryBadGroup(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }


        

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateInventoryBadGroup(string keyValue)
        {
            string msg = App.UpdateInventoryBadGroup(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }
        /// <summary>
        /// 删除料品不良信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteInventoryBadGroup(string keyValue)
        {
            string msg = App.DeleteInventoryBadGroup(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }


        #region 打印
        /// <summary>
        /// 打印数据参数:服务器的URL+打印的文件名,转化为Base64编码
        /// </summary>
        protected string strPrintData;
        /// <summary>
        /// 标识是否安装了控件
        /// </summary>
        protected bool bIsInstallPrintControl = true;
        /// <summary>
        /// 打印控件的Cookie值
        /// </summary>
        protected string strPrintControlCookie = "";


        /// <summary>
        /// 获取Url中去掉文件名的路径
        /// </summary>
        /// <returns></returns>
        private string GetUrlPath()
        {
            string strUrl = Request.Url.ToString();
            int iEnd = strUrl.LastIndexOf("/");
            strUrl = strUrl.Substring(0, iEnd + 1);

            return strUrl;
        }

        /// <summary>
        /// 设置控件调用的Cookie值,判断是否安装了打印控件
        /// </summary>
        /// <param name="pJson"></param>
        private void SetCookieAndURL(PrintJson pJson)
        {
            bIsInstallPrintControl = false;
            strPrintControlCookie = "";
            HttpCookie pCookieInstall = Request.Cookies["InstallPrintControl"];
            if (pCookieInstall != null)
            { //Cookie存在
                strPrintControlCookie = pCookieInstall.Value.ToString();

                //以Cookie值查找在数据表中是否存在
                string strSql = @"Select * From sys_SRM_CheckInstall Where Cookie = @Cookie";
                SqlParameter[] pmcCookie = { new SqlParameter("Cookie", strPrintControlCookie) };
                using (SqlDataReader drCookie = DbHelper.ExecuteReader(DbHelper.ConnectionString, CommandType.Text, strSql, pmcCookie))
                {
                    if (drCookie.Read())
                    { //标识为已经安装
                        bIsInstallPrintControl = true;
                    }
                    drCookie.Close();
                }
                //更新Cookie的保存时间
                pCookieInstall.Expires = DateTime.Now.AddYears(10);
                Response.SetCookie(pCookieInstall);
            }
            else
            {//Cookie不存在,则新建Cookie
                strPrintControlCookie = System.Guid.NewGuid().ToString();
                pCookieInstall = new HttpCookie("InstallPrintControl", strPrintControlCookie);
                pCookieInstall.Expires = DateTime.Now.AddYears(10);
                Response.Cookies.Add(pCookieInstall);
            }

            string strUrl = Server.MapPath("/PrintTemp/") + "IsCheckInstall";

            pJson.SetCookieAndURL(strPrintControlCookie, strUrl);
        }


        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult PrintStack(string keyValue)
        {
            keyValue = keyValue.Substring(1, keyValue.Length - 2);
            string strPrintFileName = Server.MapPath("/ReportFile/") + "Stack.fr3";
            PrintJson pJson = new PrintJson(Server.MapPath("./PrintTemp"), strPrintFileName);
            pJson.CheckRegister("苏州智合诚信息科技有限公司", "56DD3B4C172D0D140841CAC98A58A819F4E28EDA5D6E45711DDD64F6A439F68B6A7870CD7DAFD69A919CB870207FE4BB206F92BE3D53C221B019E0797E739EBA4");  //注册信息
            pJson.MasterOptions(1, "STNO", false);  //主从关系
            SetCookieAndURL(pJson);// 设置控件调用的Cookie值,判断是否安装了打印控件
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            string strSql = @"select a.LocationCode,
                              a.ID,
                              a.LocationName,
                              b.WarehouseCode,
                              b.WarehouseName,
                              a.Musername as 'MUSER',
                              a.MTIME
                              from ICSLocation a WITH (NOLOCK)
                              inner join ICSWarehouse b WITH (NOLOCK) on a.WHID=b.ID 
                              where  1=1
                             and a.ID IN (" + keyValue.TrimEnd(',') + ")";
            DataTable dtInInfo = DbHelper.ExecuteTable(DbHelper.ConnectionString, CommandType.Text, strSql, true, null);
            //            strSql = @"SELECT Row_Number() OVER (partition BY a.STNO ORDER BY a.CREATETIME DESC) AS rowno,c.STNO,
            //                       c.PLANQTY,c.LotnoQTY,c.CartonNOQTY,c.ITEMCODE,b.INVNAME,b.INVSTD,b.INVPARSETYPE,d.StorageName,
            //                       CONVERT(VARCHAR,CAST(ISNULL(c.Qty,0) AS money),1) AS Qty,
            //                       CONVERT(VARCHAR,CAST(ISNULL(c.Meters,0) AS money),1) AS Meters,
            //                       c.STNO AS QRSTNO,c.TransNO,c.TransLine,c.MEMO,c.VenderLotNO,CONVERT(varchar(10), c.PRODUCTDATE, 23) as PRODUCTDATE,a.remark
            //                       FROM 
            //                       (SELECT aa.STNO,aa.ITEMCODE,aa.WorkPoint,SUM(aa.PLANQTY) PLANQTY,SUM(cc.LOTQTY) Qty,
            //                       SUM(ISNULL(cc.Meters,0)) Meters,cc.TransNO,cc.TransLine,dd.MEMO,cc.VenderLotNO,cc.PRODUCTDATE,
            //                       COUNT(aa.LOTNO) LotnoQTY,COUNT(bb.CartonNO) CartonNOQTY
            //                       FROM ICSASNDETAIL aa
            //                       LEFT JOIN ICSITEMLot2Carton bb ON aa.LOTNO = bb.LotNO AND aa.WorkPoint = bb.WorkPoint
            //                       LEFT JOIN ICSITEMLot cc ON aa.LOTNO = cc.LotNO AND aa.WorkPoint = cc.WorkPoint
            //                       LEFT JOIN ICSPO_PoMain dd on dd.POCode=cc.TransNO and dd.PORow=cc.TransLine
            //                       GROUP BY aa.stno,aa.itemcode,aa.WorkPoint,cc.TransNO,cc.TransLine,dd.MEMO,cc.VenderLotNO,cc.PRODUCTDATE) c
            //                       LEFT JOIN ICSASN a on c.stno=a.stno AND c.WorkPoint=a.WorkPoint
            //                       LEFT JOIN dbo.ICSINVENTORY b ON c.ITEMCODE=b.INVCODE AND b.WorkPoint=c.WorkPoint
            //                       LEFT JOIN dbo.icsstorage d ON b.INVMACHINETYPE=d.StorageCode
            //                       WHERE c.STNO IN (" + keyValue.TrimEnd(',') + ") and c.WorkPoint='" + WorkPoint + "'";
            //            DataTable dtInMaterial = DbHelper.ExecuteTable(DbHelper.ConnectionString, CommandType.Text, strSql, true, null);

            string strPrintTempFile = pJson.ShowReport(dtInInfo, null); //产生JSON文件内容

            //把服务器的URL + 此文件名 传递给控件,由控件下载还原数据进行打印
            string strServerURL = Server.MapPath("/PrintTemp/");
            string strData = strServerURL + strPrintTempFile;
            strPrintData = PrintFunction.EnBase64(strData);
            var JsonData = new
            {
                strPrintData_1 = strPrintData,
                bIsInstallPrintControl_1 = bIsInstallPrintControl,
                strPrintControlCookie_1 = strPrintControlCookie
            };
            //            string sql = @"UPDATE dbo.ICSASN
            //                           SET ADDITION1=ISNULL(ADDITION1,0)+1,
            //                           ADDITION2=CONVERT(varchar(100), GETDATE(), 21)
            //                           WHERE STNO in (" + keyValue.TrimEnd(',') + ") and WorkPoint='" + WorkPoint + "'";
            //            SqlHelper.ExecuteNonQuery(sql);
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 写日志(用于跟踪)
        /// </summary>
        private void WriteLog(string strMessage)
        {
            string strLogFile = Server.MapPath("./Cookie_Log.txt");
            StreamWriter swLogFile = null;
            try
            {
                if (!System.IO.File.Exists(strLogFile))
                {
                    swLogFile = System.IO.File.CreateText(strLogFile);
                }
                else
                {
                    swLogFile = System.IO.File.AppendText(strLogFile);
                }
                swLogFile.WriteLine(strMessage);
            }
            catch
            {
            }
            finally
            {
                if (swLogFile != null)
                    swLogFile.Close();
            }
        }

        /// <summary>
        ///判断数据表中是否存在,若不存在则新建一条记录,若存在则更新最后访问的时间 
        /// </summary>
        /// <param name="strCookie"></param>
        private void UpdateCheckInstall(string strCookie)
        {
            bool bIsExist = false;
            //判断记录是否存在
            GetLastAccessTime(strCookie, ref bIsExist);

            string strSql = "";
            SqlParameter[] pmcCookie = { new SqlParameter("Cookie", strCookie) };
            if (bIsExist)
            { //存在则更新最后访问的时间
                strSql = @"Update sys_SRM_CheckInstall 
                           Set LastAccessTime = getDate() 
                           Where Cookie = @Cookie";
            }
            else
            { //不存在则新建一条记录
                strSql = @"Insert into sys_SRM_CheckInstall(Cookie, LastAccessTime) 
                           Values( @Cookie, getDate() )";
            }
            DbHelper.ExecuteNonQuery(DbHelper.ConnectionString, CommandType.Text, strSql, pmcCookie);
        }

        public DateTime ToDateTime(object SrcValue)
        {
            if (Convert.IsDBNull(SrcValue) == true)
                return DateTime.MinValue;
            else
            {
                try
                {
                    return Convert.ToDateTime(SrcValue);
                }
                catch
                {
                    return DateTime.MinValue;
                }
            }
        }

        /// <summary>
        /// 获取此Cookie的最后访问时间
        /// </summary>
        /// <param name="strCookie"></param>
        /// <returns></returns>
        private DateTime GetLastAccessTime(string strCookie, ref bool bIsExist)
        {
            DateTime dtLastAccessTime = DateTime.MinValue;
            bIsExist = false;

            string strSql = @"Select * From sys_SRM_CheckInstall Where Cookie = @Cookie";
            SqlParameter[] pmcCookie = { new SqlParameter("Cookie", strCookie) };
            using (SqlDataReader drCookie = DbHelper.ExecuteReader(DbHelper.ConnectionString, CommandType.Text, strSql, pmcCookie))
            {
                if (drCookie.Read())
                {
                    dtLastAccessTime = ToDateTime(drCookie["LastAccessTime"]);
                    bIsExist = true;
                }
                drCookie.Close();
            }

            return dtLastAccessTime;
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult IsCheckInstall(string checkInstall)
        {
            string strValue = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss 访问,");
            if (Request.QueryString["cookie"] != null)
            { //有控件上传的Cookie值
                string strCookie = Request.QueryString["cookie"].ToString();

                //判断数据表中是否存在,若不存在则新建一条记录,若存在则更新最后访问的时间
                UpdateCheckInstall(strCookie);

                strValue = strValue + "上传的cookie:" + strCookie;
            }
            else if (checkInstall != null)
            {//Ajax检查控件是否安装了,防止那种客户把打印控件卸装了,打印时无法检测到的情况
                string strCookie = checkInstall;
                //读取最后访问的时间
                string strResult = "PrintControlInstall";
                bool bIsExist = false;
                DateTime dtLastAccessTime = GetLastAccessTime(strCookie, ref bIsExist);
                if (!bIsExist || dtLastAccessTime.AddSeconds(30) < DateTime.Now)
                {
                    strResult = "NOT";
                }
                Response.Clear();
                Response.Write(strResult);

                strValue = strValue + "检测安装的值:" + strCookie;
            }

            WriteLog(strValue);
            return null;
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult IsCheckInstall()
        {
            string strValue = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss 访问,");
            if (Request.QueryString["cookie"] != null)
            { //有控件上传的Cookie值
                string strCookie = Request.QueryString["cookie"].ToString();

                //判断数据表中是否存在,若不存在则新建一条记录,若存在则更新最后访问的时间
                UpdateCheckInstall(strCookie);

                strValue = strValue + "上传的cookie:" + strCookie;
            }
            else if (Request.QueryString["checkInstall"] != null)
            {//Ajax检查控件是否安装了,防止那种客户把打印控件卸装了,打印时无法检测到的情况
                string strCookie = Request.QueryString["checkInstall"];
                //读取最后访问的时间
                string strResult = "PrintControlInstall";
                bool bIsExist = false;
                DateTime dtLastAccessTime = GetLastAccessTime(strCookie, ref bIsExist);
                if (!bIsExist || dtLastAccessTime.AddSeconds(30) < DateTime.Now)
                {
                    strResult = "NOT";
                }
                Response.Clear();
                Response.Write(strResult);

                strValue = strValue + "检测安装的值:" + strCookie;
            }

            WriteLog(strValue);
            return null;
        }

        #endregion
        /// <summary>
        /// 导入Excel
        /// </summary>
        /// <param name="fileURL"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Import(string fileURL)
        {
            var fileName = "";
            var filePath = fileURL;
            string path = Path.Combine(filePath, fileName);
            //file.SaveAs(path);

            DataTable excelTable = new DataTable();
            //excelTable =SqlHelper.GetExcelDataTable(path);

            DataTable dbdata = new DataTable();
            dbdata.Columns.Add("LocationCode");
            dbdata.Columns.Add("LocationName");
            dbdata.Columns.Add("Musername");
            dbdata.Columns.Add("MUSER");
            dbdata.Columns.Add("EATTRIBUTE1");
            for (int i = 0; i < excelTable.Rows.Count; i++)
            {
                string LocationCode = "";
                string LocationName = "";
                DataRow dr = excelTable.Rows[i];
                DataRow dr_ = dbdata.NewRow();
                if (!string.IsNullOrWhiteSpace(dr["区"].ToString().ToUpper()))
                {
                    LocationCode += "-" + dr["区"].ToString().ToUpper();
                    LocationName += dr["区"].ToString().ToUpper() + "区";
                }
                if (!string.IsNullOrWhiteSpace(dr["排"].ToString().ToUpper()))
                {
                    LocationCode += "-" + dr["排"].ToString().ToUpper();
                    LocationName += dr["排"].ToString().ToUpper() + "排";
                }
                if (!string.IsNullOrWhiteSpace(dr["货架"].ToString().ToUpper()))
                {
                    LocationCode += "-" + dr["货架"].ToString().ToUpper();
                    LocationName += dr["货架"].ToString().ToUpper() + "货架";
                }
                if (!string.IsNullOrWhiteSpace(dr["层"].ToString().ToUpper()))
                {
                    LocationCode += "-" + dr["层"].ToString().ToUpper();
                    LocationName += dr["层"].ToString().ToUpper() + "层";
                }
                if (!string.IsNullOrWhiteSpace(dr["格"].ToString().ToUpper()))
                {
                    LocationCode += "-" + dr["格"].ToString().ToUpper();
                    LocationName += dr["格"].ToString().ToUpper() + "格";
                }
                dr_["LocationCode"] = LocationCode;
                dr_["LocationName"] = LocationName;
                dr_["Musername"] = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
                dr_["MUSER"] = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
                dr_["Musername"] = "";
                dbdata.Rows.Add(dr_);
            }
            SqlHelper.RemoveEmpty(dbdata);
            SqlHelper.SqlBulkCopyByDatatable( "ICSLocation", dbdata);

            return View();
        }

        //库位导入
        [HttpPost]
        /// <summary>
        /// 文件上传到本地
        /// </summary>
        public string UploadFile()
        {
            try
            {
                HttpFileCollection hpFiles = System.Web.HttpContext.Current.Request.Files;
                if (hpFiles != null && hpFiles.Count > 0)
                {

                    string IsXls = System.IO.Path.GetExtension(hpFiles[0].FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
                    if (IsXls != ".xls" && IsXls != ".xlsx")
                    {

                        return "只可以选择Excel(.xls .xlsx)文件";//当选择的不是Excel文件时,返回
                    }    
                    string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + Guid.NewGuid() + IsXls;             //获取Execle文件名 DateTime日期函数
                    string savePath = System.Web.HttpContext.Current.Server.MapPath("~\\File\\UPLoadFile\\" + filename);//Server.MapPath 获得虚拟服务器相对路径

                    int iLen = hpFiles[0].ContentLength;
                    if (Directory.Exists(savePath)) return "文件已存在";
                    byte[] bData = new byte[iLen];
                    hpFiles[0].InputStream.Read(bData, 0, iLen);


                    FileStream newFile = new FileStream(savePath, FileMode.OpenOrCreate);
                    newFile.Write(bData, 0, bData.Length);
                    newFile.Flush();
                    int _FileSizeTemp = hpFiles[0].ContentLength;

                    newFile.Close();
                    newFile.Dispose();
                    //bool del = false;
                    string mess = "";
                    mess = App.SetData_PR(savePath);
                    if (System.IO.File.Exists(savePath))//删除文件
                    {

                        System.IO.File.Delete(savePath);
                    }
                    return mess;

                }
                else
                {
                    return "获取文件失败";
                }
            }
            catch (Exception ex)
            {

                return ex.ToString();
            }
        }

        /// <summary>
        ///启用的料品库位绑定关系
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult EnableInventoryLocation(string keyValue)
        {
            string msg = App.EnableInventoryLocation(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("启用成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult ForbiddenInventoryLocation(string keyValue)
        {
            string msg = App.ForbiddenInventoryLocation(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("禁用成功!");
            }
            else
            {
                return Error(msg);
            }
        }


        /// <summary>
        ///启用的料品仓库绑定关系
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult EnableBatchEnable(string keyValue)
        {
            string msg = App.EnableBatchEnable(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("启用成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult ForbiddenBatchEnable(string keyValue)
        {
            string msg = App.ForbiddenBatchEnable(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("禁用成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        //新增标签数据源
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult CreateLableDataSource( string Sys_LableDataSource)
        {
            string msg = App.CreateLableDataSource(Sys_LableDataSource);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTXT(string ID)
        {
            DataTable ListData = App.GetTXT(ID);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //修改标签数据源
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult updateLableDataSource(string Sys_LableDataSource)
        {
            string msg = App.updateLableDataSource(Sys_LableDataSource);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteLableDataSource(string keyValue)
        {
            string msg = App.DeleteLableDataSource(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetLableType()
        {
            DataTable dt = App.GetLableType();
            return Content(dt.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSys_LablesID()
        {
            DataTable dt = App.GetSys_LablesID();
            return Content(dt.ToJson());
        }

        //新增标签数据源
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult CreateLables(string txtLableName,string sel_LableType, string sel_LableSourceID )
        {
            string msg = App.CreateLables(txtLableName, sel_LableType, sel_LableSourceID);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetLabelTXT(string ID)
        {
            DataTable ListData = App.GetLabelTXT(ID);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        
       [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult updateLables(string ID,string txtLableName, string sel_LableType, string sel_LableSourceID)
        {
            string msg = App.updateLables(ID,txtLableName, sel_LableType, sel_LableSourceID);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }

        
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSLabelTemplate(string keyValue)
        {
            string msg = App.DeleteICSLabelTemplate(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        //获取父容器
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetContainerID()
        {
            DataTable dt = App.GetContainerID();
            return Content(dt.ToJson());
        }

        [HttpPost]
        public ActionResult UpLoadLabelFile()
        {
            //获取上传的文件集合
            HttpFileCollection httpFile = System.Web.HttpContext.Current.Request.Files;
            //获取送货单号
            string ID = Request.Form["ID"].ToString();
            string LableName = Request.Form["LableName"].ToString();
            
            string fileName = Path.GetFileNameWithoutExtension(httpFile[0].FileName)
                + "_" + LableName + Path.GetExtension(httpFile[0].FileName);
            string filePath = System.Web.HttpContext.Current.Server.MapPath("~\\File\\LablesFile\\" + fileName);

            int iLen = httpFile[0].ContentLength;
            byte[] bData = new byte[iLen];
            httpFile[0].InputStream.Read(bData, 0, iLen);
            FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
            fs.Write(bData, 0, bData.Length);
            fs.Flush();
            fs.Close();
            fs.Dispose();

            int count = App.UpLoadLabelFile(ID, fileName);
            if (count > 0)
            {
                return Success("上传成功!");
            }
            else
            {
                return Error("上传失败");
            }
        }


        [HttpPost]
        public ActionResult UpLoadFileTxT()
        {
            //获取上传的文件集合
            HttpFileCollection httpFile = System.Web.HttpContext.Current.Request.Files;
            //获取送货单号
            string LotNo = Request.Form["LotNo"].ToString();

            string fileName = Path.GetFileNameWithoutExtension(httpFile[0].FileName)
                 + Path.GetExtension(httpFile[0].FileName);
            string filePath = System.Web.HttpContext.Current.Server.MapPath("~\\File\\UPLoadFile\\" + fileName);

            int iLen = httpFile[0].ContentLength;
            byte[] bData = new byte[iLen];
            httpFile[0].InputStream.Read(bData, 0, iLen);
            FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
            fs.Write(bData, 0, bData.Length);
            fs.Flush();
            fs.Close();
            fs.Dispose();

            int count = App.UpLoadFiles(LotNo, fileName);
            if (count > 0)
            {
                return Success("上传成功!");
            }
            else
            {
                return Error("上传失败");
            }
        }



        //包装容器
        public ActionResult GetSendContainer(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetSendContainer(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetContainerInfo(string Code)

        {
            DataTable ListData = App.GetContainerInfo(Code);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetBindContainerID(string SireCode)
        {
            DataTable ListData = App.GetBindContainerID(SireCode);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }



        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult BindingContainerID(string ICSMTDOC, string SireID)
        {
            string msg = App.BindingContainerID(ICSMTDOC, SireID);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("绑定成功!");
            }
        }

        //包装容器关联条码
     public ActionResult GetSendContainerLot(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetSendContainerLot(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetContainerLotInfo(string LotNo)

        {
            DataTable ListData = App.GetContainerLotInfo(LotNo);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult BindingContainerLotID(string ICSMTDOC, string SireID,string SireCode)
        {
            string msg = App.BindingContainerLotID(ICSMTDOC, SireID, SireCode);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("绑定成功!");
            }
        }



        /// <summary>
        /// 新增站点
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertWorkPoint(string keyValue)
        {
            string msg = App.InsertWorkPoint(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }

        /// <summary>
        /// 修改站点
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateWorkPoint(string keyValue)
        {
            string msg = App.UpdateWorkPoint(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }
        /// <summary>
        /// 删除站点
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteWorkPoint(string keyValue)
        {
            string msg = App.DeleteWorkPoint(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }
        /// <summary>
        /// 查询站点
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetWorkPoint(string Id)
        {
            var data = App.GetWorkPoint(Id);
            return Content(data.ToJson()); ;
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetICSInventoryLotInfo(string ID)
        {
            DataTable ListData = App.GetICSInventoryLotInfo(ID);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult UpdateExpirationDate(string ID, string ExpirationDate,string Inv,string LotNo,string InvCode,string Quantity)
        {
            int i = App.UpdateExpirationDate(ID, ExpirationDate,Inv, LotNo, InvCode, Quantity);
            if (i > 0)
            {
                return Success("维护成功!");
            }
            else
            {
                return Error("维护失败!");
            }
        }

        //修改多语言
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult UpdateSys_Language(string keyValue)
        {
            string msg = App.UpdateSys_Language(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("修改成功!");
            }
        }

        //新增多语言
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult InsertSys_Language(string keyValue)
        {
            string msg = App.InsertSys_Language(keyValue);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSys_Language(string ID)
        {
            DataTable ListData = App.GetSys_Language(ID);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteSys_Language(string keyValue)
        {
            string msg = App.DeleteSys_Language(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        //料品检验明细
        public ActionResult GetICSInventoryInspectionGroup(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInventoryInspectionGroup(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //料品检验明细子表
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult ICSInventoryInspectionList(string ID, Pagination pagination)
        {
            DataTable ListData = App.ICSInventoryInspectionList(ID, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }
        //删除料品检验明细子表
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteICSInventoryInspectionGroup(string keyValue)
        {
            string msg = App.DeleteICSInventoryInspectionGroup(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        //查询物料
        [HttpGet]
        public ActionResult GetINv(string invcode, Pagination pagination)
        {
            DataTable ListData = App.Getinv(invcode, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGroupCode()
        {
            DataTable dt = App.GetGroupCode();
            return Content(dt.ToJson());
        }


        [HttpGet]
        public ActionResult DeleteItem(string ID)
        {
            try
            {
                App.DeleteItem(ID);
                return Success("删除成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }

        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetICSInventoryInspectionList(string BRGCode, Pagination pagination)
        {
            DataTable ListData = App.GetICSInventoryInspectionList(BRGCode, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetInspectionListInfo(string ID)

        {
            DataTable ListData = App.GetInspectionListInfo(ID);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        //新增料品检验明细
        [HttpPost]
        public ActionResult SubICSInventoryInspectionGroupAdd(ICSInventoryInspectionGroup main, string ID, string InvCode)
        {
            try
            {
                App.SubICSInventoryInspectionGroupAdd(main, ID, InvCode);
                return Success("保存成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        //修改料品检验明细
        //[HttpPost]
        public ActionResult UpdateICSInventoryInspectionGroup(ICSInventoryInspectionGroup main,string BID, string ID, string InvCode)
        {
            try
            {
                App.UpdateICSInventoryInspectionGroup(main, BID, ID, InvCode);
                return Success("修改成功!");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }



        [HttpGet]
        public ActionResult GetICSInventoryInspectionList2(string BID)
        {
            var data = App.GetICSInventoryInspectionList2(BID);
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult ICSHGErrorHandle()
        {
            return View();
        }


    }
}