Ejemplo n.º 1
0
        public Pageable AnalysisPostData(JqGridPostData jqGridPostData)
        {
            IEnumerable <FapColumn> fapColumns = _dbContext.Columns(jqGridPostData.QuerySet.TableName);

            jqGridPostData.Filters = jqGridPostData.Filters.IsPresent() ? jqGridPostData.Filters.Replace("query ", "select ") : "";
            //矫正当前页为0的情况
            if (jqGridPostData.Page < 0)
            {
                jqGridPostData.Page = 1;
            }
            QuerySet qs       = jqGridPostData.QuerySet;
            Pageable pageable = new Pageable(_dbContext)
            {
                TableName = qs.TableName, QueryCols = qs.QueryCols, HistoryTimePoint = jqGridPostData.TimePoint
            };

            //设置统计
            if (qs.Statsetlist != null && qs.Statsetlist.Any())
            {
                pageable.AddStatField(qs.Statsetlist);
            }
            if (qs.Parameters != null && qs.Parameters.Count > 0)
            {
                foreach (var param in qs.Parameters)
                {
                    pageable.AddParameter(param.ParamKey, param.ParamValue);
                }
            }
            //优先级高
            if (jqGridPostData.Sidx.IsPresent())
            {
                var sidxs = jqGridPostData.Sidx.SplitComma();
                foreach (var sidx in sidxs)
                {
                    if (sidx.IsPresent())
                    {
                        string[] odx = sidx.Trim().Split(' ');
                        if (odx != null)
                        {
                            var    col     = fapColumns.First(f => f.ColName == odx[0]);
                            string colName = col.ColName;
                            if (col.CtrlType == FapColumn.CTRL_TYPE_REFERENCE)
                            {
                                colName += "MC";
                            }
                            if (odx.Length > 1)
                            {
                                pageable.OrderBy.AddOrderByCondtion(colName, odx[1]);
                            }
                            else
                            {
                                pageable.OrderBy.AddOrderByCondtion(colName, jqGridPostData.Sord);
                            }
                        }
                    }
                }
            }
            if (qs.OrderByList != null && qs.OrderByList.Count > 0)
            {
                foreach (var orderby in qs.OrderByList)
                {
                    pageable.OrderBy.AddOrderByCondtion(orderby.Field, orderby.Direction);
                }
            }

            //构造初始化条件,如果没有过滤条件,又设置了初始化条件则设置初始化条件。或者设置了过滤条件且初始化条件为全局条件则同样设置where条件
            if (qs.GlobalWhere.IsPresent())
            {
                pageable.AddWhere(qs.GlobalWhere);
            }
            if (jqGridPostData.Filters.IsMissing() && qs.InitWhere.IsPresent())
            {
                pageable.AddWhere(qs.InitWhere);
            }

            //页面级条件
            JsonFilterToSql jfs = new JsonFilterToSql(_dbContext);

            if (jqGridPostData.PageCondition.IsPresent())
            {
                pageable.AddWhere(jfs.BuilderFilter(pageable.TableName, jqGridPostData.PageCondition), QuerySymbolEnum.AND);
            }
            //构造jqgrid过滤条件
            if (jqGridPostData.Filters.IsPresent())
            {
                pageable.AddWhere(jfs.BuilderFilter(pageable.TableName, jqGridPostData.Filters), QuerySymbolEnum.AND);
                //string filterWhere = JsonFilterToSql.BuildFilterCondition(fapColumns, jqGridPostData.Filters);
            }
            //事件处理
            //actionSimplepageable?.Invoke(pageable);
            pageable.CurrentPage = jqGridPostData.Page;
            pageable.PageSize    = jqGridPostData.Rows;
            //数据权限
            string dataWhere = DataWhere();

            if (dataWhere.IsPresent())
            {
                pageable.AddWhere(dataWhere);
            }
            //解析条件
            pageable.Where = AnalysisWhere(pageable.Where);
            return(pageable);

            string AnalysisWhere(string where)
            {
                if (where.IsMissing())
                {
                    return("");
                }
                //获得安全sql
                where = where.FilterDangerSql();
                //替换部门权限占位符
                if (where.IndexOf(FapPlatformConstants.DepartmentAuthority) > -1)
                {
                    where = where.Replace(FapPlatformConstants.DepartmentAuthority, DeptWhere());
                }
                return(where.ReplaceIgnoreCase("query", "select "));
            }

            string DeptWhere()
            {
                var roledepts = _rbacService.GetRoleDeptList(_applicationContext.CurrentRoleUid);

                if (roledepts.Any())
                {
                    return(string.Join(",", roledepts.Select(d => "'" + d.DeptUid + "'")));
                }
                else
                {
                    return("'meiyou'");
                }
            }

            //数据权限
            string DataWhere()
            {
                string where = string.Empty;
                var roleDatas = _rbacService.GetRoleDataList(_applicationContext.CurrentRoleUid);

                if (roleDatas != null && roleDatas.Any())
                {
                    var rd = roleDatas.FirstOrDefault <FapRoleData>(r => r.TableUid == qs.TableName);
                    if (rd != null)
                    {
                        where = rd.SqlCondition;
                        string          pattern = FapPlatformConstants.VariablePattern;
                        Regex           reg     = new Regex(pattern);
                        MatchCollection matchs  = reg.Matches(where);
                        foreach (var mtch in matchs)
                        {
                            int    length  = mtch.ToString().Length - 3;
                            string colName = mtch.ToString().Substring(2, length);
                            if (colName.EqualsWithIgnoreCase("DeptUid"))
                            {
                                where = where.Replace(mtch.ToString(), _applicationContext.DeptUid);
                            }
                            else if (colName.EqualsWithIgnoreCase("CurrentRoleUid"))
                            {
                                where = where.Replace(mtch.ToString(), _applicationContext.CurrentRoleUid);
                            }
                            else if (colName.EqualsWithIgnoreCase("EmpUid"))
                            {
                                where = where.Replace(mtch.ToString(), _applicationContext.EmpUid);
                            }
                            else if (colName.EqualsWithIgnoreCase("DeptCode"))
                            {
                                string deptCode = _applicationContext.DeptCode;
                                if (deptCode.IsMissing())
                                {
                                    OrgDept dept = _dbContext.Get <OrgDept>(_applicationContext.DeptUid);
                                    deptCode = dept.DeptCode;
                                }
                                where = where.Replace(mtch.ToString(), deptCode);
                            }
                        }
                    }
                }
                return(where);
            }
        }