using YBEE.EQM.Core;

namespace YBEE.EQM.Application;


/// <summary>
/// 高中模拟划线成绩管理服务
/// </summary>
[ApiDescriptionSettings(Name = "ncee-score")]
[Route("ncee/score")]
public class NceeScoreAppService(INceeScoreService nceeScoreService) : IDynamicApiController
{
    private readonly INceeScoreService _nceeScoreService = nceeScoreService;

    /// <summary>
    /// 上传成绩（仅原始分，适用于五区联考或本区独立赋分）
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task UploadOnlyRawScore([FromForm] UploadNceeScoreInput input)
    {
        string fileExt = Path.GetExtension(input.File.FileName).ToLower();
        if (fileExt != ".xls" && fileExt != ".xlsx")
        {
            throw Oops.Oh(ErrorCode.E1010);
        }

        string filePath = Path.Combine(FileUtil.GetTempFileRoot(), $"{Guid.NewGuid()}{fileExt}");
        using FileStream fs = File.Create(filePath);
        await input.File.CopyToAsync(fs);
        await fs.FlushAsync();
        fs.Close();

        await _nceeScoreService.UploadOnlyRawScore(filePath, input.NceePlanId);
    }
    /// <summary>
    /// 上传成绩（带转换分和等级，适用于六校联考）
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task UploadWithConvertScore([FromForm] UploadNceeScoreInput input)
    {
        string fileExt = Path.GetExtension(input.File.FileName).ToLower();
        if (fileExt != ".xls" && fileExt != ".xlsx")
        {
            throw Oops.Oh(ErrorCode.E1010);
        }

        string filePath = Path.Combine(FileUtil.GetTempFileRoot(), $"{Guid.NewGuid()}{fileExt}");
        using FileStream fs = File.Create(filePath);
        await input.File.CopyToAsync(fs);
        await fs.FlushAsync();
        fs.Close();

        await _nceeScoreService.UploadWithConvertScore(filePath, input.NceePlanId);
    }
    /// <summary>
    /// 上传未选科原始成绩（适用于高一未选科）
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task UploadNoDirectionCourse([FromForm] UploadNceeScoreInput input)
    {
        string fileExt = Path.GetExtension(input.File.FileName).ToLower();
        if (fileExt != ".xls" && fileExt != ".xlsx")
        {
            throw Oops.Oh(ErrorCode.E1010);
        }

        string filePath = Path.Combine(FileUtil.GetTempFileRoot(), $"{Guid.NewGuid()}{fileExt}");
        using FileStream fs = File.Create(filePath);
        await input.File.CopyToAsync(fs);
        await fs.FlushAsync();
        fs.Close();

        await _nceeScoreService.UploadNoDirectionCourse(filePath, input.NceePlanId);
    }
    /// <summary>
    /// 执行模拟划线
    /// </summary>
    /// <param name="nceePlanId"></param>
    /// <returns></returns>
    public async Task Execute([FromQuery][Required] int nceePlanId)
    {
        await _nceeScoreService.Execute(nceePlanId);
    }
}
