Files
U8-Import/U8AppMain/FrmMain.cs
2020-12-18 15:12:05 +08:00

113 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
namespace U8AppMain
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private DataTable dataTable;
private async void BtnBomImport_Click(object sender, EventArgs e)
{
// 判断是否是BOM导入的格式
//母件编码 BOM类别 版本号 版本说明 版本生效日 替代标识 替代说明 母件损耗率 子件编码 行号 基本用量 - 分子 基础数量 - 分母 子件损耗率 供应类型 创建日期 创建人
var colums = new Dictionary<string, string>();
colums.Add("母件编码", "InvCode");
colums.Add("BOM类别", "BomType");
colums.Add("版本号", "Version");
colums.Add("版本说明", "VersionDesc");
colums.Add("版本生效日", "版本号");
colums.Add("替代标识", "版本号");
colums.Add("替代说明", "版本号");
colums.Add("母件损耗率", "ParentScrap");
colums.Add("子件编码", "InvCode");
colums.Add("行号", "SortSeq");
colums.Add("基本用量分子", "BaseQtyN");
colums.Add("基础数量分母", "BaseQtyD");
colums.Add("子件损耗率", "CompScrap");
colums.Add("供应类型", "WIPType");
colums.Add("创建日期", "VersionEffDate");
var bomLookup = new Dictionary<string, Bom.ufinterface>();
foreach (DataRow row in this.dataTable.Rows)
{
var bom = new Bom.ufinterface();
var comp = new Bom.ComponentBody();
bom.Bom.Version.Version = Convert.ToInt32(row["版本号"]);
bom.Bom.Version.VersionDesc = row.Field<string>("版本说明");
bom.Bom.Parent.InvCode = row.Field<string>("母件编码");
bom.Bom.Version.VersionEffDate = DateTime.Parse(row["版本生效日"].ToString());
//bom.Bom.Version.BomType ="1" row.Field<string>("BOM类别");
bom.Bom.Parent.ParentScrap = decimal.Parse(row["母件损耗率"].ToString());
var strKey = $"{bom.Bom.Parent.InvCode}-{bom.Bom.Version.Version}";
if (bomLookup.ContainsKey(strKey))
{
bom = bomLookup[strKey];
}
else
{
bomLookup.Add(strKey, bom);
}
comp.Component.OpComponentId = bom.Bom.ComponentBodies.Count;
comp.Component.EffBegDate = bom.Bom.Version.VersionEffDate;
comp.Component.InvCode = row.Field<string>("子件编码");
comp.Component.SortSeq = row.Field<string>("行号");
comp.Component.OptionsId = bom.Bom.ComponentBodies.Count;
comp.Component.BaseQtyN = decimal.Parse(row["基本用量分子"].ToString());
comp.Component.BaseQtyD = decimal.Parse(row["基础数量分母"].ToString());
comp.Component.CompScrap = decimal.Parse(row["子件损耗率"].ToString());
comp.ComponentOpt.WIPType = row["供应类型"].ToString();
comp.ComponentOpt.OptionsId = bom.Bom.ComponentBodies.Count;
bom.Bom.ComponentBodies.Add(comp);
}
//colums.Add("创建人", "版本号");
//var bom = new Bom.ufinterface();
//bom.Bom.ComponentBodies.Add(new Bom.ComponentBody());
//bom.Bom.ComponentBodies.Add(new Bom.ComponentBody());
var strbody = XmlHelper.XmlSerialize(bomLookup.Values.ToList()[0], Encoding.UTF8);
//var response = await HttpHelper.Post("", strbody);
this.textBox1.Text = strbody;
MessageBox.Show(strbody);
}
private void BtnImportExcel_Click(object sender, EventArgs e)
{
var opentFileDlg = new OpenFileDialog();
opentFileDlg.Filter = "EXCEL2003|*.xls|EXCLE2007|*.xlsx";
if (opentFileDlg.ShowDialog() == DialogResult.OK)
{
var file = new FileStream(opentFileDlg.FileName, FileMode.Open);
dataTable = ExcelHelper.ExcelToDataTable(file);
this.kryptonDataGridView1.DataSource = dataTable;
}
return;
}
}
}