Form3.cs 22.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
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;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace MedicalCheckInputTool
{
    public partial class Form3 : Form
    {
        private const int PAGEMAX = 10;
        private int currentPageNo = 1;
        private int maxPageNo = 1;

        public Form3()
        {
            InitializeComponent();

            this.Text = "ロット: " + EditParam.lotNo + "  ユーザ: " + EditParam.inputUser;

            this.label2.Text = "";
        }

        private void setPage()
        {
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            MySqlConnection readerCon = new MySqlConnection(connstr);
            readerCon.Open();
            string countQuery = "select count(*) from page_compare where ";
//            string orderby = " order by `f_行政区コード`, `f_整理番号`";

            // ロットの指定は必須
            countQuery += "(`f_ロット番号` = " + EditParam.lotNo + " or `s_ロット番号` = " + EditParam.lotNo + ") ";

            if (!"".Equals(textBox1.Text))
            {
                countQuery += " and (`f_整理番号` like '%" + textBox1.Text + "%' or `s_整理番号` like '%" + textBox1.Text + "%')";
            }

            if (this.checkBox1.Checked)
            {
                countQuery += " and (`f_完了` <> 1 or `s_完了` <> 1)";
            }

            if (this.checkBox2.Checked && this.checkBox3.Checked)
            {

            }
            else
            {
                if (this.checkBox2.Checked)
                {
                    countQuery += " and `f_整理番号` is not null";
                }
                if (this.checkBox3.Checked)
                {
                    countQuery += " and `s_整理番号` is not null";
                }
            }
            // 並び順
//            countQuery += orderby;

            //ページ設定
            MySqlCommand cntCmd = new MySqlCommand(countQuery, readerCon);
            MySqlDataReader cntreader = cntCmd.ExecuteReader();
            int dataCnt = 0;
            while (cntreader.Read())
            {
                dataCnt = cntreader.GetInt32(0);
            }
            cntreader.Close();

            maxPageNo = 1;
            if (dataCnt > 0)
            {
                maxPageNo = (dataCnt / PAGEMAX) + ((dataCnt % PAGEMAX == 0) ? 0 : 1);
            }

            comboBox1.Items.Clear(); //クリアする
            for (int i = 0; i < maxPageNo; i++)
            {
                comboBox1.Items.Add((i + 1).ToString());
            }
            // 読み取り専用(テキストボックスは編集不可)にする
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.SelectedIndex = 0; // 先頭の項目を選択
            currentPageNo = 1;
            readerCon.Close();
        }

        private void search()
        {
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            MySqlConnection readerCon = new MySqlConnection(connstr);
            readerCon.Open();
            string readQuery = "select * from page_compare where ";
            string orderby = " order by `f_行政区コード`, `f_整理番号`";
            string paging = " limit " + PAGEMAX + " offset " + ((currentPageNo - 1) * PAGEMAX);

            // ロットの指定は必須
            readQuery += "(`f_ロット番号` = " + EditParam.lotNo + " or `s_ロット番号` = " + EditParam.lotNo + ") ";

            if (!"".Equals(textBox1.Text))
            {
                readQuery += " and (`f_整理番号` like '%" + textBox1.Text + "%' or `s_整理番号` like '%" + textBox1.Text + "%')";
            }

            if (this.checkBox1.Checked)
            {
//                readQuery += " and (`f_完了` <> 1 or `s_完了` <> 1)";
                readQuery += " and (`f_完了` is null or `s_完了` is null)";
            }

            if (this.checkBox2.Checked && this.checkBox3.Checked)
            {

            }
            else
            {
                if (this.checkBox2.Checked)
                {
                    readQuery += " and `f_整理番号` is not null";
                }
                if (this.checkBox3.Checked)
                {
                    readQuery += " and `s_整理番号` is not null";
                }
            }
            // 並び順
            readQuery += orderby;

            //ページ指定
            readQuery += paging;

            MySqlCommand readCmd = new MySqlCommand(readQuery, readerCon);
            MySqlDataReader reader = readCmd.ExecuteReader();

            this.dataGridView1.Rows.Clear();
            while (reader.Read())
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(this.dataGridView1);
                row.Cells[0].Value = (this.isDBNull(reader, "f_完了") == "1") ? "一致" : "不一致";
                this.dataGridView1.Rows.Add(row);
                foreach (string prefix in new string[2] { "f_", "s_" })
                {
                    row.Cells[prefix + "行政区コード"].Value = this.isDBNull(reader, prefix + "行政区コード");
                    row.Cells[prefix + "整理番号"].Value = this.isDBNull(reader, prefix + "整理番号");
                    row.Cells[prefix + "世帯番号"].Value = this.isDBNull(reader, prefix + "世帯番号");
                    row.Cells[prefix + "電話番号"].Value = this.isDBNull(reader, prefix + "電話番号");
                    row.Cells[prefix + "携帯番号"].Value = this.isDBNull(reader, prefix + "携帯番号");
                    row.Cells[prefix + "健康診査"].Value = this.isDBNull(reader, prefix + "健康診査");
                    row.Cells[prefix + "結核肺がん"].Value = this.isDBNull(reader, prefix + "結核肺がん");
                    row.Cells[prefix + "胃がん"].Value = this.isDBNull(reader, prefix + "胃がん");
                    row.Cells[prefix + "大腸がん"].Value = this.isDBNull(reader, prefix + "大腸がん");
                    row.Cells[prefix + "前立腺がん"].Value = this.isDBNull(reader, prefix + "前立腺がん");
                    row.Cells[prefix + "骨粗鬆症"].Value = this.isDBNull(reader, prefix + "骨粗鬆症");
                    row.Cells[prefix + "成人歯科"].Value = this.isDBNull(reader, prefix + "成人歯科");
                    row.Cells[prefix + "子宮頸がん"].Value = this.isDBNull(reader, prefix + "子宮頸がん");
                    row.Cells[prefix + "乳がん"].Value = this.isDBNull(reader, prefix + "乳がん");
                    row.Cells[prefix + "希望場所"].Value = this.isDBNull(reader, prefix + "希望場所");
                    row.Cells[prefix + "身体不自由"].Value = this.isDBNull(reader, prefix + "身体不自由");
                }
                if (!row.Cells["f_整理番号"].Value.ToString().Equals(row.Cells["s_整理番号"].Value.ToString()))
                {
                    row.Cells["f_整理番号"].Style.BackColor = Color.Pink;
                    row.Cells["s_整理番号"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_世帯番号"].Value.ToString().Equals(row.Cells["s_世帯番号"].Value.ToString()))
                {
                    row.Cells["f_世帯番号"].Style.BackColor = Color.Pink;
                    row.Cells["s_世帯番号"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_電話番号"].Value.ToString().Equals(row.Cells["s_電話番号"].Value.ToString()))
                {
                    row.Cells["f_電話番号"].Style.BackColor = Color.Pink;
                    row.Cells["s_電話番号"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_携帯番号"].Value.ToString().Equals(row.Cells["s_携帯番号"].Value.ToString()))
                {
                    row.Cells["f_携帯番号"].Style.BackColor = Color.Pink;
                    row.Cells["s_携帯番号"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_健康診査"].Value.ToString().Equals(row.Cells["s_健康診査"].Value.ToString()))
                {
                    row.Cells["f_健康診査"].Style.BackColor = Color.Pink;
                    row.Cells["s_健康診査"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_結核肺がん"].Value.ToString().Equals(row.Cells["s_結核肺がん"].Value.ToString()))
                {
                    row.Cells["f_結核肺がん"].Style.BackColor = Color.Pink;
                    row.Cells["s_結核肺がん"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_胃がん"].Value.ToString().Equals(row.Cells["s_胃がん"].Value.ToString()))
                {
                    row.Cells["f_胃がん"].Style.BackColor = Color.Pink;
                    row.Cells["s_胃がん"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_大腸がん"].Value.ToString().Equals(row.Cells["s_大腸がん"].Value.ToString()))
                {
                    row.Cells["f_大腸がん"].Style.BackColor = Color.Pink;
                    row.Cells["s_大腸がん"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_前立腺がん"].Value.ToString().Equals(row.Cells["s_前立腺がん"].Value.ToString()))
                {
                    row.Cells["f_前立腺がん"].Style.BackColor = Color.Pink;
                    row.Cells["s_前立腺がん"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_骨粗鬆症"].Value.ToString().Equals(row.Cells["s_骨粗鬆症"].Value.ToString()))
                {
                    row.Cells["f_骨粗鬆症"].Style.BackColor = Color.Pink;
                    row.Cells["s_骨粗鬆症"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_成人歯科"].Value.ToString().Equals(row.Cells["s_成人歯科"].Value.ToString()))
                {
                    row.Cells["f_成人歯科"].Style.BackColor = Color.Pink;
                    row.Cells["s_成人歯科"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_子宮頸がん"].Value.ToString().Equals(row.Cells["s_子宮頸がん"].Value.ToString()))
                {
                    row.Cells["f_子宮頸がん"].Style.BackColor = Color.Pink;
                    row.Cells["s_子宮頸がん"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_乳がん"].Value.ToString().Equals(row.Cells["s_乳がん"].Value.ToString()))
                {
                    row.Cells["f_乳がん"].Style.BackColor = Color.Pink;
                    row.Cells["s_乳がん"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_希望場所"].Value.ToString().Equals(row.Cells["s_希望場所"].Value.ToString()))
                {
                    row.Cells["f_希望場所"].Style.BackColor = Color.Pink;
                    row.Cells["s_希望場所"].Style.BackColor = Color.Pink;
                }
                if (!row.Cells["f_身体不自由"].Value.ToString().Equals(row.Cells["s_身体不自由"].Value.ToString()))
                {
                    row.Cells["f_身体不自由"].Style.BackColor = Color.Pink;
                    row.Cells["s_身体不自由"].Style.BackColor = Color.Pink;
                }
            }
            readerCon.Close();

            readerCon.Open();
            MySqlCommand countCmd = new MySqlCommand("select count(`完了`) as cnt from `2回目` where `完了` = 1 and `ロット番号` = " + EditParam.lotNo, readerCon);
            MySqlDataReader countReader = countCmd.ExecuteReader();

            while (countReader.Read())
            {
                this.label2.Text = countReader.GetString("cnt");
            }
            countReader.Close();
            readerCon.Close();

        }

        private string isDBNull(MySqlDataReader reader, string column)
        {
            int index = reader.GetOrdinal(column);
            if (reader.IsDBNull(index))
            {
                return "";
            }
            else
            {
                return reader.GetString(column);
            }
        }

        // 行マッチ判定
        private void check()
        {
            // 行マッチングデータを取得
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];

            // 更新用Connection
            MySqlConnection updateCon = new MySqlConnection(connstr);
            updateCon.Open();

            /*
            // 一度フラグを落とす
            string resetFsql = "update `1回目` set `完了` = 0 where `ロット番号` = @lot";
            if (!"".Equals(textBox1.Text))
            {
                resetFsql += " and `整理番号` like '%" + textBox1.Text + "%'";
            }
            MySqlCommand resetFirst = new MySqlCommand(resetFsql, updateCon);
            resetFirst.Parameters.AddWithValue("lot", EditParam.lotNo);
            resetFirst.ExecuteNonQuery();

            string resetSsql = "update `2回目` set `完了` = 0 where `ロット番号` = @lot";
            if (!"".Equals(textBox1.Text))
            {
                resetSsql += " and `整理番号` like '%" + textBox1.Text + "%'";
            }
            MySqlCommand resetSecond = new MySqlCommand(resetSsql, updateCon);
            resetSecond.Parameters.AddWithValue("lot", EditParam.lotNo);
            resetSecond.ExecuteNonQuery();
             */

            // フラグ更新用コマンド
            MySqlCommand updateFirst = new MySqlCommand("update `1回目` set `完了` = 1 where " +
              "`ロット番号` = @lot and " +
              "`整理番号` = @serialCode", updateCon);
            // フラグ更新用コマンド
            MySqlCommand updateSecond = new MySqlCommand("update `2回目` set `完了` = 1 where " +
              "`ロット番号` = @lot and " +
              "`整理番号` = @serialCode", updateCon);

            // 行突合データを取得する
            // 参照用Connection
            MySqlConnection readerCon = new MySqlConnection(connstr);
            readerCon.Open();
            string readQuery = "select * from page_compare where (`f_ロット番号` = @f_lot or `s_ロット番号` = @s_lot) and (`f_完了` <> 1 or `s_完了` <> 1)";
            string orderby = " order by `f_行政区コード`, `f_整理番号`, `s_行政区コード`, `s_整理番号`";
            if (!"".Equals(textBox1.Text))
            {
                readQuery += " and (`f_整理番号` like '%" + textBox1.Text + "%' or `s_整理番号` like '%" + textBox1.Text + "%')";
            }
            readQuery += orderby;
            MySqlCommand readCmd = new MySqlCommand(readQuery, readerCon);
            readCmd.Parameters.AddWithValue("f_lot", EditParam.lotNo);
            readCmd.Parameters.AddWithValue("s_lot", EditParam.lotNo);
            MySqlDataReader reader = readCmd.ExecuteReader();

            while (reader.Read())
            {
                // 各種値のチェック
                bool IsMatch = true;
                // 各項目の一致をチェックする
                // 整理番号
                if (!this.isDBNull(reader, "f_整理番号").Equals(this.isDBNull(reader, "s_整理番号")))
                {
                    IsMatch = false;
                }
                // 世帯番号
                if (!this.isDBNull(reader, "f_世帯番号").Equals(this.isDBNull(reader, "s_世帯番号")))
                {
                    IsMatch = false;
                }
                // 健康診査
                if (!this.isDBNull(reader, "f_健康診査").Equals(this.isDBNull(reader, "s_健康診査")))
                {
                    IsMatch = false;
                }
                // 結核肺がん
                if (!this.isDBNull(reader, "f_結核肺がん").Equals(this.isDBNull(reader, "s_結核肺がん")))
                {
                    IsMatch = false;
                }
                // 胃がん
                if (!this.isDBNull(reader, "f_胃がん").Equals(this.isDBNull(reader, "s_胃がん")))
                {
                    IsMatch = false;
                }
                // 大腸がん
                if (!this.isDBNull(reader, "f_大腸がん").Equals(this.isDBNull(reader, "s_大腸がん")))
                {
                    IsMatch = false;
                }
                // 前立腺がん
                if (!this.isDBNull(reader, "f_前立腺がん").Equals(this.isDBNull(reader, "s_前立腺がん")))
                {
                    IsMatch = false;
                }
                // 骨粗鬆症
                if (!this.isDBNull(reader, "f_骨粗鬆症").Equals(this.isDBNull(reader, "s_骨粗鬆症")))
                {
                    IsMatch = false;
                }
                // 成人歯科
                if (!this.isDBNull(reader, "f_成人歯科").Equals(this.isDBNull(reader, "s_成人歯科")))
                {
                     IsMatch = false;
                }
                // 子宮頸がん
                if (!this.isDBNull(reader, "f_子宮頸がん").Equals(this.isDBNull(reader, "s_子宮頸がん")))
                {
                    IsMatch = false;
                }
                // 乳がん
                if (!this.isDBNull(reader, "f_乳がん").Equals(this.isDBNull(reader, "s_乳がん")))
                {
                    IsMatch = false;
                }
                // 希望場所
                if (!this.isDBNull(reader, "f_希望場所").Equals(this.isDBNull(reader, "s_希望場所")))
                {
                    IsMatch = false;
                }
                // 身体不自由
                if (!this.isDBNull(reader, "f_身体不自由").Equals(this.isDBNull(reader, "s_身体不自由")))
                {
                    IsMatch = false;
                }
                // 電話番号
                if (!this.isDBNull(reader, "f_電話番号").Equals(this.isDBNull(reader, "s_電話番号")))
                {
                    IsMatch = false;
                }
                // 携帯番号
                if (!this.isDBNull(reader, "f_携帯番号").Equals(this.isDBNull(reader, "s_携帯番号")))
                {
                    IsMatch = false;
                }

                // マッチはフラグを更新する
                if (IsMatch)
                {
                    updateFirst.Parameters.Clear();
                    updateFirst.Parameters.AddWithValue("lot", EditParam.lotNo);
                    updateFirst.Parameters.AddWithValue("serialCode", this.isDBNull(reader, "f_整理番号"));
                    updateFirst.ExecuteNonQuery();

                    updateSecond.Parameters.Clear();
                    updateSecond.Parameters.AddWithValue("lot", EditParam.lotNo);
                    updateSecond.Parameters.AddWithValue("serialCode", this.isDBNull(reader, "f_整理番号"));
                    updateSecond.ExecuteNonQuery();
                }
            }

            updateCon.Close();
            readerCon.Close();
        }

        //検索
        private void button1_Click(object sender, EventArgs e)
        {
            this.setPage();
            this.check();
            this.search();
        }

        //チェック
        private void button2_Click(object sender, EventArgs e)
        {
            this.check();
            this.search();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.setPage();
            this.search();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            this.setPage();
            this.search();
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            this.setPage();
            this.search();
        }

        //前へ
        private void button3_Click(object sender, EventArgs e)
        {
            if (currentPageNo <= 1)
            {
                return;
            }
            currentPageNo--;
            comboBox1.SelectedIndex = currentPageNo - 1;

            this.search();
        }

        //次へ
        private void button4_Click(object sender, EventArgs e)
        {
            if (currentPageNo >= maxPageNo)
            {
                return;
            }
            currentPageNo++;
            comboBox1.SelectedIndex = currentPageNo - 1;

            this.search();
        }

        //ページジャンプ
        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            currentPageNo = int.Parse(comboBox1.SelectedItem.ToString());
            this.search();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            if (dgv.Columns[e.ColumnIndex].Name == "編集1")
            {
                // 整理番号を取得
                string serialNo = dgv.Rows[e.RowIndex].Cells["f_整理番号"].Value.ToString();
                Global.lotNo = EditParam.lotNo;
                Global.times = "1回目";
                Global.inputUser = EditParam.inputUser;
                if (serialNo != "")
                {
                    Form form2 = new Form2(serialNo);
                    form2.ShowDialog();
                    form2.Dispose();
                }
            }
            if (dgv.Columns[e.ColumnIndex].Name == "編集2")
            {
                // 整理番号を取得
                string serialNo = dgv.Rows[e.RowIndex].Cells["f_整理番号"].Value.ToString();
                Global.lotNo = EditParam.lotNo;
                Global.times = "2回目";
                Global.inputUser = EditParam.inputUser;
                if (serialNo != "")
                {
                    Form form2 = new Form2(serialNo);
                    form2.ShowDialog();
                    form2.Dispose();
                }
            }

        }

    }
}