using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Core.Standard.Data.Infrastructure;
using XYY.Model.Standard.Enums;
using XYY.Model.Standard.Finance;
using Dapper;
using System.Security.Policy;

namespace XYY.Data.Standard.Finance
{
    public interface ILogistics_ExpressSettlementAdjustmentRepository : IBaseRepository<Logistics_ExpressSettlementAdjustment>
    {
        Task<List<Tuple<string, string>>> ExpressSettlementAdjustmentLog(Logistics_ExpressSettlementAdjustmentDto adjustmentDto);
    }
    public class Logistics_ExpressSettlementAdjustmentRepository : BaseRepository<Logistics_ExpressSettlementAdjustment>, ILogistics_ExpressSettlementAdjustmentRepository
    {
        public Logistics_ExpressSettlementAdjustmentRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }
        public async Task<List<Tuple<string, string>>> ExpressSettlementAdjustmentLog(Logistics_ExpressSettlementAdjustmentDto adjustmentDto)
        {
            List<Tuple<string, string>> msgs = new List<Tuple<string, string>>();
            List<string> zones = adjustmentDto.Details.SelectMany(x => x.Zone.Split(",")).ToList();
            List<Logistics_ExpressSettlementAdjustmentDetail> details = new List<Logistics_ExpressSettlementAdjustmentDetail>();
            string dec = string.Empty;
            foreach (var item in zones)
            {
                string msg = $"分区[{item}],";
                var tempdata = adjustmentDto.Details.Where(x => x.Zone.Contains(item)).FirstOrDefault();
                string content = $"{tempdata.Mode.GetName()}:{tempdata.Value};";
                dec += msg + content;
                msgs.Add(Tuple.Create<string, string>(item, msg + content));
                Logistics_ExpressSettlementAdjustmentDetail detail = new Logistics_ExpressSettlementAdjustmentDetail()
                {
                    Zone = item,
                    IsAll =item== "全部"?true:false,
                    Mode = tempdata.Mode,
                    Value = tempdata.Value,
                };
                details.Add(detail);
            }
            Logistics_ExpressSettlementAdjustment expressSettlementAdjustment = new Logistics_ExpressSettlementAdjustment()
            {
                CustomerId = $",{string.Join(",", adjustmentDto.CustomerId)},",
                ExpressId = adjustmentDto.ExpressId,
                AdjTime = DateTime.Now,
                SettlementAdjustmentContent = dec,
            };
            int Id = (int)(await _unitOfWork.InsertAsync(expressSettlementAdjustment));
            details.ForEach(x => x.AdjustmentId = Id);
            await _unitOfWork.BulkToDBAsync(details);
            return msgs;
        }
    }
}
