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, 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().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 _colsSysLinList = JsonConvert.DeserializeObject(colsSys, typeof(List)) as List; List chooseArr = new List(); var colsUserEntry = Db.Queryable().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 colsUserLinList = JsonConvert.DeserializeObject(colsUser, typeof(List)) as List; //判断已添加的列 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().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 _colsSysLinList = JsonConvert.DeserializeObject(ColsJson, typeof(List)) as List; 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 oldColsJsonList = JsonConvert.DeserializeObject(oldColsJson, typeof(List)) as List; 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 newColsJsonList = JsonConvert.DeserializeObject(newColsJson, typeof(List)) as List; 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 _colsSysLinList = JsonConvert.DeserializeObject(colsSys, typeof(List)) as List; 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 ""; } } /// /// 去除json key双引号 /// /// json /// 去除key引号 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; } /// /// 加上json key双引号 /// /// /// 加上key引号 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;*/ } /// /// 表头参数 /// 参考layui官网,http://layui.dotnetcms.cn/web/doc/modules/table.html /// cols - 表头参数一览表 /// 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; } /// /// 固定列,layui原值为fixed,因为和C#系统fixed冲突,所以这里改为fixeds /// 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; } } }