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
{
    /// 
    /// 团组-出入境费用报价表 仓储
    /// 
    public class EnterExitCostQuoteRepository : BaseRepository
    {
        private readonly IMapper _mapper; 
        public EnterExitCostQuoteRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
        {
            _mapper = mapper;
        }
        /// 
        /// 初始化基础项
        /// 
        /// 是否移除换行符
        /// 
        public async Task> InitBasicItemAsync(bool removeNl)
        {
            var origList = await _sqlSugar.Queryable()
                .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;
        }
        /// 
        /// 获取报价名称列表
        /// 
        /// 
        /// 
        /// 
        /// 
        public async Task QuoteNameListAsync(EnterExitCostQuoteNameListDto dto)
        {
            int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
            string name = dto.Name;
            if (pageIndex < 1 || pageSize < 1)
            {
                var noPageList = await _sqlSugar.Queryable()
                .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 total = 0;
            var pageList = await _sqlSugar.Queryable()
                .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
            };
        }
        /// 
        /// 获取团组名称列表
        /// 
        /// 
        /// 
        /// 
        /// 
        public async Task GroupNameListAsync(EnterExitCostQuoteGroupNameListDto dto)
        {
            int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
            string name = dto.Name;
            if (pageIndex < 1 || pageSize < 1)
            {
                var noPageList = await _sqlSugar.Queryable()
                .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 total = 0;
            var pageList = await _sqlSugar.Queryable()
                .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
            };
        }
        /// 
        /// 获取团组名称列表
        /// 
        /// 
        /// 
        /// 
        /// 
        public async Task InfoAsync(EnterExitCostQuoteInfoDto dto)
        {
            if (dto.Id < 1)
            {
                return new JsonView
                {
                    Code = StatusCodes.Status400BadRequest,
                    Msg = MsgTips.Id
                };
            }
            var quoteInfo = await _sqlSugar.Queryable()
                 .Where(x => x.IsDel == 0 && x.Id == x.Id)
                 .FirstAsync();
            if (quoteInfo == null)
            {
                return new JsonView
                {
                    Code = StatusCodes.Status400BadRequest,
                    Msg = $"未找到Id为{dto.Id}的数据"
                };
            }
            //(List?)CommonFun.GetCurrencyChinaToList(enterExitCostData.CurrencyRemark)
            return new JsonView
            {
                Code = StatusCodes.Status200OK,
                Msg = "获取成功",
               // Data = pageList
            };
        }
    }
}