using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Common.Standard;
using XYY.Core.Standard.Data.Infrastructure;
using XYY.Model.Standard.Finance;
using Dapper;
namespace XYY.Data.Standard.Finance
{
    public interface IFinance_Customer_AccountRepository : IBaseRepository<Finance_Customer_Account>
    {
        Task<PageResult<View_Finance_Customer_Account>> GetPager(QueryModel querModel);
        Task<IEnumerable<Finance_Customer_AccountLog>> GetLogs(int Id);
        Task QuotaReduction(List<Finance_Customer_Account> dbModels);
        Task<IEnumerable<Finance_Customer_Account>> GetAccountList(DateTime startTime, DateTime endTime);
    }
    public class Finance_Customer_AccountRepository : BaseRepository<Finance_Customer_Account>, IFinance_Customer_AccountRepository
    {
        public Finance_Customer_AccountRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {

        }

        public Task<IEnumerable<Finance_Customer_AccountLog>> GetLogs(int Id)
        {
            return  _unitOfWork.QueryAsync<Finance_Customer_AccountLog>(x => x.Id == Id);
        }

        public Task<PageResult<View_Finance_Customer_Account>> GetPager(QueryModel querModel)
        {
            return _unitOfWork.GetPagingListAsync<View_Finance_Customer_Account>(querModel,
                @"select  a.IsActive,b.Id,b.updatetime,a.Id as CustomerId,isnull(a.Abbreviation, a.CompanyName)  as CustomerAccountName,a.SalesmanUserId
                        ,c.NickName as SealsmanUserName,b.Balance,b.Quota,b.RepaymentTermDay,b.OverdueRatio,b.CurrentOverdueDays,b.Currency,a.BillingCycle
                        from User_Customer(nolock) a left join Finance_Customer_Account(nolock)b on a.Id=b.CustomerId
						left join User_Info(nolock) c on c.id=a.SalesmanUserId");
        }
        public async Task QuotaReduction(List<Finance_Customer_Account> dbModels)
        {
            var userName = _unitOfWork.CurrentName;
            StringBuilder sb = new StringBuilder();
            sb.Append($" update Finance_Customer_Account set Quota=ISNULL(BaseQuota,0),QuotaExpirationId=0,QuotaExpirationTime=null where Id in ({string.Join(",", dbModels.Select(x=>x.Id))}) ");
            sb.Append($@" insert into Finance_Customer_AccountLog([Id]
		   ,[Field]
           ,[FieldDesc]
           ,[OldValue]
           ,[NewValue]
           ,[CreateUserName]
           ,[CreateTime])
		   select id,'Quota','临时授信到期还原',isnull(Quota,0),ISNULL(BaseQuota,0),'{userName}',GETDATE() from Finance_Customer_Account(nolock)
		   where Id in ({string.Join(",", dbModels.Select(x => x.Id))}) ");
            string sql = sb.ToString();
            await _unitOfWork.ExecuteAsync(sql);
        }
        public async Task<IEnumerable<Finance_Customer_Account>> GetAccountList(DateTime startTime, DateTime endTime)
        {
            string sql = $@"select * from Finance_Customer_Account where QuotaExpirationTime>='{startTime}' and QuotaExpirationTime<'{endTime}'";
            return await _unitOfWork.QueryBySqlAsync<Finance_Customer_Account>(sql);
        }
    }
}
