using CasunCommon.BaseUI;
using Pms.Common.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PMSUI.Config; 

namespace PMSUI
{
    public partial class CWMSSettingForm : CasunForm
    {
        private System.Windows.Forms.Timer TimeUpdate;

        private int selectRowIndexOfordersDataGridView;         // 记录选中的行号
        private int selectColumnIndexOfordersDataGridView;      // 记录选中的列号
        private int scrollingRowIndexOfordersDataGridView;      // 记录垂直滚动条位置
        private int scrollingColumnIndexOfordersDataGridView;   // 记录水平滚动条的位置


        public CWMSSettingForm()
        {
            InitializeComponent();
        }

        private void InitDgv()
        {
            dataGridView1.Columns.Clear();
            dataGridView1.DataSource = null;

            GpBatchOpt.Enabled = CConfigManager.gs_globalConfig.gs_IsUseBatchOpt;
            GpBatchOpt.Visible = CConfigManager.gs_globalConfig.gs_IsUseBatchOpt;


            dataGridView1.DataSource = CFormColumnManager.GetDGVDataSource(this.Name, "PMS_bas_Warehouse");


            TimeUpdate = new System.Windows.Forms.Timer();
            TimeUpdate.Tick += new System.EventHandler(TimeUpdate_Tick);
            TimeUpdate.Enabled = true;
            TimeUpdate.Interval = 2000;
        }


        private void InitFormShow()
        {
            //groupBox2.Visible = false;
            foreach (Control con in gp_Read.Controls)
            {
                if (!(con is Button))
                {
                    con.Visible = false;
                }
            }
            foreach (Control con in gp_Opt.Controls)
            {
                if (!(con is Button))
                {
                    con.Visible = false;
                }
            }
            //主键显示
            label1.Visible = true;
            TBWarehouseID.Visible = true;


            List<string> InitControlList = CFormColumnManager.GetNeedShowDBList(this.Name);
            foreach (string str in InitControlList)
            {
                switch (str)
                {
                    case "MaterialID":
                        lbMaterialID.Visible = true;
                        TBMaterialID.Visible = true;
                        break;
                    case "ProductRegion":
                        label8.Visible = true;
                        cbProductRegion.Visible = true;

                        string[] ProductRegionIDArr = CConfigManager.gs_WmsData.ProductRegion.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        cbProductRegion.Items.Clear();
                        cbProductRegion.Items.Add("");
                        cbProductRegion.Items.AddRange(ProductRegionIDArr);


                        //没有这个表，改为代码界面直接加下拉框选项？

                        //List<string> ProductRegionList = CasunModelDB.CDBManager.Instance.PMS_bas_ProductRegion.GetAllRegionID();
                        //cbProductRegion.Items.Clear();
                        //cbProductRegion.Items.Add("");
                        //cbProductRegion.Items.AddRange(ProductRegionList.ToArray());
                        break;
                    case "RackID":
                        lbRackID.Visible = true;
                        TBRackID.Visible = true;
                        break;
                    case "RegionID":
                        label6.Visible = true;
                        cbRegionID.Visible = true;

                        // 区域查询
                        label10.Visible = true;
                        CBSelectRegion.Visible = true;

                        string[] RegionIDArr = CConfigManager.gs_WmsData.Region.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        cbRegionID.Items.Clear();
                        cbRegionID.Items.Add("");
                        cbRegionID.Items.AddRange(RegionIDArr);

                        CBSelectRegion.Items.Clear();
                        CBSelectRegion.Items.Add("");
                        CBSelectRegion.Items.AddRange(RegionIDArr);


                        //List<string> RegionIDList = CasunModelDB.CDBManager.Instance.PMS_bas_WMSRegion.GetAllRegionID();
                        //cbRegionID.Items.Clear();
                        //cbRegionID.Items.Add("");
                        //cbRegionID.Items.AddRange(RegionIDList.ToArray());
                        break;
                    case "RegionSeq":
                        label17.Visible = true;
                        tbRegionSeq.Visible = true;
                        break;
                    case "StationID":
                        label4.Visible = true;
                        TBStationID.Visible = true;

                        //活动站点查询界面
                        label5.Visible = true;
                        tbSelStation.Visible = true;
                        break;
                    case "WareHouseStatus":
                        label2.Visible = true;
                        CBWarehouseStatus.Visible = true;
                        break;
                    case "WareHouseType":
                        label3.Visible = true;
                        CBWarehouseType.Visible = true;

                        label11.Visible = true;
                        CBSelectAddressType.Visible = true;

                        List<string> WareHouseTypeList = PmsCoreDB.DBHost.Instance.WarehouseType.GetAllWareHouseType();

                        CBWarehouseType.Items.Clear();
                        CBWarehouseType.Items.Add("");
                        CBWarehouseType.Items.AddRange(WareHouseTypeList.ToArray());
                        break;
                    case "WareHouseName":
                        label16.Visible = true;
                        tbWarehouseName.Visible = true;
                        break;

                    default:
                        break;
                }
            }

        }
        private void InitLabelShow()
        {
            if (DBNameToShowNameDic.ContainsKey("WareHouseID")) { label1.Text = DBNameToShowNameDic["WareHouseID"]; label14.Text = DBNameToShowNameDic["WareHouseID"]; }
            if (DBNameToShowNameDic.ContainsKey("WareHouseName")) { label16.Text = DBNameToShowNameDic["WareHouseName"]; }
            if (DBNameToShowNameDic.ContainsKey("WareHouseStatus")) { label2.Text = DBNameToShowNameDic["WareHouseStatus"]; }
            if (DBNameToShowNameDic.ContainsKey("WareHouseType")) { label3.Text = DBNameToShowNameDic["WareHouseType"]; label11.Text = DBNameToShowNameDic["WareHouseType"]; }
            if (DBNameToShowNameDic.ContainsKey("RegionID")) { label6.Text = DBNameToShowNameDic["RegionID"]; label10.Text = DBNameToShowNameDic["RegionID"]; }
            if (DBNameToShowNameDic.ContainsKey("StationID")) { label4.Text = DBNameToShowNameDic["StationID"]; }
            if (DBNameToShowNameDic.ContainsKey("RegionSeq")) { label17.Text = DBNameToShowNameDic["RegionSeq"]; }
            if (DBNameToShowNameDic.ContainsKey("ProductRegion")) { label8.Text = DBNameToShowNameDic["ProductRegion"]; }
            if (DBNameToShowNameDic.ContainsKey("MaterialID")) { lbMaterialID.Text = DBNameToShowNameDic["MaterialID"]; }
            if (DBNameToShowNameDic.ContainsKey("RackID")) { lbRackID.Text = DBNameToShowNameDic["RackID"]; }
        }


        private void TimeUpdate_Tick(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.CurrentCell != null && dataGridView1.RowCount != 0 && dataGridView1.ColumnCount != 0)
                {
                    selectRowIndexOfordersDataGridView = dataGridView1.CurrentCell.RowIndex;
                    selectColumnIndexOfordersDataGridView = dataGridView1.CurrentCell.ColumnIndex;
                    scrollingRowIndexOfordersDataGridView = dataGridView1.FirstDisplayedScrollingRowIndex;
                    scrollingColumnIndexOfordersDataGridView = dataGridView1.FirstDisplayedScrollingColumnIndex;
                }

                dataGridView1.DataSource = CFormColumnManager.GetDGVDataSource(this.Name, "PMS_bas_Warehouse", GetOrderBy(), GetWhere());
                int n = dataGridView1.RowCount;
                int m = dataGridView1.ColumnCount;

                if (n != 0 && m != 0)
                {
                    if (selectRowIndexOfordersDataGridView < n && selectColumnIndexOfordersDataGridView < m &&
                        scrollingRowIndexOfordersDataGridView < n && scrollingColumnIndexOfordersDataGridView < m)
                    {
                        dataGridView1.CurrentCell = dataGridView1.Rows[selectRowIndexOfordersDataGridView].Cells[selectColumnIndexOfordersDataGridView];
                        dataGridView1.FirstDisplayedScrollingRowIndex = scrollingRowIndexOfordersDataGridView;
                        dataGridView1.FirstDisplayedScrollingColumnIndex = scrollingColumnIndexOfordersDataGridView;
                    }
                }
            }
            catch (Exception ex)
            {
                string strlog = "获取库位信息异常：" + ex.Message + ex.StackTrace;
                //CLog.Instance.WriteLog("Error", strlog);
            }
        }

        private string GetOrderBy()
        {
            return "order by regionid,WareHouseID";
        }
        private string GetWhere()
        {
            string strRet = "";

            int stationID;
            if (int.TryParse(tbSelStation.Text.ToString(),out stationID))
            {
                strRet = string.Format(" StationID = {0}",stationID);
            }
            string strWarehouseType = CBSelectAddressType.Text.ToString();
            if (strWarehouseType != "")
            {
                if(strRet != "")
                {
                    strRet += string.Format(" and WareHouseType = '{0}'", strWarehouseType);
                }
                else
                {
                    strRet = string.Format(" WareHouseType = '{0}'", strWarehouseType);
                }
            }

            string strRegionID = CBSelectRegion.Text.ToString();
            if (strRegionID != "")
            {
                if (strRet != "")
                {
                    strRet += string.Format(" and RegionID = '{0}'", strRegionID);
                }
                else
                {
                    strRet = string.Format(" RegionID = '{0}'", strRegionID);
                }
            }


            return strRet;
        }
        ~CWMSSettingForm()
        {

        }

        private void CWMSSettingForm_Load(object sender, EventArgs e)
        {
            if (!InitDBNameToShowNameDic())
            {
                this.Close();
                return;
            }

            CasunUILib.ButtonHelps(btHelps);

            gp_Read.Location = new Point(btHelps.Location.X, gp_Read.Location.Y);
            gp_Opt.Location = new Point(btHelps.Location.X, gp_Opt.Location.Y);

            CasunUILib.GroupBoxReadModel(gp_Read, PermissionLevel);
            CasunUILib.GroupBoxReadWriteModel(gp_Opt, PermissionLevel);

            SetDoubleBuffered(dataGridView1);
            InitFormShow();
            InitLabelShow();
            InitDgv();
            CasunUILib.DgvModel(dataGridView1);
            CasunUILib.splitContainerModel(splitContainer1);
        }

        private static DataTable ColumnSettingDT = new DataTable();

        /// <summary>
        /// 设置双缓冲
        /// </summary>
        /// <param name="c"></param>
        public static void SetDoubleBuffered(System.Windows.Forms.Control c)
        {   //请参考下面的博客   //http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx   
            if (System.Windows.Forms.SystemInformation.TerminalServerSession)
                return;
            System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered"
                , System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            aProp.SetValue(c, true, null);
        }

        private void CWMSSettingForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            TimeUpdate.Enabled = false;
        }


        /// <summary>
        /// 输入内容验证
        /// </summary>
        /// <returns></returns>
        private bool EntryCheck()
        {
            bool bResult = true;
            string strMsg = "";

            if (cbRegionID.Text == "" && cbRegionID.Visible)
            {
                strMsg += "【" + CLanguageManager.LanguageChange("区域号不能为空") + "】\r\n";
                bResult = false;
            }
            //if (CBRegionType.Text == "")
            //{
            //    strMsg += "【"+ CLanguageManager.LanguageChange("区域类型不能为空") + "】\r\n";
            //    bResult = false;
            //}
            if (TBWarehouseID.Text == "" && TBWarehouseID.Visible)
            {
                strMsg += "【" + CLanguageManager.LanguageChange("库位号不能为空") + "】\r\n";
                bResult = false;
            }
            if (CBWarehouseStatus.Text == "" && CBWarehouseStatus.Visible)
            {
                strMsg += "【" + CLanguageManager.LanguageChange("库位状态不能为空") + "】\r\n";
                bResult = false;
            }
            if (CBWarehouseType.Text == "" && CBWarehouseType.Visible)
            {
                strMsg += "【" + CLanguageManager.LanguageChange("库位类型不能为空") + "】\r\n";
                bResult = false;
            }
            if (TBStationID.Text == "" && TBStationID.Visible)
            {
                strMsg += "【" + CLanguageManager.LanguageChange("节点号不能为空") + "】\r\n";
                bResult = false;
            }
            if (cbProductRegion.Text == "" && cbProductRegion.Visible)
            {
                strMsg += "【" + CLanguageManager.LanguageChange("生产区不能为空") + "】\r\n";
                bResult = false;
            }

            //if (CBWarehouseStatus.Visible && TBMaterialID.Visible && TBRackID.Visible)
            //{
            //    if (CBWarehouseStatus.Text == "Full")
            //    {
            //        if (TBMaterialID.Text == "")
            //        {
            //            strMsg += string.Format("【Full{0},{1}[{2}]{3}】\r\n"
            //             , CLanguageManager.LanguageChange("状态")
            //             , CLanguageManager.LanguageChange("物料号")
            //             , CLanguageManager.LanguageChange("不能")
            //             , CLanguageManager.LanguageChange("为空")
            //             );
            //            bResult = false;
            //        }
            //        if (TBRackID.Text == "")
            //        {
            //            strMsg += string.Format("【Full{0},{1}[{2}]{3}】\r\n"
            //             , CLanguageManager.LanguageChange("状态")
            //             , CLanguageManager.LanguageChange("料架号")
            //             , CLanguageManager.LanguageChange("不能")
            //             , CLanguageManager.LanguageChange("为空")
            //             );
            //            bResult = false;
            //        }
            //    }
            //    else if (CBWarehouseStatus.Text == "Rack")
            //    {
            //        if (TBMaterialID.Text != "")
            //        {
            //            strMsg += string.Format("【Rack{0},{1}[{2}]{3}】\r\n"
            //                , CLanguageManager.LanguageChange("状态")
            //                , CLanguageManager.LanguageChange("物料号")
            //                , CLanguageManager.LanguageChange("应该")
            //                , CLanguageManager.LanguageChange("为空")
            //                );
            //            bResult = false;
            //        }
            //        if (TBRackID.Text == "")
            //        {
            //            strMsg += string.Format("【Rack{0},{1}[{2}]{3}】\r\n"
            //               , CLanguageManager.LanguageChange("状态")
            //               , CLanguageManager.LanguageChange("料架号")
            //               , CLanguageManager.LanguageChange("不能")
            //               , CLanguageManager.LanguageChange("为空")
            //               );
            //            bResult = false;
            //        }
            //    }
            //    else if (CBWarehouseStatus.Text == "Empty")
            //    {
            //        if (TBMaterialID.Text != "")
            //        {
            //            strMsg += string.Format("【Empty{0},{1}[{2}]{3}】\r\n"
            //                , CLanguageManager.LanguageChange("状态")
            //                , CLanguageManager.LanguageChange("物料号")
            //                , CLanguageManager.LanguageChange("应该")
            //                , CLanguageManager.LanguageChange("为空")
            //                );
            //            bResult = false;
            //        }
            //        if (TBRackID.Text != "")
            //        {
            //            strMsg += string.Format("【Empty{0},{1}[{2}]{3}】\r\n"
            //                , CLanguageManager.LanguageChange("状态")
            //                , CLanguageManager.LanguageChange("料架号")
            //                , CLanguageManager.LanguageChange("应该")
            //                , CLanguageManager.LanguageChange("为空")
            //                );
            //            bResult = false;
            //        }
            //    }
            //}

            if (!bResult)
            {
                //MessageBox.Show("以下项目输入有误：\r\n" + strMsg , "提示");
                MessageBox.Show(CLanguageManager.LanguageChange("以下项目输入有误") + "：\r\n" + strMsg, CLanguageManager.LanguageChange("提示"));
            }
            return bResult;
        }

        /// <summary>
        /// 用于事件重复使用
        /// </summary>
        private Dictionary<string, string> DBNameToShowNameDic = new Dictionary<string, string>();
        private bool InitDBNameToShowNameDic()
        {
            if (!CFormColumnManager.GetDBNameToShowNameDic(this.Name, out DBNameToShowNameDic))
            {
                MessageBox.Show(CLanguageManager.LanguageChange("加载界面配置失败，请重启尝试恢复。"));
                return false;
            }
            return true;
        }
        #region 类容显示
        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                int DGVCurrentRow = e.RowIndex;
                if (DGVCurrentRow != -1 && dataGridView1.Rows[DGVCurrentRow] != null 
                    && dataGridView1.Rows[DGVCurrentRow].Cells[DBNameToShowNameDic["WareHouseID"]].Value != null)
                {
                    ShowText(TBWarehouseID, "WareHouseID", DGVCurrentRow);
                    ShowText(tbWarehouseName, "WareHouseName", DGVCurrentRow);
                    ShowText(CBWarehouseStatus, "WareHouseStatus", DGVCurrentRow);
                    ShowText(CBWarehouseType, "WareHouseType", DGVCurrentRow);
                    ShowText(TBStationID, "StationID", DGVCurrentRow);
                    ShowText(cbRegionID, "RegionID", DGVCurrentRow);
                    ShowText(tbRegionSeq, "RegionSeq", DGVCurrentRow);
                    ShowText(TBMaterialID, "MaterialID", DGVCurrentRow);
                    ShowText(TBRackID, "RackID", DGVCurrentRow);
                    ShowText(cbProductRegion, "ProductRegion", DGVCurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(CLanguageManager.LanguageChange("更改过设置后请重新打开该子界面"));
            }
        }

        private void ShowText(Control sControl, string strKey, int DGVCurrentRow)
        {
            if (DBNameToShowNameDic.ContainsKey(strKey) && dataGridView1.Rows[DGVCurrentRow].Cells[DBNameToShowNameDic[strKey]].Value != null)
            {
                sControl.Text = dataGridView1.Rows[DGVCurrentRow].Cells[DBNameToShowNameDic[strKey]].Value.ToString();
            }
        }
        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!EntryCheck())
            {
                return;
            }

            PmsCoreDB.Model.PMS_bas_Warehouse sWMS = new PmsCoreDB.Model.PMS_bas_Warehouse();
            
            sWMS.WareHouseID = TBWarehouseID.Text.ToString();
            if(tbWarehouseName.Visible)
                sWMS.WareHouseName = tbWarehouseName.Text.ToString();
            if (CBWarehouseStatus.Visible)
                sWMS.WareHouseStatus = CBWarehouseStatus.Text.ToString();
            if (CBWarehouseType.Visible)
                sWMS.WareHouseType = CBWarehouseType.Text.ToString();
            if (TBStationID.Visible)
                sWMS.StationID = Convert.ToInt32(TBStationID.Text.ToString());
            if (cbRegionID.Visible)
                sWMS.RegionID = cbRegionID.Text.ToString();
            if (tbRegionSeq.Visible)
                sWMS.RegionSeq = Convert.ToInt32(tbRegionSeq.Text.ToString());
            if (TBMaterialID.Visible)
                sWMS.MaterialID = TBMaterialID.Text.ToString();
            if (TBRackID.Visible)
                sWMS.RackID = TBRackID.Text.ToString();
            if (cbProductRegion.Visible)
                sWMS.ProductRegion = cbProductRegion.Text.ToString();

                sWMS.UpdateTime = DateTime.Now;
            
            if (PmsCoreDB.DBHost.Instance.Warehouse.Add(sWMS))
            {
                string strOptLog = string.Format("【{0}】{1}:{2}={3}、{4}={5}"
                    , CLanguageManager.LanguageChange("插入")
                    , CLanguageManager.LanguageChange("库位信息")
                    , CLanguageManager.LanguageChange("库位号")
                    , TBWarehouseID.Text.ToString()
                    , CLanguageManager.LanguageChange("库位状态")
                    , CBWarehouseStatus.Text.ToString());

                strOptLog += "、" + CLanguageManager.LanguageChange("库位名") + "=" + tbWarehouseName.Text.ToString();
                if (CBWarehouseType.Visible)
                    strOptLog += "、" + CLanguageManager.LanguageChange("库位类型") + "=" + CBWarehouseType.Text.ToString();
                strOptLog += "、" + CLanguageManager.LanguageChange("节点号") + " = " + TBStationID.Text.ToString();
                if (cbRegionID.Visible)
                    strOptLog += "、" + CLanguageManager.LanguageChange("区域号") + " = " + cbRegionID.Text.ToString();
                strOptLog += "、" + CLanguageManager.LanguageChange("区域顺序") + " = " + tbRegionSeq.Text.ToString();
                strOptLog += "、" + CLanguageManager.LanguageChange("物料号") + " = " + TBMaterialID.Text.ToString();
                strOptLog += "、" + CLanguageManager.LanguageChange("料架号") + " = " + TBRackID.Text.ToString();
                if (cbProductRegion.Visible)
                    strOptLog += "、" + CLanguageManager.LanguageChange("生产区") + " = " + cbProductRegion.Text.ToString();

                COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);

                MessageBox.Show(CLanguageManager.LanguageChange("成功") + "！", CLanguageManager.LanguageChange("提示信息"));
            }
            else
            {
                MessageBox.Show(CLanguageManager.LanguageChange("失败") + "！", CLanguageManager.LanguageChange("提示信息"));
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int currentIndex = dataGridView1.CurrentRow.Index;

            /////权限删除，这里？？？
            //if (dataGridView1.Rows[currentIndex].Cells[(int)EDGVColumnNumber.nWarehouseStatus].Value.ToString() == EWarehouseStatus.AGVLock.ToString() && PermissionLevel != (int)EPermission.admin)
            //{
            //    MessageBox.Show(CLanguageManager.LanguageChange("权限不足") + "," + CLanguageManager.LanguageChange("不能删除") + "！");
            //    return;
            //}
            
            PmsCoreDB.Model.PMS_bas_Warehouse sWMS = new PmsCoreDB.Model.PMS_bas_Warehouse();
            sWMS.WareHouseID = dataGridView1.Rows[currentIndex].Cells[DBNameToShowNameDic["WareHouseID"]].Value.ToString();

            string strMsg = CLanguageManager.LanguageChange("删除库位") + "【" + sWMS.WareHouseID.ToString() + "】，" + CLanguageManager.LanguageChange("是否继续") + "！";
            if (MessageBox.Show(strMsg, CLanguageManager.LanguageChange("删除库位"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
            {
                return;
            }

            if (PmsCoreDB.DBHost.Instance.Warehouse.Delete(sWMS.WareHouseID))
            {
                string strOptLog = string.Format("【{0}】{1}:{2}={3}"
                    , CLanguageManager.LanguageChange("删除")
                    , CLanguageManager.LanguageChange("库位信息")
                    , CLanguageManager.LanguageChange("库位号")
                    , sWMS.WareHouseID
                    );

                COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);
                MessageBox.Show(strOptLog + "," + CLanguageManager.LanguageChange("成功") + "！", CLanguageManager.LanguageChange("提示信息"));
            }
            else
            {
                MessageBox.Show(CLanguageManager.LanguageChange("删除失败") + "！", CLanguageManager.LanguageChange("提示信息"));
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (!EntryCheck())
            {
                return;
            }

            int currentIndex = dataGridView1.CurrentRow.Index;
            /////权限删除，这里？？？
            //if (dataGridView1.Rows[currentIndex].Cells[(int)EDGVColumnNumber.nWarehouseStatus].Value.ToString()
            //    == EWarehouseStatus.AGVLock.ToString() && PermissionLevel != (int)EPermission.admin)
            //{
            //    MessageBox.Show(CLanguageManager.LanguageChange("权限不足") + "," + CLanguageManager.LanguageChange("不允许修改") + "！");
            //    return;
            //}

            //string OldWarehouseID = dataGridView1.Rows[currentIndex].Cells[DBNameToShowNameDic["WareHouseID"]].Value.ToString();

            //PMS_mid_WMSVo sWMS = new PMS_mid_WMSVo();
            PmsCoreDB.Model.PMS_bas_Warehouse sWMS = new PmsCoreDB.Model.PMS_bas_Warehouse();
            sWMS.WareHouseID = TBWarehouseID.Text.ToString();
            if (tbWarehouseName.Visible)
                sWMS.WareHouseName = tbWarehouseName.Text.ToString();
            if (CBWarehouseStatus.Visible)
                sWMS.WareHouseStatus = CBWarehouseStatus.Text.ToString();
            if (CBWarehouseType.Visible)
                sWMS.WareHouseType = CBWarehouseType.Text.ToString();
            if (TBStationID.Visible)
                sWMS.StationID = Convert.ToInt32(TBStationID.Text.ToString());
            if (cbRegionID.Visible)
                sWMS.RegionID = cbRegionID.Text.ToString();
            if (tbRegionSeq.Visible)
                sWMS.RegionSeq = Convert.ToInt32(tbRegionSeq.Text.ToString()); 
            if (TBMaterialID.Visible)
                sWMS.MaterialID = TBMaterialID.Text.ToString();
            if (TBRackID.Visible)
                sWMS.RackID = TBRackID.Text.ToString();
            if (cbProductRegion.Visible)
                sWMS.ProductRegion = cbProductRegion.Text.ToString();
            
                sWMS.UpdateTime = DateTime.Now;

            if (PmsCoreDB.DBHost.Instance.Warehouse.Update(sWMS))
            {
                string strOptLog = string.Format("【{0}】{1}:{2}={3}、{4}={5}"
                    , CLanguageManager.LanguageChange("修改")
                    , CLanguageManager.LanguageChange("库位信息")
                    , CLanguageManager.LanguageChange("库位号")
                    , TBWarehouseID.Text.ToString()
                    , CLanguageManager.LanguageChange("库位状态")
                    , CBWarehouseStatus.Text.ToString());
                strOptLog += "、" + CLanguageManager.LanguageChange("库位名") + "=" + tbWarehouseName.Text.ToString();
                if (CBWarehouseType.Visible)
                    strOptLog += "、" + CLanguageManager.LanguageChange("库位类型") + "=" + CBWarehouseType.Text.ToString();
                strOptLog += "、" + CLanguageManager.LanguageChange("节点号") + " = " + TBStationID.Text.ToString();
                if (cbRegionID.Visible)
                    strOptLog += "、" + CLanguageManager.LanguageChange("区域号") + " = " + cbRegionID.Text.ToString();
                strOptLog += "、" + CLanguageManager.LanguageChange("区域顺序") + " = " + tbRegionSeq.Text.ToString();
                strOptLog += "、" + CLanguageManager.LanguageChange("物料号") + " = " + TBMaterialID.Text.ToString();
                strOptLog += "、" + CLanguageManager.LanguageChange("料架号") + " = " + TBRackID.Text.ToString();
                if (cbProductRegion.Visible)
                    strOptLog += "、" + CLanguageManager.LanguageChange("生产区") + " = " + cbProductRegion.Text.ToString();

                COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);
                MessageBox.Show(CLanguageManager.LanguageChange("更新成功") + "！", CLanguageManager.LanguageChange("提示信息"));
            }
            else
            {
                MessageBox.Show(CLanguageManager.LanguageChange("更新失败") + "！", CLanguageManager.LanguageChange("提示信息"));
            }
        }

        private void btBatchAdd_Click(object sender, EventArgs e)
        {
            if (!EntryCheck())
            {
                return;
            }

            int StartNum;
            int EndNum;
            if (!int.TryParse(tbpWmsStartNum.Text.ToString(), out StartNum))
            {
                MessageBox.Show(CLanguageManager.LanguageChange("输入数字") + "-" + CLanguageManager.LanguageChange("起始编号") + "！");
                return;
            }
            if (!int.TryParse(tbpWmsEndNum.Text.ToString(), out EndNum))
            {
                MessageBox.Show(CLanguageManager.LanguageChange("输入数字") + "-" + CLanguageManager.LanguageChange("结束编号") + "！");
                return;
            }
            bool bAdd = false;
            string Code = tbpWmsCode.Text.ToString();
            string strSuccessAdd = "";
            string strFailAdd = "";
            for (int i = StartNum; i <= EndNum; i++)
            {
                string str = Code + i.ToString();
                PmsCoreDB.Model.PMS_bas_Warehouse sWMS = new PmsCoreDB.Model.PMS_bas_Warehouse();
                //PMS_mid_WMSVo sWMS = new PMS_mid_WMSVo();
                sWMS.WareHouseID = str;
                if (CBWarehouseStatus.Visible)
                    sWMS.WareHouseStatus = CBWarehouseStatus.Text.ToString();
                if (CBWarehouseType.Visible)
                    sWMS.WareHouseType = CBWarehouseType.Text.ToString();
                if (TBStationID.Visible)
                    sWMS.StationID = Convert.ToInt32(TBStationID.Text.ToString());
                if (cbRegionID.Visible)
                    sWMS.RegionID = cbRegionID.Text.ToString();
                if (cbProductRegion.Visible)
                    //sWMS.RegionType = CBRegionType.Text.ToString();
                    //sWMS.RegionStatus = cbRegionStatus.Text.ToString();
                    sWMS.ProductRegion = cbProductRegion.Text.ToString();


                sWMS.UpdateTime = DateTime.Now;

                if (PmsCoreDB.DBHost.Instance.Warehouse.Add(sWMS))
                {
                    bAdd = true;
                    strSuccessAdd += "【" + str + "】";
                }
                else
                {
                    strFailAdd += "【" + str + "】";
                }
            }

            if (bAdd)
            {
                string strOptLog = CLanguageManager.LanguageChange("批量添加库位") + ",";
                if (strSuccessAdd != "")
                {
                    strOptLog += CLanguageManager.LanguageChange("成功") + strSuccessAdd;
                }
                if (strFailAdd != "")
                {
                    strOptLog += CLanguageManager.LanguageChange("失败") + strFailAdd;
                }

                MessageBox.Show(strOptLog, CLanguageManager.LanguageChange("提示信息"));
                COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);
            }
        }

        private void btHelps_Click(object sender, EventArgs e)
        {
            MessageBox.Show(PMSUI.Config.CConfigManager.gs_FormHelpConfig.GetFormHelp(this.Name));

        }
    }
}
