using AutoMapper;
using OASystem.Domain.Entities.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.Infrastructure.Repositories.Groups
{
    public class GroupCostParameterRepository : BaseRepository<Grp_GroupCostParameter, Grp_GroupCostParameterView>
    {
        public GroupCostParameterRepository(SqlSugarClient sqlSugar) :
        base(sqlSugar)
        {
        }

        public List<Grp_GroupCostParameter> GetGroupCostParameterListByDiid(int diid)
        {
           return Query(x=>x.DiId == diid).ToList();
        }

        public Grp_GroupCostParameter GetGroupCostParameterMainByDiid(int diid)
        {
            return Query(x => x.DiId == diid).First(x => x.CostType == "A");
        }

        public async Task<bool> UpdateIsShareById(int id,int IsShare)
        {
            return await UpdateAsync(x => x.Id == id, x=>new Grp_GroupCostParameter { IsShare = IsShare });
        }

        public async Task<bool> DeleteGroupCostParameterListByDiid(int diid, int userid) 
        {
            return await SoftDeleteAsync(x=>x.DiId == diid && x.IsDel == 0, userid);
        }

        public async Task<bool> SaveAsync(List<Grp_GroupCostParameter> List,int Userid, int diid)
        {
            bool isTrue = false;
            try
            {
                BeginTran();
                var mainCost = GetGroupCostParameterMainByDiid(diid) ?? new Grp_GroupCostParameter
                {
                    IsShare = 0
                };
                isTrue = await DeleteGroupCostParameterListByDiid(diid, Userid);
                if (List.Count > 0)
                {
                    foreach (var item in List)
                    {
                        if (item.CostType == "A")
                        {
                            item.IsShare = mainCost.IsShare;
                            break;
                        }
                    }
                    List.ForEach(x => { x.CreateUserId = Userid; x.CreateTime = DateTime.Now; });
                    isTrue = Adds(List) > 0;
                }
                CommitTran();
            }
            catch (Exception ex)
            {
                RollbackTran();
                isTrue = false;
            }
            return isTrue;
        }

        public object GetBaoPi(int diid)
        {
            List<dynamic> dynamics = new List<dynamic>();
            try
            {
                Grp_EnterExitCost eec = _sqlSugar.Queryable<Grp_EnterExitCost>().First(x => x.DiId == diid && x.IsDel == 0);
                if (eec != null)
                {
                    var AirJJ = new
                    {
                        Type = "JJC",
                        Price = eec.AirJJ
                    };
                    dynamics.Add(AirJJ);

                    var AirGW = new
                    {
                        Type = "GWC",
                        Price = eec.AirGW
                    };
                    dynamics.Add(AirGW);

                    List<Grp_DayAndCost> dac1 = _sqlSugar.Queryable<Grp_DayAndCost>().Where(x => x.DiId == diid && x.Type == 1 && x.NationalTravelFeeId > 0 && x.IsDel == 0).ToList();  //酒店费用
                    List<Grp_DayAndCost> dac2 = _sqlSugar.Queryable<Grp_DayAndCost>().Where(x => x.DiId == diid && x.Type == 2 && x.NationalTravelFeeId > 0 && x.IsDel == 0).ToList(); //餐费用
                    List<Grp_DayAndCost> dac3 = (from item in _sqlSugar.Queryable<Grp_DayAndCost>() //公杂费用
                                                 where item.DiId == diid && item.Type == 3 && item.NationalTravelFeeId > 0 && item.IsDel == 0
                                                 select item).ToList();

                    if (dac1.Count == 0)
                    {
                        dac1 = _sqlSugar.Queryable<Grp_DayAndCost>().Where(x => x.DiId == diid && x.Type == 1 && x.IsDel == 0).ToList();  //酒店费用
                    }
                    if (dac2.Count == 0)
                    {
                        dac2 = _sqlSugar.Queryable<Grp_DayAndCost>().Where(x => x.DiId == diid && x.Type == 2 && x.IsDel == 0).ToList();  //酒店费用
                    }
                    if (dac2.Count == 0)
                    {
                        dac3 = _sqlSugar.Queryable<Grp_DayAndCost>().Where(x => x.DiId == diid && x.Type == 3 && x.IsDel == 0).ToList();  //酒店费用
                    }

                    decimal SumHotel = 0;
                    decimal SumMeals = 0;
                    decimal SumGongZa = 0;

                    List<dynamic> hotelList = new List<dynamic>();
                    List<string> checkString = new List<string>();

                    if (dac1.Count != 0)//酒店费用
                    {
                        for (int i = 0; i < dac1.Count; i++)
                        {
                            if (!string.IsNullOrWhiteSpace(dac1[i].Place))
                            {
                                if (!checkString.Contains(dac1[i].Place))
                                {
                                    var hotelData = new
                                    {
                                        CountryOrCity = dac1[i].Place,
                                        Price = dac1[i].SubTotal
                                    };
                                    hotelList.Add(hotelData);
                                    checkString.Add(dac1[i].Place);
                                }
                            }
                            SumHotel += dac1[i].SubTotal;
                        }

                        if (hotelList.Count == 0)
                        {
                            var group = dac1.GroupBy(x => x.NationalTravelFeeId).ToList();
                            foreach (var hotel in group)
                            {
                                var city = _sqlSugar.Queryable<Grp_NationalTravelFee>().First(it => it.IsDel == 0 && it.Id == hotel.Key);
                                var hotelData = new
                                {
                                    CountryOrCity = city?.Country + city?.City,
                                    Price = hotel.ToList()[0].SubTotal,
                                };
                                hotelList.Add(hotelData);
                                checkString.Add(city?.City ?? "");
                            }
                        }
                    }
                    hotelList = hotelList.Distinct().ToList();
                    List<dynamic> MealsList = new List<dynamic>();

                    if (dac2.Count != 0) //餐费用
                    {
                        for (int i = 0; i < dac2.Count; i++)
                        {
                            if (checkString.Contains(dac2[i].Place))
                            {
                                var mealsData = new
                                {
                                    CountryOrCity = dac2[i].Place,
                                    Price = dac2[i].SubTotal
                                };

                                MealsList.Add(mealsData);
                            }
                            SumMeals += dac2[i].SubTotal;
                        }

                        if (MealsList.Count == 0)
                        {
                            var group = dac2.GroupBy(x => x.NationalTravelFeeId).ToList();
                            foreach (var hotel in group)
                            {
                                var city = _sqlSugar.Queryable<Grp_NationalTravelFee>().First(it => it.IsDel == 0 && it.Id == hotel.Key);
                                var mealsData = new
                                {
                                    CountryOrCity = city?.Country +  city?.City,
                                    Price = hotel.ToList()[0].SubTotal,
                                };
                                MealsList.Add(mealsData);
                            }
                        }

                    }
                    MealsList = MealsList.Distinct().ToList();
                    List<dynamic> GongZaList = new List<dynamic>();

                    if (dac3.Count != 0) //公杂费用
                    {
                        for (int i = 0; i < dac3.Count; i++)
                        {
                            if (checkString.Contains(dac3[i].Place))
                            {
                                var gongZaData = new
                                {
                                    CountryOrCity = dac3[i].Place,
                                    Price = dac3[i].SubTotal
                                };

                                GongZaList.Add(gongZaData);
                            }
                            SumGongZa += dac3[i].SubTotal;
                        }

                        if (GongZaList.Count == 0)
                        {
                            var group = dac3.GroupBy(x => x.NationalTravelFeeId).ToList();
                            foreach (var hotel in group)
                            {
                                var city = _sqlSugar.Queryable<Grp_NationalTravelFee>().First(it => it.IsDel == 0 && it.Id == hotel.Key);
                                var gongZaData = new
                                {
                                    CountryOrCity = city?.Country +  city?.City,
                                    Price = hotel.ToList()[0].SubTotal,
                                };
                                GongZaList.Add(gongZaData);
                            }
                        }
                    }
                    GongZaList = GongZaList.Distinct().ToList();

                    //签证
                    var Visa = new
                    {
                        Type = "Visa",
                        Price = eec.Visa,
                        Desc = eec.VisaRemark
                    };
                    dynamics.Add(Visa);

                    //核酸检测费
                    var HeSuan = new
                    {
                        Type = "HeSuan",
                        Price = eec.HeSuan  //元/人
                    };
                    dynamics.Add(HeSuan);

                    //保险费
                    var Insurance = new
                    {
                        Type = "Insurance",
                        Price = eec.Safe  //元/人
                    };
                    dynamics.Add(Insurance);

                    // 伙食费
                    var Meals = new
                    {
                        Type = "Meals",
                        Content = MealsList
                    };
                    dynamics.Add(Meals);

                    // 酒店费用
                    var Hotel = new
                    {
                        Type = "Hotel",
                        Content = hotelList
                    };
                    dynamics.Add(Hotel);

                    // 公杂费用
                    var GongZa = new
                    {
                        Type = "GongZa",
                        Content = GongZaList
                    };
                    dynamics.Add(GongZa);

                    decimal AirJJCTotal = 0;
                    decimal AirGWCTotal = 0;

                    //境内费用(其他费用)选择框
                    if (eec.ChoiceOne == 1)
                    {
                        AirJJCTotal += Math.Round((eec.InsidePay), 2);
                        AirGWCTotal += Math.Round((eec.InsidePay), 2);
                    }
                    //国际旅费合计选择框
                    if (eec.SumJJC == 1)
                    {
                        AirJJCTotal += Math.Round((eec.OutsideJJPay), 2);
                    }
                    if (eec.SumGWC == 1)
                    {
                        AirGWCTotal += Math.Round((eec.OutsaideGWPay), 2);
                    }

                    //住宿费合计选择框
                    if (eec.ChoiceThree == 1)
                    {
                        AirJJCTotal += Math.Round(SumHotel, 2);
                        AirGWCTotal += Math.Round(SumHotel, 2);
                    }

                    //伙食费合计选择框
                    if (eec.ChoiceFour == 1)
                    {
                        AirJJCTotal += Math.Round(SumMeals, 2);
                        AirGWCTotal += Math.Round(SumMeals, 2);
                    }

                    //公杂费合计选择框
                    if (eec.ChoiceFour == 1)
                    {
                        AirJJCTotal += Math.Round(SumGongZa, 2);
                        AirGWCTotal += Math.Round(SumGongZa, 2);
                    }

                    // 报批总报价 - 经济舱
                    var SumAirJJC = new
                    {
                        Type = "SumAirJJC",
                        Price = AirJJCTotal
                    };
                    dynamics.Add(SumAirJJC);

                    //报批总报价 - 公务舱
                    var SumAirGWC = new
                    {
                        Type = "SumAirGWC",
                        Price = AirGWCTotal
                    };
                    dynamics.Add(SumAirGWC);

                }
            }
            catch (Exception)
            {
                dynamics = new List<dynamic>();
            }

            return dynamics;
        }
    }
}
