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);

                    var AirTDC = new
                    {
                        Type = "TDC",
                        Price = eec.AirTD
                    };
                    dynamics.Add(AirTDC);

                    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();
                    var dac4 = _sqlSugar.Queryable<Grp_DayOtherPrice>()
                                .InnerJoin<Sys_SetData>((a,b)=> b.IsDel == 0 && b.Id == a.Currency)
                                .InnerJoin<Sys_SetData>((a, b, c) => c.IsDel == 0 && c.Id == a.SetDataId)
                                .Where((a, b, c)  => a.Diid == diid && a.IsDel == 0)
                                .Select((a, b, c) => new
                                {
                                    a.Cost,
                                    CurrencyStr = b.Remark,
                                    SetDataType = c.Name,
                                    a.SubTotal,
                                })
                                .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;
                    decimal sumOtherPrice = 0;

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

                    //币种
                    var currArr = _sqlSugar.Queryable<Sys_SetData>().Where(x=>x.IsDel == 0 && x.STid == 66).ToList();

                    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,
                                        CurrPrice = dac1[i].Cost,
                                        CurrStr = (currArr.Find(x => x.Id == dac1[i].Currency)?.Remark ?? dac1[i].Currency.ToString()),
                                    };
                                    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,
                                    CurrPrice = hotel.ToList()[0].Cost,
                                    CurrStr = (currArr.Find(x => x.Id == hotel.ToList()[0].Currency)?.Remark ?? hotel.ToList()[0].Currency.ToString()),
                                };
                                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,
                                    CurrPrice = dac1[i].Cost,
                                    CurrStr = (currArr.Find(x => x.Id == dac1[i].Currency)?.Remark ?? dac1[i].Currency.ToString()),
                                };

                                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,
                                    CurrPrice = hotel.ToList()[0].Cost,
                                    CurrStr = (currArr.Find(x => x.Id == hotel.ToList()[0].Currency)?.Remark ?? hotel.ToList()[0].Currency.ToString()),
                                };
                                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();

                    List<object> otherList = new List<object>();
                    if (dac4.Any())
                    {
                        foreach (var item in dac4)
                        {
                            otherList.Add(item);
                            sumOtherPrice += item.SubTotal;
                        }
                    }

                    //签证
                    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);

                    //其他费用
                    var other = new
                    {
                        Type = "Other",
                        Content = otherList
                    };
                    dynamics.Add(other);

                    decimal AirJJCTotal = 0;
                    decimal AirGWCTotal = 0;
                    decimal AirTDCTotal = 0;

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

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

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

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

                    //其他费用选择框
                    if (eec.OtherExpenses_Checked == 1)
                    {
                        AirJJCTotal += Math.Round(sumOtherPrice, 2);
                        AirGWCTotal += Math.Round(sumOtherPrice, 2);
                        AirTDCTotal += Math.Round(sumOtherPrice, 2);
                    }

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

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

                    //报批总报价 - 头等舱
                    if (eec.SumTDC == 0)
                    {
                        AirTDCTotal = 0;
                    }
                    var SumAirTDC = new
                    {
                        Type = "SumAirTDC",
                        Price = AirTDCTotal
                    };
                    dynamics.Add(SumAirTDC);

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

            return dynamics;
        }
    }
}