using MapsterMapper;
using SqlSugar;
using XF.Domain.Authentications;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
using XF.Domain.Repository;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Share.Dtos.Special;
using Hotline.Orders;

namespace Hotline.Application.SpecialNumber
{
    public class SpecialNumberApplication : ISpecialNumberApplication, IScopeDependency
    {
        #region 注册

        private readonly IRepository<Hotline.Special.SpecialNumber> _specialNumberRepository;
        private readonly ISessionContext _sessionContext;
        private readonly IMapper _mapper;

        public SpecialNumberApplication(
            IRepository<Hotline.Special.SpecialNumber> specialNumberRepository,
            ISessionContext sessionContext,
            IMapper mapper)
        {
            _specialNumberRepository = specialNumberRepository;
            _sessionContext = sessionContext;
            _mapper = mapper;
        }

        #endregion

        #region 特殊号码

        #region 特殊号码 - 列表

        /// <summary>
        /// 特殊号码 - 列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ISugarQueryable<Hotline.Special.SpecialNumber> QueryAllSpecialNumberListAsync(SpecialNumberDto dto)
        {
            //单表分页
            var query = _specialNumberRepository.Queryable()
                .WhereIF(!string.IsNullOrEmpty(dto.PhoneNumber), x => x.PhoneNumber.Contains(dto.PhoneNumber))
                .WhereIF(!string.IsNullOrEmpty(dto.Notes), x => x.Notes.Contains(dto.Notes))
                .WhereIF(!string.IsNullOrEmpty(dto.PoliticalIdentityValue), x => x.PoliticalIdentityValue == dto.PoliticalIdentityValue)
                .WhereIF(!string.IsNullOrEmpty(dto.PoliticalIdentityName), x => x.PoliticalIdentityName == dto.PoliticalIdentityName)
                .WhereIF(dto.CreationTimeStart.HasValue, x => x.CreationTime >= dto.CreationTimeStart)
                .WhereIF(dto.CreationTimeEnd.HasValue, x => x.CreationTime <= dto.CreationTimeEnd)
                .OrderByIF(dto is { SortField: "creationTime", SortRule: 0 }, x => x.CreationTime, OrderByType.Asc)  //创建时间升序
                .OrderByIF(dto is { SortField: "creationTime", SortRule: 1 }, x => x.CreationTime, OrderByType.Desc) //创建时间降序
                .OrderByIF(string.IsNullOrEmpty(dto.SortField), x => x.CreationTime, OrderByType.Desc) //创建时间降序
              ;
            return query;
        }

        #endregion

        #region 特殊号码 - 新增

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> AddSpecialNumberAsync(AddSpecialNumberDto dto, CancellationToken cancellationToken)
        {
            var data = _mapper.Map<Hotline.Special.SpecialNumber>(dto);

            var any = await _specialNumberRepository.Queryable().Where(x => x.PhoneNumber == dto.PhoneNumber).AnyAsync();
            if (any)
                throw UserFriendlyException.SameMessage("特殊号码已存在！");

            data.InitId();

            return await _specialNumberRepository.AddAsync(data, cancellationToken);
        }

        #endregion

        #region 特殊号码 - 修改

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task UpdateSpecialNumberAsync(UpdateSpecialNumberDto dto, CancellationToken cancellationToken)
        {
            var data = await _specialNumberRepository.GetAsync(dto.Id);

            if (data == null)
                throw UserFriendlyException.SameMessage("特殊号码查询失败");

            var any = await _specialNumberRepository.Queryable().Where(x => x.PhoneNumber == dto.PhoneNumber && x.Id != dto.Id).AnyAsync();
            if (any)
                throw UserFriendlyException.SameMessage("特殊号码已存在！");

            data.PhoneNumber = dto.PhoneNumber;
            data.Notes = dto.Notes;
            data.PoliticalIdentityValue = dto.PoliticalIdentityValue;
            data.PoliticalIdentityName = dto.PoliticalIdentityName;

            await _specialNumberRepository.UpdateAsync(data, cancellationToken);
        }

        #endregion

        #region 特殊号码 - 删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task RemoveSpecialNumberAsync(DelSpecialNumberDto dto, CancellationToken cancellationToken)
        {
            var data = await _specialNumberRepository.GetAsync(dto.Id);

            if (data == null)
                throw UserFriendlyException.SameMessage("特殊号码查询失败");

            await _specialNumberRepository.RemoveAsync(data, false, cancellationToken);
        }

        #endregion

        #region 特殊号码 - 详情

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<SpecialNumberInfoDto> GetSpecialNumberAsync(string Id, CancellationToken cancellationToken)
        {
            var data = await _specialNumberRepository.Queryable()
                .FirstAsync(x => x.Id == Id, cancellationToken);
            if (data == null) return new();
            return _mapper.Map<SpecialNumberInfoDto>(data);
        }

        #endregion

        #region 特殊号码 - 详情

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="PhoneNumber"></param>
        /// <returns></returns>
        public async Task<SpecialNumberInfoDto> GetSpecialNumberByAsync(string PhoneNumber, CancellationToken cancellationToken)
        {
            var data = await _specialNumberRepository.Queryable()
                .FirstAsync(x => x.PhoneNumber == PhoneNumber, cancellationToken);
            if (data == null) return new();
            return _mapper.Map<SpecialNumberInfoDto>(data);
        }

        #endregion

        #endregion
    }
}
