using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Data.Standard.Finance;
using XYY.Core.Standard.ExcelHelper.MSExcelHelper;
using XYY.Model.Standard.Finance;
using XYY.Common.Standard;
using XYY.Service.Standard.Finance.RedisCache;

namespace XYY.Service.Standard.Finance
{
    public interface IEUExchangeRateService
    {
        Task AddEUExchangeRate(DateTime start, DateTime end, byte[] file);
        Task ChangeEUExchangeDetail(int rateId, byte[] file);
        Task ChangeEUExchangeRate(Finance_EUExchangeRate rate);
        Task<PageResult<Finance_EUExchangeRate>> GetPageResult(QueryModel queryModel);
        Task<List<Finance_EUExchangeRateDetail>> GetList(int baseId);
    }

    public class EUExchangeRateService : IEUExchangeRateService
    {
        IFinance_EUExchangeRateRepository exchangeRateRepository;
        IFnance_EUExchangeRateDetailRepository exchangeRateDetailRepository;

        public EUExchangeRateService(IFinance_EUExchangeRateRepository exchangeRateRepository, IFnance_EUExchangeRateDetailRepository exchangeRateDetailRepository)
        {
            this.exchangeRateRepository = exchangeRateRepository;
            this.exchangeRateDetailRepository = exchangeRateDetailRepository;
        }

        public async Task AddEUExchangeRate(DateTime start, DateTime end, byte[] file)
        {
            if (await exchangeRateRepository.IsExistsAsync(x => x.StartTime <= start && x.EndTime >= start))
            {
                throw new Exception("开始时间已存在于其他时间范围内");
            }
            if (await exchangeRateDetailRepository.IsExistsAsync(x => x.StartTime <= end && x.EndTime >= end))
            {
                throw new Exception("结束时间已存在于其他时间范围内");
            }
            var msHelper = new MSExcelHelper();
            var detail = msHelper.LoadDataByByte<EUExchangeRateDetailMapping, Finance_EUExchangeRateDetail>(file);
            detail.ForEach(x =>
            {
                x.StartTime = start;
                x.EndTime = end;
                x.CreateTime = DateTime.Now;
            });
            if (detail == null)
                throw new Exception("不可传入空的表格!");
            int baseId = (int)(await exchangeRateRepository.InsertAsync(new Finance_EUExchangeRate { StartTime = start, EndTime = end }));
            detail.ForEach(x => x.BaseId = baseId);
            await exchangeRateDetailRepository.BulkToDBAsync(detail);
            ChargesService.RefreshRedisData(0, 0, RefreshRedisType.RefreshExchangeRedisCache);
        }

        public async Task ChangeEUExchangeRate(Finance_EUExchangeRate rate)
        {
            if (await exchangeRateRepository.IsExistsAsync(x => x.StartTime >= rate.StartTime && x.EndTime <= rate.StartTime && x.Id != rate.Id))
            {
                throw new Exception("开始时间已存在于其他时间范围内");
            }
            if (await exchangeRateDetailRepository.IsExistsAsync(x => x.StartTime >= rate.EndTime && x.EndTime <= rate.EndTime && x.Id != rate.Id))
            {
                throw new Exception("结束时间已存在于其他时间范围内");
            }
            await exchangeRateDetailRepository.UpdateByConditionAsync(x => x.BaseId == rate.Id, new { StartTime = rate.StartTime, EndTime = rate.EndTime });
            await exchangeRateRepository.UpdateByConditionAsync(x => x.Id == rate.Id, new { StartTime = rate.StartTime, EndTime = rate.EndTime });
            ChargesService.RefreshRedisData(0, 0, RefreshRedisType.RefreshExchangeRedisCache);
        }


        public async Task ChangeEUExchangeDetail(int rateId, byte[] file)
        {
            var rate = await exchangeRateRepository.GetAsync(rateId);
            var msHelper = new MSExcelHelper();
            var detail = msHelper.LoadDataByByte<EUExchangeRateDetailMapping, Finance_EUExchangeRateDetail>(file);
            detail.ForEach(x =>
            {
                x.StartTime = rate.StartTime;
                x.EndTime = rate.EndTime;
                x.CreateTime = DateTime.Now;
                x.CreateUserName = rate.CreateUserName;
                x.BaseId = rateId;
            });
            if (detail == null)
                throw new Exception("不可传入空的表格!");
            await exchangeRateDetailRepository.DeleteAsync(x => x.BaseId == rateId);
            await exchangeRateDetailRepository.BulkToDBAsync(detail);
            ChargesService.RefreshRedisData(0,0,RefreshRedisType.RefreshExchangeRedisCache);
        }

        public async Task<PageResult<Finance_EUExchangeRate>> GetPageResult(QueryModel queryModel)
        {
            return await exchangeRateRepository.QueryPager2Async(queryModel);
        }

        public async Task<List<Finance_EUExchangeRateDetail>> GetList(int baseId)
        {
            return (await exchangeRateDetailRepository.QueryAsync(x => x.BaseId == baseId)).ToList();
        }

        public class EUExchangeRateDetailMapping : MSExcelClassMapping<Finance_EUExchangeRateDetail>
        {
            public EUExchangeRateDetailMapping()
            {
                Map(x => x.ReceiveryCountryCode).Name("二字简码");
                Map(x => x.Rate).Name("税率");
            }
        }
    }
}
