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

namespace OASystem.Infrastructure.Repositories.Groups
{
    public class DelegationVisaRepository : BaseRepository<Grp_VisaProgress, Grp_DelegationVisaView>
    {
        public DelegationVisaRepository(SqlSugarClient sqlSugar) : base(sqlSugar)
        {
        }


        public DelegationVisaViewList GetDelegationList(IOS_VisaDto _dto)
        {
            string sqlWhere = string.Format(" Where IsDel=0 ");

            int currPIndex = (((_dto.PageIndex > 0) ? (_dto.PageIndex - 1) : 0) * _dto.PageSize) + 1;
            int currPSize = (((_dto.PageIndex > 0) ? (_dto.PageIndex - 1) : 0) + 1) * _dto.PageSize;

            ChangeDataBase(DBEnum.OA2014DB);

            string sql = string.Format(@" Select * From(Select ROW_NUMBER() Over(order By Id desc) as RowNumber, Id as DiId,
TeamName,ClientUnit,ClientName,TeamLev,VisitDate,VisitDays,VisitPNumber
From DelegationInfo With(Nolock) {2}
) as tb Where tb.RowNumber Between {0} And {1} ", currPIndex, currPSize, sqlWhere);
            DelegationVisaViewList rst = new DelegationVisaViewList();
            rst.CurrPageIndex = currPIndex;
            rst.CurrPageSize = currPSize;
            List<Grp_DelegationVisaView> dataSource = _sqlSugar.SqlQueryable<Grp_DelegationVisaView>(sql).ToList();

            foreach (var item in dataSource)
            {
                string sql2 = string.Format(@" Select * From Grp_VisaProgressCustomer With(Nolock) Where DiId={0} And IsDel=0 ", item.DiId);
                List<Grp_VisaProgressCustomer> listComplete = _sqlSugar.SqlQueryable<Grp_VisaProgressCustomer>(sql2).ToList();

                item.CompletePNumber = string.Format(@"已完成{0}人", listComplete.Count);
            }
            rst.DataList = new List<Grp_DelegationVisaView>(dataSource);

            if (rst.DataList.Count > 0)
            {
                string sqlCount = string.Format(@" Select Id as DiId From DelegationInfo With(Nolock) {0} ", sqlWhere);
                int dataCount = _sqlSugar.SqlQueryable<Grp_DelegationVisaView>(sqlCount).Count();
                rst.DataCount = dataCount;
            }

            return rst;
        }

        public List<DelegationVisaPersonView> GetDelegationPersonList(int diid)
        {
            ChangeDataBase(DBEnum.OA2014DB);

            string sqlDeleClient = string.Format(@" Select 
Id as 'PersonViewId',
Diid,
LastName,
[Name] as 'FirstName'
From DeleClient With(Nolock) Where Diid = {0} And IsDel = 0 ", diid);
            List<DelegationVisaPersonView> clientList = _sqlSugar.SqlQueryable<DelegationVisaPersonView>(sqlDeleClient).ToList();

            string sqlDelegation = string.Format(@" Select * From DelegationInfo With(Nolock) Where Id = {0} ", diid);
            OA2021_DelegationInfo delegationInfo = _sqlSugar.SqlQueryable<OA2021_DelegationInfo>(sqlDelegation).First();
            string countryName = delegationInfo.VisitCountry;


            #region 配置

            #endregion

            return null;
        }

        public int initVisitCountry(int diid)
        {
            ChangeDataBase(DBEnum.OA2023DB);

            string sqlCheck = string.Format(@" Select * From  ");

            return 0;
        }

    }
}