Example #1
0
        public async Task <IActionResult> GetPageData(QueryBase queryBase)
        {
            var tableId     = queryBase.Value.Ext_ToInt32(); //表ID 保存在value中
            var tableDto    = (await _tableListService.GetByIdAsync(tableId)).data;
            var colDtos     = (await _tableColumnService.GetListAsync(item => item.TableId == tableId && item.ListVisible == 1)).data.OrderBy(item => item.ListOrder);
            var columnNames = string.Join(',', colDtos.Select(item => item.Name));

            if (tableDto.AllowView != 1)
            {
                return(Json(new DataTableModel()));
            }
            var queryCondition = "1=1";
            var count          = 0;

            foreach (var queryKey in Request.Query.Keys)
            {
                queryCondition = string.Empty;
                //首字母大写
                var firstUpper = queryKey.Substring(0, 1).ToUpper() + queryKey.Substring(1).ToLower();
                if (columnNames.Contains(firstUpper))
                {
                    queryCondition += $" ({queryKey}='{Request.Query[queryKey]}') and";
                    count++;
                }
            }
            if (count > 0)
            {
                queryCondition = queryCondition.Substring(0, queryCondition.Length - 3);
            }
            else
            {
                queryCondition = "1=1";
            }

            string condition = " and 1=1";

            if (queryBase.SearchKey.Ext_IsNotEmpty())
            {
                condition = string.Empty;
                var searchColDtos = (await _tableColumnService.GetListAsync(item => item.TableId == tableId && item.SearchVisible == 1)).data;
                var searchDic     = JsonConvert.DeserializeObject <Dictionary <string, string> >(queryBase.SearchKey);
                count = 0;
                foreach (var searchKey in searchDic.Keys)
                {
                    var keyValue     = searchDic[searchKey];
                    var searchColDto = searchColDtos.Where(x => x.Name == searchKey).FirstOrDefault();
                    if (searchColDto != null && keyValue.Ext_IsNotEmpty())
                    {
                        count++;
                        var searchColDataType = searchColDto.DataType;
                        if (searchColDataType == ColumnType.Custom || searchColDataType == ColumnType.String || searchColDataType == ColumnType.RichText)
                        {
                            condition += $" ({searchKey} like '%{keyValue}%') and";
                        }
                        else if (searchColDataType == ColumnType.Int || searchColDataType == ColumnType.Decimal || searchColDataType == ColumnType.Out)
                        {
                            condition += $" ({searchKey} = '{keyValue}') and";
                        }
                        else if (searchColDataType == ColumnType.MultiSelect) //暂不需要
                        {
                            var selectItems = keyValue.Split(',');
                        }
                        else if (searchColDataType == ColumnType.MultiSelect_Out) //暂不需要
                        {
                            var selectItems = keyValue.Split(',');
                        }
                        else if (searchColDataType == ColumnType.Datetime)
                        {
                            var endDate = searchDic[searchKey + "_end"];
                            condition += $" ({searchKey}>='{keyValue}' and {searchKey} <= '{endDate}') and";
                        }
                    }
                }
                if (count > 0)
                {
                    condition = " and " + condition.Substring(0, condition.Length - 3);
                }
                else
                {
                    condition = " and 1=1";
                }
            }
            if (queryBase.OrderBy.Ext_IsEmpty())
            {
                if (tableDto.DefaultSort.Ext_IsEmpty())
                {
                    queryBase.OrderBy = "Id";
                }
                else
                {
                    queryBase.OrderBy = tableDto.DefaultSort;
                }
            }

            var listData = await SysService.GetProcessedPageData(tableId, queryCondition + condition, queryBase);

            var data = new DataTableModel
            {
                draw            = queryBase.Draw,
                recordsTotal    = listData.recordsTotal,
                recordsFiltered = listData.recordsTotal,
                data            = listData.data
            };

            return(Json(data));
        }