ningshuxia
2023-02-24 1e4b358de58e36bfbf692ab2538ff9e7d60fd025
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using DevExpress.XtraEditors.Controls;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Tile;
using IStation.Extensions;
 
namespace IStation.WinFormUI.Project
{
    public partial class ProjectTileViewCtrl : DevExpress.XtraEditors.XtraUserControl
    {
        public ProjectTileViewCtrl()
        {
            InitializeComponent();
        }
 
        #region 内部视图类
 
        public class CurrentViewModel
        {
            public CurrentViewModel(Model.Project rhs)
            {
                this.Id = rhs.Id;
                this.Name = rhs.Name;
                this.IsExist = true;
                this.LastSaveTime = rhs.UpdateTime;
                if (this.IsExist)
                {
                    if (this.LastSaveTime > DateTime.Now.Date)
                        this.TimeDescription = " 今天";
                    else if (this.LastSaveTime > DateTime.Now.GetWeekFirst())
                        this.TimeDescription = " 本周";
                    else if (this.LastSaveTime > DateTime.Now.GetMonthFirst())
                        this.TimeDescription = " 本月";
                    else
                        this.TimeDescription = " 历史";
                }
                else
                {
                    this.TimeDescription = " 不存在";
                }  
                this.Model = rhs;
            }
 
            /// <summary>
            /// 标识
            /// </summary>
            public long Id { get; set; }
 
            /// <summary>
            /// 名称
            /// </summary>
            public string Name { get; set; }
 
            /// <summary>
            /// 状态
            /// </summary>
            public string Status { get; set; }
 
            /// <summary>
            /// 文件全路径
            /// </summary>
            public string FileFullName { get; set; }
 
            /// <summary>
            /// 说明
            /// </summary>
            public string Description { get; set; }
 
            /// <summary>
            /// 是否存在
            /// </summary>
            public bool IsExist { get; set; }
 
            /// <summary>
            /// 最后保存时间
            /// </summary>
            public DateTime? LastSaveTime { get; set; }
 
            /// <summary>
            /// 时间说明
            /// </summary>
            public string TimeDescription { get; set; }
 
            /// <summary>
            /// Model
            /// </summary>
            public Model.Project Model { get; set; }
 
        }
 
        #endregion
 
        /// <summary>
        /// 回调事件
        /// </summary>
        public event Action<Model.Project> ReloadDataEvent;
        private List<CurrentViewModel> _allBindList = null;//所有绑定列表
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData()
        {
            _allBindList = new List<CurrentViewModel>();
            var allProjects = new BLL.Project().GetAll();
            if (allProjects != null && allProjects.Count > 0)
                allProjects.ForEach(x => _allBindList.Add(new CurrentViewModel(x)));
            _allBindList = _allBindList.OrderByDescending(t => t.LastSaveTime).ToList();
            this.bindingSource1.DataSource = _allBindList;
            this.bindingSource1.ResetBindings(false);
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(List<Model.Project> allProjects)
        {
            _allBindList = new List<CurrentViewModel>();
            if (allProjects != null && allProjects.Count > 0)
                allProjects.ForEach(x => _allBindList.Add(new CurrentViewModel(x)));
            _allBindList = _allBindList.OrderBy(t => t.LastSaveTime).ToList();
            this.bindingSource1.DataSource = _allBindList;
            this.bindingSource1.ResetBindings(false);
        }
 
        /// <summary>
        /// 获取项目
        /// </summary>
        public Model.Project GetProject()
        {
            var row = this.tileView1.GetFocusedRow() as CurrentViewModel;
            if (row == null)
            {
                XtraMessageBox.Show("未检索到项目信息!");
                return default;
            }
            if (!File.Exists(row.FileFullName))
            {
                XtraMessageBox.Show("项目文件不存在!");
                return default;
            }
            if (row.Model.Id == GlobalParas.ProjectId)
            {
                XtraMessageBox.Show("该项目已经打开!");
                return default;
            }
            return row.Model;
        }
 
        /// <summary>
        /// 是否显示边框
        /// </summary>
        public bool BorderVisible
        {
            get
            {
                if (this.tileView1.BorderStyle == BorderStyles.NoBorder)
                    return false;
                return true;
            }
            set
            {
                if (value)
                    this.tileView1.BorderStyle = BorderStyles.Simple;
                else
                    this.tileView1.BorderStyle = BorderStyles.NoBorder;
            }
        }
 
        //显示提示信息
        private void toolTipController1_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
        {
            if (e.SelectedControl != this.gridControl1)
                return;
            var view = this.gridControl1.GetViewAt(e.ControlMousePosition) as TileView;
            if (view == null)
                return;
            var hi = view.CalcHitInfo(e.ControlMousePosition);
            ToolTipControlInfo info = null;
            if (hi.InItem)
            {
                var row = view.GetRow(hi.RowHandle) as CurrentViewModel;
                if (row != null)
                {
                    if (row.IsExist)
                    {
                        info = new ToolTipControlInfo(row, row.Description);
                    }
                    else
                    {
                        info = new ToolTipControlInfo(row, "项目不存在");
                    }
                }
            }
            if (info != null)
            {
                e.Info = info;
            }
        }
 
        //点击事件
        private void tileView1_ContextButtonClick(object sender, ContextItemClickEventArgs e)
        {
            var tileViewItem = e.DataItem as TileViewItem;
            var row = this.tileView1.GetRow(tileViewItem.RowHandle) as CurrentViewModel;
            if (row == null)
                return;
 
            //判断项目状态
            if (row.Id == GlobalParas.ProjectId)
            {
                XtraMessageBox.Show("项目已打开,不能移除!");
                return;
            }
            /*//如果不存在直接删除
            if (!File.Exists(row.FileFullName))
            {
                if (!new BLL.Project().DeleteById(row.Id))
                {
                    XtraMessageBox.Show("项目移除失败!");
                    return;
                }
                return;
            }*/
            //提示用户是否移除项目
            if (XtraMessageBox.Show("是否要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                return;
            if (!new BLL.Project().DeleteById(row.Id,out string msg))
            {
                XtraMessageBox.Show("项目移除失败!");
                return;
            } 
            _allBindList.Remove(row);
            this.bindingSource1.ResetBindings(false);
            XtraMessageBox.Show("项目移除成功!");
        }
         
        //双击
        private void tileView1_ItemDoubleClick(object sender, TileViewItemClickEventArgs e)
        {
            var row = this.tileView1.GetRow(e.Item.RowHandle) as CurrentViewModel;
            if (row == null)
                return;
            this.ReloadDataEvent?.Invoke(row.Model);
        }
 
 
    }
}