using KPlan.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.ComponentModel;
using Teamcenter.Soa.Client.Model.Strong;
using Teamcenter.Soa.Client.Model;
using Eplan.EplApi.MasterData;
namespace KPlan.Forms {
///
/// Interaction logic for PartSyncResult.xaml
///
public partial class PartSyncResult : Window {
private ObservableCollection tableData;
public const string EXPORT_FILE_NAME = "同步失败条目";
public const string EXPORT_FILE_EXT = "xlsx";
private KBackgroundWorker bgWorkder_apply;
private MDPartsDatabase mdPartsDatabase;
public PartSyncResult(MDPartsDatabase mdPartsDatabase, Window parent, int success, int canceled, ObservableCollection resData) {
InitializeComponent();
this.mdPartsDatabase = mdPartsDatabase;
this.Owner = parent;
table_Sync_Result.ItemsSource = resData;
this.tableData = resData;
l_Summary.Content = "同步成功"+success+"行,同步失败"+resData.Count+"行"+(canceled>0?", 取消"+canceled+"行":"");
bgWorkder_apply = new KBackgroundWorker(this);
bgWorkder_apply.backgroundWorker.DoWork += Async_DoApply;
}
private void doExport() {
int rowCnt = tableData.Count;
if (rowCnt == 0) {
return;
}
string targetPath = null;
using (var fbd = new FolderBrowserDialog()) {
DialogResult result = fbd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK) {
targetPath = fbd.SelectedPath;
}
}
if (string.IsNullOrWhiteSpace(targetPath)) {
return;
}
KUtil.Log("导出同步失败条目...");
KUtil.Log("导出路径:"+targetPath);
string filePath = KUtil.GetFilePath(targetPath, EXPORT_FILE_NAME, EXPORT_FILE_EXT);
KUtil.Log("导出文件:"+filePath);
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null) {
System.Windows.MessageBox.Show("Excel 未正确安装");
return;
}
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "部件编号";
xlWorkSheet.Cells[1, 2] = "订货号";
xlWorkSheet.Cells[1, 3] = "名称";
xlWorkSheet.Cells[1, 4] = "计量单位";
xlWorkSheet.Cells[1, 5] = "描述(中文)";
xlWorkSheet.Cells[1, 6] = "描述(英文)";
xlWorkSheet.Cells[1, 7] = "失败原因";
for(int i = 0; i < rowCnt; i++) {
int r = i + 2;
SyncResult bean = tableData[i];
xlWorkSheet.Cells[r, 1] = bean.partNr;
xlWorkSheet.Cells[r, 2] = bean.orderNr;
xlWorkSheet.Cells[r, 3] = bean.name;
xlWorkSheet.Cells[r, 4] = bean.jldw;
xlWorkSheet.Cells[r, 5] = bean.desc_cn;
xlWorkSheet.Cells[r, 6] = bean.desc_en;
xlWorkSheet.Cells[r, 7] = bean.reason;
}
//Here saving the file in xlsx
xlWorkBook.SaveAs(filePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, misValue,
misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
System.Windows.MessageBox.Show("导出完成");
}
private void Async_DoApply(object sender, DoWorkEventArgs e) {
KUtil.Log("发起编码申请...");
List applyData = new List();
bgWorkder_apply.Progress("检查数据...", 0);
bool descEmpty = false;
this.Dispatcher.Invoke(new Action(delegate {
int len = tableData.Count;
for (int i = 0; i < len; i++) {
SyncResult bean = tableData[i];
if (KUtil.IsEmpty(bean.desc_en)) {
KUtil.Log("描述缺失:"+bean.partNr);
descEmpty = true;
//break;
}
if (KPartSync.SAP_NEED_ID.Equals(bean.reason)) {
applyData.Add(bean);
}
}
}));
if (descEmpty) {
KUtil.Log("缺少英文描述,请填写后重新申请");
KUtil.DispatcherShow(this, "缺少英文描述,请填写后重新申请");
return;
}
DoApply(this, applyData,mdPartsDatabase);
}
public static void DoApply(Window window, List applyData, MDPartsDatabase mdPartsDatabase) {
if (applyData.Count == 0) {
KUtil.Log("没有数据");
KUtil.DispatcherShow(window, "请选择“"+KPartSync.SAP_NEED_ID+"”的数据进行操作");
return;
}
//新建对象
string itemType = KUtil.GetConfigValue(KConfigure.BMSQ_SECTION, KConfigure.BMSQ_ITEMTYPE);
if (KUtil.IsEmpty(itemType)) {
throw new Exception("未配置编码申请对象类型");
}
ItemRevision rev = TCUtil.CreateNewDocument(itemType, "编码申请单");
if (rev == null) {
throw new Exception("新建对象异常");
}
ModelObject[] mo = TCUtil.Refresh(rev);
TCUtil.GetProperties(false, mo, "object_string", "items_tag", "IMAN_master_form_rev");
KUtil.Log("新建对象完成:" + rev.Object_string);
//设置表单属性
ModelObject[] form = rev.IMAN_master_form_rev;
string tableProp = KUtil.GetConfigValue(KConfigure.BMSQ_SECTION, KConfigure.BMSQ_TABLE);
string rowType = KUtil.GetConfigValue(KConfigure.BMSQ_SECTION, KConfigure.BMSQ_ROWTYPE);
if (!KUtil.IsEmpty(tableProp) && !KUtil.IsEmpty(rowType) && form.Length > 0) {
KUtil.Log("保存表格属性:" + rowType + "|" + tableProp);
List rows = new List();
for (int i = 0; i < applyData.Count; i++) {
SyncResult bean = applyData[i];
Dictionary props = new Dictionary();
AddProp(props, bean.orderNr, KUtil.GetConfigValue(KConfigure.BMSQ_SECTION, KConfigure.BMSQ_ORDERNR));
AddProp(props, Guid.NewGuid().ToString("D"), KUtil.GetConfigValue(KConfigure.BMSQ_SECTION, KConfigure.BMSQ_GUID));
AddProp(props, bean.jldw, KUtil.GetConfigValue(KConfigure.BMSQ_SECTION, KConfigure.BMSQ_UNIT));
AddProp(props, bean.desc_cn, KUtil.GetConfigValue(KConfigure.BMSQ_SECTION, KConfigure.BMSQ_DESC_CN));
AddProp(props, bean.desc_en, KUtil.GetConfigValue(KConfigure.BMSQ_SECTION, KConfigure.BMSQ_DESC_EN));
// AddProp(props, "ElecElement", "sf6_Classification");
ModelObject rowComp = TCUtil.CreateNewRow(rowType, props);
if (rowComp != null) {
KUtil.Log("添加行数据:" + KUtil.ToString(props));
rows.Add(rowComp);
}
}
TCUtil.SetRefProperty(form[0], tableProp, rows);
}
//存放位置
Folder targetFolder = GetSaveFolder();
if (targetFolder != null) {
TCUtil.CreateNewRelation(targetFolder, rev.Items_tag, "contents");
}
//发起流程
string prefName = KUtil.GetConfigValue(KConfigure.BMSQ_SECTION, KConfigure.BMSQ_PROCESS_TEMPLATE);
string[] templateNames = TCUtil.GetPrefVals(prefName);
if (templateNames != null && templateNames.Length > 0) {
KUtil.Log("准备发起流程:" + templateNames[0]);
TCUtil.CreateNewProcess(templateNames[0], rev, 1);//root_target_attachments EPM_target_attachment
KUtil.Log("发起流程结束");
try {
SetStatus(mdPartsDatabase, applyData, KPartSync.APPLYING);
}catch(System.Exception ex) {
KUtil.LogErr(ex);
KUtil.Log("更改状态失败,再次尝试更改");
SetStatus(mdPartsDatabase, applyData, KPartSync.APPLYING);
}
KUtil.DispatcherShow(window, "编码申请流程已经发起");
}
else {
KUtil.DispatcherShow(window, "新建对象完成,未找到编码申请流程信息");
}
}
private static void SetStatus( MDPartsDatabase mdPartsDatabase, List applyData,string status) {
if (applyData==null||applyData.Count == 0) {
return;
}
Eplan.EplApi.MasterData.MDPartsDatabaseTransaction trans = mdPartsDatabase.CreateTransaction();
try {
for (int i = 0; i < applyData.Count; i++) {
SyncResult bean = applyData[i];
if (bean.dataRow.part != null) {
KUtil.Log("更改状态:" + bean.partNr + " -> " + status);
bean.dataRow.part.Properties.ARTICLE_CERTIFICATE_ATEX = status;
bean.dataRow.sync_status = status;
}
}
trans.Commit();
}
finally {
trans.Dispose();
}
}
private static void AddProp(Dictionary props, string propVal, string propName) {
if (!KUtil.IsEmpty(propName)) {
int ind = propName.IndexOf('=');
string def = "";
if (ind > 0) {
def = propName.Substring(ind + 1);
propName = propName.Substring(0, ind);
}
if (KUtil.IsEmpty(propVal)) {
props.Add(propName, def);
}
else {
props.Add(propName, propVal);
}
}
}
private static Folder GetSaveFolder() {
string prefName = KUtil.GetConfigValue(KConfigure.BMSQ_SECTION, KConfigure.BMSQ_POSITION_PREF);
Folder targetFolder = null;
if (!KUtil.IsEmpty(prefName)) {
string[] uids = TCUtil.GetPrefVals(prefName);
if (uids != null && uids.Length > 0) {
string uid = uids[0].Trim();
KUtil.Log("从首选项获取存放文件夹:" + uid);
ModelObject comp = TCUtil.StringToComponent(uid);
if (comp != null) {
targetFolder = comp as Folder;
TCUtil.Refresh(targetFolder);
TCUtil.GetProperties(false, new ModelObject[] { targetFolder }, "object_name","contents");
KUtil.Log("找到文件夹:" + targetFolder.Object_name);
WorkspaceObject[] contents = targetFolder.Contents;
TCUtil.GetProperties(false, contents, "object_name","creation_date");
int len = contents==null?0: contents.Length;
DateTime latest = DateTime.MinValue;
for(int i = 0; i < len; i++) {
Folder cFolder = contents[i] as Folder;
if (cFolder == null) {
continue;
}
DateTime cDate = cFolder.Creation_date;
KUtil.Log("找到子文件夹:"+cFolder.Object_name+"|创建时间:"+cDate);
if (latest == null) {
latest = cDate;
targetFolder = cFolder;
}
else {
if (latest.CompareTo(cDate) < 0) {
latest = cDate;
targetFolder = cFolder;
}
}
}
}
}
}
if (targetFolder == null) {
KUtil.Log("没有指定存放文件夹,放到NewStuff");
TCUtil.GetProperties(false, new ModelObject[] { TCUtil.user }, "newstuff_folder");
targetFolder = TCUtil.user.Newstuff_folder;
}
else {
KUtil.Log("找到最新的文件夹:"+targetFolder.Object_name);
}
return targetFolder;
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
int len = tableData.Count;
bool needApply = false;
for(int i = 0; i < len; i++) {
if (KPartSync.SAP_NEED_ID.Equals(tableData[i].reason)) {
needApply = true;
}
}
b_Apply.IsEnabled = needApply;
b_Export.IsEnabled = len != 0;
}
private void CLose_Button_Click(object sender, RoutedEventArgs e) {
Close();
}
private void Export_Button_Click(object sender, RoutedEventArgs e) {
try {
doExport();
}catch(System.Exception ex) {
System.Windows.MessageBox.Show(this, "导出出错:"+ex.Message);
KUtil.LogErr(ex);
}
}
private void Apply_Button_Click(object sender, RoutedEventArgs e) {
this.bgWorkder_apply.Start(null);
}
}
}