using JNPF.Common.Const; using JNPF.Common.Extension; using JNPF.Common.Models; using JNPF.Common.Models.Authorize; using JNPF.Common.Security; using JNPF.VisualDev.Engine.Core; using JNPF.VisualDev.Entitys; using JNPF.VisualDev.Entitys.Enum; using SqlSugar; namespace JNPF.VisualDev.Engine.Security; /// /// 代码生成控件属性帮助类. /// public class CodeGenControlsAttributeHelper { /// /// 转换静态数据. /// /// /// public static List ConversionStaticData(string data) { var list = new List(); if (!string.IsNullOrEmpty(data)) { var conData = data.ToObject>(); if (conData != null) { foreach (var item in conData) { list.Add(new StaticDataModel() { id = item.id, fullName = item.fullName, }); if (item.children != null) list.AddRange(ConversionStaticData(item.children.ToJsonString())); } } } return list; } /// /// 判断控件是否数据转换. /// /// public static bool JudgeControlIsDataConversion(string jnpfKey, string dataType, bool multiple) { bool tag = false; switch (jnpfKey) { case JnpfKeyConst.UPLOADFZ: case JnpfKeyConst.UPLOADIMG: tag = true; break; case JnpfKeyConst.SELECT: case JnpfKeyConst.TREESELECT: { switch (dataType) { case "dictionary": if (!multiple) { tag = false; } else { tag = true; } break; default: tag = true; break; } } break; case JnpfKeyConst.RADIO: { switch (dataType) { case "dictionary": tag = false; break; default: tag = true; break; } } break; case JnpfKeyConst.DEPSELECT: case JnpfKeyConst.POSSELECT: case JnpfKeyConst.USERSELECT: case JnpfKeyConst.ROLESELECT: case JnpfKeyConst.GROUPSELECT: { if (!multiple) tag = false; else tag = true; } break; case JnpfKeyConst.CHECKBOX: case JnpfKeyConst.CASCADER: case JnpfKeyConst.COMSELECT: case JnpfKeyConst.POPUPTABLESELECT: case JnpfKeyConst.ADDRESS: case JnpfKeyConst.USERSSELECT: tag = true; break; } return tag; } /// /// 获取各模式控件是否列表转换. /// /// 生成模式. /// 控件Key. /// 数据类型 dictionary-数据字段,dynamic-远端数据. /// 是否多选. /// 子表列表显示. /// true-ThenMapper转换,false-列表转换. public static bool GetWhetherToConvertAllModeControlsIntoLists(GeneratePatterns generatePattern, string jnpfKey, string dataType, bool multiple, bool subTableListDisplay) { bool tag = false; /* * 因ORM原因 导航查询 一对多 列表查询 * 不能使用ORM 自带函数 待作者开放.Select() * 导致一对多列表查询转换必须全使用子查询 * 远端数据与静态数据无法列表转换所以全部ThenMapper内转换 * 数据字典又分为两种值转换ID与EnCode */ switch (subTableListDisplay) { case true: switch (generatePattern) { case GeneratePatterns.MainBelt: case GeneratePatterns.PrimarySecondary: switch (jnpfKey) { case JnpfKeyConst.CREATEUSER: case JnpfKeyConst.MODIFYUSER: case JnpfKeyConst.CURRORGANIZE: case JnpfKeyConst.CURRPOSITION: case JnpfKeyConst.DEPSELECT: case JnpfKeyConst.POSSELECT: case JnpfKeyConst.USERSELECT: case JnpfKeyConst.POPUPTABLESELECT: case JnpfKeyConst.ROLESELECT: case JnpfKeyConst.GROUPSELECT: case JnpfKeyConst.RADIO: case JnpfKeyConst.SELECT: case JnpfKeyConst.TREESELECT: case JnpfKeyConst.CHECKBOX: case JnpfKeyConst.CASCADER: case JnpfKeyConst.COMSELECT: case JnpfKeyConst.ADDRESS: case JnpfKeyConst.SWITCH: tag = true; break; } break; } break; default: switch (jnpfKey) { case JnpfKeyConst.SELECT: case JnpfKeyConst.TREESELECT: { switch (dataType) { case "dictionary": if (multiple) tag = true; else tag = false; break; default: tag = true; break; } } break; case JnpfKeyConst.RADIO: { switch (dataType) { case "dictionary": tag = false; break; default: tag = true; break; } } break; case JnpfKeyConst.DEPSELECT: case JnpfKeyConst.POSSELECT: case JnpfKeyConst.USERSELECT: case JnpfKeyConst.ROLESELECT: case JnpfKeyConst.GROUPSELECT: { if (multiple) tag = true; else tag = false; } break; case JnpfKeyConst.CHECKBOX: case JnpfKeyConst.CASCADER: case JnpfKeyConst.COMSELECT: case JnpfKeyConst.ADDRESS: case JnpfKeyConst.POPUPTABLESELECT: tag = true; break; } break; } return tag; } /// /// 判断含子表字段控件是否数据转换. /// /// 控件Key. /// public static bool JudgeContainsChildTableControlIsDataConversion(string jnpfKey) { bool tag = false; switch (jnpfKey) { case JnpfKeyConst.UPLOADFZ: case JnpfKeyConst.UPLOADIMG: case JnpfKeyConst.CREATEUSER: case JnpfKeyConst.MODIFYUSER: case JnpfKeyConst.CURRORGANIZE: case JnpfKeyConst.CURRPOSITION: case JnpfKeyConst.DEPSELECT: case JnpfKeyConst.POSSELECT: case JnpfKeyConst.USERSELECT: case JnpfKeyConst.USERSSELECT: case JnpfKeyConst.POPUPTABLESELECT: case JnpfKeyConst.ROLESELECT: case JnpfKeyConst.GROUPSELECT: case JnpfKeyConst.RADIO: case JnpfKeyConst.SELECT: case JnpfKeyConst.TREESELECT: case JnpfKeyConst.CHECKBOX: case JnpfKeyConst.CASCADER: case JnpfKeyConst.COMSELECT: case JnpfKeyConst.ADDRESS: case JnpfKeyConst.SWITCH: case JnpfKeyConst.DATE: tag = true; break; } return tag; } /// /// 系统控件不更新. /// /// 控件Key. /// public static bool JudgeControlIsSystemControls(string jnpfKey) { bool tag = true; switch (jnpfKey) { case JnpfKeyConst.CREATEUSER: case JnpfKeyConst.CREATETIME: case JnpfKeyConst.CURRPOSITION: case JnpfKeyConst.CURRORGANIZE: case JnpfKeyConst.BILLRULE: tag = false; break; } return tag; } /// /// 获取控件数据来源ID. /// /// 控件Key. /// 数据类型. /// 控件全属性. /// public static string GetControlsPropsUrl(string jnpfKey, string dataType, FieldsModel control) { string propsUrl = string.Empty; switch (jnpfKey) { case JnpfKeyConst.POPUPTABLESELECT: propsUrl = control.interfaceId; break; default: switch (dataType) { case "dictionary": propsUrl = control.__config__.dictionaryType; break; default: propsUrl = control.__config__.propsUrl; break; } break; } return propsUrl; } /// /// 获取控件指定选项的值. /// /// 控件Key. /// 数据类型. /// 控件全属性. /// public static string GetControlsLabel(string jnpfKey, string dataType, FieldsModel control) { string label = string.Empty; switch (jnpfKey) { case JnpfKeyConst.POPUPTABLESELECT: label = control.relationField; break; case JnpfKeyConst.CASCADER: case JnpfKeyConst.TREESELECT: label = control.props.props.label; break; default: label = control.__config__.props?.label; break; } return label; } /// /// 获取控件指定选项标签. /// /// 控件Key. /// 数据类型. /// 控件全属性. /// public static string GetControlsValue(string jnpfKey, string dataType, FieldsModel control) { string value = string.Empty; switch (jnpfKey) { case JnpfKeyConst.POPUPTABLESELECT: value = control.propsValue; break; case JnpfKeyConst.CASCADER: case JnpfKeyConst.TREESELECT: value = control.props.props.value; break; default: value = control.__config__.props?.value; break; } return value; } /// /// 获取控件指定选项的子选项. /// /// 控件Key. /// 数据类型. /// 控件全属性. /// public static string GetControlsChildren(string jnpfKey, string dataType, FieldsModel control) { string children = string.Empty; switch (jnpfKey) { case JnpfKeyConst.CASCADER: case JnpfKeyConst.TREESELECT: children = control.props.props.children; break; default: children = control.__config__.props?.children; break; } return children; } /// /// 获取导出配置. /// /// 控件全属性. /// 数据库真实字段. /// 表名称. /// public static CodeGenFieldsModel GetImportConfig(FieldsModel control, string model, string tableName) { var fieldModel = new CodeGenFieldsModel(); var configModel = new CodeGenConfigModel(); fieldModel.__vModel__ = model; fieldModel.level = control.level; fieldModel.min = control.min; fieldModel.max = control.max; fieldModel.activeTxt = control.activeTxt; fieldModel.inactiveTxt = control.inactiveTxt; fieldModel.format = control.format; fieldModel.multiple = CodeGenFieldJudgeHelper.IsMultipleColumn(control, model); fieldModel.separator = control.separator; fieldModel.__slot__ = control.__slot__?.ToObject()?.ToJsonString().ToJsonString(); fieldModel.props = control.props?.ToObject()?.ToJsonString().ToJsonString(); fieldModel.options = control.options?.ToObject>()?.ToJsonString().ToJsonString(); fieldModel.propsValue = control.propsValue; fieldModel.relationField = control.relationField; fieldModel.modelId = control.modelId; fieldModel.interfaceId = control.interfaceId; fieldModel.selectType = control.selectType; fieldModel.ableDepIds = control.ableDepIds?.ToJsonString().ToJsonString(); fieldModel.ablePosIds = control.ablePosIds?.ToJsonString().ToJsonString(); fieldModel.ableUserIds = control.ableUserIds?.ToJsonString().ToJsonString(); fieldModel.ableRoleIds = control.ableRoleIds?.ToJsonString().ToJsonString(); fieldModel.ableGroupIds = control.ableGroupIds?.ToJsonString().ToJsonString(); fieldModel.ableIds = control.ableIds?.ToJsonString().ToJsonString(); configModel = control.__config__.ToObject(); configModel.tableName = tableName; fieldModel.__config__ = configModel.ToJsonString().ToJsonString(); return fieldModel; } /// /// 获取需解析的字段集合. /// /// /// /// jnpfKey @@ vmodel集合以 , 号隔开. public static List GetParsJnpfKeyConstList(List control, bool isInlineEditor) { var res = new Dictionary>(); control.ForEach(item => { switch (item.__config__.jnpfKey) { case JnpfKeyConst.USERSSELECT: // 用户选择组件(包含组织、角色、岗位、分组、用户 Id) if (!res.ContainsKey(JnpfKeyConst.USERSSELECT)) res.Add(JnpfKeyConst.USERSSELECT, new List()); res[JnpfKeyConst.USERSSELECT].Add(item.__vModel__); break; case JnpfKeyConst.POPUPSELECT: // 弹窗选择 if (!res.ContainsKey(JnpfKeyConst.POPUPSELECT)) res.Add(JnpfKeyConst.POPUPSELECT, new List()); res[JnpfKeyConst.POPUPSELECT].Add(item.__vModel__); break; case JnpfKeyConst.RELATIONFORM: // 关联表单 if (!res.ContainsKey(JnpfKeyConst.RELATIONFORM)) res.Add(JnpfKeyConst.RELATIONFORM, new List()); res[JnpfKeyConst.RELATIONFORM].Add(item.__vModel__); break; case JnpfKeyConst.TABLE: // 遍历 子表 控件 var ctRes = GetParsJnpfKeyConstList(item.__config__.children, false); if (ctRes != null && ctRes.Any()) { foreach (var ct in ctRes) { if (!res.ContainsKey(ct.FirstOrDefault())) res.Add(ct.FirstOrDefault(), new List()); res[ct.FirstOrDefault()].Add(item.__vModel__ + "-" + ct.LastOrDefault()); } } break; } }); var ret = new List(); foreach (var item in res) { // 如果是行内编辑 if (isInlineEditor) { var newValue = new List(); foreach (var it in item.Value) newValue.Add(it + "_name"); res[item.Key] = newValue; } } foreach (var item in res) { ret.Add(new string[] { item.Key, string.Join(",", item.Value) }); } return ret; } /// /// 获取需解析的字段集合. /// /// /// /// jnpfKey @@ vmodel集合以 , 号隔开. public static List GetParsJnpfKeyConstListDetails(List control) { var res = new Dictionary>(); control.ForEach(item => { switch (item.__config__.jnpfKey) { case JnpfKeyConst.USERSSELECT: // 用户选择组件(包含组织、角色、岗位、分组、用户 Id) if (!res.ContainsKey(JnpfKeyConst.USERSSELECT)) res.Add(JnpfKeyConst.USERSSELECT, new List()); res[JnpfKeyConst.USERSSELECT].Add(item.__vModel__); break; case JnpfKeyConst.POPUPSELECT: // 弹窗选择. if (!res.ContainsKey(JnpfKeyConst.POPUPSELECT)) res.Add(JnpfKeyConst.POPUPSELECT, new List()); res[JnpfKeyConst.POPUPSELECT].Add(item.__vModel__); break; case JnpfKeyConst.RELATIONFORM: // 关联表单. if (!res.ContainsKey(JnpfKeyConst.RELATIONFORM)) res.Add(JnpfKeyConst.RELATIONFORM, new List()); res[JnpfKeyConst.RELATIONFORM].Add(item.__vModel__); break; case JnpfKeyConst.TABLE: // 遍历 子表 控件 var ctRes = GetParsJnpfKeyConstListDetails(item.__config__.children); if (ctRes != null && ctRes.Any()) { foreach (var ct in ctRes) { if (!res.ContainsKey(ct.FirstOrDefault())) res.Add(ct.FirstOrDefault(), new List()); res[ct.FirstOrDefault()].Add(item.__vModel__ + "-" + ct.LastOrDefault()); } } break; } }); var ret = new List(); foreach (var item in res) { ret.Add(new string[] { item.Key, string.Join(",", item.Value) }); } return ret; } /// /// 获取模板配置的数据过滤. /// /// /// /// public static List GetDataRuleList(VisualDevEntity templateEntity, Model.CodeGen.CodeGenConfigModel codeGenConfigModel) { var ruleList = new List(); var tInfo = new TemplateParsingBase(templateEntity); // 数据过滤 if (tInfo.ColumnData.ruleList != null && tInfo.ColumnData.ruleList.Any()) tInfo.ColumnData.ruleList.ForEach(item => ruleList.Add(GetItemRule(item, tInfo, "pc", codeGenConfigModel, ref ruleList))); if (tInfo.AppColumnData.ruleListApp != null && tInfo.AppColumnData.ruleListApp.Any()) tInfo.AppColumnData.ruleListApp.ForEach(item => ruleList.Add(GetItemRule(item, tInfo, "app", codeGenConfigModel, ref ruleList))); var res = new List(); foreach (var userOriginItem in new List() { "pc", "app" }) { ruleList.Where(x => x.UserOrigin.Equals(userOriginItem)).Select(x => x.TableName).Distinct().ToList().ForEach(tName => { var first = ruleList.FirstOrDefault(x => x.UserOrigin.Equals(userOriginItem) && x.TableName.Equals(tName)); var condList = ruleList.Where(x => x.UserOrigin.Equals(userOriginItem) && x.TableName.Equals(tName)).Select(x => x.conditionalModel.First()).ToList(); var dataRuleListJson = new List(); var condTree = new ConditionalTree() { ConditionalList = new List>() }; condList.ForEach(cItem => condTree.ConditionalList.Add(new KeyValuePair(WhereType.And, cItem))); res.Add(new CodeGenDataRuleModuleResourceModel() { FieldRule = first.TableName.Contains("@ChildFieldIsNull") ? -1 : first.FieldRule, TableName = first.TableName.Replace("@ChildFieldIsNull", string.Empty), UserOrigin = first.UserOrigin, conditionalModelJson = new List() { condTree }.ToJsonString() }); }); } return res; } private static CodeGenDataRuleModuleResourceModel GetItemRule(RuleFieldModel item, TemplateParsingBase tInfo, string userOrigin, Model.CodeGen.CodeGenConfigModel codeGenConfigModel, ref List ruleList) { var result = new CodeGenDataRuleModuleResourceModel() { FieldRule = 0, TableName = tInfo.MainTableName.ToLower(), UserOrigin = userOrigin, conditionalModel = new List() }; if (tInfo.AuxiliaryTableFields.ContainsKey(item.__vModel__)) { var tf = tInfo.AuxiliaryTableFields[item.__vModel__].Split('.'); result.FieldRule = 1; result.TableName = tf.FirstOrDefault().ToLower(); item.field = tf.LastOrDefault(); } else if (tInfo.ChildTableFields.ContainsKey(item.__vModel__)) { var tf = tInfo.ChildTableFields[item.__vModel__].Split('.'); result.FieldRule = 2; result.TableName = tf.FirstOrDefault().ToLower(); item.field = tf.LastOrDefault(); if (item.symbol.Equals("null")) { var mainTableRelationsQuery = result.Copy(); mainTableRelationsQuery.TableName = mainTableRelationsQuery.TableName + "@ChildFieldIsNull"; var ctPrimaryKey = codeGenConfigModel.TableRelations.Find(x => x.OriginalTableName.Equals(result.TableName)).ChilderColumnConfigList.Find(x => x.ColumnName.Equals(codeGenConfigModel.TableRelations.Find(x => x.OriginalTableName.Equals(result.TableName)).PrimaryKey)).OriginalColumnName; var condTree = new ConditionalCollections() { ConditionalList = new List>() { new KeyValuePair(WhereType.Or, new ConditionalModel() { FieldName = ctPrimaryKey, ConditionalType = ConditionalType.NoEqual, FieldValue = "0", FieldValueConvertFunc = it => SqlSugar.UtilMethods.ChangeType2(it, typeof(string)) }) } }; mainTableRelationsQuery.conditionalModel = new List() { condTree }; ruleList.Add(mainTableRelationsQuery); } } if (codeGenConfigModel.TableField.Any(x => x.LowerColumnName.Equals(item.field))) { var fieldList = codeGenConfigModel.TableField.Where(x => x.LowerColumnName.Equals(item.field)).ToList(); if (fieldList.Any() && fieldList.Count.Equals(1)) item.field = fieldList.First().OriginalColumnName; else item.field = fieldList.Find(x => x.TableName != null && x.TableName.Equals(item.__config__.tableName)).OriginalColumnName; } else if (codeGenConfigModel.TableRelations.Any(x => x.OriginalTableName.Equals(result.TableName))) { var tableRelations = codeGenConfigModel.TableRelations.Find(x => x.OriginalTableName.Equals(result.TableName) && x.ChilderColumnConfigList.Any(xx => xx.LowerColumnName.Equals(item.field))); if (tableRelations != null) item.field = tableRelations.ChilderColumnConfigList.Find(x => x.LowerColumnName.Equals(item.field)).OriginalColumnName; } var conditionalType = ConditionalType.Equal; var between = new List(); if (item.fieldValue.IsNotEmptyOrNull()) { if (item.symbol.Equals("between")) between = item.fieldValue.ToObject>(); switch (item.jnpfKey) { case JnpfKeyConst.CREATETIME: case JnpfKeyConst.MODIFYTIME: case JnpfKeyConst.DATE: { if (item.symbol.Equals("between")) { var startTime = between.First().TimeStampToDateTime(); var endTime = between.Last().TimeStampToDateTime(); between[0] = startTime.ToString(); between[1] = endTime.ToString(); if (item.format == "yyyy-MM-dd") { between[0] = new DateTime(startTime.Year, startTime.Month, startTime.Day, 0, 0, 0, 0).ToString(); between[1] = new DateTime(endTime.Year, endTime.Month, endTime.Day, 23, 59, 59, 999).ToString(); } } else { DateTime dtDate; if (DateTime.TryParse(item.fieldValue.ToString(), out dtDate)) item.fieldValue = item.fieldValue.ToString(); else item.fieldValue = string.Format("{0:yyyy-MM-dd HH:mm:ss}", item.fieldValue.ToString().TimeStampToDateTime()); if (item.format == "yyyy-MM-dd") { var value = item.fieldValue.ToString().ParseToDateTime(); item.fieldValue = new DateTime(value.Year, value.Month, value.Day, 0, 0, 0, 0).ToString(); } else { item.fieldValue = item.fieldValue.IsNotEmptyOrNull() ? item.fieldValue.ToString() : item.fieldValue; } } } break; } } switch (item.symbol) { case ">=": conditionalType = ConditionalType.GreaterThanOrEqual; break; case ">": conditionalType = ConditionalType.GreaterThan; break; case "==": conditionalType = ConditionalType.Equal; break; case "<=": conditionalType = ConditionalType.LessThanOrEqual; break; case "<": conditionalType = ConditionalType.LessThan; break; case "<>": conditionalType = ConditionalType.NoEqual; break; case "like": if (item.fieldValue != null && item.fieldValue.ToString().Contains("[")) item.fieldValue = item.fieldValue.ToString().Replace("[", string.Empty).Replace("]", string.Empty); conditionalType = ConditionalType.Like; break; case "notLike": if (item.fieldValue != null && item.fieldValue.ToString().Contains("[")) item.fieldValue = item.fieldValue.ToString().Replace("[", string.Empty).Replace("]", string.Empty); conditionalType = ConditionalType.NoLike; break; case "in": case "notIn": if (item.fieldValue != null && item.fieldValue.ToString().Contains("[")) { var isListValue = false; var itemField = tInfo.AllFieldsModel.Find(x => x.__vModel__.Equals(item.__vModel__)); if (itemField.multiple || item.jnpfKey.Equals(JnpfKeyConst.CHECKBOX) || item.jnpfKey.Equals(JnpfKeyConst.CASCADER) || item.jnpfKey.Equals(JnpfKeyConst.ADDRESS)) isListValue = true; var conditionalList = new ConditionalCollections() { ConditionalList = new List>() }; var ids = new List(); if (item.fieldValue.ToString().Replace("\r\n", "").Replace(" ", "").Contains("[[")) ids = item.fieldValue.ToObject>>().Select(x => x.Last()).ToList(); else ids = item.fieldValue.ToObject>(); for (var i = 0; i < ids.Count; i++) { var it = ids[i]; var whereType = WhereType.And; if (item.symbol.Equals("in")) whereType = i.Equals(0) && item.logic.Equals("&&") ? WhereType.And : WhereType.Or; else whereType = i.Equals(0) && item.logic.Equals("||") ? WhereType.Or : WhereType.And; conditionalList.ConditionalList.Add(new KeyValuePair(whereType, new ConditionalModel { FieldName = item.field, ConditionalType = item.symbol.Equals("in") ? ConditionalType.Like : ConditionalType.NoLike, FieldValue = isListValue ? it.ToJsonString() : it })); } if (item.symbol.Equals("notIn")) { conditionalList.ConditionalList.Add(new KeyValuePair(WhereType.And, new ConditionalModel { FieldName = item.field, ConditionalType = ConditionalType.IsNot, FieldValue = null })); conditionalList.ConditionalList.Add(new KeyValuePair(WhereType.And, new ConditionalModel { FieldName = item.field, ConditionalType = ConditionalType.IsNot, FieldValue = string.Empty })); } result.conditionalModel.Add(conditionalList); return result; } conditionalType = item.symbol.Equals("in") ? ConditionalType.In : ConditionalType.NotIn; break; case "null": conditionalType = (item.jnpfKey.Equals(JnpfKeyConst.CALCULATE) || item.jnpfKey.Equals(JnpfKeyConst.NUMINPUT)) ? ConditionalType.EqualNull : ConditionalType.IsNullOrEmpty; break; case "notNull": conditionalType = ConditionalType.IsNot; break; case "between": var condItem = new ConditionalCollections() { ConditionalList = new List>() { new KeyValuePair((item.logic.Equals("&&") ? WhereType.And : WhereType.Or), new ConditionalModel { FieldName = item.field, ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = between.First(), FieldValueConvertFunc = it => Convert.ToDateTime(it) }), new KeyValuePair(WhereType.And, new ConditionalModel { FieldName = item.field, ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = between.Last(), FieldValueConvertFunc = it => Convert.ToDateTime(it) }) } }; result.conditionalModel.Add(condItem); return result; } var resItem = new ConditionalCollections() { ConditionalList = new List>() { new KeyValuePair((item.logic.Equals("&&") ? WhereType.And : WhereType.Or), new ConditionalModel { FieldName = item.field, ConditionalType = conditionalType, FieldValue = item.fieldValue == null ? null : item.fieldValue.ToString() }) } }; result.conditionalModel.Add(resItem); return result; } }