using JNPF.Common.Const; using JNPF.Common.Extension; using JNPF.DependencyInjection; using SqlSugar; namespace JNPF.Common.Security; /// /// 代码生成帮助类. /// [SuppressSniffer] public static class CodeGenHelper { public static string ConvertDataType(string dataType) { switch (dataType.ToLower()) { case "text": case "varchar": case "char": case "nvarchar": case "nchar": case "timestamp": case "string": return "string?"; case "int": case "smallint": return "int?"; case "tinyint": return "byte?"; case "bigint": // sqlite数据库 case "integer": return "long"; case "bit": return "bool"; case "money": case "smallmoney": case "numeric": case "decimal": return "decimal?"; case "real": return "Single?"; case "datetime": case "datetime2": case "smalldatetime": case "date": return "DateTime?"; case "float": return "double?"; case "image": case "binary": case "varbinary": return "byte[]"; case "uniqueidentifier": return "Guid"; default: return "object"; } } /// /// 数据类型转显示类型. /// /// /// public static string DataTypeToEff(string dataType) { if (string.IsNullOrEmpty(dataType)) return string.Empty; return dataType switch { "string" => "input", "int" => "inputnumber", "long" => "input", "float" => "input", "double" => "input", "decimal" => "input", "bool" => "switch", "Guid" => "input", "DateTime" => "datepicker", _ => "input", }; } /// /// 是否通用字段. /// /// /// public static bool IsCommonColumn(string columnName) { var columnList = new List() { "CreatedTime", "UpdatedTime", "CreatedUserId", "CreatedUserName", "UpdatedUserId", "UpdatedUserName", "IsDeleted" }; return columnList.Contains(columnName); } /// /// 数据列表生成分组表格. /// /// 数据列表. /// 分组字段名. /// 分组显示字段名. /// public static List> GetGroupList(List> realList, string groupField, string groupShowField) { if (realList.Any()) { var fList = realList.FirstOrDefault().Select(x => x.Key).ToList(); var prop = fList.Where(x => x.Equals(groupShowField)).FirstOrDefault(); // 分组数据 Dictionary>> groupDic = new Dictionary>>(); foreach (var item in realList) { if (item.ContainsKey(groupField)) { var groupDicKey = item[groupField] is null ? string.Empty : item[groupField].ToString(); if (!groupDic.ContainsKey(groupDicKey)) groupDic.Add(groupDicKey, new List>()); // 初始化 item.Remove(groupField); groupDic[groupDicKey].Add(item); } else { var groupDicKey = "null"; if (!groupDic.ContainsKey(groupDicKey)) groupDic.Add(groupDicKey, new List>()); // 初始化 groupDic[groupDicKey].Add(item); } } List> realGroupDic = new List>(); foreach (var item in groupDic) { Dictionary dataMap = new Dictionary(); dataMap.Add("top", true); dataMap.Add("id", SnowflakeIdHelper.NextId()); dataMap.Add("children", item.Value); if (!string.IsNullOrWhiteSpace(prop)) dataMap.Add(prop, item.Key); else dataMap.Add(groupField, item.Key); realGroupDic.Add(dataMap); } return realGroupDic; } else { return new List>(); } } /// /// 数据列表生成树形表格. /// /// 数据列表. /// 树形父级字段. /// 树形显示字段. /// public static List> GetTreeList(List> realList, string parentField, string treeShowField) { var res = new List>(); if (realList.Any()) { var parentFieldId = SnowflakeIdHelper.NextId(); foreach (var item in realList) { if (realList.Any(x => x["id"].Equals(item[parentField]))) item[parentFieldId] = item[parentField]; else item[parentFieldId] = null; item[parentField] = realList.Find(x => x["id"] == item["id"])[treeShowField]; } var parentFieldRep = parentField.Substring(0, parentField.Length - 4); for (int i = 0; i < realList.Count; i++) { if (realList[i][parentFieldId].IsNullOrEmpty()) { if (realList[i][parentFieldRep] == null) realList[i][parentFieldRep] = realList[i][treeShowField]; var childList = realList.Where(x => x[parentFieldId] != null && x[parentFieldId].Equals(realList[i]["id"])).ToList(); if (childList.Any()) GetTreeList(realList, realList[i], parentFieldId); res.Add(realList[i]); } } } return res; } private static void GetTreeList(List> allList, Dictionary currentItem, string pId) { var childList = allList.Where(x => x[pId] != null && x[pId].Equals(currentItem["id"])).ToList(); if (childList.Any()) childList.ForEach(x => GetTreeList(allList, x, pId)); if (childList.Any()) { var item = allList.Find(x => x["id"].Equals(currentItem["id"])); item["children"] = childList; } } /// /// 根据集合捞取所有子集id. /// /// key : 主键Id , value : 父亲Id. /// 当前id. /// res. public static List GetChildIdList(Dictionary allList, string currentId, List resList) { if (resList == null) resList = new List() { currentId }; else resList.Add(currentId); if (allList.Any()) { var cItemList = allList.Where(x => x.Value.IsNotEmptyOrNull() && x.Value.Equals(currentId)).ToList(); if (cItemList.Any()) { foreach (var item in cItemList) { var cIdList = GetChildIdList(allList, item.Key, resList); resList.Add(item.Key); if (cIdList.Any()) resList.AddRange(cIdList); } } } return resList; } /// /// 获取排序真实字段. /// /// 排序字段. /// 取代内容. /// 实体信息. /// 表类型 0-主表,1-子表,2-副表. /// public static string GetSortRealField(string sort, string replaceContent, EntityInfo entityInfo, int tableType) { var field = string.Empty; switch (tableType) { case 1: if (sort.Contains(replaceContent)) { field = entityInfo.Columns.Find(it => it.PropertyName.Equals(sort.Replace(replaceContent, "").ToUpperCase()))?.DbColumnName; } break; case 2: if (sort.Contains("_jnpf_")) { var queryField = sort.Replace("_jnpf_", "@").Split('@')[1]; field = entityInfo.Columns.Find(it => it.PropertyName.Equals(queryField.ToUpperCase()))?.DbColumnName; } break; default: field = entityInfo.Columns.Find(it => it.PropertyName.Equals(sort.ToUpperCase()))?.DbColumnName; break; } return string.IsNullOrEmpty(field) ? null : field; } /// /// 代码生成导出模板. /// /// 控件Key. /// 是否多选. /// 标题. /// 时间格式化. /// 等级. /// public static Dictionary CodeGenTemplate(string jnpfKey, bool multiple, string label, string format, int level) { Dictionary result = new Dictionary(); switch (jnpfKey) { case JnpfKeyConst.CREATEUSER: case JnpfKeyConst.MODIFYUSER: case JnpfKeyConst.CREATETIME: case JnpfKeyConst.MODIFYTIME: case JnpfKeyConst.CURRORGANIZE: case JnpfKeyConst.CURRPOSITION: case JnpfKeyConst.CURRDEPT: case JnpfKeyConst.BILLRULE: result.Add(label, "系统自动生成"); break; case JnpfKeyConst.COMSELECT: result.Add(label, multiple ? "例:引迈信息/产品部,引迈信息/技术部" : "例:引迈信息/技术部"); break; case JnpfKeyConst.DEPSELECT: result.Add(label, multiple ? "例:产品部/部门编码,技术部/部门编码" : "例:技术部/部门编码"); break; case JnpfKeyConst.POSSELECT: result.Add(label, multiple ? "例:技术经理/岗位编码,技术员/岗位编码" : "例:技术员/岗位编码"); break; case JnpfKeyConst.USERSELECT: result.Add(label, multiple ? "例:张三/账号,李四/账号" : "例:张三/账号"); break; case JnpfKeyConst.USERSSELECT: result.Add(label, multiple ? "例:引迈信息/产品部,产品部/部门编码,技术经理/岗位编码,研发人员/角色编码,A分组/分组编码,张三/账号" : "例:李四/账号"); break; case JnpfKeyConst.ROLESELECT: result.Add(label, multiple ? "例:研发人员/角色编码,测试人员/角色编码" : "例:研发人员/角色编码"); break; case JnpfKeyConst.GROUPSELECT: result.Add(label, multiple ? "例:A分组/分组编码,B分组/分组编码" : "例:A分组/分组编码"); break; case JnpfKeyConst.DATE: result.Add(label, string.Format("例:{0}", format)); break; case JnpfKeyConst.TIME: result.Add(label, "例: HH:mm:ss"); break; case JnpfKeyConst.ADDRESS: switch (level) { case 0: result.Add(label, multiple ? "例:福建省,广东省" : "例:福建省"); break; case 1: result.Add(label, multiple ? "例:福建省/莆田市,广东省/广州市" : "例:福建省/莆田市"); break; case 2: result.Add(label, multiple ? "例:福建省/莆田市/城厢区,广东省/广州市/荔湾区" : "例:福建省/莆田市/城厢区"); break; case 3: result.Add(label, multiple ? "例:福建省/莆田市/城厢区/霞林街道,广东省/广州市/荔湾区/沙面街道" : "例:福建省/莆田市/城厢区/霞林街道"); break; } break; default: result.Add(label, string.Empty); break; } return result; } }