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 System.Collections;
using CoreFS.CA06;
using Core.Mes.Client.Comm.Tool;
using Core.Mes.Client.Comm.Server;
using com.steering.pss.plnsaleord.order.model;
using System.Globalization;
using Core.Mes.Client.Comm;
using Core.StlMes.Client.PlnSaleOrd.工序排产.entity;
using Core.Mes.Client.Comm.Format;
namespace Core.StlMes.Client.PlnSaleOrd.PopupWindow
{
public partial class FrmImportSetPlnZg : FrmBase
{
///
/// 模板路径
///
string modelPatch = "";
///
/// 模板路径
///
public string ModelPatch
{
get { return modelPatch; }
set { modelPatch = value; }
}
public FrmImportSetPlnZg()
{
InitializeComponent();
}
public FrmImportSetPlnZg(OpeBase openbase)
{
this.ob = openbase;
InitializeComponent();
}
private void FrmImportSetPlnZg_Load(object sender, EventArgs e)
{
Init();
}
///
/// 初始化
///
private void Init()
{
ultraNumRowStart.Value = 2;
ultraNumRowEnd.Value = 2;
if (!System.IO.File.Exists(ModelPatch))//检察文件是否存在
{
return;
}
try
{
System.IO.File.Move(ModelPatch, ModelPatch);
}
catch
{
MessageUtil.ShowTips("文件已经被打开,请先关闭!");
return;
}
uC_EmbedExcelIntoWinFormNoDLL1.AppExcel_Open(ModelPatch, true);
this.ultraNumRowEnd.Value = getExcelRowsCount();
}
///
/// 获取excl的行数
///
private int getExcelRowsCount()
{
//获取行数(上限5000)
int colOrderNo = 0;
int j = 4999;
string[,] arr = null;
string[,] head = ObjectToString(uC_EmbedExcelIntoWinFormNoDLL1.GetRangeArray_Value("A" + 1, "G" + 1));
if (head == null)
{
return 1;
}
for (int i = 0; i < head.GetLength(1); i++)
{
if (head[0, i].Trim().Equals("轧批号"))
{
colOrderNo = i;
break;
}
}
arr = ObjectToString(uC_EmbedExcelIntoWinFormNoDLL1.GetRangeArray_Value(
ExcelHelper.SeqToXlsCol[colOrderNo + 1] + 2,
ExcelHelper.SeqToXlsCol[colOrderNo + 1] + 5001));
if (arr == null)
{
return 1;
}
for (j = 4999; j >= 0; j--)
{
if (!arr[j, 0].Equals(""))
{
break;
}
}
return j + 2;
}
private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
{
switch (e.Tool.Key)
{
case "Save":
doSave();
break;
case "Open":
doOpen();
break;
case "Close":
doClose();
break;
}
}
///
/// 保存
///
private void doSave()
{
try
{
ArrayList messageErr = new ArrayList();
int startRow = (int)ultraNumRowStart.Value;
int endRow = (int)ultraNumRowEnd.Value;
if (endRow - startRow < 0)
{
MessageUtil.ShowTips("没有任何行数据!");
return;
}
string[,] head = ObjectToString(uC_EmbedExcelIntoWinFormNoDLL1.GetRangeArray_Value("A" + 1, "G" + 1));
if (head == null)
{
return;
}
string[] colhead = new string[head.GetLength(1)];
for (int i = 0; i < colhead.Length; i++)
{
colhead[i] = head[0, i];
}
#region 获取订单号和排产序号等所在列
int colNum = 0;
int colProPlanId = -1;
int colGxPlanNo = -1;
int colLenGpSingle = -1;
for (int i = head.GetLength(1) - 1; i >= 0; i--)
{
if (!head[0, i].Equals("") && colNum == 0)
{
colNum = i;
}
if (head[0, i].Trim().Equals("订单编号"))
{
colProPlanId = i;
}
if (head[0, i].Trim().Equals("排产序号"))
{
colGxPlanNo = i;
}
if (head[0, i].Trim().Equals("单倍坯长"))
{
colLenGpSingle = i;
}
}
if (colProPlanId == -1)
{
MessageUtil.ShowError("未找到订单编号的列");
return;
}
if (colGxPlanNo == -1)
{
MessageUtil.ShowError("未找到排产序号的列");
return;
}
if (colLenGpSingle == -1)
{
MessageUtil.ShowError("未找到单倍坯长的列");
return;
}
#endregion
ArrayList list = new ArrayList();
string[,] arr = ObjectToString(uC_EmbedExcelIntoWinFormNoDLL1.GetRangeArray_Value(
"A" + startRow, ExcelHelper.SeqToXlsCol[colNum + 1] + endRow));
if (arr == null)
{
return;
}
WaitFromOpen();
for (int i = 0; i < arr.GetLength(0); i++)
{
ArrayList param = new ArrayList();
PlnOrderZgSOEntity entity = new PlnOrderZgSOEntity();
string proPlanId = arr[i, colProPlanId].Trim();
string gxPlanNo = arr[i, colGxPlanNo].ToString(); ;
string lenGpSingle = arr[i, colLenGpSingle].ToString();
if (proPlanId.Equals(""))
{
messageErr.Add("第" + (i + startRow).ToString() + "条数据订单编号为空有误。");
continue;
}
if (proPlanId.Equals(""))
{
messageErr.Add("第" + (i + startRow).ToString() + "条数据排产序号为空有误。");
continue;
}
if (lenGpSingle.Equals(""))
{
messageErr.Add("第" + (i + startRow).ToString() + "条数据单倍坯长为空有误。");
continue;
}
entity.ProPlanId = proPlanId;
entity.GxPlanNo = gxPlanNo;
int lenGpSinglea = Convert.ToInt32(lenGpSingle);
if (lenGpSinglea % 10 != 0)
{
MessageUtil.ShowWarning("第" + (i + startRow).ToString() + "条数据单倍坯长个位数不是0,请重新调整!");
return;
}
entity.LenGpSingle = lenGpSinglea;
list.Add(JSONFormat.Format(entity));
}
CoreClientParam ccp = new CoreClientParam();
ccp.ServerName = "com.steering.pss.plnsaleord.order.CoreOrderStatusSet";
ccp.MethodName = "updatePlnZgLenGpSin";
ccp.ServerParams = new object[] { list };
ccp = this.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
if (ccp.ReturnCode == -1) return;
MessageUtil.ShowTips(ccp.ReturnInfo);
//int succed = ServerHelper.SetData("com.steering.pss.plnsaleord.order.CoreOrderStatusSet.updatePlnZgLenGpSin",
// new object[] { list }, this.ob);
WaitFromColse();
//if (succed > 0)
//{
if (ccp.ReturnInfo.Equals("导入成功!"))
{
//if (messageErr.Count == 0)
//{
// MessageUtil.ShowTips("导入成功,共" + list.Count + "条。");
//}
//else
//{
// MessageUtil.ShowTips(list.Count + "条导入成功," + messageErr.Count + "条导入失败。\n" + messageErr[0]);
//}
} else
{
MessageUtil.ShowTips("导入失败,共" + list.Count + "条。");
}
}
//}
catch (Exception ex)
{
WaitFromColse();
if (!(ex is MESException))
{
MessageUtil.ShowError(ex.Message);
}
}
}
///
/// 打开excel文件
///
private void doOpen()
{
try
{
OpenFileDialog dialog = new OpenFileDialog();
uC_EmbedExcelIntoWinFormNoDLL1.AppExcel_Exit();
if (dialog.ShowDialog() == DialogResult.OK)
{
ModelPatch = dialog.FileName;
if (!System.IO.File.Exists(ModelPatch))//检察文件是否存在
{
return;
}
try
{
System.IO.File.Move(ModelPatch, ModelPatch);
}
catch
{
MessageUtil.ShowTips("文件已经被打开,请先关闭!");
return;
}
uC_EmbedExcelIntoWinFormNoDLL1.AppExcel_Open(ModelPatch, true);
this.ultraNumRowEnd.Value = getExcelRowsCount();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
///
/// 关闭
///
private void doClose()
{
uC_EmbedExcelIntoWinFormNoDLL1.AppExcel_Exit();
}
///
/// object二维数组转string二维数组
///
///
///
private string[,] ObjectToString(object[,] obj)
{
int rows = obj.GetLength(0); //获取m_objRangeArrayValue的行数
int columns = obj.GetLength(1); //获取m_objRangeArrayValue的列数
object[,] param = new object[rows, columns]; //转化为起始为0的数组
Array.Copy(obj, param, rows * columns); //临时结果,学习用
string[,] str = new string[param.GetLength(0), param.GetLength(1)];
for (int i = 0; i < str.GetLength(0); i++)
{
for (int j = 0; j < str.GetLength(1); j++)
{
if (param[i, j] == null)
{
str[i, j] = "";
}
else
{
str[i, j] = param[i, j].ToString();
}
}
}
return str;
}
///
/// 等待窗口
///
private void WaitFromOpen()
{
this.Cursor = Cursors.WaitCursor; //控制鼠标的样式为等待
if (Constant.WaitingForm == null)
{
Constant.WaitingForm = new WaitingForm();
}
Constant.WaitingForm.ShowToUser = true;
Constant.WaitingForm.Show();
Constant.WaitingForm.Update();
}
///
/// 关闭等待
///
private void WaitFromColse()
{
this.Cursor = Cursors.Default;
Constant.WaitingForm.ShowToUser = false;
Constant.WaitingForm.Close();
Constant.WaitingForm = null;
}
private void FrmImportSetOrder_FormClosed(object sender, FormClosedEventArgs e)
{
uC_EmbedExcelIntoWinFormNoDLL1.AppExcel_Exit();
}
///
/// 结束EXCEL.EXE进程的方法
///
/// EXCEL对象
//static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
//private static void KillSpecialExcel(Microsoft.Office.Interop.Excel.Application m_objExcel)
//{
// try
// {
// if (m_objExcel != null)
// {
// int lpdwProcessId;
// GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out lpdwProcessId);
// System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
// }
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
//}
}
}