using Hotline.CallCenter.Tels;
using Hotline.Share.Dtos.CallCenter;
using Hotline.Share.Enums.CallCenter;
using SqlSugar;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace Hotline.Application.CallCenter
{
    public class PbxApplication : IPbxApplication, IScopeDependency
    {
        private readonly IRepository<TelRestApply> _telRestApplyRepository;

        public PbxApplication(IRepository<TelRestApply> telRestApplyRepository)
        {
            _telRestApplyRepository = telRestApplyRepository;
        }

        /// <summary>
        /// 小修申请记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ISugarQueryable<TelRestApplyListDto> GetRestApplyList(TelRestApplyRequestDto dto)
        {
            var query = _telRestApplyRepository.Queryable()
                .WhereIF(dto.Type is 0, p => p.AuditStatus == ETelRestAuditStatus.NoAudit)
                .WhereIF(dto.Type is 1, p => p.AuditStatus == ETelRestAuditStatus.Pass || p.AuditStatus == ETelRestAuditStatus.NoPass)
                .WhereIF(!string.IsNullOrEmpty(dto.ApplyUserName), p => p.CreatorName == dto.ApplyUserName)
                .WhereIF(dto.ApplyStartTime.HasValue, p => p.CreationTime >= dto.ApplyStartTime)
                .WhereIF(dto.ApplyEndTime.HasValue, p => p.CreationTime <= dto.ApplyEndTime)
                .WhereIF(dto.AuditStatus.HasValue, p => p.AuditStatus == dto.AuditStatus)
                .WhereIF(!string.IsNullOrEmpty(dto.AuditUserName), p => p.AuditUserName == dto.AuditUserName)
                .WhereIF(dto.AuditStartTime.HasValue, p => p.AuditTime >= dto.AuditStartTime)
                .WhereIF(dto.AuditEndTime.HasValue, p => p.AuditTime <= dto.AuditEndTime)
                .WhereIF(!string.IsNullOrEmpty(dto.ReasonId), p => p.ReasonId == dto.ReasonId)
                .Select(p => new TelRestApplyListDto
                {
                    Id = p.Id,
                    ApplyUserName = p.CreatorName,
                    ApplyTime = p.CreationTime,
                    Reason = p.Reason,
                    ReasonId = p.ReasonId,
                    AuditUserName = p.AuditUserName,
                    AuditUserId = p.AuditUserId,
                    AuditOrgName = p.AuditOrgName,
                    AuditOrgId = p.AuditOrgId,
                    AuditTime = p.AuditTime,
                    AuditStatus = p.AuditStatus,
                    AuditOpinion = p.AuditOpinion
                })
                .OrderByIF(dto is { SortField: "applyTime", SortRule: 0 }, p => p.ApplyTime, OrderByType.Asc) //申请时间升序
                .OrderByIF(dto is { SortField: "applyTime", SortRule: 1 }, p => p.ApplyTime, OrderByType.Desc) //申请时间降序
                .OrderByIF(dto is { SortField: "auditTime", SortRule: 0 }, p => p.AuditTime, OrderByType.Asc) //审核时间升序
                .OrderByIF(dto is { SortField: "auditTime", SortRule: 1 }, p => p.AuditTime, OrderByType.Desc) //审核时间降序
                .OrderByIF(string.IsNullOrEmpty(dto.SortField), p => p.ApplyTime, OrderByType.Desc)
               ;
            return query;
        }
    }
}
