1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
#region
 
using System.Collections;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Interfaces;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
 
#endregion
 
namespace CSFrameworkV5.Library.CommonClass
{
    /// <summary>
    ///     分页查询通用类(缓存分页数据)
    /// </summary>
    public class QueryByPageCache : IMyPageDataCache
    {
        private ISupportPageSearch _BusinessBLL; //业务逻辑层接口
        private int _CurrentPageNo = 1; //当前页号
        private GridView _DataView; //数据视图,如GridControl        
 
        private bool _IsChcheData;
        private int _PageCount; //本次搜索结果总页数
        private Hashtable _PageDataMapping; //哈希表,存储每页的数据
        private int _PageSize = 30; //预设每页返回30条记录        
 
        private object[] _SearchConditions; //本次查询的查询条件
 
        /// <summary>
        ///     构造器
        /// </summary>
        /// <param name="dataView">GridView表格</param>
        /// <param name="BLL">支持分页查询的BLL层</param>
        /// <param name="defaultPageSize">每页记录数</param>
        /// <param name="cacheData">是否缓存每页的数据(缺点:数据会过时,变成脏数据)</param>
        public QueryByPageCache(GridView dataView, ISupportPageSearch BLL,
            int defaultPageSize,
            bool cacheData = false)
        {
            _PageSize = defaultPageSize;
            _PageDataMapping = new Hashtable();
            _DataView = dataView;
            _BusinessBLL = BLL;
            _IsChcheData = cacheData;
            _DataView.GridControl.EmbeddedNavigator.ButtonClick +=
                navigator_ButtonClick;
 
            AddNavigatorPageButton(_DataView.GridControl.EmbeddedNavigator);
            SetPageButtonEnable(false);
        }
 
        /// <summary>
        ///     添加分页按钮
        /// </summary>
        /// <param name="navigator"></param>
        private void AddNavigatorPageButton(ControlNavigator navigator)
        {
            var img = new ImageList();
            img.ImageSize = new Size(40, 16);
            img.Images.Add(Globals.LoadImage("NAV_FIRST_PAGE.ico"));
            img.Images.Add(Globals.LoadImage("NAV_PRIOR_PAGE.ico"));
            img.Images.Add(Globals.LoadImage("NAV_NEXT_PAGE.ico"));
            img.Images.Add(Globals.LoadImage("NAV_LAST_PAGE.ico"));
            navigator.Buttons.ImageList = img;
 
            navigator.CustomButtons.Clear();
 
            var btnMoveFirst = new NavigatorCustomButton(0, "最前一页");
            var btnMovePrior = new NavigatorCustomButton(1, "上一页");
            var btnMoveNext1 = new NavigatorCustomButton(2, "下一页");
            var btnMoveLast1 = new NavigatorCustomButton(3, "最后一页");
 
            btnMoveFirst.Tag = "FIRST_PAGE";
            btnMovePrior.Tag = "PRIOR_PAGE";
            btnMoveNext1.Tag = "NEXT_PAGE";
            btnMoveLast1.Tag = "LAST_PAGE";
 
            navigator.CustomButtons.AddRange(new[]
                { btnMoveFirst, btnMovePrior, btnMoveNext1, btnMoveLast1 });
        }
 
        #region 导航按钮及事件
 
        public void SetPageButtonEnable(bool enable)
        {
            var navigator = _DataView.GridControl.EmbeddedNavigator;
            foreach (NavigatorCustomButton btn in navigator.Buttons
                         .CustomButtons) btn.Enabled = enable;
        }
 
        private void navigator_ButtonClick(object sender,
            NavigatorButtonClickEventArgs e)
        {
            if (_SearchConditions == null)
            {
                Msg.Warning("搜索器没有初始化,请先输入查询条件并查询数据!");
                return;
            }
 
            if (e.Button.Tag != null)
            {
                var tag = e.Button.Tag.ToStringEx().ToUpper();
                if (tag == "FIRST_PAGE") GotoFirstPage(); //第一页
 
                if (tag == "PRIOR_PAGE") GotoPriorPage(); //上一页
 
                if (tag == "NEXT_PAGE") GotoNextPage(); //下一页
 
                if (tag == "LAST_PAGE") GotoLastPage(); //最后页
            }
        }
 
        private void GotoNextPage()
        {
            _CurrentPageNo++;
            if (_CurrentPageNo > _PageCount)
            {
                _CurrentPageNo--;
                return;
            }
 
            var data = GetPageData(_CurrentPageNo);
            _DataView.GridControl.DataSource = data;
        }
 
        private void GotoPriorPage()
        {
            _CurrentPageNo--;
            if (_CurrentPageNo <= 0)
            {
                _CurrentPageNo++;
                return;
            }
 
            var data = GetPageData(_CurrentPageNo);
            _DataView.GridControl.DataSource = data;
        }
 
        private void GotoFirstPage()
        {
            _CurrentPageNo = 1;
            var data = GetPageData(_CurrentPageNo);
            _DataView.GridControl.DataSource = data;
        }
 
        private void GotoLastPage()
        {
            _CurrentPageNo = _PageCount;
            var data = GetPageData(_CurrentPageNo);
            _DataView.GridControl.DataSource = data;
        }
 
        #endregion
 
        #region IPageDataCache Members
 
        public int PageSize
        {
            get => _PageSize;
            set => _PageSize = value;
        }
 
        public int PageCount => _PageCount;
 
        public int CurrentPage => _CurrentPageNo;
 
        public int CachedPage => _PageDataMapping.Keys.Count;
 
        /// <summary>
        ///     获取指定页码的数据
        /// </summary>
        /// <param name="pageNo"></param>
        /// <returns></returns>
        public DataTable GetPageData(int pageNo)
        {
            if (pageNo <= 0) pageNo = 1;
 
            //不缓存每页数据,清空缓存器,程序会从数据库下载
            if (_IsChcheData == false) _PageDataMapping.Clear();
 
            //有缓存数据
            if (_PageDataMapping.ContainsKey(pageNo))
                return _PageDataMapping[pageNo] as DataTable; //从缓存表撷取数据
 
            try
            {
                Cursor.Current = Cursors.WaitCursor;
 
                //无缓存数据,从数据库下载然后加入缓存表
                var thisPageData =
                    _BusinessBLL.QueryByPage(_SearchConditions, pageNo,
                        _PageSize); //下载数据
 
                if (thisPageData != null && thisPageData.Rows.Count > 0)
                    _PageDataMapping.Add(pageNo,
                        thisPageData); //加入缓存表                
 
                return thisPageData; //返回数据
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
 
        /// <summary>
        ///     初始化缓存数据管理器
        /// </summary>
        /// <param name="searchConditions">查询条件</param>
        public void InitializeSearch(object[] searchConditions)
        {
            _PageDataMapping.Clear(); //清空哈希表            
            _SearchConditions = searchConditions;
 
            var firstPageData =
                _BusinessBLL.QueryByPage(searchConditions, 1, _PageSize);
 
            if (_BusinessBLL.TotalPages > 0)
            {
                _CurrentPageNo = 1;
                _PageDataMapping.Add(1,
                    firstPageData); //下载第一页数据,加入缓存表                
                _PageCount =
                    _BusinessBLL
                        .TotalPages; //取总页数                                
            }
 
            _DataView.GridControl.DataSource = firstPageData;
        }
 
        public void ClearCache()
        {
            _CurrentPageNo = 0;
            _PageCount = 0;
            _PageDataMapping.Clear();
        }
 
        #endregion
    }
}