using Dapper;
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;
using XYY.Model.Standard.Finance;

namespace XYY.Data.Standard.Finance
{
    public interface IFinance_Customer_Account_ApplyRecordRepository : IBaseRepository<Finance_Customer_Account_ApplyRecord>
    {
        Task<PageResult<View_Finance_Customer_Account_ApplyRecord>> GetPager(QueryModel querModel);
        Task ApplySubmit(ApplySubmitRequest model);
        Task<IEnumerable<View_Finance_Customer_Account_ApplyRecord>> ApplyListByIds(List<int> idArry);
    }
    public class Finance_Customer_Account_ApplyRecordRepository : BaseRepository<Finance_Customer_Account_ApplyRecord>, IFinance_Customer_Account_ApplyRecordRepository
    {
        public Finance_Customer_Account_ApplyRecordRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {

        }
        public Task<PageResult<View_Finance_Customer_Account_ApplyRecord>> GetPager(QueryModel querModel)
        {
            string sql = @"select a.*, isnull(b.Abbreviation, b.CompanyName) as CustomerAccountName,b.BillingCycle  from Finance_Customer_Account_ApplyRecord a
left join User_Customer b on b.Id = a.CustomerId";
            return _unitOfWork.GetPagingListAsync<View_Finance_Customer_Account_ApplyRecord>(querModel, sql);
        }
        public async Task ApplySubmit(ApplySubmitRequest model)
        {
            var userName = _unitOfWork.CurrentName;
            StringBuilder sb = new StringBuilder();
            //            sb.Append(@" begin tran
            //begin try ");
            sb.Append($" update Finance_Customer_Account_ApplyRecord set ApplyStatus={model.applyStatu},ReplyDec='{model.ReplyDec}',UpdateUserName='{userName}',UpdateTime=GETDATE() where Id in ({model.Ids}) ");
            if (model.applyStatu == (int)ObjectApplyEnum.通过)
            {
                sb.Append($@" insert into Finance_Customer_AccountLog([Id]
		   ,[Field]
           ,[FieldDesc]
           ,[OldValue]
           ,[NewValue]
           ,[CreateUserName]
           ,[CreateTime])
		   select b.id,'Quota','授信',isnull(b.Quota,0),ISNULL(b.BaseQuota,0)+a.ApplyValue,'{userName}',GETDATE() from Finance_Customer_Account_ApplyRecord(nolock) a
		   left join Finance_Customer_Account(nolock) b on b.CustomerId=a.CustomerId
		   where a.Id in ({model.Ids}) ");
                sb.Append($@" update Finance_Customer_Account set Quota=ISNULL(Finance_Customer_Account.BaseQuota,0)+b.ApplyValue,QuotaExpirationTime=b.EndTime,QuotaExpirationId=b.Id
from (select * from Finance_Customer_Account_ApplyRecord(nolock) where id in({model.Ids}))b
where b.CustomerId=Finance_Customer_Account.CustomerId");
            }
            //            sb.Append(@" end try
            //begin catch
            // if(@@trancount>0) 
            //      rollback tran  
            //end catch
            //if(@@trancount>0)
            //commit tran  ");
            string sql = sb.ToString();
            await _unitOfWork.ExecuteAsync(sql);
        }
        public async Task<IEnumerable<View_Finance_Customer_Account_ApplyRecord>> ApplyListByIds(List<int> idArry)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($@" select a.*,b.CompanyName CustomerAccountName from Finance_Customer_Account_ApplyRecord(nolock) a
		   left join User_Customer b on b.Id=a.CustomerId
		   where ISNULL(a.OriginatorUserid,'')!='' and a.Id in ({string.Join(",", idArry)}) ");
            string sql = sb.ToString();
            return await _unitOfWork.QueryBySqlAsync<View_Finance_Customer_Account_ApplyRecord>(sql);
        }
    }
}
