namespace Tiobon.Core.Common.Helper;

public class ExtensionHelper
{


    public static async Task<List<ResumeFormColumn1>> QueryResumeFormColumn(ISqlSugarClient Db, string groupType, long resumeTemplateID = 0)
    {
        resumeTemplateID = await Db.Ado.GetLongAsync("select top 1 Id from Ghrh_ResumeTemplate where IsEnable=1 and IsPublish=1");
        var sql = @$"DECLARE @ResumeTemplateID   BIGINT = {resumeTemplateID}

            SELECT A.ColumnName, A.ResumeInfoColumnName ColumnNameDesc, D.GroupType, A.MapTableName, A.MapColumnName, 
A.DataType, A.ColumnType, A.IsRequired, A.DataSourceType, A.DataSource, A.DataSourceID
            FROM Ghrh_ResumeInfoColumn A
                 LEFT JOIN Ghrh_ResumeTemplateInfoGroupColumn B
                    ON     B.ResumeInfoColumnID = A.ID
                       AND B.IsDisplay = 1
                       AND B.IsEnable = 1
                       AND A.IsEnable = 1
                 JOIN Ghrh_ResumeTemplateInfoGroup C
                    ON B.ResumeTemplateInfoGroupID = C.Id
                 JOIN Ghrh_ResumeInfoGroup D
                    ON C.ResumeInfoGroupId = D.ID AND D.IsEnable = 1
            WHERE C.ResumeTemplateID = @ResumeTemplateID 
             AND D.GroupType = '{groupType}' and A.ColumnName !='AttachmentIDs'
            ORDER BY D.GroupType, b.SortNo";

        return await Db.Ado.SqlQueryAsync<ResumeFormColumn1>(sql);
    }
    public static async Task<ServiceResult<string>> GenerateReumeArchivesImportTemplate(ISqlSugarClient Db, string groupType, string groupName)
    {
        var physicsPath = $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}wwwroot";
        var path = $"{$"{Path.DirectorySeparatorChar}files{Path.DirectorySeparatorChar}ExcelTemplate{Path.DirectorySeparatorChar}"}";
        if (!Directory.Exists(physicsPath + path))
            Directory.CreateDirectory(physicsPath + path);

        DataTable dt = new DataTable("TempTable");

        var formColumns = await QueryResumeFormColumn(Db, groupType);
        string fileName = path + SnowFlakeSingle.instance.getID() + ".xlsx";
        if (groupType == "Family")
            dt.Columns.Add("简历姓名", typeof(string));
        else
            dt.Columns.Add("姓名", typeof(string));
        formColumns
              .ToList()
              .ForEach(x =>
              {
                  if (!dt.Columns.Contains(x.ColumnNameDesc))
                      dt.Columns.Add(x.ColumnNameDesc, typeof(string));
              });

        var dr = dt.NewRow();
        dt.Rows.Add(dr);
        NPOIHelper.ExportExcel(dt, null, groupName, physicsPath + fileName);

        var physicsPath1 = physicsPath + fileName;
        //if (dataSourceLists.Any())
        //    physicsPath1 = physicsPath + path + newFileName;
        return ServiceResult<string>.OprateSuccess(groupName + "_" + DateTimeHelper.ConvertToSecondString1(DateTime.Now) + ".xlsx", physicsPath1);
    }
}
