예제 #1
0
        public IHttpActionResult List()
        {
            try
            {
                var request = Context.GetCurrentRequest();
                var siteId  = request.GetQueryInt("siteId");
                if (!request.IsAdminLoggin || !request.AdminPermissions.HasSitePermissions(siteId, ApplicationUtils.PluginId))
                {
                    return(Unauthorized());
                }

                var pageType  = request.GetQueryString("pageType");
                var stateList = new List <DataState>();
                if (StringUtils.EqualsIgnoreCase(pageType, ApplicationUtils.PageTypeAccept))
                {
                    stateList.Add(DataState.New);
                }
                else if (StringUtils.EqualsIgnoreCase(pageType, ApplicationUtils.PageTypeReply))
                {
                    stateList.Add(DataState.Accepted);
                    stateList.Add(DataState.Redo);
                }
                else if (StringUtils.EqualsIgnoreCase(pageType, ApplicationUtils.PageTypeCheck))
                {
                    stateList.Add(DataState.Replied);
                }
                else
                {
                    var state = request.GetQueryString("state");
                    if (!string.IsNullOrEmpty(state))
                    {
                        stateList.Add(DataState.ToDataState(state));
                    }
                }

                var page         = request.GetQueryInt("page", 1);
                var isSearch     = request.GetQueryBool("isSearch");
                var keyword      = request.GetQueryString("keyword");
                var departmentId = request.GetQueryInt("departmentId");

                int totalCount;
                var isSiteAdmin          = request.AdminPermissions.IsSiteAdmin(siteId);
                var userDepartmentIdList = new List <int>();

                if (isSiteAdmin)
                {
                    if (isSearch)
                    {
                        totalCount = DataDao.GetCount(siteId, stateList, keyword, departmentId);
                    }
                    else
                    {
                        keyword      = string.Empty;
                        departmentId = 0;
                        totalCount   = DataCountManager.GetCount(siteId, stateList);
                    }
                }
                else
                {
                    userDepartmentIdList = DepartmentManager.GetDepartmentIdList(siteId, request.AdminName);
                    totalCount           = DataDao.GetUserCount(siteId, stateList, keyword, departmentId, userDepartmentIdList);
                }

                var pages = Convert.ToInt32(Math.Ceiling((double)totalCount / ApplicationUtils.PageSize));
                if (pages == 0)
                {
                    pages = 1;
                }
                if (page > pages)
                {
                    page = pages;
                }

                List <DataInfo> dataInfoList;
                if (totalCount == 0)
                {
                    dataInfoList = new List <DataInfo>();
                }
                else
                {
                    if (isSiteAdmin)
                    {
                        if (totalCount <= ApplicationUtils.PageSize)
                        {
                            dataInfoList = DataDao.GetDataInfoList(siteId, stateList, keyword, departmentId, 0, totalCount);
                        }
                        else
                        {
                            if (page == 0)
                            {
                                page = 1;
                            }
                            var offset = (page - 1) * ApplicationUtils.PageSize;
                            var limit  = totalCount - offset > ApplicationUtils.PageSize
                                ? ApplicationUtils.PageSize
                                : totalCount - offset;
                            dataInfoList = DataDao.GetDataInfoList(siteId, stateList, keyword, departmentId, offset, limit);
                        }
                    }
                    else
                    {
                        if (page == 0)
                        {
                            page = 1;
                        }
                        var offset = (page - 1) * ApplicationUtils.PageSize;
                        var limit  = totalCount - offset > ApplicationUtils.PageSize
                            ? ApplicationUtils.PageSize
                            : totalCount - offset;
                        dataInfoList = DataDao.GetUserDataInfoList(siteId, stateList, keyword, departmentId, userDepartmentIdList, offset, limit);
                    }
                }

                List <DepartmentInfo>    departmentInfoList = null;
                Dictionary <string, int> countDict          = null;
                Settings settings = null;

                if (page == 1)
                {
                    if (isSiteAdmin)
                    {
                        departmentInfoList = DepartmentManager.GetDepartmentInfoList(siteId);
                    }
                    else
                    {
                        departmentInfoList = new List <DepartmentInfo>();
                        foreach (var userDepartmentId in userDepartmentIdList)
                        {
                            departmentInfoList.Add(DepartmentManager.GetDepartmentInfo(siteId, userDepartmentId));
                        }
                    }

                    countDict = new Dictionary <string, int>
                    {
                        { "All", DataCountManager.GetTotalCount(siteId) }
                    };
                    foreach (var state in DataState.AllStates)
                    {
                        countDict.Add(state.Value, DataCountManager.GetCount(siteId, state));
                    }

                    settings = ApplicationUtils.GetSettings(siteId);
                }

                return(Ok(new
                {
                    Value = dataInfoList,
                    Count = totalCount,
                    Pages = pages,
                    Page = page,
                    DepartmentInfoList = departmentInfoList,
                    CountDict = countDict,
                    Settings = settings
                }));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }