コード例 #1
0
ファイル: MainForm.cs プロジェクト: yax51/C969-2
        static public Array getCalendar(bool weekView)
        {
            MySqlConnection c = new MySqlConnection(SqlUpdater.conString);

            c.Open();
            string          query = $"SELECT customerId, type, start, end, appointmentId, userId FROM appointment WHERE userid = '{SqlUpdater.getCurrentUserID()}'";
            MySqlCommand    cmd   = new MySqlCommand(query, c);
            MySqlDataReader rdr   = cmd.ExecuteReader();

            Dictionary <int, Hashtable> appointments = SqlUpdater.getAppointments();//new Dictionary<int, Hashtable>();


            while (rdr.Read())
            {
                Hashtable appointment = new Hashtable();
                appointment.Add("customerId", rdr[0]);
                appointment.Add("type", rdr[1]);
                appointment.Add("start", rdr[2]);
                appointment.Add("end", rdr[3]);
                appointment.Add("userId", rdr[5]);

                appointment.Add(Convert.ToInt32(rdr[4]), appointment);
            }
            rdr.Close();

            foreach (var app in appointments.Values)
            {
                query = $"SELECT userName FROM user WHERE userId = '{app["userId"]}'";
                cmd   = new MySqlCommand(query, c);
                rdr   = cmd.ExecuteReader();
                rdr.Read();
                app.Add("userName", rdr[0]);
                rdr.Close();
            }

            foreach (var app in appointments.Values)
            {
                query = $"SELECT customerName FROM customer WHERE customerId = '{app["customerId"]}'";
                cmd   = new MySqlCommand(query, c);
                rdr   = cmd.ExecuteReader();
                rdr.Read();
                app.Add("customerName", rdr[0]);
                rdr.Close();
            }

            Dictionary <int, Hashtable> parsedAppointments = new Dictionary <int, Hashtable>();

            foreach (var app in appointments)
            {
                DateTime startTime = DateTime.Parse(app.Value["start"].ToString());
                DateTime endTime   = DateTime.Parse(app.Value["end"].ToString());
                DateTime today     = DateTime.UtcNow;

                if (weekView)
                {
                    DateTime sunday   = today.AddDays(-(int)today.DayOfWeek);
                    DateTime saturday = today.AddDays(-(int)today.DayOfWeek + (int)DayOfWeek.Saturday);

                    if (startTime >= sunday && endTime < saturday)
                    {
                        parsedAppointments.Add(app.Key, app.Value);
                    }
                }
                else
                {
                    DateTime firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
                    DateTime lastDayOfMonth  = firstDayOfMonth.AddMonths(1).AddDays(-1);
                    if (startTime >= firstDayOfMonth && endTime < lastDayOfMonth)
                    {
                        parsedAppointments.Add(app.Key, app.Value);
                    }
                }
            }

            SqlUpdater.setAppointments(appointments);

            var appointmentArray = from row in parsedAppointments select new {
                ID        = row.Key,
                Type      = row.Value["type"],
                StartTime = SqlUpdater.convertToTimezone(row.Value["start"].ToString()),
                EndTime   = SqlUpdater.convertToTimezone(row.Value["end"].ToString()),
                Customer  = row.Value["customerName"]
            };

            c.Close();


            return(appointmentArray.ToArray());
        }