예제 #1
0
        private void ExportContentExcel(int channelId, List <int> contentIdList, List <string> displayAttributes, bool isPeriods, string dateFrom, string dateTo, ETriState checkedState, out string fileName)
        {
            var nodeInfo = ChannelManager.GetChannelInfo(SiteId, channelId);

            fileName = $"{nodeInfo.ChannelName}.csv";
            var filePath = PathUtils.GetTemporaryFilesPath(fileName);

            ExcelObject.CreateExcelFileForContents(filePath, SiteInfo, nodeInfo, contentIdList, displayAttributes, isPeriods, dateFrom, dateTo, checkedState);
        }
        public IHttpActionResult Submit()
        {
            try
            {
                var request = new RequestImpl();

                var downloadUrl = string.Empty;

                var siteId            = request.GetPostInt("siteId");
                var channelId         = request.GetPostInt("channelId");
                var exportType        = request.GetPostString("exportType");
                var isAllCheckedLevel = request.GetPostBool("isAllCheckedLevel");
                var checkedLevelKeys  = request.GetPostObject <List <int> >("checkedLevelKeys");
                var isAllDate         = request.GetPostBool("isAllDate");
                var startDate         = request.GetPostDateTime("startDate", DateTime.Now);
                var endDate           = request.GetPostDateTime("endDate", DateTime.Now);
                var columnNames       = request.GetPostObject <List <string> >("columnNames");

                if (!request.IsAdminLoggin ||
                    !request.AdminPermissions.HasChannelPermissions(siteId, channelId,
                                                                    ConfigManager.ChannelPermissions.ChannelEdit))
                {
                    return(Unauthorized());
                }

                var siteInfo = SiteManager.GetSiteInfo(siteId);
                if (siteInfo == null)
                {
                    return(BadRequest("无法确定内容对应的站点"));
                }

                var channelInfo = ChannelManager.GetChannelInfo(siteId, channelId);
                if (channelInfo == null)
                {
                    return(BadRequest("无法确定内容对应的栏目"));
                }

                var columns       = ContentManager.GetContentColumns(siteInfo, channelInfo, true);
                var pluginIds     = PluginContentManager.GetContentPluginIds(channelInfo);
                var pluginColumns = PluginContentManager.GetContentColumns(pluginIds);

                var contentInfoList = new List <ContentInfo>();
                var count           = ContentManager.GetCount(siteInfo, channelInfo);
                var pages           = Convert.ToInt32(Math.Ceiling((double)count / siteInfo.Additional.PageSize));
                if (pages == 0)
                {
                    pages = 1;
                }

                if (count > 0)
                {
                    for (var page = 1; page <= pages; page++)
                    {
                        var offset = siteInfo.Additional.PageSize * (page - 1);
                        var limit  = siteInfo.Additional.PageSize;

                        var pageContentIds = ContentManager.GetContentIdList(siteInfo, channelInfo, offset, limit);

                        var sequence = offset + 1;

                        foreach (var contentId in pageContentIds)
                        {
                            var contentInfo = ContentManager.GetContentInfo(siteInfo, channelInfo, contentId);
                            if (contentInfo == null)
                            {
                                continue;
                            }

                            if (!isAllCheckedLevel)
                            {
                                var checkedLevel = contentInfo.CheckedLevel;
                                if (contentInfo.IsChecked)
                                {
                                    checkedLevel = siteInfo.Additional.CheckContentLevel;
                                }
                                if (!checkedLevelKeys.Contains(checkedLevel))
                                {
                                    continue;
                                }
                            }

                            if (!isAllDate)
                            {
                                if (contentInfo.AddDate < startDate || contentInfo.AddDate > endDate)
                                {
                                    continue;
                                }
                            }

                            contentInfoList.Add(ContentManager.Calculate(sequence++, contentInfo, columns, pluginColumns));
                        }
                    }

                    if (contentInfoList.Count > 0)
                    {
                        if (exportType == "zip")
                        {
                            var fileName     = $"{channelInfo.ChannelName}.zip";
                            var filePath     = PathUtils.GetTemporaryFilesPath(fileName);
                            var exportObject = new ExportObject(siteId, request.AdminName);
                            contentInfoList.Reverse();
                            if (exportObject.ExportContents(filePath, contentInfoList))
                            {
                                downloadUrl = PageUtils.GetTemporaryFilesUrl(fileName);
                            }
                        }
                        else if (exportType == "excel")
                        {
                            var fileName = $"{channelInfo.ChannelName}.csv";
                            var filePath = PathUtils.GetTemporaryFilesPath(fileName);
                            ExcelObject.CreateExcelFileForContents(filePath, siteInfo, channelInfo, contentInfoList, columnNames);
                            downloadUrl = PageUtils.GetTemporaryFilesUrl(fileName);
                        }
                    }
                }

                return(Ok(new
                {
                    Value = downloadUrl,
                    IsSuccess = !string.IsNullOrEmpty(downloadUrl)
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }