public static string ParseDataTableToJsonAndWriteToFile(DataTable dataTable)
        {
            string resultFile = string.Empty;

            if (dataTable == null)
            {
                return(resultFile);
            }

            Dictionary <long, List <string> > doctorList = new Dictionary <long, List <string> >();

            try {
                foreach (DataRow row in dataTable.Rows)
                {
                    long   dcode   = (long)row["dcode"];
                    string kodoper = row["wschema_kodoper"].ToString();

                    if (!doctorList.ContainsKey(dcode))
                    {
                        doctorList.Add(dcode, new List <string>());
                    }

                    doctorList[dcode].Add(kodoper);
                }
            } catch (Exception e) {
                Logging.ToLog(e.Message + Environment.NewLine + e.StackTrace);
            }

            string json = JsonConvert.SerializeObject(doctorList);

            resultFile = ExcelGeneral.GetResultFilePath("ServiceListByDoctorsToSiteJson", isPlainText: true).Replace(".txt", ".json");
            try {
                File.WriteAllText(resultFile, json);
            } catch (Exception e) {
                Logging.ToLog(e.Message + Environment.NewLine + e.StackTrace);
            }

            return(resultFile);
        }
Пример #2
0
        public static string ParseDataTableToJsonAndWriteToFile(DataTable dataTable)
        {
            string resultFile = string.Empty;

            Dictionary <long, ItemDoctor> doctorList = new Dictionary <long, ItemDoctor>();

            if (dataTable == null)
            {
                return(resultFile);
            }

            try {
                foreach (DataRow row in dataTable.Rows)
                {
                    long   dcode = (long)row["dcode"];
                    string date  = ((DateTime)row["date"]).ToShortDateString();

                    if (!doctorList.ContainsKey(dcode))
                    {
                        doctorList.Add(dcode, new ItemDoctor {
                            DCode = dcode
                        });
                    }

                    if (!doctorList[dcode].DaysCheck.ContainsKey(date))
                    {
                        doctorList[dcode].DaysCheck.Add(date, new Dictionary <string, ItemInterval>());
                    }

                    if (row["start"] == null || string.IsNullOrEmpty(row["start"].ToString()) ||
                        row["end"] == null || string.IsNullOrEmpty(row["end"].ToString()) ||
                        row["status"] == null || string.IsNullOrEmpty(row["status"].ToString()))
                    {
                        continue;
                    }

                    TimeSpan start        = (TimeSpan)row["start"];
                    TimeSpan end          = (TimeSpan)row["end"];
                    string   startTime    = start.ToString("hh\\:mm");
                    string   endTime      = end.ToString("hh\\:mm");
                    string   status       = (string)row["status"];
                    int      statusResult = status.Equals("Свободное время") || status.Equals("Резерв с возможностью записи") ? 1 : 2;

                    ItemInterval interval = new ItemInterval {
                        TStart = startTime,
                        TEnd   = endTime,
                        Status = statusResult
                    };

                    string intervalKey = startTime + "-" + endTime;

                    if (!doctorList[dcode].DaysCheck[date].ContainsKey(intervalKey))
                    {
                        doctorList[dcode].DaysCheck[date].Add(intervalKey, interval);
                    }
                    else
                    {
                        if (doctorList[dcode].DaysCheck[date][intervalKey].Status == 1)
                        {
                            if (interval.Status == 1)
                            {
                                doctorList[dcode].DaysCheck[date][intervalKey] = interval;
                            }
                        }
                    }
                }
            } catch (Exception e) {
                Logging.ToLog(e.Message + Environment.NewLine + e.StackTrace);
            }

            foreach (ItemDoctor doc in doctorList.Values)
            {
                foreach (string date in doc.DaysCheck.Keys)
                {
                    doctorList[doc.DCode].Days.Add(date, doc.DaysCheck[date].Values.ToList());
                }
            }

            string json = JsonConvert.SerializeObject(doctorList.Values);

            resultFile = ExcelGeneral.GetResultFilePath("FreeCellsToSiteJSON", isPlainText: true).Replace(".txt", ".json");
            try {
                File.WriteAllText(resultFile, json);
            } catch (Exception e) {
                Logging.ToLog(e.Message + Environment.NewLine + e.StackTrace);
            }

            return(resultFile);
        }