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 製品工数管理
{
public partial class Form5 : Form
{
public Form5(string worker_name, string wokers_id, string orders_id)
{
InitializeComponent();
ImeMode = ImeMode.Off;
timer1.Start();
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns[0].ReadOnly = true;
dataGridView1.Columns[1].ReadOnly = true;
dataGridView1.Columns[2].ReadOnly = true;//枝番
dataGridView1.Columns[3].ReadOnly = true;//開始
dataGridView1.Columns[4].ReadOnly = true;//終了
dataGridView1.Columns[5].ReadOnly = true;//調整
dataGridView1.Columns[6].ReadOnly = true;//稼働
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.Columns[2].Width = 50;
dataGridView1.Columns[3].Width = 200;
dataGridView1.Columns[4].Width = 200;
dataGridView1.Columns[5].Width = 110;
dataGridView1.Columns[6].Width = 110;
dataGridView1.Columns[7].Width = 50;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
dataGridView1.DefaultCellStyle.Font = new Font("Meiryo UI", 11);
dataGridView1.Columns[3].DefaultCellStyle.Font = new Font("Meiryo UI", 16);
dataGridView1.Columns[4].DefaultCellStyle.Font = new Font("Meiryo UI", 16);
dataGridView1.Columns[5].DefaultCellStyle.Font = new Font("Meiryo UI", 16);
dataGridView1.Columns[6].DefaultCellStyle.Font = new Font("Meiryo UI", 16);
textBox7.Text = wokers_id;
label10.Text = "" + worker_name + " さんの入力画面です。";
label10.Tag = worker_name;
comboBox1.Items.AddRange(new string[]{ "00:15" , "00:30" , "01:00" , "02:00" , "04:00" , "08:00"});
comboBox1.Text = "00:15";
//作業中一覧から起動
if(orders_id != "")
{
textBox5.Text = orders_id;
button1.PerformClick();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label7.Text = DateTime.Now.ToString("yyyy/MM/dd (dddd)");
label8.Text = DateTime.Now.ToString("HH:mm:ss");
}
public string DefaultQuery()
{
return "select items.orders_id , items.items_id , items.suffix_no , " +
"date_format(tasks_s.start_dt, '%y-%m-%d %H:%i') , " +
"date_format(tasks_e.end_dt, '%y-%m-%d %H:%i') , " +
"substring(tasks_p.adjust,1,length(tasks_p.adjust)-3) ,'','False' " +
"from items left outer join tasks_s on items.items_id = tasks_s.items_id and tasks_s.workers_id = " + textBox7.Text + " " +
"left outer join tasks_e on items.items_id = tasks_e.items_id and tasks_e.workers_id = " + textBox7.Text + " " +
"left outer join tasks_p on items.items_id = tasks_p.items_id and tasks_p.workers_id = " + textBox7.Text + " " +
"where items.orders_id = " + textBox5.Text + " order by items.items_id;";
//items.items_id は primary key。tasks_x.items_id と tasks_x.workers_id で 複合unique key。1対1のjoin。
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox5.Text == "")
{
MessageBox.Show("オーダーIDが入力されていません。");
return;
}
Db d = new Db();
if (!d.DbState)
{
MessageBox.Show(d.DbStateMessage);
return;
}
List<TextBox> t = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4, textBox6 };
if (!d.FormLoadExecuteReader("select customer,model,serial_no,quantity,notes from orders where orders_id = " + textBox5.Text + ";", t))
{
MessageBox.Show("オーダーIDが存在しません。");
return;
}
d.ExecuteReader(DefaultQuery(), "", dataGridView1);
SetWorkTime();
checkBox1.Checked = false;
}
private void SetWorkTime()
{
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
string s = dataGridView1.Rows[r].Cells[3].Value.ToString();
string e = dataGridView1.Rows[r].Cells[4].Value.ToString();
string a = dataGridView1.Rows[r].Cells[5].Value.ToString();
if (s != "" && e != "" && a != "")
{
dataGridView1.Rows[r].Cells[6].Value = Db.WorkTime(DateTime.Parse(s), DateTime.Parse(e), TimeSpan.Parse(a));
}
if (s != "" && e != "" && a == "")
{
dataGridView1.Rows[r].Cells[6].Value = Db.WorkTime(DateTime.Parse(s), DateTime.Parse(e), new TimeSpan(0,0,0));
}
if (s == "" || e == "")
{
dataGridView1.Rows[r].Cells[6].Value = "-";
}
}
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
int r = e.RowIndex; int c = e.ColumnIndex;
if (c == 3 && r >= 0 && dataGridView1.Rows[r].Cells[c].Value.ToString() != "")//開始
{
Db d = new Db();
if (!d.DbState)
{
MessageBox.Show(d.DbStateMessage);
return;
}
Form10 f = new Form10();
f.ShowDialog();
if (f.getDatetime == "")
{
DialogResult yn = MessageBox.Show("削除しますか?(終了も削除されます)", "", MessageBoxButtons.YesNo);
if (yn == DialogResult.No) return;
string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString();
string workers_id = textBox7.Text;
d.ExecuteNonReader("delete from tasks_s where items_id = " + items_id + " and workers_id = " + workers_id + ";", "");
d.ExecuteNonReader("delete from tasks_e where items_id = " + items_id + " and workers_id = " + workers_id + ";", "");
d.ExecuteReader(DefaultQuery(), "", dataGridView1);
SetWorkTime();
checkBox1.Checked = false;
MessageBox.Show("削除しました。");
}
else
{
string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString();
string workers_id = textBox7.Text;
d.ExecuteNonReader("update tasks_s set start_dt = '" + f.getDatetime + "' where items_id = " + items_id + " and workers_id = " + workers_id + ";", "");
d.ExecuteReader(DefaultQuery(), "", dataGridView1);
SetWorkTime();
checkBox1.Checked = false;
MessageBox.Show("更新しました。");
}
}
if (c == 4 && r >= 0 && dataGridView1.Rows[r].Cells[c].Value.ToString() != "")//終了
{
Db d = new Db();
if (!d.DbState)
{
MessageBox.Show(d.DbStateMessage);
return;
}
Form10 f = new Form10();
f.ShowDialog();
if (f.getDatetime == "")
{
DialogResult yn = MessageBox.Show("削除しますか?", "", MessageBoxButtons.YesNo);
if (yn == DialogResult.No) return;
string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString();
string workers_id = textBox7.Text;
d.ExecuteNonReader("delete from tasks_e where items_id = " + items_id + " and workers_id = " + workers_id + ";", "");
d.ExecuteReader(DefaultQuery(), "", dataGridView1);
SetWorkTime();
checkBox1.Checked = false;
MessageBox.Show("削除しました。");
}
else
{
string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString();
string workers_id = textBox7.Text;
d.ExecuteNonReader("update tasks_e set end_dt = '" + f.getDatetime + "' where items_id = " + items_id + " and workers_id = " + workers_id + ";", "");
d.ExecuteReader(DefaultQuery(), "", dataGridView1);
SetWorkTime();
checkBox1.Checked = false;
MessageBox.Show("更新しました。");
}
}
if (c == 5 && r >= 0 && dataGridView1.Rows[r].Cells[c].Value.ToString() != "")//調整
{
DialogResult yn = MessageBox.Show("削除しますか?", "", MessageBoxButtons.YesNo);
if (yn == DialogResult.No) return;
string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString();
string workers_id = textBox7.Text;
Db d = new Db();
if (!d.DbState)
{
MessageBox.Show(d.DbStateMessage);
return;
}
d.ExecuteNonReader("delete from tasks_p where items_id = " + items_id + " and workers_id = " + workers_id + ";", "");
d.ExecuteReader(DefaultQuery(), "", dataGridView1);
SetWorkTime();
checkBox1.Checked = false;
MessageBox.Show("削除しました。");
}
}
private void OnOff(object sender, EventArgs e)
{
if (button2 == sender) InsertDatetime("開始", 3, "tasks_s", "start_dt");
if (button3 == sender) InsertDatetime("終了", 4, "tasks_e", "end_dt");
}
public void InsertDatetime(string cap, int col_no, string tbl,string col_name)
{
if (dataGridView1.Rows.Count == 0) return;
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
string check = dataGridView1.Rows[r].Cells[7].Value.ToString();
if (check == "True" && dataGridView1.Rows[r].Cells[col_no].Value.ToString() != "")
{
MessageBox.Show("既に" + cap + "時刻が登録されています。削除後に登録してください。");
return;
}
if (check == "True" && col_no == 4 && dataGridView1.Rows[r].Cells[3].Value.ToString() == "")
{
MessageBox.Show("作業開始の前に作業終了を登録できません。");
return;
}
}
string query = "insert into " + tbl + " (items_id,workers_id," + col_name + ") values ";
Boolean queryExists = false;
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString();
string check = dataGridView1.Rows[r].Cells[7].Value.ToString();
if (check == "True" && dataGridView1.Rows[r].Cells[col_no].Value.ToString() == "")
{
query += "(" + items_id + "," + textBox7.Text + ",'" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "'),";
queryExists = true;
}
}
if (queryExists)
{
query = query.Substring(0, query.Length - 1) + ";";
Db d = new Db();
if (!d.DbState)
{
MessageBox.Show(d.DbStateMessage);
return;
}
d.ExecuteNonReader(query, "");
d.ExecuteReader(DefaultQuery(), "", dataGridView1);
SetWorkTime();
checkBox1.Checked = false;
MessageBox.Show("登録しました。");
}
}
private void UpDown(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0) return;
Boolean f = false;
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
if ("True" == dataGridView1.Rows[r].Cells[7].Value.ToString()) f = true;
}
if (f && (Button)sender == button4) PauseClick(TimeSpan.Parse("" + comboBox1.Text));
if (f && (Button)sender == button5) PauseClick(TimeSpan.Parse("-" + comboBox1.Text));
}
public void PauseClick(TimeSpan t)
{
Db d = new Db();
if (!d.DbState)
{
MessageBox.Show(d.DbStateMessage);
return;
}
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString();
string check = dataGridView1.Rows[r].Cells[7].Value.ToString();
if (check == "True")
{
string adjust_col = dataGridView1.Rows[r].Cells[5].Value.ToString();
TimeSpan ts ;
if(adjust_col == "")
{
ts = t;
}
else
{
ts = t + TimeSpan.Parse(adjust_col);
}
string q = "delete from tasks_p where items_id = " + items_id + " and workers_id = " + textBox7.Text + ";";
d.ExecuteNonReader(q, "");
if (ts != TimeSpan.Zero)
{
q = "insert into tasks_p (items_id,workers_id,adjust) values (" + items_id + "," + textBox7.Text + ",'" + ts.ToString() + "');";
d.ExecuteNonReader(q, "");
}
}
}
d.ExecuteReader(DefaultQuery(), "", dataGridView1);
SetWorkTime();
checkBox1.Checked = false;
MessageBox.Show("登録しました。");
}
private void button6_Click(object sender, EventArgs e)
{
Form8 f = new Form8(textBox7.Text,label10.Tag.ToString());
f.ShowDialog();
if(f.ReturnOrders_id != "")
{
textBox5.Text = f.ReturnOrders_id;
button1.PerformClick();
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0) return;
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
dataGridView1.Rows[r].Cells[7].Value = checkBox1.Checked.ToString();
}
}
}
}