Files
kreta/Kreta.IntezmenyLetrehozo/Kreta.IntezmenyLetrehozo/HelperClasses/Helper.cs
T
2024-03-13 00:33:46 +01:00

252 lines
8.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Controls;
using Newtonsoft.Json;
namespace Kreta.IntezmenyLetrehozo
{
internal static class Helper
{
internal static Dictionary<string, string> GetFenntartokList(string kornyezet)
{
string JSONFile = "fenntartokJSON_set";
FileInfo executingAssembly = new FileInfo(Assembly.GetExecutingAssembly().Location);
string jsonString = File.ReadAllText(Path.Combine(executingAssembly.DirectoryName, "Fenntartok_JSON_set", $"{JSONFile}.json"));
Dictionary<string, Dictionary<string, string>> fenntartokListak = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(jsonString);
Dictionary<string, string> kornyezetLista = new Dictionary<string, string>();
fenntartokListak.TryGetValue(kornyezet, out kornyezetLista);
return kornyezetLista;
}
internal static Color MetroBlue()
{
Color myRgbColor = new Color();
myRgbColor = Color.FromArgb(0, 195, 227);
return myRgbColor;
}
internal static object GetPropertyValue(this object T, string PropName)
{
return T.GetType().GetProperty(PropName) == null ? null : T.GetType().GetProperty(PropName).GetValue(T, null);
}
internal static bool AreCellIsEmpty(this DataRow row)
{
bool isNullValue = false;
for (int i = 0; i < row.ItemArray.Length; i++)
{
if (row.ItemArray[i] == null || string.IsNullOrEmpty(row.ItemArray[i].ToString()) && i != 15)
{
isNullValue = true;
break;
}
}
return isNullValue;
}
internal static void SaveLog(string log, string intezemnyKod)
{
string strPath = Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
using (var logFile = new StreamWriter($"{strPath}\\log_{intezemnyKod}_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}.txt"))
{
logFile.Write(log);
}
}
internal static string TelepitettIntezmenyAzonositokString(List<IntezmenyAdatok> intezmenyek)
{
StringBuilder sb = new StringBuilder();
foreach (var intezmeny in intezmenyek)
{
sb.Append("'" + intezmeny.IntezmenyKod + "'" + ",");
}
return sb.ToString().TrimEnd(',');
}
internal static string CreatePassword(int length)
{
var jelszoGeneralas = Convert.ToBoolean(ConfigurationManager.AppSettings["jelszogeneralas"]);
if (jelszoGeneralas)
{
const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var res = new StringBuilder();
while (0 < length--)
{
res.Append(valid[RandomGeneretator.Next(valid.Length)]);
}
return res.ToString();
}
return "a";
}
static class RandomGeneretator
{
private static readonly Random globalRandom = new Random(Environment.TickCount);
[ThreadStatic]
private static Random localRandom;
public static int Next(int maxValue)
{
if (localRandom == null)
{
int seed;
lock (globalRandom)
{
seed = globalRandom.Next();
}
localRandom = new Random(seed);
}
return localRandom.Next(maxValue);
}
}
internal static int GetKozteruletJellegId(string kozteruletJellege)
{
int kozteruletJellegId = 911; //Na
switch (kozteruletJellege?.Trim().ToLower())
{
case "fasor":
kozteruletJellegId = 920;
break;
case "körút":
kozteruletJellegId = 938;
break;
case "köz":
kozteruletJellegId = 939;
break;
case "sor":
kozteruletJellegId = 954;
break;
case "sugárút":
kozteruletJellegId = 955;
break;
case "sétány":
kozteruletJellegId = 958;
break;
case "tér":
kozteruletJellegId = 961;
break;
case "utca":
kozteruletJellegId = 964;
break;
case "út":
kozteruletJellegId = 965;
break;
case "útja":
kozteruletJellegId = 966;
break;
}
return kozteruletJellegId;
}
internal static void UpdateStatusBar(MetroProgressBar progressBar, int status, bool? Visible = null)
{
progressBar.BeginInvoke(
new Action(() =>
{
if (Visible.HasValue)
progressBar.Visible = Visible.Value;
progressBar.Value = status;
progressBar.Update();
}));
}
internal static void UpdateLogText(TextBox control, string text)
{
control.BeginInvoke(
new Action(() =>
{
control.AppendText(string.Format(text + " - {0}! {1}", GetDatumForLog(), Environment.NewLine));
control.Update();
}));
}
internal static DataTable ConvertListToDataTable(List<IntezmenyAdatok> list)
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(IntezmenyAdatok));
DataTable dt = new DataTable();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
dt.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (IntezmenyAdatok item in list)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
dt.Rows.Add(values);
}
return dt;
}
internal static List<IntezmenyAdatok> ConvertDataTableToIntemenyAdatokList(DataTable dt)
{
var result = new List<IntezmenyAdatok>();
foreach (DataRow row in dt.Rows)
{
if (!row.AreCellIsEmpty())
{
var intezmeny = new IntezmenyAdatok();
intezmeny.Sorszam = row.Field<int>("Sorszam");
intezmeny.OMKod = row.Field<string>("OMKod");
intezmeny.IntezmenyNev = row.Field<string>("IntezmenyNev");
intezmeny.IntezmenyVezeto = row.Field<string>("IntezmenyVezeto");
intezmeny.EmailCim = row.Field<string>("EmailCim");
intezmeny.Tagintezmenykod = row.Field<string>("Tagintezmenykod");
intezmeny.Megye = row.Field<string>("Megye");
intezmeny.Iranyitoszam = row.Field<string>("Iranyitoszam");
intezmeny.Telepules = row.Field<string>("Telepules");
intezmeny.Kozterulet = row.Field<string>("Kozterulet");
intezmeny.KozteruletJellege = row.Field<string>("KozteruletJellege");
intezmeny.Hazszam = row.Field<string>("Hazszam");
intezmeny.FenntartoAzonosito = row.Field<string>("FenntartoAzonosito");
intezmeny.IntezmenyKod = row.Field<string>("IntezmenyKod");
intezmeny.AktivTanev = row.Field<string>("AktivTanev");
intezmeny.KovetkezoTanev = row.Field<string>("KovetkezoTanev");
intezmeny.KellABHet = row.Field<bool>("KellABHet");
intezmeny.TeljesKreta = row.Field<bool>("TeljesKreta");
result.Add(intezmeny);
}
}
return result;
}
internal static string GetDatumForLog()
{
return DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}