示例#1
0
    static void Main()
    {
        Program program = new Program();

        int g = typeof(Hotels).GetProperties()
                .Select(field => field.Name)
                .ToList().Count;
        var hotelsList = new ExcelMapper("bustrips.xlsx").Fetch <Hotels>();
        var puls       = new ExcelMapper("PUL.xlsx").Fetch <PUL>();
        var trips      = new ExcelMapper("bustrips.xlsx").Fetch <BusTrip>();
        var slots      = new ExcelMapper("Assignments.xlsx").Fetch <Slot>();
        var volunteers = new ExcelMapper("TLBC.xlsx").Fetch <Volunteer>();
        var busIds     = new ExcelMapper("BUSID.xlsx").Fetch <BusID>();


        trips = trips.Where(x => !x.SlotName.ToUpper().Contains("CONV".ToUpper()));

        List <string> locationsDone = new List <string>();

        program.StartWordApp();

        string currentLocation;
        string currentDay;

        foreach (BusTrip busTrip in trips)
        {
            int water      = 0;
            int startBuses = 0;
            int midBuses   = 0;


            currentLocation = busTrip.Location;

            // Slot may have been done before
            if (locationsDone.Contains(currentLocation))
            {
                continue;
            }

            List <BusTrip> locationTrips = trips.Where(x => x.Location.Equals(currentLocation)).ToList();

            locationTrips.Sort(new StartTimeComparer());

            currentDay = locationTrips.First().StartTimeDate;

            Console.WriteLine($"Location: {currentLocation}");
            Console.WriteLine($"Day: {currentDay}");

            program.OpenDocument("input.docx");
            program.ActivateDocument();
            program.CopyTable();

            int busIndex = 1;

            for (int i = 0; i < locationTrips.Count; i++)
            {
                if (!currentDay.Equals(locationTrips[i].StartTimeDate))
                {
                    program.HeaderFindAndReplace("{TOTALWATER}", water.ToString());
                    program.HeaderFindAndReplace("{BUSCOUNT}", busIndex - 1);
                    program.HeaderFindAndReplace("{FIRSTBUSCOUNT}", startBuses);
                    program.HeaderFindAndReplace("{MIDBUSCOUNT}", midBuses);

                    var filename1 = $"{currentLocation}_{currentDay.Replace("/", "")}";
                    program.SaveAs(filename1);
                    program.SaveAsPDF(filename1);
                    program.CloseDocument();

                    var emailThese1 = slots.Where(y => y.Location.Contains(currentLocation) && y.StartDate.Equals(currentDay) && y.Usage.Equals("AT_Pick")).ToList();

                    if (!emailThese1.Any())
                    {
                        program.SendEmail("*****@*****.**", "At@Jw2019", "*****@*****.**", "Fail to send Assignment", $"Não foi possível encontrar assignments para a localização e dia: {currentLocation} / {currentDay}", $"{filename1}.pdf");
                    }

                    emailThese1.ForEach(y =>
                    {
                        program.SendEmail("*****@*****.**", "At@Jw2019", y.Email, $"Relatório Diário de Pickup {currentDay}", program.GetEmailBody(currentDay), $"{filename1}.pdf");
                    });

                    program.DeleteFile(filename1 + ".pdf");
                    program.DeleteFile(filename1 + ".docx");

                    currentDay = locationTrips[i].StartTimeDate;
                    Console.WriteLine($"Day: {currentDay}");
                    busIndex   = 1;
                    water      = 0;
                    startBuses = 0;
                    midBuses   = 0;

                    program.OpenDocument("input.docx");
                    program.ActivateDocument();
                    program.CopyTable();
                }

                if (busIndex > 1)
                {
                    program.PasteTable();
                }

                if (((busIndex % 3) == 0) && !busIndex.Equals(program.CountTotalBus(locationTrips[i].StartTime, locationTrips)))
                {
                    program.Document.Words.Last.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
                }


                BusTrip x = locationTrips.ElementAt(i);

                if (busIndex == 1)
                {
                    program.HeaderFindAndReplace("{LOCATION}", program.GetNameByCode(puls.ToList(), x.Location));
                    program.HeaderFindAndReplace("{DATE}", x.StartTimeDate);
                    program.FooterFindAndReplace("{LOCATION}", program.GetNameByCode(puls.ToList(), x.Location));
                    program.FooterFindAndReplace("{DATE}", x.StartTimeDate);
                }

                program.FindAndReplace("{LOCATION}", program.GetAddressByCode(puls.ToList(), x.Location));
                program.FindAndReplace("{SLOTNAME}", x.SlotName);
                var busid = busIds.FirstOrDefault(y => y.SlotName.Equals(x.SlotName));
                program.FindAndReplace("{BUSID}", busid == null ? "N/A" : busid.BUSID);
                program.FindAndReplace("{ACTIVITYNAME}", x.ActivityName);
                program.FindAndReplace("{LEAVETIME}", x.StartTime.ToString("dd/MM/yyyy HH:mm"));
                program.FindAndReplace("{DELEGATES}", x.Delegates);
                program.FindAndReplace("{ACTIVITYNAME}", x.ActivityName);
                program.FindAndReplace("{BUSINDEX}", busIndex);
                program.FindAndReplace("{OBS}", x.Obs);
                program.FindAndReplace("{TOURLEADER}", program.GetTLBySlot(slots.ToList(), volunteers.ToList(), x.SlotName));
                program.FindAndReplace("{BUSCAPTAIN}", program.GetBCBySlot(slots.ToList(), volunteers.ToList(), x.SlotName));


                Hotels         hotel      = hotelsList.ElementAt(trips.ToList().IndexOf(x));
                string         hotelsCell = String.Empty;
                Type           fieldsType = typeof(Hotels);
                PropertyInfo[] fields     = fieldsType.GetProperties();
                for (int j = 0; j < fields.Length; j++)
                {
                    if (((int)fields[j].GetValue(hotel)) > 0)
                    {
                        hotelsCell += String.IsNullOrEmpty(hotelsCell) ? String.Empty : ",\n";
                        hotelsCell += $"{program.GetNameByCode(puls.ToList(), fields[j].Name)} ({fields[j].GetValue(hotel)})";
                    }
                }

                program.FindAndReplace("{HOTEL}", hotelsCell);

                program.CheckIndex(trips.ToList(), locationTrips[i]);

                if (program.IsFirst && program.IsLast)
                {
                    startBuses++;
                    water += 6;
                    program.FindAndReplace("{HASWATER}", "Sim");
                    program.FindAndReplace("{PREVIOUSLOCATION}", "Início de Trajeto");
                    program.FindAndReplace("{NEXTLOCATION}", "Última Paragem");
                }
                else if (program.IsFirst)
                {
                    startBuses++;
                    water += 6;
                    program.FindAndReplace("{HASWATER}", "Sim");
                    program.FindAndReplace("{PREVIOUSLOCATION}", "Início de Trajeto");
                    program.FindAndReplace("{NEXTLOCATION}", program.GetNameByCode(puls.ToList(), program.NextTripInSlot));
                }
                else if (program.IsLast)
                {
                    midBuses++;
                    program.FindAndReplace("{HASWATER}", "Não");
                    program.FindAndReplace("{NEXTLOCATION}", "Última Paragem");
                    program.FindAndReplace("{PREVIOUSLOCATION}", program.GetNameByCode(puls.ToList(), program.PreviousTripInSlot));
                }
                else
                {
                    midBuses++;
                    program.FindAndReplace("{HASWATER}", "Não");
                    program.FindAndReplace("{PREVIOUSLOCATION}", program.GetNameByCode(puls.ToList(), program.PreviousTripInSlot));
                    program.FindAndReplace("{NEXTLOCATION}", program.GetNameByCode(puls.ToList(), program.NextTripInSlot));
                }

                busIndex++;
            }

            locationsDone.Add(currentLocation);

            program.HeaderFindAndReplace("{TOTALWATER}", water.ToString());
            program.HeaderFindAndReplace("{BUSCOUNT}", busIndex - 1);
            program.HeaderFindAndReplace("{FIRSTBUSCOUNT}", startBuses);
            program.HeaderFindAndReplace("{MIDBUSCOUNT}", midBuses);

            var filename2 = $"{currentLocation}_{currentDay.Replace("/", "")}";
            program.SaveAs(filename2);
            program.SaveAsPDF(filename2);
            program.CloseDocument();

            var emailThese = slots.Where(x => x.Location.Contains(currentLocation) && x.StartDate.Equals(currentDay) && x.Usage.Equals("AT_Pick")).ToList();

            if (!emailThese.Any())
            {
                program.SendEmail("*****@*****.**", "At@Jw2019", "*****@*****.**", "Fail to send Assignment", $"Não foi possível encontrar assignments para a localização e dia: {currentLocation} / {currentDay}", $"{filename2}.pdf");
            }

            emailThese.ForEach(x =>
            {
                program.SendEmail("*****@*****.**", "At@Jw2019", x.Email, $"Relatório Diário de Pickup {currentDay}", program.GetEmailBody(currentDay), $"{filename2}.pdf");
            });

            program.DeleteFile(filename2 + ".pdf");
            program.DeleteFile(filename2 + ".docx");
        }

        //for (int i = 0; i < nomes.Count; i++)
        //{
        //    program.OpenDocument("input.docx");
        //    program.ActivateDocument();
        //    program.FindAndReplace("{NAME}", nomes.ElementAt(i));
        //    program.FindAndReplace("{DESIGNACAO}", "Same for everyone");
        //    program.SaveAs($"Test{i}");
        //    program.SaveAsPDF($"Test{i}");
        //    program.CloseDocument();
        //    program.SendEmail("*****@*****.**", "basquet7GMru", emails.ElementAt(i), "Test", program.GetEmailBody(), $"Test{i}.pdf");
        //    program.DeleteFile($"Test{i}.pdf");
        //    program.DeleteFile($"Test{i}.docx");
        //}

        program.QuitWordApp();
    }