using DevExpress.XtraBars.ToolbarForm; using DevExpress.XtraEditors; using DevExpress.XtraEditors.DXErrorProvider; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data; using System.Text.RegularExpressions; using System.Windows.Forms; using TsSFCDeivceClient.Common; using TsSFCDeivceClient.Email; using TsSFCDeivceClient.Model; using TsSFCDeivceClient.Model.Common; using TsSFCDeivceClient.Model.DeviceWarrantyRequest; namespace TsSFCDeivceClient { public partial class DowntimeFormAdd : ToolbarForm { MailKitHelp mail = new MailKitHelp(); string emailPattern = @"^(?("")("".+?(? lst = apiResponseData.ToDeserializeObject>(); ddlWhereFailureOccurred.Properties.DataSource = lst; ddlWhereFailureOccurred.Properties.DisplayMember = "FieldText"; ddlWhereFailureOccurred.Properties.ValueMember = "AutoID"; ddlWhereFailureOccurred.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup; if (ddlWhereFailureOccurred.Properties.DataSource != null && (ddlWhereFailureOccurred.Properties.DataSource as List).Count > 0) { ddlWhereFailureOccurred.EditValue = (ddlWhereFailureOccurred.Properties.DataSource as List)[0].AutoID; } } bool InitializeMail() { try { if (mail == null) { mail = new MailKitHelp(); } //发件服务器设置 // mail.medtronic.com mail.SmtpAddress = m_Config.AppSettings.Settings["SmtpServer"].Value.Trim(); // false mail.IsSSL = bool.Parse(m_Config.AppSettings.Settings["SmtpSSL"].Value.Trim()); // 25 mail.SmtpPort = int.Parse(m_Config.AppSettings.Settings["SmtpPort"].Value.Trim()); //是否启用无密码模式 if (bool.Parse(m_Config.AppSettings.Settings["EmailNoPass"].Value.Trim())) { mail.IsUseDefaultCredentials = true; } else mail.PassWord = m_Config.AppSettings.Settings["EmailPassWord"].Value.Trim(); mail.FromMailAddress = m_Config.AppSettings.Settings["EmailAddress"].Value.Trim(); return true; } catch (Exception) { return false; } } /// /// 故障发生时是否有产品在加工/测试在进行 - 是否开启批次号输入控件 /// /// /// private void rg_NeedValidate_EditValueChanged(object sender, EventArgs e) { try { txtBatch.Enabled = InProduction; if (!InProduction) txtBatch.EditValue = "N/A"; } catch (Exception ex) { XtraMessageBoxHelper.Error(ex.Message); } } /// /// 选择设备信息 /// /// /// private void bbtnDevice_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { try { CurrentDeviceInfo = null; using (pageDeivceView view = new pageDeivceView()) { if (view.ShowDialog() == DialogResult.OK) { CurrentDeviceInfo = view.CurrentDeviceInfo; bbtnDevice.EditValue = view.CurrentDeviceInfo.EquipmentName; } } } catch (Exception ex) { XtraMessageBoxHelper.Error(ex.Message); } } /// /// 初始化验证控件 /// private void InitializeValidationProvider() { try { List param = new List { textEdit2, bbtnDevice, ddlWhereFailureOccurred, memoEdit1 }; foreach (var item in param) { dxValidationProvider1.RemoveControlError(item); } dxValidationProvider1.RemoveControlError(txtBatch); dxValidationProvider1 = new DXValidationProvider(); //实例化一个必填规则,错误提示为:该字段不能为空 ConditionValidationRule required = new ConditionValidationRule("RequiredRule", ConditionOperator.IsNotBlank) { ErrorText = $"字段不能为空" }; if (InProduction) { param.Add(txtBatch); } for (int i = 0; i < param.Count; i++) { dxValidationProvider1.SetValidationRule(param[i], required); dxValidationProvider1.SetIconAlignment(param[i], ErrorIconAlignment.MiddleRight); } } catch (Exception ex) { XtraMessageBoxHelper.Error(ex.Message); } } /// /// 提交 /// /// /// private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { try { InitializeValidationProvider(); if (dxValidationProvider1.Validate()) { string cBatch = txtBatch.EditValue + ""; // 有在生产的批次 DataTable dtRtn = null; if (InProduction) { JObject js = JObject.Parse(Runtime.inParams); js.Add("Batch", cBatch); APIResponseData apiResponseData2 = Biz.HttpHelper.Instance.Post($"{ServiceUrl}{DeviceApiUrlConstValue.GetBatchInfoToStaff}", JsonConvert.SerializeObject(new { LoginCode = Runtime.CurrentUser.UserCode, Password = Runtime.CurrentUser.Password, inParams = js.ToString() })); if (!apiResponseData2.IsSuccess) throw new Exception(apiResponseData2.Message); dtRtn = apiResponseData2.ToDeserializeObject(); } DeviceWarrantyRequestForm deviceWarrantyRequestForm = new DeviceWarrantyRequestForm { EquipmentPK = CurrentDeviceInfo.AutoID, EquipmentID = CurrentDeviceInfo.EquipmentID, EquipmentName = CurrentDeviceInfo.EquipmentName, Location = WhereFailureOccurred, LocationName = ddlWhereFailureOccurred.Text?.Trim(), InProduction = InProduction, Batch = cBatch, FaultSymptoms = memoEdit1.EditValue + "", IsDown = IsDown, ReceivingDep = "设备设施部" }; APIResponseData apiResponseData = Biz.HttpHelper.Instance.Post($"{ServiceUrl}{DeviceApiUrlConstValue.DeviceDownFormAdd}", deviceWarrantyRequestForm.toJson()); if (!apiResponseData.IsSuccess) throw new Exception(apiResponseData.Message); int MaintenanceAutoID = Convert.ToInt32(apiResponseData.Data); try { List EmailList = new List(); if (dtRtn != null && dtRtn.Rows.Count > 0 && dtRtn.Columns.Contains("Product") && dtRtn.Columns.Contains("Technology")) { string Product = dtRtn.Rows[0]["Product"] + ""; string Technology = dtRtn.Rows[0]["Technology"] + ""; JObject js = JObject.Parse(Runtime.inParams); js.Add("Product", Product); js.Add("Technology", Technology); apiResponseData = Biz.HttpHelper.Instance.Post($"{ServiceUrl}{DeviceApiUrlConstValue.ProductResourceAllocationsGet}", JsonConvert.SerializeObject(new { LoginCode = Runtime.CurrentUser.UserCode, Password = Runtime.CurrentUser.Password, inParams = js.ToString() })); if (!apiResponseData.IsSuccess) throw new Exception(apiResponseData.Message); DataTable staffResourceAllocations = apiResponseData.ToDeserializeObject(); //IList staffResourceAllocations = p_InspRespos.ProductResourceAllocationsGet(Product, Technology); if (staffResourceAllocations != null && (staffResourceAllocations.Rows?.Count ?? 0) > 0) { foreach (DataRow item in staffResourceAllocations.Rows) { if ((item["Post"]?.ToString() ?? "-1") == "0" || (item["Post"]?.ToString() ?? "-1") == "1") { string eml = item["EMail"]?.ToString() ?? ""; if (!string.IsNullOrWhiteSpace(eml) && Regex.IsMatch(eml, emailPattern)) { EmailList.Add(eml); } } } } } if (EmailList.Count > 0 && InitializeMail()) { mail.Title = $"有在生产的设备出现故障,请您尽快评估故障状态!"; mail.IsBodyHtml = true; System.Text.StringBuilder builder = new System.Text.StringBuilder(); builder.AppendLine($"

在 {ddlWhereFailureOccurred.Text} 发起的编号为:{MaintenanceAutoID} 的保修单,设备 {CurrentDeviceInfo.EquipmentName}({CurrentDeviceInfo.EquipmentID}) 发生故障,{(deviceWarrantyRequestForm.InProduction ? "存有产品在加工中,产品批次号:" + deviceWarrantyRequestForm.Batch : "")} ,请您尽快评估故障情况。

"); builder.AppendLine($"

发起人:生产部 - {Runtime.CurrentUser.UserName}

"); mail.Body = builder.ToString(); string msgResult = ""; //收件人 mail.ToMailAddress = EmailList.ToArray(); MailKitHelp.SendStatus ss = mail.Send(out msgResult); } } catch (Exception ex) { XtraMessageBoxHelper.Warn($"新增数据成功,邮件发送失败,失败原因:{ex.Message}"); } DialogResult = DialogResult.OK; } } catch (Exception ex) { XtraMessageBoxHelper.Error(ex.Message); } } private void txtBatch_Properties_KeyPress(object sender, KeyPressEventArgs e) { //增加了自动切换大写,控制只能输入大写字母、数字、回退按钮、复制快捷键(复制的数据没有控制格式) e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper()); } } }