DeviceManager/TsSFCDeivceClient/DowntimeFormAdd.cs
2024-06-03 00:38:52 +08:00

292 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DevExpress.XtraBars.ToolbarForm;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using DeviceRepair.Models;
using DeviceRepair.Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TsSFCDeivceClient.Common;
using TsSFCDeivceClient.Email;
namespace TsSFCDeivceClient
{
public partial class DowntimeFormAdd : ToolbarForm
{
MailKitHelp mail = new MailKitHelp();
public DeviceInformationInfo CurrentDeviceInfo = null;
bool InProduction
{
get { return Convert.ToBoolean(rg_NeedValidate.EditValue); }
}
bool IsDown
{
get { return Convert.ToBoolean(rg_IsDown.EditValue); }
}
int WhereFailureOccurred
{
get { return Convert.ToInt32(ddlWhereFailureOccurred.EditValue); }
}
public DowntimeFormAdd()
{
InitializeComponent();
this.Load += DowntimeFormAdd_Load;
}
private void DowntimeFormAdd_Load(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(RunConfig.config.ServiceApiUrl))
{
XtraMessageBoxHelper.Error($"缺少配置字段设备管理软件接口地址【ServiceApiUrl】。");
this.Close();
}
APIResponseData apiResponseData = Biz.HttpHelper.Instance.Get($"{RunConfig.config.ServiceApiUrl}{DeviceApiUrlConstValue.GetWhereFailureOccurred}");
if (!apiResponseData.IsSuccess)
throw new Exception(apiResponseData.Message);
List<FieldsInfo> lst = apiResponseData.ToDeserializeObject<List<FieldsInfo>>();
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<FieldsInfo>).Count > 0)
{
ddlWhereFailureOccurred.EditValue = (ddlWhereFailureOccurred.Properties.DataSource as List<FieldsInfo>)[0].AutoID;
}
}
bool InitializeMail()
{
try
{
if (mail == null)
{
mail = new MailKitHelp();
}
//发件服务器设置
APIResponseData apiRtn = Biz.HttpHelper.Instance.Get($"{RunConfig.config.ServiceApiUrl}{DeviceApiUrlConstValue.SysEmailConfig}?ModuleCode=DeviceWarranty");
if (!apiRtn.IsSuccess)
throw new Exception(apiRtn.Message);
SysEmailConfigInfo config = apiRtn.ToDeserializeObject<SysEmailConfigInfo>();
mail.FromMailAddress = config.EmailAddress;
mail.SmtpAddress = config.SmtpServer;
mail.IsSSL = config.SmtpSSL;
mail.SmtpPort = config.SmtpPort;
if (!config.EmailNoPass)
{
mail.PassWord = DESEncrypt.Decrypt(config.EmailPassWord);
}
else
{
mail.IsUseDefaultCredentials = true;
}
//// 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;
}
}
/// <summary>
/// 故障发生时是否有产品在加工/测试在进行 - 是否开启批次号输入控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void rg_NeedValidate_EditValueChanged(object sender, EventArgs e)
{
try
{
txtBatch.Enabled = InProduction;
if (!InProduction)
txtBatch.EditValue = "N/A";
else
txtBatch.EditValue = "";
}
catch (Exception ex)
{
XtraMessageBoxHelper.Error(ex.Message);
}
}
/// <summary>
/// 选择设备信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
/// <summary>
/// 初始化验证控件
/// </summary>
private void InitializeValidationProvider()
{
try
{
List<BaseEdit> param = new List<BaseEdit> { 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);
}
}
/// <summary>
/// 提交
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
try
{
InitializeValidationProvider();
if (dxValidationProvider1.Validate())
{
string cBatch = txtBatch.EditValue + "";
List<string> emailLst = new List<string>();
if (InProduction)
{
APIResponseData apiRtn = Biz.HttpHelper.Instance.Get($"{RunConfig.config.ServiceApiUrl}{DeviceApiUrlConstValue.GetBatchInfoToStaff}?Batch={cBatch}");
if (!apiRtn.IsSuccess)
throw new Exception(apiRtn.Message);
if (apiRtn.ToInt() == 0)
throw new Exception("当前批次不存在!");
apiRtn = Biz.HttpHelper.Instance.Get($"{RunConfig.config.ServiceApiUrl}{DeviceApiUrlConstValue.CurrentBatchManagerEmail}?Batch={cBatch}");
if (!apiRtn.IsSuccess)
throw new Exception(apiRtn.Message);
emailLst.AddRange(apiRtn.ToDeserializeObject<List<string>>());
}
DeviceWarrantyRequestFormView deviceWarrantyRequestForm = new DeviceWarrantyRequestFormView
{
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($"{RunConfig.config.ServiceApiUrl}{DeviceApiUrlConstValue.DeviceDownFormAdd}", JsonConvert.SerializeObject(deviceWarrantyRequestForm));
if (!apiResponseData.IsSuccess)
throw new Exception(apiResponseData.Message);
int MaintenanceAutoID = Convert.ToInt32(apiResponseData.Data);
try
{
if (emailLst.Count > 0 && InitializeMail())
{
mail.Title = $"有在生产的设备出现故障,请您尽快评估故障状态!";
mail.IsBodyHtml = true;
System.Text.StringBuilder builder = new System.Text.StringBuilder();
builder.AppendLine($"<p>在 {ddlWhereFailureOccurred.Text} 发起的编号为:{MaintenanceAutoID} 的保修单,设备 {CurrentDeviceInfo.EquipmentName}{CurrentDeviceInfo.EquipmentID} 发生故障,{(deviceWarrantyRequestForm.InProduction ? "" + deviceWarrantyRequestForm.Batch : "")} ,请您尽快评估故障情况。</p>");
builder.AppendLine($"<p>发起人:生产部 - {Runtime.CurrentUser.UserName}</p>");
mail.Body = builder.ToString();
string msgResult = "";
//收件人
mail.ToMailAddress = emailLst.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());
}
}
}