zhaowc
2024-09-06 1b108e8d7335d5a5a59dfcf2f4eeef034a53b9b8
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using Model.ModelVm.SysVm;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using WMS.DAL;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
 
namespace WMS.BLL.SysServer
{
    public class HeaderSettingsServer : DbHelper<SysHeaderSettings>, IHeaderSettingsServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
 
        public HeaderSettingsServer() : base(Db)
        {
        }
 
        //获取用户自定义表头
        public string GetTableColsByUserId(HeaderSetVm model)
        {
            try
            {
                string cols = string.Empty;
                var data = Db.Queryable<SysHeaderSettings>().First(w => w.IsDel == "0" && w.Href == model.Href && w.CreateUser == model.UserId);
                if (data != null)
                {
                    cols = data.ColsJson;
                }
                return cols;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
 
        //获取系统表头
        public object GetTableColsSys(HeaderSetVm model)
        {
            try
            {
                string colsSys = string.Empty;
                string colsUser = string.Empty;
 
                colsSys = model.ColsSysArr.Substring(1);
                colsSys = colsSys.Substring(0, colsSys.Length - 1);
                colsSys = colsSys.Replace("field", "value");//穿梭框的名称属性字段是value,所以这里将table里field属性临时改为value
                colsSys = colsSys.Replace("fixed", "fixeds");//table里fixed属性会和穿梭框的fixed属性冲突,所以这里将table里fixed属性临时改为fixeds
                if (colsSys.Contains("[["))
                {
                    colsSys = colsSys.Substring(1);
                    colsSys = colsSys.Substring(0, colsSys.Length - 1);
                    colsSys = colsSys.Replace("'", "\"");
                    colsSys = JsonRegex2(colsSys);
                }
 
                List<ClosData> _colsSysLinList = JsonConvert.DeserializeObject(colsSys, typeof(List<ClosData>)) as List<ClosData>;
 
                List<string> chooseArr = new List<string>();
                var colsUserEntry = Db.Queryable<SysHeaderSettings>().First(w => w.IsDel == "0" && w.Href == model.Href && w.CreateUser == model.UserId);
                if (colsUserEntry != null)
                {
                    colsUser = colsUserEntry.ColsJson.Substring(1);
                    colsUser = colsUser.Substring(0, colsUser.Length - 1);
                    colsUser = colsUser.Replace("field", "value");
 
                    List<ClosData> colsUserLinList = JsonConvert.DeserializeObject(colsUser, typeof(List<ClosData>)) as List<ClosData>;
                    //判断已添加的列
                    foreach (var item in colsUserLinList)
                    {
                        var entry = _colsSysLinList.Find(w => w.value == item.value);
                        if (entry != null)
                        {
                            chooseArr.Add(item.value);
                        }
                    }
                }
                else
                {
                    foreach (var item in _colsSysLinList)
                    {
                        chooseArr.Add(item.value);
                    }
                }
                var obj = new
                {
                    colsSys = colsSys,
                    colsUser = chooseArr
                };
                return obj;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
 
        //保存自定义表头
        public string SaveClosUser(HeaderSetVm model)
        {
            try
            {
                string ColsJson = model.ColsJson;
                string ColsSysArr = model.ColsSysArr;
                //前端转json会自动给key加上单引号,这里把单引号给去掉
                if (!string.IsNullOrEmpty(ColsJson) && ColsJson.Length > 2)
                {
                    ColsJson = JsonRegex(ColsJson);
                    ColsJson = ColsJson.Replace("value", "field").Replace("fixeds", "fixed").Replace("\"", "'");
                    ColsJson = $"[{ColsJson}]";
                }
                ColsSysArr = JsonRegex(ColsSysArr);
                ColsSysArr = ColsSysArr.Replace("\"", "'");
                SysHeaderSettings entry = Db.Queryable<SysHeaderSettings>().First(w => w.IsDel == "0" && w.Href == model.Href && w.CreateUser == model.UserId);
                if (entry != null)
                {
                    //更新列宽
                    if (!string.IsNullOrEmpty(model.ColText))
                    {
                        ColsJson = entry.ColsJson.Replace("fixed", "fixeds");
                        ColsJson = ColsJson.Substring(1);
                        ColsJson = ColsJson.Substring(0, ColsJson.Length - 1);
                        ColsJson = ColsJson.Replace("'", "\"");
                        ColsJson = JsonRegex2(ColsJson);
                        List<TableCols> _colsSysLinList = JsonConvert.DeserializeObject(ColsJson, typeof(List<TableCols>)) as List<TableCols>;
                        foreach (var item in _colsSysLinList)
                        {
                            if (item.title == model.ColText)
                            {
                                item.width = Convert.ToInt32(model.ColWidth);
                                break;
                            }
                        }
                        var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };//滤掉值为NULL得属性
                        ColsJson = JsonConvert.SerializeObject(_colsSysLinList, Formatting.Indented, jsonSetting);
                        ColsJson = JsonRegex(ColsJson);
                        ColsJson = $"[{ColsJson}]";
                        ColsJson = ColsJson.Replace("fixeds", "fixed").Replace("\"", "'").Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
 
                        entry.ColsJson = ColsJson;
                        var x = Db.Storageable(entry).ToStorage();
                        x.AsUpdateable.ExecuteCommand();
                    }
                    else
                    {
                        //删除
                        if (string.IsNullOrEmpty(ColsJson) || ColsJson.Length <= 2 || ColsJson == ColsSysArr)
                        {
                            //物理删除
                            Db.Deleteable(entry).ExecuteCommand();
 
                            //假删
                            /*string sqlString = string.Empty;                        
                            sqlString += $"UPDATE SysHeaderSettings SET IsDel = '1',";
                            sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.UserId}' ";
                            sqlString += $"WHERE Id = '{entry.Id}' and IsDel = '0';";
                            Db.Ado.ExecuteCommand(sqlString);*/
                            ColsJson = "删除成功";
                        }
                        else//更新列显示隐藏/排序
                        {
                            string oldColsJson = string.Empty;
                            oldColsJson = entry.ColsJson.Substring(1);
                            oldColsJson = oldColsJson.Substring(0, oldColsJson.Length - 1);
                            oldColsJson = oldColsJson.Replace("fixed", "fixeds");//table里fixed属性会和穿梭框的fixed属性冲突,所以这里将table里fixed属性临时改为fixeds
 
                            List<TableCols> oldColsJsonList = JsonConvert.DeserializeObject(oldColsJson, typeof(List<TableCols>)) as List<TableCols>;
 
                            string newColsJson = string.Empty;
                            newColsJson = ColsJson.Substring(1);
                            newColsJson = newColsJson.Substring(0, newColsJson.Length - 1);
                            newColsJson = newColsJson.Replace("fixed", "fixeds");//table里fixed属性会和穿梭框的fixed属性冲突,所以这里将table里fixed属性临时改为fixeds
 
                            List<TableCols> newColsJsonList = JsonConvert.DeserializeObject(newColsJson, typeof(List<TableCols>)) as List<TableCols>;
                            foreach (var item in newColsJsonList)
                            {
                                var info = oldColsJsonList.Find(w => w.field == item.field);
                                if (info != null && info.width != item.width)
                                {
                                    //判断之前是否设置列宽
                                    item.width = info.width;
                                }
                            }
                            var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };//滤掉值为NULL得属性
                            ColsJson = JsonConvert.SerializeObject(newColsJsonList, Formatting.Indented, jsonSetting);
                            ColsJson = JsonRegex(ColsJson);
                            ColsJson = $"[{ColsJson}]";
                            ColsJson = ColsJson.Replace("fixeds", "fixed").Replace("\"", "'").Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
 
                            entry.ColsJson = ColsJson;
                            var x = Db.Storageable(entry).ToStorage();
                            x.AsUpdateable.ExecuteCommand();
                        }
                    }                    
                }
                else
                {
                    if (string.IsNullOrEmpty(ColsJson) && !string.IsNullOrEmpty(model.ColText))//设置列宽
                    {
                        string colsSys = string.Empty;
                        colsSys = model.ColsSysArr.Substring(1);
                        colsSys = colsSys.Substring(0, colsSys.Length - 1);
                        colsSys = colsSys.Replace("fixed", "fixeds");//table里fixed属性会和穿梭框的fixed属性冲突,所以这里将table里fixed属性临时改为fixeds
 
                        List<TableCols> _colsSysLinList = JsonConvert.DeserializeObject(colsSys, typeof(List<TableCols>)) as List<TableCols>;
                        foreach (var item in _colsSysLinList)
                        {
                            if (item.title == model.ColText)
                            {
                                item.width =Convert.ToInt32(model.ColWidth);
                                break;
                            }
                        }
                        var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };//滤掉值为NULL得属性
                        ColsJson = JsonConvert.SerializeObject(_colsSysLinList, Formatting.Indented, jsonSetting);
                        ColsJson = JsonRegex(ColsJson);
                        ColsJson = $"[{ColsJson}]";
                        ColsJson = ColsJson.Replace("fixeds", "fixed").Replace("\"", "'").Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
 
                        entry = new SysHeaderSettings();
                        entry.Href = model.Href;
                        entry.ColsJson = ColsJson;
                        entry.CreateUser = model.UserId;
 
                        var x = Db.Storageable(entry).ToStorage();
                        x.AsInsertable.ExecuteCommand();
                    }
                    else//设置列隐藏显示/排序
                    {
                        if (string.IsNullOrEmpty(ColsJson) || ColsJson.Length <= 2 || ColsJson == ColsSysArr)
                        {
                            return "设置成功";
                        }
 
                        entry = new SysHeaderSettings();
                        entry.Href = model.Href;
                        entry.ColsJson = ColsJson;
                        entry.CreateUser = model.UserId;
 
                        var x = Db.Storageable(entry).ToStorage();
                        x.AsInsertable.ExecuteCommand();
                    }
                }
                return ColsJson;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
 
        /// <summary>
        /// 去除json key双引号
        /// </summary>
        /// <param name="jsonInput">json</param>
        /// <returns>去除key引号</returns>
        public string JsonRegex(string jsonInput)
        {
            string result = string.Empty;
            string pattern = "\"(\\w+)\"(\\s*:\\s*)";
            string replacement = "$1$2";
            System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(pattern);
            result = rgx.Replace(jsonInput, replacement);
 
            return result;
        }
        /// <summary>
        /// 加上json key双引号
        /// </summary>
        /// <param name="jsonInput"></param>
        /// <returns>加上key引号</returns>
        public string JsonRegex2(string jsonInput)
        {
            string pattern = @"(\w+):";
            string replacement = "\"$1\":";
 
            System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(pattern);
            return rgx.Replace(jsonInput, replacement);
        }
 
    }
    public class ClosData
    {
        public string value { get; set; }
        /*public string title { get; set; }
        public string fixeds { get; set; }
        public bool disabled { get; set; } = false;*/
    }
    /// <summary>
    /// 表头参数
    /// 参考layui官网,http://layui.dotnetcms.cn/web/doc/modules/table.html
    /// cols - 表头参数一览表
    /// </summary>
    public class TableCols
    { 
        public string field { get; set; }
        public string title { get; set; }       
        public int? width { get; set; }
        public int? minWidth { get; set; }
        public string type { get; set; }
        public bool? LAY_CHECKED { get; set; }
        /// <summary>
        /// 固定列,layui原值为fixed,因为和C#系统fixed冲突,所以这里改为fixeds
        /// </summary>
        public string fixeds { get; set; }
        public bool? hide { get; set; }
        public string totalRow { get; set; }
        public string totalRowText { get; set; }
        public bool? sort { get; set; }
        public bool? unresize { get; set; }
        public string edit { get; set; }
        //public string event{ get;set; }
        public string style { get; set; }
        public string align { get; set; }    
        public int? colspan { get; set; }
        public int? rowspan { get; set; }
        public string templet { get; set; }
        public string toolbar { get; set; }
        public bool? disabled { get; set; }
    }
}