예제 #1
0
        public IActionResult SelectPage(int pageIndex, int pageSize, [FromBody] List <FilterStr> filter, string order, bool isAsc)
        {
            try
            {
                string _order = string.Empty;
                Expression <Func <CameraView, string> > orderby    = null;
                Expression <Func <CameraView, int> >    orderbyint = null;
                Expression <Func <CameraView, bool> > where = null;

                GetOrder(order, ref _order, ref orderby, ref orderbyint);
                if (filter != null && filter.Count > 0)
                {
                    string _filter = string.Empty;
                    foreach (FilterStr item in filter)
                    {
                        _filter += "p." + item.FieldName;
                        _filter  = SwitchOper.SwitchOperation(_filter, item);
                        switch (item.Value.GetType().Name.ToString())
                        {
                        case "String":
                        case "string":
                            if (item.Operation == OperationStr.Like)
                            {
                                _filter += item.Value;
                                _filter += "\")";
                            }
                            else if (item.Operation == OperationStr.Equal || item.Operation == OperationStr.NotEqual)
                            {
                                _filter += "\"";
                                _filter += item.Value;
                                _filter += "\"";
                            }
                            else
                            {
                                return(Json(new
                                {
                                    state = "-1",
                                    msg = "条件无效!"
                                }));
                            }
                            break;

                        case "Int32":
                        case "Int64":
                        case "Int":
                        case "Double":
                            if (item.Operation == OperationStr.Like)
                            {
                                return(Json(new
                                {
                                    state = "-1",
                                    msg = "条件无效!"
                                }));
                            }
                            else
                            {
                                _filter += item.Value;
                            }
                            break;

                        case "DateTime":
                            if (item.Operation == OperationStr.Like)
                            {
                                return(Json(new
                                {
                                    state = "-1",
                                    msg = "条件无效!"
                                }));
                            }
                            else
                            {
                                _filter += "DateTime.Parse(\"";
                                _filter += item.Value;
                                _filter += "\")";
                            }
                            break;

                        default:
                            break;
                        }
                        _filter += "&&";
                    }
                    _filter = _filter.Substring(0, _filter.Length - 2);
                    where   = new Interpreter().ParseAsExpression <Func <CameraView, bool> >(_filter, "p");
                }

                if (orderbyint == null)
                {
                    return(Json(new
                    {
                        table = _repository.Query <CameraView, string>(pageIndex, pageSize, where, orderby, null, isAsc),
                        state = "0",
                        msg = "操作成功!"
                    }));
                }
                else
                {
                    return(Json(new
                    {
                        table = _repository.Query <CameraView, int>(pageIndex, pageSize, where, orderbyint, null, isAsc),
                        state = "0",
                        msg = "操作成功!"
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    state = "-1",
                    msg = "非法操作!"
                }));
            }
        }