Files
ramb_stacja/StacjaProb/MotorStockEditForm.cs
T
2026-09-03 10:50:01 +02:00

226 lines
9.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StacjaProb
{
public partial class MotorStockEditForm : Form
{
public int devID;
public string selectedStatus = "";
public string selectedCompanies = "";
public static int newMotorID = 0;
public static string newMotorName = "";
public MotorStockEditForm()
{
InitializeComponent();
}
private void MotorStockEditForm_Load(object sender, EventArgs e)
{
LoadStatuses();
LoadCompanies();
if (devID > 0)
{
pictureBoxSearchMotor.Visible = false;
LoadConectPump();
buttonSave.Text = "Aktualizuj";
richTextBoxUwagi.Text = mySqlFunctions.sqlStringValue("select DRS_DEV_DESCRIPTION from seg_dev_resources where DRS_ID=" + devID.ToString(), "DRS_DEV_DESCRIPTION");
}
else
{
buttonSave.Enabled = false;
buttonDelete.Enabled = false;
}
}
private void LoadStatuses()
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
int index = 0;
// Ładuje listę dostępnych Pomp
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("select DIC_ID,DIC_POL_LANG from sys_dictionaries where DIC_TYPE_ID=1 order by DIC_POL_LANG", mySqlFunctions.connection);
MySql.Data.MySqlClient.MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
comboBoxStatus.Items.Insert(index, dataReader["DIC_POL_LANG"].ToString());
// zaznacza jako selected
if (dataReader["DIC_POL_LANG"].ToString() == selectedStatus)
comboBoxStatus.SelectedIndex = comboBoxStatus.Items.Count - 1;
index++;
}
dataReader.Close();
mySqlFunctions.CloseConnection();
}
}
private void LoadCompanies()
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
int index = 0;
// Ładuje listę dostępnych Pomp
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("select * from sys_participants order by PRT_NAME", mySqlFunctions.connection);
MySql.Data.MySqlClient.MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
comboBoxCompany.Items.Insert(index, dataReader["PRT_DESCRIPTION"].ToString());
if (dataReader["PRT_DESCRIPTION"].ToString() == selectedCompanies)
comboBoxCompany.SelectedIndex = comboBoxCompany.Items.Count - 1;
index++;
}
dataReader.Close();
mySqlFunctions.CloseConnection();
}
}
//połączone pompy
private void LoadConectPump()
{
try
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
// Ładuje listę dostępnych Pomp
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("select APE_PUP_NAME,APE_PUP_NO,PUP_Q_N,PUP_H_P from seg_aggregate_pup_eng left join seg_pumps on PUP_ID=APE_PUP_ID where APE_ENG_DRS_ID=" + devID.ToString(), mySqlFunctions.connection);
MySql.Data.MySqlClient.MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
labelMName.Text = dataReader["APE_PUP_NAME"].ToString();
labelMSN.Text = dataReader["APE_PUP_NO"].ToString();
labelMV.Text = dataReader["PUP_Q_N"].ToString();
labelMkW.Text = dataReader["PUP_H_P"].ToString();
buttonDelete.Enabled = false;
}
dataReader.Close();
mySqlFunctions.CloseConnection();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
string statID = mySqlFunctions.sqlStringValue("select DIC_ID from sys_dictionaries where DIC_POL_LANG='" + comboBoxStatus.SelectedItem.ToString() + "'", "DIC_ID");
string partID = mySqlFunctions.sqlStringValue("select PRT_ID from sys_participants where PRT_DESCRIPTION ='" + comboBoxCompany.SelectedItem.ToString() + "'", "PRT_ID");
//jeśli mamy do czynienia z EDYCJĄ - aktualizaujemy
if (devID > 0)
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("UPDATE seg_dev_resources SET DRS_SN_NUMBER='" + textBoxNS.Text + "',DRS_BUY_DATE='" + dateTimePickerBuy.Value.ToString("yyyy-MM-dd HH:mm:ss") + "',DRS_AVAILABLE_DATE='" + dateTimePickerAva.Value.ToString("yyyy-MM-dd HH:mm:ss") + "',DRS_STATUS_DIC_ID='" + statID + "',DRS_DEV_DESCRIPTION='" + richTextBoxUwagi.Text + "',DRS_AUDIT_DATE=now(), DRS_AUDIT_USR= '" + partID + "' WHERE DRS_ID=" + devID.ToString(), mySqlFunctions.connection);
cmd.ExecuteNonQuery();
// tabela historyczna
MySql.Data.MySqlClient.MySqlCommand his = new MySql.Data.MySqlClient.MySqlCommand("insert into seg_dev_resources_his select * from seg_dev_resources where DRS_ID = " + devID.ToString(), mySqlFunctions.connection);
his.ExecuteNonQuery();
mySqlFunctions.CloseConnection();
}
this.Close();
}
//dodajemy
else
{
MessageBox.Show("Dodaje");
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("INSERT INTO seg_dev_resources SET DRS_DEV_ID=" + newMotorID.ToString() + ",DRS_DEV_TYPE=11,DRS_SN_NUMBER='" + textBoxNS.Text + "',DRS_BUY_DATE='" + dateTimePickerBuy.Value.ToString("yyyy-MM-dd HH:mm:ss") + "',DRS_AVAILABLE_DATE='" + dateTimePickerAva.Value.ToString("yyyy-MM-dd HH:mm:ss") + "',DRS_STATUS_DIC_ID='" + statID + "',DRS_DEV_DESCRIPTION='" + richTextBoxUwagi.Text + "',DRS_AUDIT_USR='" + partID + "',DRS_AUDIT_DATE=now()", mySqlFunctions.connection);
cmd.ExecuteNonQuery();
// tabela historyczna
MySql.Data.MySqlClient.MySqlCommand his = new MySql.Data.MySqlClient.MySqlCommand("insert into seg_dev_resources_his select * from seg_dev_resources where DRS_ID=(select max(DRS_ID) from seg_dev_resources)", mySqlFunctions.connection);
his.ExecuteNonQuery();
mySqlFunctions.CloseConnection();
}
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void pictureBoxSearchMotor_FormClosed(object sender, FormClosedEventArgs e)
{
textBoxPName.Text = newMotorName;
}
private void textBoxNS_Leave(object sender, EventArgs e)
{
if (textBoxNS.TextLength > 0)
buttonSave.Enabled = true;
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void pictureBoxSearchMotor_Click(object sender, EventArgs e)
{
MotorStockChFromCatalogForm mscfcForm = new MotorStockChFromCatalogForm();
mscfcForm.FormClosed += new FormClosedEventHandler(pictureBoxSearchMotor_FormClosed);
mscfcForm.ShowDialog();
}
private void linkLabelHistoria_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MotorStockHistoryForm mshForm = new MotorStockHistoryForm();
mshForm.devID = devID;
mshForm.ShowDialog();
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("UWAGA!\nProces nieodwracalny.\nCzy na pewno chcesz usunąć silnik ze stanu?", "Potwierdzenie usunięcia z bazy", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("DELETE FROM seg_dev_resources WHERE DRS_ID = " + devID.ToString(), mySqlFunctions.connection);
cmd.ExecuteNonQuery();
mySqlFunctions.CloseConnection();
}
this.Close();
}
}
}
}