using System; using System.Collections.Generic; using System.Linq; using DevExpress.XtraEditors; using DevExpress.XtraBars.ToolbarForm; using DeviceRepairAndOptimization.Models; using System.Reflection; using SqlSugar; using DeviceRepairAndOptimization.Common; namespace DeviceRepairAndOptimization.Pages.RepairView { public partial class page_RepairRecordEdit : ToolbarForm { private DeviceRepairRecordModel CurrentData; Dictionary ControlDictionary = null; public page_RepairRecordEdit(DeviceRepairRecordModel model) { InitializeComponent(); CurrentData = model; this.Load += Page_RepairRecordEdit_Load; } private void Page_RepairRecordEdit_Load(object sender, EventArgs e) { #region 初始化固定值 ControlDictionary = new Dictionary { {"Plant",txt_Plant}, {"Installations",txt_Installations}, {"PurchaseOrder",txt_PurchaseOrder}, {"OrderType",txt_OrderType}, {"TypesOfMaintenanceOperations",txt_TypesOfMaintenanceOperations}, {"FullProgramCosts",txt_FullProgramCosts}, {"Acttotcost",txt_Acttotcost}, {"SystemStatus",txt_SystemStatus}, {"TestLot",txt_TestLot}, {"Tin",txt_Tin}, {"DescriptionOfTechnicalObjects",txt_DescriptionOfTechnicalObjects}, {"Descriptive",txt_Descriptive}, {"Importer",txt_Importer}, {"ActualDateOfIssuance",de_ActualDateOfIssuance}, {"BasicStartTime",te_BasicStartTime}, {"ReferenceDate",de_ReferenceDate}, {"ReferenceTime",te_ReferenceTime}, {"FinalModifier",txt_FinalModifier}, {"ReasonForDowntime",txt_ReasonForDowntime}, {"MaintenancePersonnel",txt_MaintenancePersonnel}, {"StartOfMaintenance",de_StartOfMaintenance}, {"MaintenanceCompletionTime",te_MaintenanceCompletionTime}, {"CauseOfFailure",txt_CauseOfFailure}, {"MaintenanceContent",txt_MaintenanceContent}, {"ReplacementParts",txt_ReplacementParts}, {"Downtime",txt_Downtime}, {"MaintenancePeriod",txt_MaintenancePeriod}, {"RepairMethods",txt_RepairMethods}, {"PhenomenologicalDistinction",txt_PhenomenologicalDistinction}, {"VerificationNumber",txt_VerificationNumber}, {"OtherThan",txt_OtherThan}, {"DeviceStatus",txt_DeviceStatus}, {"Tier",txt_Tier}, {"DidTheFaultCauseAShutdown",txt_DidTheFaultCauseAShutdown}, {"TierTrackers",txt_TierTrackers}, {"StatusFeedbackAndTime",txt_StatusFeedbackAndTime}, {"PartsAndServiceRequisitionTime",txt_PartsAndServiceRequisitionTime}, {"PartsAndServiceArrivalTime",txt_PartsAndServiceArrivalTime}, {"PmAndAmAssociatedProjectLocation",txt_PmAndAmAssociatedProjectLocation}, }; #endregion InitializeData(); } /// /// 加载数据 /// void InitializeData() { try { splashScreenManager1.ShowWaitForm(); if (CurrentData == null) throw new Exception("编辑的行数据不能为空!"); // 绑定数据到控件 ObjectBindControl(CurrentData, ControlDictionary); splashScreenManager1.TryCloseWait(); } catch (Exception ex) { splashScreenManager1.TryCloseWait(); XtraMessageBoxHelper.Error(ex.Message); } } /// /// 保存 /// /// /// private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { try { //splashScreenManager1.ShowWaitForm(); //// 数据校验 //if (ValidataForm()) //{ // APIResponseData apiResponseData = DeviceRepairManager.Instance.Editable(CurrentData); // if (apiResponseData.Code != 1) // throw new Exception(apiResponseData.Message); // else // { // splashScreenManager1.TryCloseWait(); // DialogResult = System.Windows.Forms.DialogResult.OK; // } //} //splashScreenManager1.TryCloseWait(); } catch (Exception ex) { splashScreenManager1.TryCloseWait(); XtraMessageBoxHelper.Error(ex.Message); } } /// /// 数据对象校验 /// /// bool ValidataForm() { try { //int ErrorCount = 0; //DeviceRepairRecordModel valModel = new DeviceRepairRecordModel(); //Type personType = valModel.GetType(); //PropertyInfo[] properties = personType.GetProperties(); //foreach (var item in ControlDictionary) //{ // PropertyInfo property = properties.FirstOrDefault(x => x.Name.Equals(item.Key, StringComparison.CurrentCultureIgnoreCase)); // if (property != null) // { // property.SetValue(valModel, item.Value.EditValue); // } //} //APIResponseData apiResponseData = CommonManager.Instance.GetColumnInfo("DriveRepairRecord"); //if (apiResponseData.Code != 1) // throw new Exception(apiResponseData.Message); //List columns = apiResponseData.ToDeserializeObject>(); //Dictionary dColumns = columns.ToDictionary(x => x.DbColumnName, x => x); //foreach (PropertyInfo property in properties) //{ // if (!dColumns.ContainsKey(property.Name)) // { // throw new Exception($"{property.Name}字段,不存在表中!"); // } // else // { // DbColumnInfo colINfo = dColumns[property.Name]; // if (!ControlDictionary.ContainsKey(property.Name) || colINfo.IsPrimarykey) // continue; // object value = property.GetValue(valModel, null); // if (value == null) // { // if (colINfo.IsNullable) // continue; // dxErrorProvider1.SetError(ControlDictionary[property.Name], $"{colINfo.ColumnDescription}字段,不允许为空!"); // ErrorCount++; // } // switch (colINfo.DataType.ToLower()) // { // case "int": // int iValue = 0; // if (!int.TryParse(value + "", out iValue)) // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], $"{colINfo.ColumnDescription}字段,只能为整数!"); // ErrorCount++; // } // else // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], ""); // } // break; // case "decimal": // decimal dValue = 0; // if (!decimal.TryParse(value + "", out dValue)) // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], $"{colINfo.ColumnDescription}字段,只能为十进制数值!"); // ErrorCount++; // } // else // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], ""); // } // break; // case "datetime": // DateTime tValue = DateTime.MinValue; // if (!DateTime.TryParse(value + "", out tValue)) // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], $"{colINfo.ColumnDescription}字段,只能为时间类型!"); // ErrorCount++; // } // else // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], ""); // } // break; // case "float": // float fValue = 0; // if (!float.TryParse(value + "", out fValue)) // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], $"{colINfo.ColumnDescription}字段,只能为小数类型!"); // ErrorCount++; // } // else // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], ""); // } // break; // case "bit": // bool bValue = false; // if (!bool.TryParse(value + "", out bValue)) // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], $"{colINfo.ColumnDescription}字段,只能为是或否!"); // ErrorCount++; // } // else // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], ""); // } // break; // case "uniqueidentifier": // Guid gValue = Guid.Empty; // if (!Guid.TryParse(gValue + "", out gValue)) // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], $"{colINfo.ColumnDescription}字段,只能为Guid类型!"); // ErrorCount++; // } // else // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], ""); // } // break; // default: // if (value.ToString().Length > colINfo.Length) // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], $"{colINfo.ColumnDescription}字段,最大长度不允许超出{colINfo.Length}!"); // ErrorCount++; // } // else // { // dxErrorProvider1.SetError(ControlDictionary[property.Name], ""); // } // break; // } // } //} //if (ErrorCount == 0) //{ // valModel.AutoID = CurrentData.AutoID; // CurrentData = valModel; // return true; //} return false; } catch (Exception) { throw; } } /// /// 数据绑定到控件 /// /// /// /// void ObjectBindControl(T model, Dictionary dictionary) where T : class { Type personType = model.GetType(); PropertyInfo[] properties = personType.GetProperties(); foreach (PropertyInfo property in properties) { if (dictionary.ContainsKey(property.Name)) { object value = property.GetValue(model, null); dictionary[property.Name].EditValue = value; } } } } }