tangxu
2024-03-26 edd23f115dba31d764fdaf75a6207d888d0419d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using DevExpress.XtraEditors; 
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text; 
using System.Windows.Forms;
 
namespace IStation.WinFrmUI.CalcErQu
{
    public partial class SetPrjItemSpanTimeGrid : XtraUserControl
    {
        public SetPrjItemSpanTimeGrid()
        {
            InitializeComponent();
            this.bandedGridView1.RowHeight = 35;
            this.bandedGridView1.InitNewRow += new DevExpress.XtraGrid.Views.Grid.InitNewRowEventHandler(this.gridView1_InitNewRow);
   
         
        }
        public Action<string, List<CalcModel.PumpRunRange>> OnRefreshData = null;
 
        private DateTime _startTime,_endTime;
        internal void SetAnaDay(DateTime rangeStartTime, DateTime rangeEndTime)
        {
            _startTime = rangeStartTime;
            _endTime = rangeEndTime;
        }
        internal void SetBindingData( CalcModel.AnaPrj anaPrj)
        {
            if (anaPrj == null)
                return;
 
            _startTime = anaPrj.StartTime;
            _endTime = anaPrj.EndTime;
 
            if (_bindList == null)
                _bindList = new BindingList<CurrentViewModel>();
            else
                _bindList.Clear();
 
            for (int i = 0; i < anaPrj.BlockTimes.Count(); i++)
            {
                _bindList.Add(new CurrentViewModel(
                    anaPrj.BlockTimes[i].OpenPumpCount,
                    anaPrj.BlockTimes[i].StartTime,
                    anaPrj.BlockTimes[i].EndTime)); 
            }
  
 
            this.bindingSource1.DataSource = _bindList;
            this.bindingSource1.ResetBindings(false);
        }
        internal void SetBindingData(List<CalcModel.PumpRunRange> list)
        {
            if (_bindList == null)
                _bindList = new BindingList<CurrentViewModel>();
            else
                _bindList.Clear();
 
            foreach (var item in list)
            {
                var vm = new CurrentViewModel();
                vm.PumpNumber = item.PumpNumber;
 
 
                vm.StartH = item.StartTime.Hour;
                vm.StartM = item.StartTime.Minute;
                vm.EndH = item.EndTime.Hour;
                vm.EndM = item.EndTime.Minute;
 
                vm.StartDay = item.StartTime;
                vm.EndDay = item.EndTime;
 
                _bindList.Add(vm);
            }
            this.bindingSource1.DataSource = _bindList;
        }
 
        public class CurrentViewModel 
        {
            public CurrentViewModel() { }
            public CurrentViewModel(int pumpNumber, DateTime start, DateTime end)
            {
                PumpNumber = pumpNumber;
                StartDay = start; 
                EndDay = end;
                StartH = start.Hour;
                StartM = start.Minute;
                EndH = end.Hour;
                EndM = end.Minute;
            }
            public int PumpNumber { get; set; }
            public DateTime StartDay { get; set; }
            public DateTime EndDay { get; set; }
            public int? StartH { get; set; }
            public int? StartM { get; set; } = 0;
            public int? EndH { get; set; }
            public int? EndM { get; set; } = 0;
 
            public double Duration
            {
                get
                {
                   return  Math.Round((EndDay - StartDay).TotalHours, 1);
                }
            } 
        }
        private BindingList<CurrentViewModel> _bindList;
        private void AddManuPrjDlg_Load(object sender, EventArgs e)
        {
            if(_bindList == null)
            {
                _bindList = new BindingList<CurrentViewModel>();
                this.bindingSource1.DataSource = _bindList;
                this.bandedGridView1.RowHeight = 35;
            }
 
 
         
        }
 
        public List<CalcModel.PumpRunRange> GetPumpRunRange()
        {
            if (_bindList.Count == 0)
            {
                return null;
            }
            var vm_list = new List<CalcModel.PumpRunRange>();
            foreach (var m in _bindList)
            {
                if (m.PumpNumber <= 0)
                    continue;
 
                if (m.StartH == null && m.EndH == null)
                {
                    continue;
                }
 
                CalcModel.PumpRunRange v = new CalcModel.PumpRunRange();
                v.PumpNumber = m.PumpNumber;
                int StartH = m.StartH == null ? _startTime.Hour : m.StartH.Value;
                int StartM = m.StartM == null ? 0 : m.StartM.Value;
                v.StartTime = new DateTime(m.StartDay.Year, m.StartDay.Month, m.StartDay.Day, StartH, StartM, 0);
                int EndH = m.EndH == null ? _endTime.Hour : m.EndH.Value;
                int EndM = m.EndM == null ? 0 : m.EndM.Value;
                if (EndH >= 24)
                {
                    v.EndTime = new DateTime(m.EndDay.Year, m.EndDay.Month, m.EndDay.Day);
                    v.EndTime = v.EndTime.AddDays(1);
                }
                else
                {
                    v.EndTime = new DateTime(m.EndDay.Year, m.EndDay.Month, m.EndDay.Day, EndH, EndM, 0);
                }
                if (v.StartTime > v.EndTime)
                {
                    continue;
                }
                vm_list.Add(v);
            }
 
            return vm_list;
        }
 
        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            bandedGridView1.SetRowCellValue(e.RowHandle, colStartDay, this._startTime.Date);
            bandedGridView1.SetRowCellValue(e.RowHandle, colEndDay, this._startTime.Date);
            bandedGridView1.SetRowCellValue(e.RowHandle, colStartH, this._startTime.Hour);
            bandedGridView1.SetRowCellValue(e.RowHandle, colEndH, this._startTime.Hour);
        }
 
      
 
 
 
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemImageComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var row =
                 this.bandedGridView1.FocusedRowObject as IStation.WinFrmUI.CalcErQu.SetPrjItemSpanTimeGrid.CurrentViewModel;
            if (row == null)
                return;
            var sel_combox = sender as DevExpress.XtraEditors.ImageComboBoxEdit;
            row.PumpNumber = Convert.ToInt32(sel_combox.EditValue);
            if (OnRefreshRangePumpCount != null)
            {
                OnRefreshRangePumpCount(row.StartDay, row.EndDay, row.PumpNumber);
            } 
        }
 
 
 
        public Action<DateTime, DateTime, int> OnRefreshRangePumpCount;
 
    }
}