using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using ZXing;
namespace Code2DReader
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
FilterInfoCollection filterInfoCollection;
VideoCaptureDevice captureDevice;
private void Form1_Load(object sender, EventArgs e)
{
rBtAll.Checked = true;
filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo filterInfo in filterInfoCollection)
{
cboDevice.Items.Add(filterInfo.Name);
cboDevice.SelectedIndex = 0;
cboSex.SelectedIndex = 0;
dtpBirthday.Value = Convert.ToDateTime("01/01/2000");
}
}
private void btnStart_Click(object sender, EventArgs e)
{
ResetInput();
lbCount.Text = "";
captureDevice = new VideoCaptureDevice(filterInfoCollection[cboDevice.SelectedIndex].MonikerString);
captureDevice.NewFrame += CapterDevice_Newframe;
captureDevice.Start();
timer1.Start();
txtLastName.Focus();
}
private void ResetInput()
{
txtQRcode.Clear();
txtAddress.Clear();
txtEmail.Clear();
txtFirstName.Clear();
txtLastName.Clear();
txtPhone.Clear();
cboSex.SelectedIndex = 0;
dtpBirthday.Value = Convert.ToDateTime("01/01/2000");
}
private void CapterDevice_Newframe(object sender, NewFrameEventArgs eventArgs)
{
pictureBox.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (captureDevice.IsRunning)
captureDevice.Stop();
}
string strZalo;
private void timer1_Tick(object sender, EventArgs e)
{
var context = new MSQRCodeEntities();
if (pictureBox.Image != null)
{
BarcodeReader barcodeReader = new BarcodeReader();
Result result = barcodeReader.Decode((Bitmap)pictureBox.Image);
if (result != null)
{
strZalo = result.ToString().Trim();
Customer cs = new Customer();
incoming icm = new incoming();
if (!checkExits(strZalo))
{
cs.zaloId = strZalo;
cs.firstName = txtFirstName.Text.Trim();
cs.lastName = txtLastName.Text.Trim();
cs.birthday = dtpBirthday.Value;
cs.sex = cboSex.SelectedIndex == 1 ? true : false;
cs.mobile = txtPhone.Text.Trim();
cs.email = txtEmail.Text.Trim();
cs.address = txtAddress.Text.Trim();
context.Customers.Add(cs);
context.SaveChanges();
icm.cusId = cs.id;
icm.count = 1;
icm.timeVisit = DateTime.Now;
context.incomings.Add(icm);
context.SaveChanges();
}
else
{
btnUpdate.Visible = true;
cs = context.Customers.First(c => c.zaloId == strZalo);
icm.cusId = cs.id;
LoadCustomer(cs);
icm = context.incomings.OrderByDescending(i => i.id).FirstOrDefault(i => i.cusId == cs.id);
icm.count += 1;
icm.timeVisit = DateTime.Now;
context.incomings.Add(icm);
context.SaveChanges();
}
txtQRcode.Text = strZalo;
lbCount.Text = LoadCountNumber(icm);
timer1.Stop();
if (captureDevice.IsRunning)
captureDevice.Stop();
}
}
}
private string LoadCountNumber(incoming icm)
{
string str = string.Empty;
var context = new MSQRCodeEntities();
if (rBtAll.Checked)
{
str = icm.count.ToString();
}
if (rBtYear.Checked)
{
str = context.incomings.Where(i => i.cusId == icm.cusId).Where(i => i.timeVisit.Year == icm.timeVisit.Year).ToList().Count.ToString();
}
if (rBtquarter.Checked)
{
int month = icm.timeVisit.Month;
int[] months = { month - 1, month, month + 1 };
str = context.incomings.Where(i => i.cusId == icm.cusId).Where(i => months.Contains(i.timeVisit.Month)).ToList().Count.ToString();
//.Where(i => i.timeVisit.Month == (month - 1) || i.timeVisit.Month == month || i.timeVisit.Month == (month + 1)).ToList().Count.ToString();
}
if (rBtMonth.Checked)
{
str = context.incomings.Where(i => i.cusId == icm.cusId).Where(i => i.timeVisit.Month == icm.timeVisit.Month).ToList().Count.ToString();
}
return str;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
var context = new MSQRCodeEntities();
Customer cs = context.Customers.First(c => c.zaloId == strZalo);
UpdateCustomer(cs);
context.Entry(cs).State = EntityState.Modified;
context.SaveChanges();
btnUpdate.Visible = false;
MessageBox.Show("Successful!");
}
private void LoadCustomer(Customer cs)
{
txtFirstName.Text = cs.firstName;
txtLastName.Text = cs.lastName;
cboSex.SelectedIndex = cs.sex == true ? 1 : 0;
dtpBirthday.Value = !(cs.birthday.Equals(null)) ? cs.birthday.Value : Convert.ToDateTime("01/01/2000");
txtPhone.Text = cs.mobile;
txtEmail.Text = cs.email;
txtAddress.Text = cs.address;
}
private void UpdateCustomer(Customer cs)
{
cs.firstName = txtFirstName.Text.Trim();
cs.lastName = txtLastName.Text.Trim();
cs.birthday = dtpBirthday.Value;
cs.sex = cboSex.SelectedIndex == 1 ? true : false;
cs.mobile = txtPhone.Text.Trim();
cs.email = txtEmail.Text.Trim();
cs.address = txtAddress.Text.Trim();
}
private bool checkExits(string strZalo)
{
bool result = false;
var context = new MSQRCodeEntities();
result = context.Customers.Any(c => c.zaloId == strZalo);
return result;
}
private void FrmMain_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.F5:
btnStart_Click(null, null);
break;
case Keys.Enter:
if (btnUpdate.Visible)
{
btnUpdate_Click(null, null);
}
break;
case Keys.Escape:
this.Close();
break;
}
}
}
}