using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace ImpulseVision
{
public partial class FormAutorize : Form
{
public FormAutorize()
{
InitializeComponent();
TbxLogin.Focus();
TbxLogin.SelectionStart = 0;
}
#region <Переменные>
SqlConnection SCon = new SqlConnection(Properties.Settings.Default.ImpulseVisionAppConnectionString);
#endregion
private void BtnLogin_Click(object sender, EventArgs e)
{
if(TbxLogin.Text.Trim() == string.Empty || TbxPassword.Text.Trim() == string.Empty)
{
MessageBox.Show("Введите учётные данные и повторите попытку!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
BsUsers.Filter = $"Login = '{TbxLogin.Text.Trim()}' and Password = '{TbxPassword.Text.Trim()}'";
if (BsUsers.Count == 0)
{
MessageBox.Show("Пользователя с таким логином или паролем не существует!", "ImpilseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
else
{
Notify.Visible = false;
try
{
SCon.Open();
string QueryLogining = $@"insert into HistoryLogin (StaffsID, TimeLogin)
values ('{LblUserID.Text.Trim()}',GETDATE())";
SqlCommand Cmd = new SqlCommand(QueryLogining, SCon);
Cmd.ExecuteNonQuery();
SCon.Close();
}
catch
{
MessageBox.Show("Сервер не отвечает..\r\nПовторите попытку позже", "ImpulseVision", MessageBoxButtons
.OK, MessageBoxIcon.Exclamation);
return;
}
if (LblRole.Text == "1")
{
this.Hide();
FormMain FMain = new FormMain();
FMain.BsAdministrator.Filter = BsUsers.Filter;
FMain.ShowDialog();
this.Show();
}
else
{
this.Hide();
FormGuard FGuard = new FormGuard();
FGuard.BsStaffs.Filter = BsUsers.Filter;
FGuard.ShowDialog();
this.Show();
}
Notify.Visible = true;
TbxLogin.Text = string.Empty;
TbxPassword.Text = string.Empty;
this.staffsTableAdapter.Fill(this.impulseVisionAppDataSet1.Staffs);
GetLastTimeLogin();
}
}
private void FormAutorize_FormClosing(object sender, FormClosingEventArgs e)
{
if(File.Exists(Application.StartupPath+"\\autorun.txt"))
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
Notify.Visible = true;
}
else
{
Application.ExitThread();
}
}
private void FormAutorize_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'impulseVisionAppDataSet1.Staffs' table. You can move, or remove it, as needed.
try
{
this.staffsTableAdapter.Fill(this.impulseVisionAppDataSet1.Staffs);
}
catch
{
MessageBox.Show("Проверьте подключение к сети интернет и повторите попытку!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Application.ExitThread();
}
FormStart FStart = new FormStart();
FStart.ShowDialog();
ApplicationParameters application = new ApplicationParameters();
ClearOldRecordInJournal(application.CountDay);
GetLastTimeLogin();
LblRole.Hide();
LblUserID.Hide();
if (File.Exists(Application.StartupPath + "\\autorun.txt"))
{
Notify.Visible = true;
}
}
///
/// получение последнего времени входа
///
private void GetLastTimeLogin()
{
string LastLogin = string.Empty;
SCon.Open();
string QueryLastLogin = $@"select Max(TimeLogin) as LastTimeLogin
from HistoryLogin";
SqlCommand Cmd = new SqlCommand(QueryLastLogin, SCon);
SqlDataReader Res = Cmd.ExecuteReader();
if(Res.HasRows)
{
Res.Read();
LastLogin = "Последнее время входа: " + Res["LastTimeLogin"].ToString();
}
else
{
LastLogin = "Здесь будет отображаться последнее время входа в систему.";
}
SCon.Close();
Card.TextDescrition = LastLogin;
}
///
/// удаление фотографий в журнале,
/// которые старше указанного количества дней
///
private void ClearOldRecordInJournal(int CountDay)
{
if (!Directory.Exists(Application.StartupPath + "\\Source\\log"))
{
return;
}
DirectoryInfo Dir = new DirectoryInfo(Application.StartupPath + "\\Source\\log\\");
FileInfo[] Files = Dir.GetFiles();
string Path = string.Empty;
foreach (var t in Files)
{
Path = Application.StartupPath + "\\Source\\log\\" + t;
if (!File.Exists(Path))
continue;
string[] Parts = t.ToString().Split('_');
try
{
DateTime Tm = DateTime.Parse(Parts[3]);
TimeSpan Span = DateTime.Now - Tm;
int Dif = Span.Days;
if(Dif > CountDay)
{
File.Delete(Path);
}
}
catch
{
continue;
}
}
}
///
/// рисование градиентной заливки на форме
///
private void FormAutorize_Paint(object sender, PaintEventArgs e)
{
Point PtStart = new Point(0, 0);
Point PtEnd = new Point(Width, Height);
Color ColFirst = ColorTranslator.FromHtml("#0F2834");
Color ColThird = ColorTranslator.FromHtml("#496870");
//градиентный цвет
LinearGradientBrush LGB = new LinearGradientBrush(PtStart, PtEnd, ColFirst, ColThird);
Pen MyPen = new Pen(LGB);
Rectangle Rect = new Rectangle(PtStart,new Size(PtEnd));
e.Graphics.FillRectangle(LGB, Rect);
}
private void FormAutorize_Resize(object sender, EventArgs e)
{
Invalidate();
}
private void Notify_MouseClick(object sender, MouseEventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = true;
this.Show();
this.WindowState = FormWindowState.Normal;
}
else
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
Notify.Visible = true;
}
private void CbxShowPass_CheckedChanged(object sender, EventArgs e)
{
TbxPassword.UseSystemPasswordChar = !CbxShowPass.Checked;
}
}
}