using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Services;
using NPP.SmartSchedue.Api.Contracts.Services.Time;
using NPP.SmartSchedue.Api.Contracts.Services.Time.Input;
using NPP.SmartSchedue.Api.Contracts.Services.Time.Output;
using NPP.SmartSchedue.Api.Repositories.Time;
using NPP.SmartSchedue.Api.Contracts.Domain.Time;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;

namespace NPP.SmartSchedue.Api.Services.Time;

/// <summary>
/// 基础日历服务
/// </summary>
[DynamicApi(Area = "app")]
public class CalendarService : BaseService, ICalendarService, IDynamicApi
{
    private readonly CalendarRepository _calendarRepository;

    public CalendarService(CalendarRepository calendarRepository)
    {
        _calendarRepository = calendarRepository;
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<CalendarGetOutput> GetAsync(long id)
    {
        var output = await _calendarRepository.Select
            .WhereDynamic(id)
            .ToOneAsync<CalendarGetOutput>();

        return output;
    }

    /// <summary>
    /// 查询分页
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<PageOutput<CalendarGetPageOutput>> GetPageAsync(PageInput<CalendarGetPageInput> input)
    {
        var list = await _calendarRepository.Select
            .WhereIf(input.Filter?.StartDate.HasValue == true, a => a.Date >= input.Filter.StartDate.Value)
            .WhereIf(input.Filter?.EndDate.HasValue == true, a => a.Date <= input.Filter.EndDate.Value)
            .WhereIf(!string.IsNullOrEmpty(input.Filter?.DateType), a => a.DateType.Contains(input.Filter.DateType))
            .Count(out var total)
            .OrderByDescending(a => a.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync<CalendarGetPageOutput>();

        var data = new PageOutput<CalendarGetPageOutput>()
        {
            List = list,
            Total = total
        };

        return data;
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<long> AddAsync(CalendarAddInput input)
    {
        var entity = Mapper.Map<CalendarEntity>(input);
        var result = await _calendarRepository.InsertAsync(entity);
        return result.Id;
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task UpdateAsync(CalendarUpdateInput input)
    {
        var entity = await _calendarRepository.GetAsync(input.Id);
        Mapper.Map(input, entity);
        await _calendarRepository.UpdateAsync(entity);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task DeleteAsync(long id)
    {
        await _calendarRepository.DeleteAsync(id);
    }

    /// <summary>
    /// 软删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task SoftDeleteAsync(long id)
    {
        await _calendarRepository.SoftDeleteAsync(id);
    }

    /// <summary>
    /// 批量软删除
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    public async Task BatchSoftDeleteAsync(long[] ids)
    {
        await _calendarRepository.SoftDeleteAsync(ids);
    }
} 