123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using DataSimulation.Forms.EditForms;
- using DataSimulation.Repostory;
- using DataSimulation.Repostory.EFContext;
- using DataSimulation.Repostory.Model;
- using DevExpress.Utils;
- using DevExpress.Utils.About;
- using DevExpress.XtraBars.Customization;
- using DevExpress.XtraEditors.ButtonsPanelControl;
- using DevExpress.XtraMap;
- using DxHelper;
- using ExtensionsDev;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Data;
- using System.Data.Entity;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement;
- namespace DataSimulation.Forms.UserControl
- {
- public partial class CtrlHome : DevExpress.XtraEditors.XtraUserControl
- {
- List<TaskInfo> taskList = new List<TaskInfo>();
- public CtrlHome()
- {
- InitializeComponent();
- gridControl.Init<TaskInfo>().UseFilter();
- layoutControl1.UseDefault();
- mapControl.UseDefalutOptions()
- .UseClearAll()
- .UseDistanceLine()
- .UseMarkDot()
- .UseExportImg()
- .UseExportXlsx()
- .UseExportCsv()
- .SetMapLayerType(null)
- .UseDrawRect(rect =>
- {
- (double startLon, double startLat, double centerLon, double centerLat, double endLon, double endLat, double lonRange, double latRange) = rect;
- }).UseFlightLine()
- ;
- }
- private async void CtrlHome_Load(object sender, EventArgs e)
- {
- try
- {
- Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart"));
- var yearDirs = Directory.EnumerateDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart")).OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name));//年目录,倒叙排列
- foreach (var yearDir in yearDirs)
- {
- //每一天的db文件,倒序排列
- var dayFiles = Directory.EnumerateFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, yearDir), "*.db").OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name.Substring(0, 4)));
- foreach (var dayFile in dayFiles)
- {
- using (SimulationPartContext db = SimulationPartContext.GetContext(dayFile))
- {
- taskList.AddRange(await db.TaskInfos.Where(w => !w.isHistory).ToListAsync());
- var runningList = await db.TaskInfos.Where(p => p.TaskState == EnumTaskState.Running && !p.isHistory).ToListAsync();
- foreach (var item in runningList)
- {
- item.TaskState = EnumTaskState.Stopped;
- }
- await db.SaveChangesAsync();
- }
- }
- }
- gridControl.DataSource = taskList;
- gridView.Columns[nameof(TaskInfo.TaskName)].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
- this.svgImageCollection1.Add("Stopped", SvgHelper.CreateCycle("#1E8B58"));
- this.svgImageCollection1.Add("Running", SvgHelper.CreateCycle("#2E8B57"));
- }
- catch (Exception)
- {
- throw;
- }
- }
- private async void layoutControlGroup_CustomButtonClick(object sender, DevExpress.XtraBars.Docking2010.BaseButtonEventArgs e)
- {
-
- var txt = (e.Button as GroupBoxButton).Caption;
- if (txt == "新建任务")
- {
- try
- {
- TaskEditor frm = new TaskEditor();
- if (frm.ShowDialog() != DialogResult.OK) return;
- var addItem = frm.info;
- // var taskListCache = await TaskCache.GetAllAsync();
- if (taskList.ToList().Any(p => p.TaskName == addItem.TaskName))
- {
- DxHelper.MsgBoxHelper.ShowWarning("任务名称重复");
- return;
- }
- using (SimulationPartContext db = SimulationPartContext.GetContext(DateTime.Now))
- {
- db.TaskInfos.Add(addItem);
- await db.SaveChangesAsync();
- }
- taskList.Insert(0, addItem);
- gridView.RefreshData();
- gridView.FocusedRowHandle = 0;
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, "保存任务异常");
- DxHelper.MsgBoxHelper.ShowError("保存任务异常");
- }
- }
- else if (txt == "编辑任务")
- {
- try
- {
- var editItem = gridView.GetFocusedRow() as TaskInfo;
- TaskEditor frm = new TaskEditor(editItem);
- if (frm.ShowDialog() != DialogResult.OK) return;
- editItem = frm.info;
- var taskListCache = await TaskCache.GetAllAsync();
- if (taskListCache.ToList().Any(p => p.TaskName == editItem.TaskName))
- {
- DxHelper.MsgBoxHelper.ShowWarning("任务名称重复");
- return;
- }
- using (SimulationPartContext db = SimulationPartContext.GetContext(editItem.CreateTime))
- {
- var find = db.TaskInfos.Where(p => p.ID == editItem.ID).ToList().FirstOrDefault();
- if (find == null) return;
- find.TaskName = editItem.TaskName;
- find.SimulationType = editItem.SimulationType;
- find.UpdateTime = DateTime.Now;
- find.Freq = editItem.Freq;
- find.Band = editItem.Band;
- find.MainSat = editItem.MainSat;
- if (find.SimulationType == EnumSimulationType.X3TwoDto)
- {
- find.Adja1Sat = editItem.Adja1Sat;
- find.Adja2Sat = editItem.Adja2Sat;
- }
- await db.SaveChangesAsync();
- }
- gridView.RefreshData();
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, "保存任务异常");
- DxHelper.MsgBoxHelper.ShowError("保存任务异常");
- }
- }
- else
- {
- try
- {
- var tsk = gridView.GetFocusedRow() as TaskInfo;
- if (tsk == null) return;
- if (!DxHelper.MsgBoxHelper.ShowConfirm($"删除任务[{tsk.TaskName}]?"))
- return;
- using (SimulationPartContext db = SimulationPartContext.GetContext(tsk.CreateTime))
- {
- var item = db.TaskInfos.FirstOrDefault(p => p.ID == tsk.ID);
- if (item == null) return;
- db.TaskInfos.Remove(item);
- await db.SaveChangesAsync();
- }
- gridView.DeleteSelectedRows();
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, "删除任务异常");
- DxHelper.MsgBoxHelper.ShowError("删除任务异常");
- }
- }
- }
- private void gridView_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
- {
- if (e.Column.FieldName == nameof(TaskInfo.Freq))
- {
- e.DisplayText = $"{(long)e.Value / 1e6}MHz";
- }
- else if (e.Column.FieldName == nameof(TaskInfo.Band))
- {
- e.DisplayText = $"{(long)e.Value / 1e6}MHz";
- }
- }
- private async void btnStart_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
- {
- var tsk = gridView.GetFocusedRow() as TaskInfo;
- if (e.Item.Caption == "启动任务")
- {
- try
- {
- if (tsk.TaskState == EnumTaskState.Running)
- {
- DxHelper.MsgBoxHelper.ShowWarning($"该任务正在执行!");
- return;
- }
- using (SimulationPartContext db = SimulationPartContext.GetContext(tsk.CreateTime))
- {
- var item = await db.TaskInfos.FirstOrDefaultAsync(p => p.ID == tsk.ID);
- item.TaskState = EnumTaskState.Running;
- item.isHistory = true;
- //gridView.DeleteSelectedRows();
- await db.SaveChangesAsync();
- Serilog.Log.Information($"用户启动了任务,ID={tsk.ID}");
- }
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, "启动任务异常");
- DxHelper.MsgBoxHelper.ShowError($"启动任务异常.{ex.Message}");
- }
- }
- }
- }
- }
|