using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using SqlSugar;
using Tiobon.Core.Common;
using Tiobon.Core.Common.DB;
using Tiobon.Core.Common.Extensions;
using Tiobon.Core.Model.Models;
namespace Tiobon.Core.DataAccess;
///
/// 报表辅助类
///
public static class ReportHelper
{
#region 文件下载
///
/// 生成文件,并通知用户下载(IQueryable)
///
///
///
/// 导出模块名称(JobSetting.xxx)
///
///
///
///
///
///
///
public static async Task SendFile(IQueryable list, string modelName, List exportFields, List exportFieldsWidth, string sort = null, string headText = "", string totalText = "", bool isNeedItemNo = false) where T : class
{
if (list == null)
throw new Exception("生成文件失败");
//生成文件至文件服务器
var fid = SnowFlakeSingle.Instance.NextId();
var path = $"{$"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}wwwroot{Path.DirectorySeparatorChar}files{Path.DirectorySeparatorChar}export{Path.DirectorySeparatorChar}{fid}{Path.DirectorySeparatorChar}"}";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var fname = $"{modelName}.xlsx";
list = string.IsNullOrEmpty(sort) ? list : list.OrderBy(sort);
list.IntoFileFromLinqExcel(path + fname, ",", exportFields, exportFieldsWidth, headText, totalText, isNeedItemNo);
using var _context = ContextFactory.CreateContext();
Ghre_Attachment fileAttachment = new Ghre_Attachment();
fileAttachment.Id = fid;
fileAttachment.AttachFileName = fname;
fileAttachment.CreateBy = App.User.ID;
fileAttachment.CreateTime = DateTime.Now;
fileAttachment.AttachmentName = fname;
fileAttachment.AttachFileExtension = "xlsx";
fileAttachment.AttachFileSize = 0;
fileAttachment.PhysicsPath = $"/files/export/{fid}/" + fname;
fileAttachment.AttachmentType = "xlsx";
await _context.Ghre_Attachment.AddAsync(fileAttachment);
await _context.SaveChangesAsync();
return fid;
}
#endregion
}