using DevExpress.Utils.Colors;
|
using DevExpress.Utils.Extensions;
|
using DevExpress.XtraEditors;
|
using IStation.BLL;
|
using IStation.Model;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Linq;
|
using System.Windows.Forms;
|
|
namespace IStation.WinFrmUI.Basic
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public partial class ElecPriceMgrCtrl : XtraUserControl
|
{
|
public ElecPriceMgrCtrl()
|
{
|
InitializeComponent();
|
this.gridView1.SetNormalView();
|
this.gridView1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
|
this.gridView1.OptionsView.AllowCellMerge = true;
|
this.gridView1.ActiveFilterEnabled = false;
|
this.gridView1.ShowViewCaption();
|
this.layoutControl1.SetupLayoutControl();
|
}
|
|
public class CurrentViewModel : Model.ElecPriceHourSetting
|
{
|
public CurrentViewModel()
|
{ }
|
|
public CurrentViewModel(Model.ElecPriceHourSetting rhs, string belongName) : base(rhs)
|
{
|
this.BelongName = belongName;
|
}
|
|
public string BelongName { get; set; }
|
|
public int StartMonth { get; set; }
|
|
public int EndMonth { get; set; }
|
}
|
|
private BindingList<CurrentViewModel> _allBindingList = null;//所有绑定列表
|
|
private Model.ElecPrice _elecPrice = null;
|
|
/// <summary>
|
/// 绑定数据
|
/// </summary>
|
public void Clear()
|
{
|
_allBindingList = new BindingList<CurrentViewModel>();
|
this.currentViewModelBindingSource.DataSource = _allBindingList;
|
}
|
|
/// <summary>
|
/// 绑定数据
|
/// </summary>
|
public void SetBindingData()
|
{
|
WaitFrmHelper.ShowWaitForm("正在加载数据...");
|
_allBindingList = new BindingList<CurrentViewModel>();
|
var bll = new BLL.ElecPrice();
|
_elecPrice = bll.GetAll()?.FirstOrDefault();
|
|
if (_elecPrice != null)
|
{
|
this.gridView1.ViewCaption = _elecPrice.Name;
|
|
if (_elecPrice.Settings != null && _elecPrice.Settings.MonthList != null)
|
{
|
var months = _elecPrice.Settings.MonthList.OrderBy(x => x.StartMonth);
|
foreach (var month in months)
|
{
|
if (month.HourList == null)
|
continue;
|
var belongName = $"{month.StartMonth}~{month.EndMonth}(月)";
|
var hours = month.HourList.OrderBy(x => x.StartHour);
|
foreach (var hour in hours)
|
{
|
var vm = new CurrentViewModel(hour, belongName);
|
vm.StartMonth = month.StartMonth;
|
vm.EndMonth = month.EndMonth;
|
_allBindingList.Add(vm);
|
}
|
}
|
}
|
else
|
{
|
_elecPrice.Settings = new ElecPriceSetting();
|
_elecPrice.Settings.MonthList = new List<ElecPriceMonthSetting> { new ElecPriceMonthSetting() };
|
}
|
}
|
this.currentViewModelBindingSource.DataSource = _allBindingList;
|
this.gridView1.BestFitColumns();
|
WaitFrmHelper.HideWaitForm();
|
}
|
|
//添加电费
|
public void AddElePrice()
|
{
|
var dlg = new AddElectricDlg();
|
dlg.SetBindingData();
|
dlg.ReloadDataEvent += (price) =>
|
{
|
var bll = new BLL.ElecPrice();
|
_elecPrice.Settings.MonthList.Add(price);
|
var bol = bll.Update(_elecPrice);
|
if (bol)
|
{
|
this.SetBindingData();
|
this.currentViewModelBindingSource.ResetBindings(false);
|
return true;
|
}
|
return false;
|
};
|
dlg.ShowDialog();
|
}
|
|
// var oldbelongName= _allBindingList.Where(e=>e.BelongName==belongName).FirstOrDefault();
|
//if (oldbelongName == null)
|
|
//编辑电费
|
public void EditElePrice()
|
{
|
var row = gridView1.GetCurrentViewModel(_allBindingList);
|
if (row == null)
|
return;
|
var editTime = _elecPrice.Settings; //提问:这句话点不到hourlist new一个monthsetting就能.到hourlist
|
var FindMonth = editTime.MonthList.Find(x => x.StartMonth == row.StartMonth && x.EndMonth == row.EndMonth);
|
var addHour = _elecPrice.Settings.MonthList.IndexOf(FindMonth);
|
var editHour = editTime.MonthList[addHour];
|
var dlg = new EditElectricDlg();
|
dlg.SetBindingData(editHour.HourList);
|
dlg.ReLoadDataevent += (price) =>
|
{
|
editHour.HourList.Clear();
|
editHour.HourList = price;
|
var bll = new BLL.ElecPrice();
|
var isok = bll.Update(_elecPrice);
|
if (isok)
|
{
|
this.currentViewModelBindingSource.ResetBindings(false);
|
this.SetBindingData();
|
return true;
|
}
|
return false;
|
};
|
dlg.ShowDialog();
|
}
|
|
/// <summary>
|
/// 删除电费
|
/// </summary>
|
public void DeletePrice()
|
{
|
var row = this.gridView1.GetCurrentViewModel(_allBindingList);
|
if (row == null)
|
return;
|
if (XtraMessageBox.Show("确定要删除所选中?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) != DialogResult.OK)
|
return;
|
|
_elecPrice.Settings.MonthList.RemoveAll(x => x.StartMonth == row.StartMonth && x.EndMonth == row.EndMonth);
|
|
var isok = new BLL.ElecPrice().Update(_elecPrice);
|
if (isok)
|
{
|
this.currentViewModelBindingSource.ResetBindings(false);
|
}
|
this.SetBindingData();
|
this.currentViewModelBindingSource.ResetBindings(false);
|
MessageBox.Show("删除成功", "提示");
|
}
|
}
|
}
|