using System; using System.Collections.Generic; 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 KPlan.Util; using KPlan.Forms.Bean; using System.Collections.ObjectModel; using Eplan.EplApi.MasterData; using Eplan.EplApi.Base; namespace KPlan.Forms { /// /// Interaction logic for Config_PartSync.xaml /// public partial class Config_PartSync : Grid { private string loadedType; private List ePlanPropBeanList = new List(); private ObservableCollection ePlanPropBeanData = new ObservableCollection(); private ObservableCollection tcPropBeanData = new ObservableCollection(); private ObservableCollection eplan_to_tc_config = new ObservableCollection(); private ObservableCollection tc_to_eplan_config = new ObservableCollection(); private Dictionary> tcPropMap = new Dictionary>(); public ObservableCollection language_list = new ObservableCollection(); public Config_PartSync() { InitializeComponent(); ReadEplanProperty(); table_Eplan_Prop.ItemsSource = ePlanPropBeanData; table_TC_Prop.ItemsSource = tcPropBeanData; table_EPLAN_TO_TC.ItemsSource = eplan_to_tc_config; table_TC_TO_EPLAN.ItemsSource = tc_to_eplan_config; string[] ENUM_LANGUAGES = Enum.GetNames(typeof(ISOCode.Language)); Array.Sort(ENUM_LANGUAGES); foreach (string l in ENUM_LANGUAGES) { language_list.Add(l); } col_language.ItemsSource = language_list; try { string itemTypeName = KUtil.GetConfigValue(KConfigure.PART_SYNC_SECTION, KConfigure.PART_SYNC_TC_ITEM_TYPE); TCUtil.GetDefinedProperties(itemTypeName); ReadTcProperty(itemTypeName); } catch (System.Exception ex) { KUtil.LogErr(ex); //MessageBox.Show("加载数据出错:" + ex.Message); } } private void ChangeType() { string newType = tb_ObjectType.Text; if (KUtil.IsEmpty(newType)) { MessageBox.Show("请输入对象类型"); return; } if (newType.Equals(loadedType)) { return; } MessageBoxResult res = MessageBox.Show("重新加载对象类型将清空当前映射配置,是否继续?", "", MessageBoxButton.YesNo); if (res != MessageBoxResult.Yes) { return; } //清空 ClearPropConfig(); //加载TC属性 ReadTcProperty(newType); loadedType = newType; } private void ReadTcProperty(string itemTypeName) { tcPropBeanData.Clear(); cb_Location.Items.Clear(); cb_Location.Items.Add(""); this.tcPropMap = TCUtil.GetDefinedProperties(itemTypeName); foreach (string key in tcPropMap.Keys) { cb_Location.Items.Add(key); List list = tcPropMap[key]; foreach (TCPropBean b in list) { tcPropBeanData.Add(b); } } } private void ReadEplanProperty() { ePlanPropBeanList.Clear(); ePlanPropBeanData.Clear(); Eplan.EplApi.DataModel.AnyPropertyId[] props = Eplan.EplApi.DataModel.Properties.AllArticlePropIDs; for (int i = 0; i < props.Length; i++) { Eplan.EplApi.DataModel.AnyPropertyId prop = props[i]; //MessageBox.Show("disName:"+ prop.Definition.Name+ " realName:"+ prop.AsArticle.ToString()); if (prop.Definition.IsReadOnly) { continue; } EPlanPropBean bean = new EPlanPropBean(prop.Definition.Type.ToString()) { disName = prop.Definition.Name, realName = prop.AsArticle.ToString() }; ePlanPropBeanList.Add(bean); ePlanPropBeanData.Add(bean); } } private void ClearPropConfig() { eplan_to_tc_config.Clear(); tc_to_eplan_config.Clear(); } private void Remove_Config(DataGrid dataGrid, ObservableCollection data) { try { System.Collections.IList selectedItems = dataGrid.SelectedItems; if (selectedItems == null || selectedItems.Count == 0) { return; } while (selectedItems.Count > 0) { data.Remove((PartSyncConfigBean)selectedItems[0]); dataGrid.Items.Refresh(); } dataGrid.Items.Refresh(); } catch (System.Exception ex) { KUtil.LogErr(ex); MessageBox.Show(ex.Message); } } private void Modify_Config(DataGrid dataGrid, ObservableCollection data, string from, string to) { if (KUtil.IsEmpty(from) || KUtil.IsEmpty(to)) { return; } PartSyncConfigBean selectedBean = dataGrid.SelectedItem as PartSyncConfigBean; if (selectedBean == null) { return; } int existInd = data.IndexOf(new PartSyncConfigBean() { from = from, to = to }); if (existInd >= 0 && !to.Equals(selectedBean.to)) { MessageBox.Show("已存在属性映射到"+to); return; } //PartSyncConfigBean bean = (PartSyncConfigBean)dataGrid.Items[ind]; selectedBean.from = from; selectedBean.to = to; dataGrid.Items.Refresh(); dataGrid.SelectedItem = selectedBean; } private void Add_Config(DataGrid dataGrid, ObservableCollection data, string from, string to) { if (KUtil.IsEmpty(from) || KUtil.IsEmpty(to)) { return; } PartSyncConfigBean bean = new PartSyncConfigBean() { from = from, to = to }; if (!data.Contains(bean)) { data.Add(bean); } dataGrid.SelectedItem = bean; dataGrid.ScrollIntoView(bean); } private void Read_Prop_Refection_to_Textbox() { EPlanPropBean eplanBean = table_Eplan_Prop.SelectedItem as EPlanPropBean; TCPropBean tcBean = table_TC_Prop.SelectedItem as TCPropBean; string eplanConfig = ""; string tcConfig = ""; if (eplanBean != null) { eplanConfig = eplanBean.getConfigValue(); } if (tcBean != null) { tcConfig = tcBean.location + "." + tcBean.realName; } tb_TC_TO_EPLAN_tc.Text = tcConfig; tb_TC_TO_EPLAN_eplan.Text = eplanConfig; tb_EPLAN_TO_TC_eplan.Text = eplanConfig; tb_EPLAN_TO_TC_tc.Text = tcConfig; } private void Find_TC_Props() { string text = tb_TCProp.Text; string type = cb_Location.SelectedItem as string; tcPropBeanData.Clear(); if (KUtil.IsEmpty(type) && KUtil.IsEmpty(text)) { foreach (List list in tcPropMap.Values) { foreach (TCPropBean b in list) { tcPropBeanData.Add(b); } } } else { text = text.ToLower(); foreach (List list in tcPropMap.Values) { foreach (TCPropBean b in list) { if (KUtil.RegexMatch(b.disName.ToLower(), text) || KUtil.RegexMatch(b.realName.ToLower(), text)) { //if (b.disName.ToLower().Contains(text) || b.realName.ToLower().Contains(text)) { if (KUtil.IsEmpty(type) || b.typeName.Equals(type)) { tcPropBeanData.Add(b); } } } } } } private void Find_EPlan_Props() { string text = tb_EPlanProp.Text; ePlanPropBeanData.Clear(); if (KUtil.IsEmpty(text)) { foreach (EPlanPropBean bean in ePlanPropBeanList) { ePlanPropBeanData.Add(bean); } } else { text = text.ToLower(); foreach (EPlanPropBean bean in ePlanPropBeanList) { if (KUtil.RegexMatch(bean.disName.ToLower(), text) || KUtil.RegexMatch(bean.realName.ToLower(), text)) { //if (bean.disName.ToLower().Contains(text) || bean.realName.ToLower().Contains(text)) { ePlanPropBeanData.Add(bean); } } } } public void SaveConfiguration() { Dictionary syncConfig = new Dictionary(); syncConfig.Add(KConfigure.PART_SYNC_TC_ITEM_TYPE, tb_ObjectType.Text); syncConfig.Add(KConfigure.PART_SYNC_OrderNr_TC, tb_OrderNr_Prop.Text); //syncConfig.Add(KConfigure.PART_SYNC_ItemID_EPlan, tb_ItemID_Prop.Text); KUtil.SetConfigValue(KConfigure.PART_SYNC_SECTION, syncConfig); KUtil.ClearConfigSection(KConfigure.PART_SYNC_EPLAN_TO_TC_SECTION); KUtil.ClearConfigSection(KConfigure.PART_SYNC_TC_TO_EPLAN_SECTION); //eplan to tc Dictionary eplan_to_tc = new Dictionary(); for(int i=0;i< eplan_to_tc_config.Count;i++) { PartSyncConfigBean bean = eplan_to_tc_config[i]; eplan_to_tc.Add(bean.to, bean.from); } KUtil.SetConfigValue(KConfigure.PART_SYNC_EPLAN_TO_TC_SECTION, eplan_to_tc); //tc to eplan Dictionary tc_to_eplan = new Dictionary(); for(int i = 0; i < tc_to_eplan_config.Count; i++) { PartSyncConfigBean bean = tc_to_eplan_config[i]; tc_to_eplan.Add(bean.to, bean.from); } KUtil.SetConfigValue(KConfigure.PART_SYNC_TC_TO_EPLAN_SECTION, tc_to_eplan); } public void LoadConfiguration() { this.loadedType = KUtil.GetConfigValue(KConfigure.PART_SYNC_SECTION, KConfigure.PART_SYNC_TC_ITEM_TYPE); tb_ObjectType.Text = this.loadedType; tb_OrderNr_Prop.Text = KUtil.GetConfigValue(KConfigure.PART_SYNC_SECTION, KConfigure.PART_SYNC_OrderNr_TC); //tb_ItemID_Prop.Text = KUtil.GetConfigValue(KConfigure.PART_SYNC_SECTION, KConfigure.PART_SYNC_ItemID_EPlan); //eplan to tc Dictionary eplan_to_tc = KUtil.GetConfigValue(KConfigure.PART_SYNC_EPLAN_TO_TC_SECTION); foreach(string key in eplan_to_tc.Keys) { PartSyncConfigBean bean = new PartSyncConfigBean() { from=eplan_to_tc[key],to = key }; eplan_to_tc_config.Add(bean); } //tc to eplan Dictionary tc_to_eplan = KUtil.GetConfigValue(KConfigure.PART_SYNC_TC_TO_EPLAN_SECTION); foreach(string key in tc_to_eplan.Keys) { PartSyncConfigBean bean = new PartSyncConfigBean() { from = tc_to_eplan[key], to = key }; tc_to_eplan_config.Add(bean); } } private void Find_EPlan_Prop_Button_Click(object sender, RoutedEventArgs e) { Find_EPlan_Props(); } private void Tb_EPlanProp_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Return) { Find_EPlan_Props(); } } private void Find_TC_PropButton_Click(object sender, RoutedEventArgs e) { Find_TC_Props(); } private void Tb_TCProp_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Return) { Find_TC_Props(); } } private void ChangeType_Button_Click(object sender, RoutedEventArgs e) { try { ChangeType(); } catch (System.Exception ex) { KUtil.LogErr(ex); MessageBox.Show("加载数据出错:" + ex.Message); } } private void Tb_ObjectType_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Return) { try { ChangeType(); } catch (System.Exception ex) { KUtil.LogErr(ex); MessageBox.Show("加载数据出错:" + ex.Message); } } } private void Cb_Location_SelectionChanged(object sender, SelectionChangedEventArgs e) { Find_TC_Props(); } private void PropTable_SelectionChanged(object sender, SelectionChangedEventArgs e) { Read_Prop_Refection_to_Textbox(); } private void EPLAN_TO_TC_ADD_Button_Click(object sender, RoutedEventArgs e) { Add_Config(table_EPLAN_TO_TC, eplan_to_tc_config, tb_EPLAN_TO_TC_eplan.Text, tb_EPLAN_TO_TC_tc.Text); } private void TC_TO_EPLAN_ADD_Button_Click(object sender, RoutedEventArgs e) { Add_Config(table_TC_TO_EPLAN, tc_to_eplan_config, tb_TC_TO_EPLAN_tc.Text, tb_TC_TO_EPLAN_eplan.Text); } private void EPLAN_TO_TC_REMOVE_Button_Click(object sender, RoutedEventArgs e) { Remove_Config(table_EPLAN_TO_TC, eplan_to_tc_config); } private void TC_TO_EPLAN_REMOVE_Button_Click(object sender, RoutedEventArgs e) { Remove_Config(table_TC_TO_EPLAN, tc_to_eplan_config); } private void EPLAN_TO_TC_MODIFY_Button_Click(object sender, RoutedEventArgs e) { Modify_Config(table_EPLAN_TO_TC, eplan_to_tc_config, tb_EPLAN_TO_TC_eplan.Text, tb_EPLAN_TO_TC_tc.Text); } private void TC_TO_EPLAN_MODIFY_Button_Click(object sender, RoutedEventArgs e) { Modify_Config(table_TC_TO_EPLAN, tc_to_eplan_config, tb_TC_TO_EPLAN_tc.Text, tb_TC_TO_EPLAN_eplan.Text); } private void Clear_All_Button_Click(object sender, RoutedEventArgs e) { MessageBoxResult res = MessageBox.Show("确认清空?", "", MessageBoxButton.YesNo); if (res != MessageBoxResult.Yes) { return; } ClearPropConfig(); } private void Add_All_Button_Click(object sender, RoutedEventArgs e) { Add_Config(table_EPLAN_TO_TC, eplan_to_tc_config, tb_EPLAN_TO_TC_eplan.Text, tb_EPLAN_TO_TC_tc.Text); Add_Config(table_TC_TO_EPLAN, tc_to_eplan_config, tb_TC_TO_EPLAN_tc.Text, tb_TC_TO_EPLAN_eplan.Text); } private void COL_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { EPlanPropBean eplanBean = table_Eplan_Prop.SelectedItem as EPlanPropBean; ComboBox cb = e.Source as ComboBox; if (eplanBean == null||cb==null) { return; } eplanBean.language = cb.SelectedItem as string; } } }