using Furion.DatabaseAccessor.Extensions;
using YBEE.EQM.Core;

namespace YBEE.EQM.Application;

/// <summary>
/// 监测发布内容管理服务
/// </summary>
public class ExamDataPublishService : IExamDataPublishService, ITransient
{
    private readonly IRepository<ExamDataPublish> _rep;
    public ExamDataPublishService(IRepository<ExamDataPublish> rep)
    {
        _rep = rep;
    }

    #region 创建更新
    /// <summary>
    /// 添加发布内容
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Add(AddExamDataPublishInput input)
    {
        //if (await _rep.AnyAsync(t => t.ExamPlanId == input.ExamPlanId && t.Type == input.Type))
        //{
        //    throw Oops.Oh(ErrorCode.E2002, "该类型");
        //}
        var item = input.Adapt<ExamDataPublish>();
        await item.InsertAsync();
    }
    /// <summary>
    /// 更新发布内容
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Update(UpdateExamDataPublishInput input)
    {
        var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
        item.Name = input.Name;
        item.Remark = input.Remark;
        await item.UpdateIncludeAsync(new[] { nameof(item.Name), nameof(item.Remark) });
    }
    /// <summary>
    /// 删除发布内容
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Del(BaseId input)
    {
        var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
        if (item.Status != PublishStatus.UNPUBLISH)
        {
            throw Oops.Oh(ErrorCode.E3001);
        }
        await item.DeleteAsync();
    }
    #endregion

    #region 状态处理
    /// <summary>
    /// 发布
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Publish(BaseId input)
    {
        var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
        if (item.Status == PublishStatus.PUBLISHED)
        {
            throw Oops.Oh(ErrorCode.E2006);
        }
        item.Status = PublishStatus.PUBLISHED;
        item.PublishTime = DateTime.Now;
        item.PublishSysUserId = CurrentSysUserInfo.SysUserId;
        await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.PublishTime), nameof(item.PublishSysUserId) });
    }
    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Unpublish(BaseId input)
    {
        var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
        if (item.Status != PublishStatus.PUBLISHED)
        {
            throw Oops.Oh(ErrorCode.E2006);
        }
        item.Status = PublishStatus.UNPUBLISH; // 设置为未发布
        await item.UpdateIncludeAsync(new[] { nameof(item.Status) });
    }
    #endregion

    #region 查询
    /// <summary>
    /// 根据ID获取监测数据发布内容
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<ExamDataPublishOutput> GetById(int id)
    {
        var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
        return item.Adapt<ExamDataPublishOutput>();
    }
    /// <summary>
    /// 根据监测计划ID获取数据发布内容列表
    /// </summary>
    /// <param name="examPlanId"></param>
    /// <returns></returns>
    public async Task<List<ExamDataPublishOutput>> GetListByExamPlanId(int examPlanId)
    {
        var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId).ProjectToType<ExamDataPublishOutput>().OrderBy(t => t.Type).ToListAsync();
        return items;
    }
    #endregion

    #region 上传匹配

    #endregion
}
