DeviceManager/DeviceRepairAndOptimization/Pages/AM/Plan/pageAmMaintenancePlanView.cs

443 lines
16 KiB
C#
Raw Normal View History

2024-07-01 16:52:48 +00:00
using DevExpress.XtraEditors;
using DeviceRepair.Models;
using DeviceRepairAndOptimization.Biz.AM;
using DeviceRepairAndOptimization.Common;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace DeviceRepairAndOptimization.Pages.AM.Plan
{
public partial class pageAmMaintenancePlanView : FormBase
{
AnnualMaintenancePlan CurrentModel;
public int PlanYear
{
get
{
int year;
if (int.TryParse(teYear.EditValue + "", out year))
return year;
return DateTime.Today.Year;
}
}
public string FilterText
{
get
{
return txtFilter.EditValue + "";
}
}
public pageAmMaintenancePlanView()
{
InitializeComponent();
// 关闭列头右键菜单
gridView1.OptionsMenu.EnableColumnMenu = false;
gridView1.OptionsBehavior.Editable = false;
gridView1.OptionsBehavior.ReadOnly = true;
gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
gridView1.OptionsScrollAnnotations.ShowSelectedRows = DevExpress.Utils.DefaultBoolean.False;
foreach (DevExpress.XtraGrid.Columns.GridColumn item in gridView1.Columns)
{
item.OptionsColumn.AllowEdit = false;
item.OptionsColumn.AllowGroup = DevExpress.Utils.DefaultBoolean.True;
item.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
item.OptionsColumn.AllowShowHide = false;
}
gridView1.CustomDrawRowIndicator += GridView1_CustomDrawRowIndicator;
}
private void pageAmMaintenancePlanView_Load(object sender, EventArgs e)
{
try
{
splashScreenManager1.ShowWaitForm();
GridDataInitialize();
splashScreenManager1.TryCloseWait();
}
catch (Exception ex)
{
splashScreenManager1.TryCloseWait();
XtraMessageBoxHelper.Error(ex.Message);
}
}
/// <summary>
/// 自增长行号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle >= 0)
{
e.Info.DisplayText = (e.RowHandle + 1).ToString();
e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
e.Appearance.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
}
}
void GridDataInitialize()
{
try
{
// 获取维修数据
APIResponseData apiResponseData = Biz.AM.PlanManager.Instance.GetDatas(FilterText, PlanYear, "OEM");
if (!apiResponseData.IsSuccess)
throw new Exception(apiResponseData.Message);
List<AnnualMaintenancePlan> Datas = apiResponseData.ToDeserializeObject<List<AnnualMaintenancePlan>>();
gridControl1.BeginUpdate();
gridControl1.DataSource = null;
gridControl1.DataSource = Datas;
gridControl1.EndUpdate();
// 设置行号列宽度
SizeF size = this.CreateGraphics().MeasureString(Datas.Count.ToString(), this.Font);
gridView1.IndicatorWidth = Convert.ToInt32(size.Width) + 30;
// 绑定行点击事件
gridView1.RowCellClick += GridView1_RowCellClick; ;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 单元格点击事件 - 显示右键菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
{
CurrentModel = gridView1.GetRow(e.RowHandle) as AnnualMaintenancePlan;
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(gridControl1, e.Location);
return;
}
}
/// <summary>
/// 右键菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
try
{
if (CurrentModel == null)
throw new Exception("请选中要操作的设备数据行!");
(new Pages.Plan.pageDevicePlans(CurrentModel.EquipmentID, CurrentModel.MaintenanceYear)).ShowDialog(this);
}
catch (Exception ex)
{
XtraMessageBoxHelper.Error(ex.Message);
}
}
/// <summary>
/// 搜索
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtFilter_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
try
{
splashScreenManager1.ShowWaitForm();
GridDataInitialize();
splashScreenManager1.TryCloseWait();
}
catch (Exception ex)
{
splashScreenManager1.TryCloseWait();
XtraMessageBoxHelper.Error(ex.Message);
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Remove_Click(object sender, EventArgs e)
{
try
{
if (!GlobalInfo.HasRole("BIZ_PLAN_DELETE"))
{
XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
return;
}
AnnualMaintenancePlan model = (AnnualMaintenancePlan)gridView1.GetFocusedRow();
if (model == null)
{
XtraMessageBoxHelper.Error("请先选择要删除的行!");
return;
}
if (XtraMessageBoxHelper.AskYesNo("确认删除该条数据?") == DialogResult.Yes)
{
splashScreenManager1.ShowWaitForm();
APIResponseData apiResponseData = PlanManager.Instance.GetPlanRecordProgress(model.EquipmentID, model.MaintenanceYear);
if (apiResponseData.IsSuccess)
{
List<PlanProgress> progress = apiResponseData.ToDeserializeObject<List<PlanProgress>>();
if (progress != null && progress.Count > 0)
{
throw new Exception("当前计划下存在保养数据,无法删除。");
}
}
apiResponseData = PlanManager.Instance.DeleteByYearAndEquipmentPk(model.MaintenanceYear, model.EquipmentID);
if (!apiResponseData.IsSuccess)
throw new Exception(apiResponseData.Message);
GridDataInitialize();
splashScreenManager1.TryCloseWait();
XtraMessageBoxHelper.Info("操作成功!");
}
}
catch (Exception ex)
{
GridDataInitialize();
// 关闭等待窗口
splashScreenManager1.TryCloseWait();
XtraMessageBoxHelper.Error(ex.Message);
}
}
/// <summary>
/// 数据导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExport_Click(object sender, EventArgs e)
{
try
{
if (!GlobalInfo.HasRole("BIZ_PLAN_EXPORT"))
{
XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
return;
}
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog(this) == DialogResult.OK)
{
string path = dlg.SelectedPath.ToString();
try
{
splashScreenManager1.ShowWaitForm();
APIResponseData apiResponseData = PlanManager.Instance.ExportXlsxDatas(PlanYear);
if (!apiResponseData.IsSuccess)
throw new Exception(apiResponseData.Message);
List<View_YearsMaintenancePlansExport> lst = apiResponseData.ToDeserializeObject<List<View_YearsMaintenancePlansExport>>();
if (lst == null || lst.Count == 0)
throw new Exception("数据为空,无法导出。");
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet($"{PlanYear}年度OEM设备PM计划");
// 创建表头
IRow headerRow = sheet.CreateRow(0);
PropertyInfo[] properties = typeof(View_YearsMaintenancePlansExport).GetProperties();
for (int i = 0; i < properties.Length; i++)
{
DisplayNameAttribute[] attrs =
(DisplayNameAttribute[])properties[i]
.GetCustomAttributes(typeof(DisplayNameAttribute), true);
if (attrs[0].DisplayName.Equals("RootName", StringComparison.CurrentCultureIgnoreCase))
{
continue;
}
ICell cell = headerRow.CreateCell(i);
cell.SetCellValue(attrs[0].DisplayName);
}
// 填充数据
for (int rowIndex = 0; rowIndex < lst.Count; rowIndex++)
{
IRow dataRow = sheet.CreateRow(rowIndex + 1);
for (int colIndex = 0; colIndex < properties.Length; colIndex++)
{
ICell cell = dataRow.CreateCell(colIndex);
object value = properties[colIndex].GetValue(lst[rowIndex]);
cell.SetCellValue(value + "");
}
}
using (FileStream fs =
new FileStream(
Path.Combine(path,
$"OEM{PlanYear}年度设备保养计划-{DateTime.Now.ToString("yyyyMMddhhmmssfff")}.xlsx"),
FileMode.Create, FileAccess.Write))
{
workbook.Write(fs);
splashScreenManager1.TryCloseWait();
XtraMessageBoxHelper.Info("操作成功!");
}
}
catch (Exception ex)
{
throw ex;
}
}
}
catch (Exception ex)
{
splashScreenManager1.TryCloseWait();
XtraMessageBoxHelper.Error(ex.Message);
}
}
/// <summary>
/// 新增
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (!GlobalInfo.HasRole("BIZ_PLAN_ADD"))
{
throw new Exception($"当前账号缺少此操作的权限");
}
if (new pageDevicePlanEdit(new AnnualMaintenancePlan { MaintenanceYear = PlanYear }).ShowDialog(this) == DialogResult.OK)
{
XtraMessageBoxHelper.Info("操作成功!");
GridDataInitialize();
}
}
catch (Exception ex)
{
GridDataInitialize();
XtraMessageBoxHelper.Error(ex.Message);
}
}
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEdit_Click(object sender, EventArgs e)
{
try
{
if (!GlobalInfo.HasRole("BIZ_PLAN_EDIT"))
{
throw new Exception($"当前账号缺少此操作的权限");
}
if (CurrentModel == null)
{
throw new Exception($"没有选择内容");
}
if (new pageDevicePlanEdit(CurrentModel).ShowDialog(this) == DialogResult.OK)
{
XtraMessageBoxHelper.Info("操作成功!");
GridDataInitialize();
}
}
catch (Exception ex)
{
GridDataInitialize();
XtraMessageBoxHelper.Error(ex.Message);
}
}
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImport_Click(object sender, EventArgs e)
{
try
{
if (!GlobalInfo.HasRole("BIZ_PLAN_EDIT"))
{
throw new Exception($"当前账号缺少此操作的权限");
}
using (pagePlanImport view = new pagePlanImport())
{
if (view.ShowDialog(this) == DialogResult.OK)
{
XtraMessageBoxHelper.Info("操作成功!");
}
// Form view = new Form
// {
// Text = "AM 的保养计划导入",
// StartPosition = FormStartPosition.CenterScreen,
// MinimumSize = new Size(650, 225)
// };
// DevExpress.Utils.Layout.StackPanel sp = new DevExpress.Utils.Layout.StackPanel() { Dock = DockStyle.Bottom, Height = 70, Padding = new Padding(10), LayoutDirection = DevExpress.Utils.Layout.StackPanelLayoutDirection.RightToLeft };
// SidePanel sp2 = new SidePanel() { Dock = DockStyle.Fill };
// SimpleButton add = new SimpleButton { Text = "导入", Width = 120, Height = 38 };
// add.Click += (ss, ee) =>
// {
// XtraMessageBoxHelper.Info("执行导入!");
// };
// SimpleButton cancel = new SimpleButton { Text = "取消", Width = 120, Height = 38 };
// cancel.Click += (ss, ee) => view.Close();
// view.Controls.Add(sp);
// sp.Controls.Add(cancel);
// sp.Controls.Add(add);
// sp2.Controls.Add(con);
// view.Controls.Add(sp2);
// view.ShowDialog(this);
}
}
catch (Exception ex)
{
GridDataInitialize();
XtraMessageBoxHelper.Error(ex.Message);
}
}
}
}