using JNPF.Common.Const; using JNPF.Common.Extension; using JNPF.Common.Models; using SqlSugar; namespace JNPF.Common.Security; /// /// 高级查询帮助类. /// public class SuperQueryHelper { /// /// 组装高级查询信息. /// /// 查询条件json. /// 取代内容. /// 实体信息. /// 表类型 0-主表,1-子表,2-副表. public static List GetSuperQueryInput(string superQueryJson, string replaceContent, EntityInfo entityInfo, int tableType) { SuperQueryModel result = new SuperQueryModel(); SuperQueryModel? model = string.IsNullOrEmpty(superQueryJson) ? null : superQueryJson.ToObject(); var queryList = new List(); if (model != null) { var matchLogic = model.matchLogic; var whereType = matchLogic.Equals("AND") ? WhereType.And : WhereType.Or; foreach (var item in model.conditionJson) { var field = string.Empty; switch (tableType) { case 1: if (item.field.Contains(replaceContent)) { field = entityInfo.Columns.Find(it => it.PropertyName.Equals(item.field.Replace(replaceContent, "").ToUpperCase()))?.DbColumnName; } break; case 2: if (item.field.Contains(replaceContent) && item.field.Contains("_jnpf_")) { var queryField = item.field.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(item.field.ToUpperCase()))?.DbColumnName; break; } if (string.IsNullOrEmpty(field)) { continue; } var conditionalType = ConditionalType.IsNullOrEmpty; switch (item.jnpfKey) { case JnpfKeyConst.COMINPUT: case JnpfKeyConst.TEXTAREA: case JnpfKeyConst.CHECKBOX: case JnpfKeyConst.SELECT: case JnpfKeyConst.TIME: case JnpfKeyConst.DEPSELECT: case JnpfKeyConst.GROUPSELECT: case JnpfKeyConst.POSSELECT: case JnpfKeyConst.USERSELECT: case JnpfKeyConst.ROLESELECT: case JnpfKeyConst.TREESELECT: case JnpfKeyConst.RELATIONFORM: case JnpfKeyConst.RELATIONFORMATTR: case JnpfKeyConst.POPUPSELECT: case JnpfKeyConst.POPUPATTR: case JnpfKeyConst.CALCULATE: case JnpfKeyConst.CREATEUSER: case JnpfKeyConst.MODIFYUSER: item.fieldValue = item.fieldValue?.ToString().Replace("\r\n", string.Empty); switch (item.symbol) { case "==": // 等于 conditionalType = ConditionalType.Equal; break; case "like": // 包含 conditionalType = ConditionalType.Like; break; case "notLike": // 不包含 conditionalType = ConditionalType.NoLike; break; case ">=": // 大于等于 conditionalType = ConditionalType.GreaterThanOrEqual; break; case "<=": // 小于等于 conditionalType = ConditionalType.LessThanOrEqual; break; case "<": // 小于 conditionalType = ConditionalType.LessThan; break; case "<>": // 不等于 conditionalType = ConditionalType.NoEqual; break; case ">": // 大于 conditionalType = ConditionalType.GreaterThan; break; } queryList.Add(ControlAdvancedQueryAssembly(whereType, item.jnpfKey, field, item.fieldValue.ParseToString(), conditionalType, true)); switch (item.symbol) { case "notLike": // 不包含 case "<>": // 不等于 conditionalType = ConditionalType.IsNullOrEmpty; queryList.Add(ControlAdvancedQueryAssembly(WhereType.Or, item.jnpfKey, field, null, conditionalType)); break; } break; case JnpfKeyConst.DATE: 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": // 包含 conditionalType = ConditionalType.Equal; break; case "notLike": // 不包含 conditionalType = ConditionalType.NoEqual; break; } queryList.Add(ControlAdvancedQueryAssembly(whereType, item.jnpfKey, field, item.fieldValue.ToObject().ToString(), conditionalType, true)); switch (item.symbol) { case "notLike": // 不包含 case "<>": // 不等于 conditionalType = ConditionalType.IsNullOrEmpty; queryList.Add(ControlAdvancedQueryAssembly(WhereType.Or, item.jnpfKey, field, null, conditionalType)); break; } break; case JnpfKeyConst.CREATETIME: switch (item.symbol) { case ">=": // 大于等于 conditionalType = ConditionalType.GreaterThanOrEqual; break; case ">": // 大于 conditionalType = ConditionalType.GreaterThan; break; case "==": // 等于 conditionalType = ConditionalType.Equal; break; case "<=": // 小于等于 conditionalType = ConditionalType.LessThan; break; case "<": // 小于 conditionalType = ConditionalType.LessThan; break; case "<>": // 不等于 conditionalType = ConditionalType.NoEqual; break; case "like": // 包含 conditionalType = ConditionalType.Equal; break; case "notLike": // 不包含 conditionalType = ConditionalType.NoEqual; break; } queryList.Add(ControlAdvancedQueryAssembly(whereType, item.jnpfKey, field, string.Format("{0:yyyy-MM-dd HH:mm:ss}", item.fieldValue.ToObject()), conditionalType, true)); switch (item.symbol) { case "notLike": // 不包含 case "<>": // 不等于 conditionalType = ConditionalType.IsNullOrEmpty; queryList.Add(ControlAdvancedQueryAssembly(WhereType.Or, item.jnpfKey, field, null, conditionalType)); break; } break; case JnpfKeyConst.MODIFYTIME: 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": // 包含 conditionalType = ConditionalType.Equal; break; case "notLike": // 不包含 conditionalType = ConditionalType.NoEqual; break; } queryList.Add(ControlAdvancedQueryAssembly(whereType, item.jnpfKey, field, string.Format("{0:yyyy-MM-dd HH:mm:ss}", item.fieldValue.ToObject()), conditionalType, true)); switch (item.symbol) { case "notLike": // 不包含 case "<>": // 不等于 conditionalType = ConditionalType.IsNullOrEmpty; queryList.Add(ControlAdvancedQueryAssembly(WhereType.Or, item.jnpfKey, field, null, conditionalType)); break; } break; case JnpfKeyConst.NUMINPUT: case JnpfKeyConst.SWITCH: case JnpfKeyConst.RADIO: case JnpfKeyConst.POPUPTABLESELECT: case JnpfKeyConst.COMSELECT: case JnpfKeyConst.BILLRULE: case JnpfKeyConst.CURRDEPT: case JnpfKeyConst.CURRPOSITION: 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": // 包含 conditionalType = ConditionalType.Like; break; case "notLike": // 不包含 conditionalType = ConditionalType.NoLike; break; } queryList.Add(ControlAdvancedQueryAssembly(whereType, item.jnpfKey, field, item.fieldValue.ParseToString(), conditionalType, true)); break; case JnpfKeyConst.USERSSELECT: if (item.symbol.Equals("==")) item.symbol = "like"; if (item.symbol.Equals("<>")) item.symbol = "notLike"; if (item.fieldValue != null && (item.symbol.Equals("like") || item.symbol.Equals("notLike"))) { var rIdList = GetUserRelationByUserId(item.fieldValue.ToString()); var objIdList = new List() { item.fieldValue.ToString() }; rIdList.ForEach(x => { if (x["OBJECTTYPE"].Equals("Organize")) { objIdList.Add(x["OBJECTID"] + "--company"); objIdList.Add(x["OBJECTID"] + "--department"); } else { objIdList.Add(x["OBJECTID"] + "--" + x["OBJECTTYPE"].ToLower()); } }); var whereList = new List>(); for (var i = 0; i < objIdList.Count(); i++) { if (i == 0) { var queryOr = new ConvertSuperQuery(); queryOr.whereType = WhereType.And; queryOr.jnpfKey = item.jnpfKey; queryOr.field = field; queryOr.fieldValue = objIdList[i]; queryOr.conditionalType = item.symbol.Equals("like") ? ConditionalType.Like : ConditionalType.NoLike; queryOr.mainWhere = true; queryList.Add(queryOr); } else { var queryOr = new ConvertSuperQuery(); queryOr.whereType = item.symbol.Equals("like") ? WhereType.Or : WhereType.And; queryOr.jnpfKey = item.jnpfKey; queryOr.field = field; queryOr.fieldValue = objIdList[i]; queryOr.conditionalType = item.symbol.Equals("like") ? ConditionalType.Like : ConditionalType.NoLike; queryOr.mainWhere = true; queryList.Add(queryOr); } } if (item.symbol.Equals("notLike")) { var queryOr = new ConvertSuperQuery(); queryOr.whereType = WhereType.Or; queryOr.jnpfKey = item.jnpfKey; queryOr.field = field; queryOr.fieldValue = null; queryOr.conditionalType = ConditionalType.EqualNull; queryOr.mainWhere = true; queryList.Add(queryOr); } } continue; case JnpfKeyConst.ADDRESS: case JnpfKeyConst.CASCADER: switch (item.symbol) { case ">=": // 大于等于 conditionalType = ConditionalType.GreaterThanOrEqual; break; case ">": // 大于 conditionalType = ConditionalType.GreaterThan; break; case "==": // 等于 conditionalType = ConditionalType.Equal; item.fieldValue = item.fieldValue.ToObject>().ToJsonString(); break; case "<=": // 小于等于 conditionalType = ConditionalType.LessThanOrEqual; break; case "<": // 小于 conditionalType = ConditionalType.LessThan; break; case "<>": // 不等于 conditionalType = ConditionalType.NoEqual; item.fieldValue = item.fieldValue.ToObject>().ToJsonString(); break; case "like": // 包含 conditionalType = ConditionalType.Like; item.fieldValue = item.fieldValue.ToObject>().Last(); break; case "notLike": // 不包含 conditionalType = ConditionalType.NoLike; item.fieldValue = item.fieldValue.ToObject>().Last(); break; } queryList.Add(ControlAdvancedQueryAssembly(whereType, item.jnpfKey, field, item.fieldValue.ParseToString(), conditionalType, true)); switch (item.symbol) { case "notLike": // 不包含 conditionalType = ConditionalType.IsNullOrEmpty; queryList.Add(ControlAdvancedQueryAssembly(WhereType.Or, item.jnpfKey, field, null, conditionalType)); break; case "<>": // 不等于 conditionalType = ConditionalType.IsNullOrEmpty; queryList.Add(ControlAdvancedQueryAssembly(WhereType.Or, item.jnpfKey, field, null, conditionalType)); break; } break; case JnpfKeyConst.CURRORGANIZE: switch (item.symbol) { case ">=": // 大于等于 conditionalType = ConditionalType.GreaterThanOrEqual; break; case ">": // 大于 conditionalType = ConditionalType.GreaterThan; break; case "==": // 等于 conditionalType = ConditionalType.Equal; item.fieldValue = item.fieldValue.ToObject>().Last(); break; case "<=": // 小于等于 conditionalType = ConditionalType.LessThanOrEqual; break; case "<": // 小于 conditionalType = ConditionalType.LessThan; break; case "<>": // 不等于 conditionalType = ConditionalType.NoEqual; item.fieldValue = item.fieldValue.ToObject>().Last(); break; case "like": // 包含 conditionalType = ConditionalType.Equal; item.fieldValue = item.fieldValue.ToObject>().Last(); break; case "notLike": // 不包含 conditionalType = ConditionalType.NoEqual; item.fieldValue = item.fieldValue.ToObject>().Last(); break; } queryList.Add(ControlAdvancedQueryAssembly(whereType, item.jnpfKey, field, item.fieldValue.ParseToString(), conditionalType, true)); switch (item.symbol) { case "notLike": // 不包含 conditionalType = ConditionalType.IsNullOrEmpty; queryList.Add(ControlAdvancedQueryAssembly(WhereType.Or, item.jnpfKey, field, null, conditionalType)); break; case "<>": // 不等于 conditionalType = ConditionalType.IsNullOrEmpty; queryList.Add(ControlAdvancedQueryAssembly(WhereType.Or, item.jnpfKey, field, null, conditionalType)); break; } break; } } } return queryList; } public static ConvertSuperQuery ControlAdvancedQueryAssembly(WhereType whereType, string jnpfKey, string field, string fieldValue, ConditionalType conditionalType, bool mainWhere = false, string symbol = "") { return new ConvertSuperQuery { whereType = whereType, jnpfKey = jnpfKey, field = field, fieldValue = fieldValue, conditionalType = conditionalType, mainWhere = mainWhere, symbol = symbol }; } /// /// 组装高级查询条件. /// /// public static List GetSuperQueryJson(List list) { List conModels = new List(); list.FindAll(it => it.mainWhere).ForEach(item => { ConditionalCollections conditional = new ConditionalCollections(); conditional.ConditionalList = new List>(); var sameList = list.FindAll(it => it.field.Equals(item.field)); sameList.ForEach(items => { conditional.ConditionalList.Add(new KeyValuePair(items.whereType, new ConditionalModel { FieldName = items.field, ConditionalType = items.conditionalType, FieldValue = !string.IsNullOrEmpty(items.fieldValue) ? items.fieldValue : null })); }); conModels.Add(conditional); }); return conModels; } /// /// 根据用户Id,获取用户关系id集合. /// /// /// private static List> GetUserRelationByUserId(string fieldValue) { // 获取数据库连接选项 ConnectionStringsOptions connectionStrings = App.GetConfig("ConnectionStrings", true); SqlSugarClient db = new SqlSugarClient(new ConnectionConfig { ConnectionString = connectionStrings.ConnectString, // string.Format(connectionStrings.DefaultConnection, connectionStrings.DBName), DbType = (DbType)Enum.Parse(typeof(DbType), connectionStrings.DBType), IsAutoCloseConnection = true, ConfigId = connectionStrings.ConfigId, InitKeyType = InitKeyType.Attribute, MoreSettings = new ConnMoreSettings() { IsAutoRemoveDataCache = true // 自动清理缓存 } }); var sql = string.Format("SELECT F_OBJECTID OBJECTID,F_OBJECTTYPE OBJECTTYPE FROM BASE_USERRELATION WHERE F_USERID='{0}'", fieldValue.ToString().Replace("--user", string.Empty)); var res = db.SqlQueryable(sql).ToDataTable(); return res.ToObject>>(); } }