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

namespace NPP.SmartSchedue.Api.Services.Personnel;

/// <summary>
/// 人员工作限制服务
/// </summary>
[DynamicApi(Area = "app")]
public class PersonnelWorkLimitService : BaseService, IPersonnelWorkLimitService, IDynamicApi
{
    private readonly PersonnelWorkLimitRepository _personnelWorkLimitRepository;

    public PersonnelWorkLimitService(PersonnelWorkLimitRepository personnelWorkLimitRepository)
    {
        _personnelWorkLimitRepository = personnelWorkLimitRepository;
    }

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

        return output;
    }

    /// <summary>
    /// 查询分页
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<PageOutput<PersonnelWorkLimitGetPageOutput>> GetPageAsync(PageInput<PersonnelWorkLimitGetPageInput> input)
    {
        var list = await _personnelWorkLimitRepository.Select
            .WhereIf(input.Filter?.PersonnelId.HasValue == true, a => a.PersonnelId == input.Filter.PersonnelId)
            .Count(out var total)
            .OrderByDescending(a => a.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync<PersonnelWorkLimitGetPageOutput>();

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

        return data;
    }

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

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

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

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

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

    /// <summary>
    /// 获取人员工作限制
    /// </summary>
    /// <param name="personnelId"></param>
    /// <returns></returns>
    public async Task<PersonnelWorkLimitEntity> GetPersonnelWorkLimitAsync(long personnelId)
    {
        return await _personnelWorkLimitRepository.Select
            .Where(a => a.PersonnelId == personnelId)
            .FirstAsync();
    }

    /// <summary>
    /// 根据人员ID获取其所有工作限制记录
    /// </summary>
    /// <param name="personnelId">人员ID</param>
    /// <returns>人员的所有工作限制记录</returns>
    public async Task<List<PersonnelWorkLimitGetPageOutput>> GetByPersonnelIdAsync(long personnelId)
    {
        try
        {
            var workLimitations = await _personnelWorkLimitRepository.Select
                .Where(pwl => pwl.PersonnelId == personnelId)
                .OrderByDescending(pwl => pwl.CreatedTime)
                .ToListAsync<PersonnelWorkLimitGetPageOutput>();

            return workLimitations ?? new List<PersonnelWorkLimitGetPageOutput>();
        }
        catch (Exception ex)
        {
            return new List<PersonnelWorkLimitGetPageOutput>();
        }
    }
} 