using AutoMapper;
using OASystem.Domain;
using OASystem.Domain.Dtos.Groups;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.ViewModels.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.Infrastructure.Repositories.Groups
{
    /// <summary>
    /// 团组-出入境费用报价表 仓储
    /// </summary>
    public class EnterExitCostQuoteRepository : BaseRepository<Grp_EnterExitCostQuoteItem, EnterExitCostQuoteView>
    {
        private readonly IMapper _mapper; 
        public EnterExitCostQuoteRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 初始化基础项
        /// </summary>
        /// <param name="removeNl">是否移除换行符</param>
        /// <returns></returns>
        public async Task<List<InitBasicItemView>> InitBasicItemAsync(bool removeNl)
        {
            var origList = await _sqlSugar.Queryable<Sys_SetData>()
                .Where(x => x.IsDel == 0 && x.STid == 105)
                .Select(x => new
                {
                    Id = x.Id,
                    Name = x.Name,
                    Index = x.Remark ?? "-1"
                }).ToListAsync();

            var newList = origList.Select(x => new InitBasicItemView
                {
                    Id = x.Id,
                    Name = removeNl ? x.Name.Replace("\r\n", "") : x.Name,
                    Index = int.TryParse(x.Index,out int index) ? index : -1
                })
                .OrderBy(x => x.Index)
                .ToList();
            return newList;
        }

        /// <summary>
        /// 获取报价名称列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<JsonView> QuoteNameListAsync(EnterExitCostQuoteNameListDto dto)
        {
            int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
            string name = dto.Name;

            if (pageIndex < 1 || pageSize < 1)
            {
                var noPageList = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
                .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.Name))
                .WhereIF(!string.IsNullOrEmpty(name), x => x.Name.Contains(name))
                .Select(x => new EnterExitCostQuoteNameView
                {
                    Id = x.Id,
                    Name = x.Name
                }).ToListAsync();

                return new JsonView
                {
                    Code = StatusCodes.Status200OK,
                    Msg = "获取成功",
                    Count = noPageList.Count,
                    Data = noPageList
                };
            }

            RefAsync<int> total = 0;
            var pageList = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
                .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.Name))
                .WhereIF(!string.IsNullOrEmpty(name), x => x.Name.Contains(name))
                .Select(x => new EnterExitCostQuoteNameView
                {
                    Id = x.Id,
                    Name = x.Name
                }).ToPageListAsync(pageIndex, pageSize, total);

            return new JsonView
            {
                Code = StatusCodes.Status200OK,
                Msg = "获取成功",
                Count = total,
                Data = pageList
            };
        }

        /// <summary>
        /// 获取团组名称列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<JsonView> GroupNameListAsync(EnterExitCostQuoteGroupNameListDto dto)
        {
            int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
            string name = dto.Name;

            if (pageIndex < 1 || pageSize < 1)
            {
                var noPageList = await _sqlSugar.Queryable<Grp_DelegationInfo>()
                .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.TeamName))
                .WhereIF(!string.IsNullOrEmpty(name), x => x.TeamName.Contains(name))
                .OrderByDescending(x => x.CreateTime)
                .Select(x => new EnterExitCostQuoteGroupNameView
                {
                    Id = x.Id,
                    Name = x.TeamName
                }).ToListAsync();

                return new JsonView
                {
                    Code = StatusCodes.Status200OK,
                    Msg = "获取成功",
                    Count = noPageList.Count,
                    Data = noPageList
                };
            }

            RefAsync<int> total = 0;
            var pageList = await _sqlSugar.Queryable<Grp_DelegationInfo>()
                .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.TeamName))
                .WhereIF(!string.IsNullOrEmpty(name), x => x.TeamName.Contains(name))
                .OrderByDescending(x => x.CreateTime)
                .Select(x => new EnterExitCostQuoteGroupNameView
                {
                    Id = x.Id,
                    Name = x.TeamName
                }).ToPageListAsync(pageIndex, pageSize, total);

            return new JsonView
            {
                Code = StatusCodes.Status200OK,
                Msg = "获取成功",
                Count = total,
                Data = pageList
            };
        }

        /// <summary>
        /// 获取团组名称列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<JsonView> InfoAsync(EnterExitCostQuoteInfoDto dto)
        {
            if (dto.Id < 1)
            {
                return new JsonView
                {
                    Code = StatusCodes.Status400BadRequest,
                    Msg = MsgTips.Id
                };
            }

            var quoteInfo = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
                 .Where(x => x.IsDel == 0 && x.Id == x.Id)
                 .FirstAsync();
            if (quoteInfo == null)
            {
                return new JsonView
                {
                    Code = StatusCodes.Status400BadRequest,
                    Msg = $"未找到Id为{dto.Id}的数据"
                };
            }

            //(List<CurrencyInfo>?)CommonFun.GetCurrencyChinaToList(enterExitCostData.CurrencyRemark)

            return new JsonView
            {
                Code = StatusCodes.Status200OK,
                Msg = "获取成功",
               // Data = pageList
            };
        }
    }
}