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
#region
 
using System;
using System.Data;
using CSFrameworkV5.Business;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Library.CommonClass;
using CSFrameworkV5.Models;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
 
#endregion
 
namespace CSFrameworkV5.Library.CommonForms
{
    public partial class frmDataFieldsConfig : frmBase
    {
        private bllDataFieldConfig _BLL;
        private DataConfigType _configType;
        private GridView _view;
 
        private frmDataFieldsConfig()
        {
            InitializeComponent();
 
            _BLL = new bllDataFieldConfig();
        }
 
        /// <summary>
        ///     应用表格,显示/隐藏栏位,设置只读和可编辑
        /// </summary>
        /// <param name="gvDetail"></param>
        /// <param name="dataConfigType"></param>
        public static void ApplyConfig(GridView view, DataConfigType configType)
        {
            //取当前用户的配置
            var userConfig =
                new bllDataFieldConfig().GetConfig(Loginer.CurrentUser.Account,
                    configType.ToStringEx());
            ApplyConfig(view, configType, userConfig);
        }
 
        private static void ApplyConfig(GridView view,
            DataConfigType configType, DataTable userConfig)
        {
            foreach (GridColumn col in view.Columns)
            {
                col.Visible = IsVisible(userConfig, col.FieldName);
                col.OptionsColumn.AllowEdit =
                    IsEditable(userConfig, col.FieldName);
                col.OptionsColumn.ReadOnly =
                    col.OptionsColumn.AllowEdit == false;
                col.VisibleIndex = col.Visible
                    ? GetIndex(userConfig, col.FieldName)
                    : -1; //只有栏目可见时设置显示序号
            }
        }
 
        private void btnApply_Click(object sender, EventArgs e)
        {
            var dt = gc.DataSource as DataTable;
            var success = _BLL.Update(dt);
            if (success)
            {
                ApplyConfig(_view, _configType, dt);
                Msg.ShowInformation("保存成功!");
                Close();
            }
        }
 
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
 
        /// <summary>
        ///     打开配置窗体
        /// </summary>
        /// <param name="view">视图名称</param>
        /// <param name="configType">配置类型</param>
        public static void Execute(GridView view, DataConfigType configType)
        {
            var form = new frmDataFieldsConfig();
            form.Init(view, configType);
            form.ShowDialog();
        }
 
        private void frmDataFieldsConfig_Load(object sender, EventArgs e)
        {
            //
        }
 
        private static int GetIndex(DataTable userConfig, string fieldName)
        {
            var rs = userConfig.Select(sys_DataFieldConfig.FieldName + "='" +
                                       fieldName + "'");
            if (rs.Length > 0) return userConfig.Rows.IndexOf(rs[0]);
 
            return userConfig.Rows.Count - 1;
        }
 
        private void Init(GridView view, DataConfigType configType)
        {
            _view = view;
            _configType = configType;
 
            //绑定用户数据源
            DataBinder.BindingLookupEditDataSource(txtConfigUser,
                DataDictCache.Cache.User, tb_MyUser.UserName,
                tb_MyUser.Account);
            txtConfigUser.EditValue = Loginer.CurrentUser.Account;
            txtConfigName.Text = configType.ToStringEx();
            txtConfigUser.Enabled = false; //如果不按用户配置,禁用掉
        }
 
        private static bool IsEditable(DataTable userConfig, string fieldName)
        {
            var rs = userConfig.Select(sys_DataFieldConfig.FieldName + "='" +
                                       fieldName + "'");
            if (rs.Length > 0)
                return ConvertEx.ToString(
                    rs[0][sys_DataFieldConfig.IsEditable]) == "Y";
 
            return true;
        }
 
        private static bool IsVisible(DataTable userConfig, string fieldName)
        {
            var rs = userConfig.Select(sys_DataFieldConfig.FieldName + "='" +
                                       fieldName + "'");
            if (rs.Length > 0)
                return ConvertEx.ToString(
                    rs[0][sys_DataFieldConfig.IsDisplay]) == "Y";
 
            return true;
        }
 
        /// <summary>
        ///     加载配置
        /// </summary>
        private void LoadConfig()
        {
            var user = txtConfigUser.EditValue.ToStringEx();
 
            //取当前用户的配置
            var userConfig = _BLL.GetConfig(_configType.ToStringEx());
            var gridData = userConfig.Clone();
            gridData.Columns.Add("OrderID", typeof(int));
 
            var i = 1;
 
            //加载表格所有栏位
            var dt = _view.GridControl.DataSource as DataTable;
 
            foreach (GridColumn col in _view.Columns)
            {
                var R = gridData.NewRow();
                R["OrderID"] = i;
                R[sys_DataFieldConfig.ConfigName] = _configType.ToStringEx();
                R[sys_DataFieldConfig.UserName] = user;
                R[sys_DataFieldConfig.FieldCaption] = col.Caption;
                R[sys_DataFieldConfig.FieldName] = col.FieldName;
                R[sys_DataFieldConfig.IsDisplay] = col.Visible ? "Y" : "N";
                R[sys_DataFieldConfig.IsEditable] =
                    col.OptionsColumn.AllowEdit &&
                    col.OptionsColumn.ReadOnly == false
                        ? "Y"
                        : "N";
                gridData.Rows.Add(R);
                i++;
            }
 
            //绑定表格数据源
            gc.DataSource = gridData;
        }
 
        private void txtConfigUser_EditValueChanged(object sender, EventArgs e)
        {
            LoadConfig();
        }
    }
 
    /// <summary>
    ///     数据配置类型
    /// </summary>
    public enum DataConfigType
    {
        汇总表_Part1,
        汇总表_Part2,
        汇总表_Part3,
        库存明细表
    }
}