using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using WMS.Frm.Base; using WMS.Model.Base; using WMS.Base.WebService; using WMS.Common; using WMS.Ctrl; using System.Collections; namespace WMS.FrmBaseData { /// /// 窗体:快递公司信息 /// public partial class FrmLogisticsComInfo : FormBase { #region 变量 public static FrmLogisticsComInfo frmLogisticsComInfo; private LogisticsComInfoModel LogisticsComInfoModel = new LogisticsComInfoModel(); private List listLogisticsComInfo = new List(); private int flag;//0:新增;1:修改 private bool importing = false; #endregion #region 初始化 /// /// 无参构造 查所有 /// public FrmLogisticsComInfo() { InitializeComponent(); } /// /// 本窗体加载的时候 /// /// /// public void FrmLogisticsComInfo_Load(object sender, EventArgs e) { bindGrid(); FrmSelectModle = new LogisticsComInfoModel(); if (frmButtonItem.ContainsKey("SaveImport")) { frmButtonItem["SaveImport"].Enabled = false; } } public static FrmLogisticsComInfo getInstance() { if (frmLogisticsComInfo == null || frmLogisticsComInfo.IsDisposed) { frmLogisticsComInfo = new FrmLogisticsComInfo(); } return frmLogisticsComInfo; } #endregion #region 绑定数据 /// /// 整体绑定数据 /// private void bindGrid() { if (!importing) { string errText = string.Empty; LogisticsComInfoModel LogisticsComInfoModel = new LogisticsComInfoModel(); string strParameter = Newtonsoft.Json.JsonConvert.SerializeObject(LogisticsComInfoModel); //整体绑定数据 string strResult = string.Empty; try { strResult = WebLockConfig.Instance.WebLogisticsComInfo.GetLogisticsComInfoListStr(strParameter, ref errText); } catch (Exception) { SystemCommon.ShowInfoMessageBox("未知错误,请检查网络连接!如仍无法解决问题请联系我们!"); } if (errText == string.Empty) { listLogisticsComInfo = Newtonsoft.Json.JsonConvert.DeserializeObject>(strResult); if (listLogisticsComInfo.Count > 0) { //bsLogisticsComInfo.DataSource = listLogisticsComInfo; //GVLogisticsComInfo.RefreshData(); } else { //SystemCommon.ShowInfoMessageBox("没有快递公司信息"); } } else { SystemCommon.ShowInfoMessageBox(errText); } } //else { // bsLogisticsComInfo.DataSource = listLogisticsComInfo; // GVLogisticsComInfo.RefreshData(); //} bsLogisticsComInfo.DataSource = listLogisticsComInfo; GVLogisticsComInfo.RefreshData(); } /// /// GridView选中行改变 /// /// /// private void GVLogisticsComInfo_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { LogisticsComInfoModel = new LogisticsComInfoModel(); LogisticsComInfoModel = GVLogisticsComInfo.GetFocusedRow() as LogisticsComInfoModel; } /// /// 双击GridView,得到属于该快递公司的配送范围 /// /// /// private void GVLogisticsComInfo_DoubleClick(object sender, EventArgs e) { /*当该快递公司有配送范围信息,显示它的配送范围*/ string errText = string.Empty; LogisticsRangeModel logisticsRangeModel = new LogisticsRangeModel(); logisticsRangeModel.LOG_COM_ID = LogisticsComInfoModel.LOG_COM_ID; string strParameter = Newtonsoft.Json.JsonConvert.SerializeObject(logisticsRangeModel); string strDataSet = string.Empty; try { strDataSet = WebLockConfig.Instance.WebLogisticsRange.GetLogisticsRangeListStr(strParameter, ref errText); } catch (Exception) { SystemCommon.ShowInfoMessageBox("未知错误,请检查网络连接!如仍无法解决问题请联系我们!"); } if (errText == string.Empty) { //调用Web服务读取快递公司配送范围信息 List listLogisticsRangeModel = Newtonsoft.Json.JsonConvert.DeserializeObject>(strDataSet); if (listLogisticsRangeModel.Count > 0) { /*在Tab页打开新窗体*/ FormBase formBase = OpenFrmTable("WMS.FrmBaseData", "FrmLogisticsRange");//创建窗体 if (formBase != null) { FrmLogisticsRange frmLogisticsRange = formBase as FrmLogisticsRange; frmLogisticsRange.LogComID = LogisticsComInfoModel.LOG_COM_ID;//为 刚创建的窗体 的 属性 赋值 //OpenTabAddFrm打开窗体,打开之前检测该窗体是否已经被打开 是则返回false if (!OpenTabAddFrm("1613", "FrmLogisticsRange", "快递公司配送范围-" + LogisticsComInfoModel.LOG_COM_ID)) { frmLogisticsRange.FrmLogisticsRange_Load(null, null);//如果已经被打开,执行一下窗体加载方法 } } } else { SystemCommon.ShowInfoMessageBox("该快递公司下没有可配送的范围"); } } else { SystemCommon.ShowInfoMessageBox(errText); } } /// /// 通用查询 /// /// public override void LoadListData(DataTable table) { listLogisticsComInfo = ConvertHelper.ConvertToList(table); bsLogisticsComInfo.DataSource = listLogisticsComInfo; GVLogisticsComInfo.RefreshData(); } #endregion #region 按钮事件 /// /// 新增按钮 /// public override void Add() { LoadForm load = new LoadForm(); DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(load, load.GetType(), false, true, false, 10); DevExpress.XtraSplashScreen.SplashScreenManager.Default.SetWaitFormDescription("正在加载窗体,请等待...."); FrmLogisticsComInfoHandle frmLogisticsComInfoHandle = new FrmLogisticsComInfoHandle(); frmLogisticsComInfoHandle.userData = this.userData; frmLogisticsComInfoHandle.ShowDialog(); bindGrid(); } /// /// 编辑按钮 /// public override void Edit() { if (!frmButtonItem.ContainsKey("Edit")) { return; } else if (frmButtonItem["Edit"].Enabled == false) { return; } LoadForm load = new LoadForm(); DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(load, load.GetType(), false, true, false, 10); DevExpress.XtraSplashScreen.SplashScreenManager.Default.SetWaitFormDescription("正在加载窗体,请等待...."); FrmLogisticsComInfoHandle frmLogisticsComInfoHandle = new FrmLogisticsComInfoHandle(LogisticsComInfoModel); frmLogisticsComInfoHandle.userData = this.userData; if (importing) { frmLogisticsComInfoHandle.flag = 3; } frmLogisticsComInfoHandle.ShowDialog(); bindGrid(); } /// /// 刷新按钮 /// public override void Refresh() { importing = false; bindGrid(); if (frmButtonItem.ContainsKey("Add")) { frmButtonItem["Add"].Enabled = true; } if (frmButtonItem.ContainsKey("SaveImport")) { frmButtonItem["SaveImport"].Enabled = false; } } /// /// 删除按钮 /// public override void Del() { string errText = string.Empty; if (DialogResult.Yes == SystemCommon.ShowMessageBoxResult("确定删除")) { if (!importing) { string str = LogisticsComInfoModel.LOG_COM_ID; if (!string.IsNullOrEmpty(str)) { /*检测该快递公司下是否存在配送范围数据,若有提示用户是否一并删除*/ /*得到该快递公司下配送范围数据*/ LogisticsRangeModel logisticsRangeModel = new LogisticsRangeModel(); logisticsRangeModel.LOG_COM_ID = str; string strModel = Newtonsoft.Json.JsonConvert.SerializeObject(logisticsRangeModel); string strDataSet = string.Empty; try { strDataSet = WebLockConfig.Instance.WebLogisticsRange.GetLogisticsRangeListStr(strModel, ref errText); } catch (Exception) { SystemCommon.ShowInfoMessageBox("未知错误,请检查网络连接!如仍无法解决问题请联系我们!"); } if (errText == string.Empty) { List logisticsRangeList = Newtonsoft.Json.JsonConvert.DeserializeObject>(strDataSet); if (logisticsRangeList.Count > 0) { if (DialogResult.Yes == SystemCommon.ShowMessageBoxResult("该快递公司下存在配送范围数据,是否删除?(点击是将一并删除,点击否不进行任何操作)")) { errText = string.Empty; try { errText = WebLockConfig.Instance.WebLogisticsComInfo.DeleteLogisticsComInfoAndRange(str); } catch (Exception) { SystemCommon.ShowInfoMessageBox("未知错误,请检查网络连接!如仍无法解决问题请联系我们!"); } if (string.IsNullOrEmpty(errText)) { bindGrid(); } else { SystemCommon.ShowInfoMessageBox("删除失败:" + errText); return; } } else { return; } } else { //删除快递公司 errText = string.Empty; try { errText = WebLockConfig.Instance.WebLogisticsComInfo.DeleteLogisticsComInfo(str); } catch (Exception) { SystemCommon.ShowInfoMessageBox("未知错误,请检查网络连接!如仍无法解决问题请联系我们!"); } if (string.IsNullOrEmpty(errText)) { bindGrid(); } else { SystemCommon.ShowInfoMessageBox("删除失败" + errText); } } } else { SystemCommon.ShowInfoMessageBox(errText); } } else { SystemCommon.ShowInfoMessageBox("请选择一条记录"); } } else { listLogisticsComInfo.RemoveAll(p=>p.LOG_COM_ID == LogisticsComInfoModel.LOG_COM_ID && p.LOG_COM_NAME == LogisticsComInfoModel.LOG_COM_NAME);//.RemoveAt(GVLogisticsComInfo.GetFocusedDataSourceRowIndex()); bindGrid(); } } } #endregion /// /// 导入 /// public void Import() { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Title = "导入快递公司信息"; fileDialog.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx"; DialogResult dialogResult = fileDialog.ShowDialog(this); if (dialogResult == DialogResult.OK) { Hashtable ht = new Hashtable(); ht.Add("LOG_COM_ID", "物流公司代码"); ht.Add("LOG_COM_NAME", "物流公司名称"); List tempList = NPOIHelper.Import(fileDialog, ht); if (tempList == null) { bindGrid(); return; } if (importing) { listLogisticsComInfo.AddRange(tempList); } else { listLogisticsComInfo = tempList; } //if (listLogisticsComInfo == null) { // bindGrid(); // return; //} if (frmButtonItem.ContainsKey("Add")) { frmButtonItem["Add"].Enabled = false; } if (frmButtonItem.ContainsKey("SaveImport")) { frmButtonItem["SaveImport"].Enabled = true; } importing = true; bindGrid(); } } /// /// 保存 /// public void SaveImport() { //将list存入数据库 if (listLogisticsComInfo != null && listLogisticsComInfo.Count > 0) { string errText = string.Empty; string strList = Newtonsoft.Json.JsonConvert.SerializeObject(listLogisticsComInfo); List errorIndex = new List(); int hadNum = 0; int lengthNum = 0; string error = string.Empty; try { errorIndex = Newtonsoft.Json.JsonConvert.DeserializeObject>(WebLockConfig.Instance.WebLogisticsComInfo.InsLogisticsComList(strList, ref errText, ref hadNum, ref lengthNum, ref error)); } catch (Exception) { SystemCommon.ShowErrorMessageBox("未知错误,请检查网络连接!如仍无法解决问题请联系我们!"); } if (errorIndex.Count == 0) { SystemCommon.ShowInfoMessageBox("所有记录已保存成功"); Refresh(); } else { List tempList = new List(); for (int i = errorIndex.Count - 1; i >= 0; i--) { tempList.Add(listLogisticsComInfo[i]); } listLogisticsComInfo = tempList; bindGrid(); string fileName = @"D:\WMS\保存导入快递公司错误日志" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"; SystemCommon.WriteFile(fileName, error);///写文件 /*错误提示*/ string info = string.Empty; if (errorIndex.Count > (hadNum + lengthNum)) { info += ((errorIndex.Count - hadNum - lengthNum) + "个快递公司导入时遇到未知错误,"); } if (hadNum > 0) { info += (hadNum + "个快递公司编号已存在,"); } if (lengthNum > 0) { info += (lengthNum + "条快递公司记录数据长度过长,"); } info += "日志已写入" + fileName + ",未出错记录已写入数据库并从窗体中移除。"; SystemCommon.ShowErrorMessageBox(info); } } } /// /// 导出 /// public void Export() { GCLogisticsComInfo.ShowExportDialog(); } } }