Form2.cs 66.9 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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
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 Form2 : Form
    {
        private List<string> history = new List<string>();
        private const int historyMax = 10;
        private int historyPos = 0;

        public Form2(string serialNo = null)
        {
            InitializeComponent();

            if (serialNo != null)
            {
                initDsp(serialNo);
            }

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

            label3.Text = "ロット:" + Global.lotNo;
            label21.Text = "行政区番号:" + Global.adminCode + " " + Global.adminName;

            label20.Text = ""; //エラーメッセージなし

            if("1回目".Equals(Global.times))
            {
                button6.Enabled = false;
            }
            dspCount();
        }

        //修正から遷移
        private void initDsp(string serialNo)
        {
            MySqlConnection conn;
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            conn = new MySqlConnection(connstr);
            conn.Open();
            string code = serialNo.PadLeft(10, '0');

            textBox1.Text = code;
            MySqlCommand cmd = new MySqlCommand("select * from `" + Global.times + "` where `ロット番号` = @lotNo and `整理番号` = @serialNo", conn);
            // パラメータ設定
            cmd.Parameters.Add(new MySqlParameter("lotNo", Global.lotNo));
            cmd.Parameters.Add(new MySqlParameter("serialNo", serialNo));

            try
            {
                MySqlDataReader reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    //無かった
                    reader.Close();
                }
                else
                {
                    string[] row = new string[reader.FieldCount];
                    while (reader.Read())
                    {
                        textBox1.Text = isDBNull(reader, "整理番号");
                        textBox2.Text = isDBNull(reader, "世帯番号");
                        textBox4.Text = isDBNull(reader, "電話番号");
                        textBox5.Text = isDBNull(reader, "携帯番号");
                        textBox6.Text = isDBNull(reader, "健康診査");
                        textBox7.Text = isDBNull(reader, "結核肺がん");
                        textBox8.Text = isDBNull(reader, "胃がん");
                        textBox9.Text = isDBNull(reader, "大腸がん");
                        textBox10.Text = isDBNull(reader, "前立腺がん");
                        textBox11.Text = isDBNull(reader, "骨粗鬆症");
                        textBox12.Text = isDBNull(reader, "成人歯科");

                        string cervicalCancer = isDBNull(reader, "子宮頸がん");
                        specialInputTwo(cervicalCancer, textBox13, textBox31);

                        string breastCancer = isDBNull(reader, "乳がん");
                        specialInputTwo(breastCancer, textBox14, textBox32);

                        string place = isDBNull(reader, "希望場所");
                        specialInputThree(place, textBox15, textBox33, textBox34);

                        textBox16.Text = "".Equals(isDBNull(reader, "身体不自由")) ? "" : "0";

                        Global.adminCode = isDBNull(reader, "行政区コード");
                        Global.adminName = isDBNull(reader, "行政区名称");
                        Global.healthArea = isDBNull(reader, "保健区");
                    }
                    reader.Close();
                }
            }
            catch (SqlException )
            {
            }

            conn.Close();
        }



        private void dspCount()
        {
            MySqlConnection conn;
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            conn = new MySqlConnection(connstr);
            conn.Open();

            int cnt = getCount(Global.times, Global.lotNo, Global.adminCode, conn);

            conn.Close();

            label19.Text = "入力済み  " + cnt + "件";
        }

        //保存
        private void button1_Click(object sender, EventArgs e)
        {
            bool match = false;
            //入力チェック
            if("".Equals(textBox1.Text)) //必須は整理番号のみ
            {
                MessageBox.Show("整理番号が入力されていません");
                return;
            }

            SaveData sv = makeSaveData(); //保存用データ生成(パディング済み)

            //マッチングチェック
            bool matchErr = matchInput(sv, Global.times);
            //一致しない場合、エラー表示はするが保存は実行してしまう
            if(matchErr)
            {
                //保存へ進む
            }
            else
            {
                //エラーなしは完了フラグを立てる
                match = true;
            }

            //保存処理
            if(match)
            {
                sv.done = "1";  //マッチングOKの場合、完了フラグを立てる
            }
            bool res = doSave(sv);

            historyAdd(sv.serialNo);  //履歴に積む

            dspCount();
            
        }

        private SaveData makeSaveData()
        {
            SaveData sv = new SaveData();

            sv.lotNo = int.Parse(Global.lotNo);            //ロット番号
            sv.adminCode = Global.adminCode;        //行政区コード
            sv.adminName = Global.adminName;        //行政区名称
            sv.healthArea = Global.healthArea;       //保健区
            sv.serialNo = textBox1.Text.PadLeft(10,'0');         //整理番号
            if ("".Equals(textBox2.Text))
            {
                sv.houseNo = "";
            }
            else
            {
                sv.houseNo = textBox2.Text.PadLeft(10, '0');          //世帯番号
            }
            sv.healthCheck = textBox6.Text;      //健康診査
            sv.lungCancer = textBox7.Text;       //結核肺がん
            sv.stomachCancer = textBox8.Text;    //胃がん
            sv.colorectalCancer = textBox9.Text; //大腸がん
            sv.prostateCancer = textBox10.Text;   //前立腺がん
            sv.osteoporosis = textBox11.Text;     //骨粗鬆症
            sv.dental = textBox12.Text;           //成人歯科
            sv.cervicalCancer = specialReadTwo(textBox13,textBox31);   //子宮頸がん
            sv.breastCancer = specialReadTwo(textBox14, textBox32);    //乳がん
            sv.place = specialReadThree(textBox15, textBox33,textBox34);            //希望場所
            sv.handicapped = "".Equals(textBox16.Text) ? "" : "8";      //身体不自由
            sv.phone = textBox4.Text;            //電話番号
            sv.mobilePhone = textBox5.Text;      //携帯番号
            sv.inputDate = null;        //入力日
            sv.inputUser = null;        //入力者
            sv.updateDate = null;       //修正日
            sv.updateUser = null;       //修正者
            sv.done = "";             //完了

            return sv;
        }

        private bool doSave(SaveData sv)
        {
            bool err = false;

            MySqlConnection conn;
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            conn = new MySqlConnection(connstr);
            conn.Open();

            bool isExist = isExistData(sv, Global.times, conn);
            if (isExist)
            {
                sv.updateUser = Global.inputUser;
                err = updateData(sv, Global.times, conn);
            }
            else
            {
                sv.inputUser = Global.inputUser;
                err = insertData(sv, Global.times, conn);
            }

            SaveData sv1 = new SaveData();
            sv1.lotNo = int.Parse(Global.lotNo);
            sv1.serialNo = sv.serialNo;
            sv1.done = sv.done;
            string tableName = "1回目".Equals(Global.times) ? "2回目" : "1回目";
            updateDoneFlag(sv1, tableName, conn);//別の回の完了フラグも立てる

            conn.Close();
            return err;
        }

        private int getCount(string tableName, string lotNo, string adminCode, MySqlConnection conn)
        {
            int cnt = 0;
            MySqlCommand cmd = new MySqlCommand("select count(*) from `" + tableName + "` where `ロット番号` = @lotNo and `行政区コード` = @adminCode", conn);
            // パラメータ設定
            cmd.Parameters.Add(new MySqlParameter("lotNo", lotNo));
            cmd.Parameters.Add(new MySqlParameter("adminCode", adminCode));

            try
            {
                // 実行
                MySqlDataReader reader = cmd.ExecuteReader();
                reader.Read();
                cnt = reader.GetInt32(0);
                reader.Close();
            }
            catch (SqlException )
            {
            }

            return cnt;
        }

        private bool selectData(string tableName, string lotNo, string serialNo, MySqlConnection conn)
        {
            bool err = false;
            MySqlCommand cmd = new MySqlCommand("select * from `" + tableName + "` where `ロット番号` = @lotNo and `整理番号` = @serialNo", conn);
            // パラメータ設定
            cmd.Parameters.Add(new MySqlParameter("lotNo", lotNo));
            cmd.Parameters.Add(new MySqlParameter("serialNo", serialNo));

            try
            {
                MySqlDataReader reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    //無かった
                    reader.Close();
                }
                else
                {
                    string[] row = new string[reader.FieldCount];
                    while (reader.Read())
                    {
                        textBox1.Text = isDBNull(reader,"整理番号");
                        textBox2.Text = isDBNull(reader, "世帯番号");
                        textBox4.Text = isDBNull(reader, "電話番号");
                        textBox5.Text = isDBNull(reader, "携帯番号");
                        textBox6.Text = isDBNull(reader, "健康診査");
                        textBox7.Text = isDBNull(reader, "結核肺がん");
                        textBox8.Text = isDBNull(reader, "胃がん");
                        textBox9.Text = isDBNull(reader, "大腸がん");
                        textBox10.Text = isDBNull(reader, "前立腺がん");
                        textBox11.Text = isDBNull(reader, "骨粗鬆症");
                        textBox12.Text = isDBNull(reader, "成人歯科");

                        string cervicalCancer = isDBNull(reader, "子宮頸がん");
                        specialInputTwo(cervicalCancer, textBox13, textBox31);

                        string breastCancer = isDBNull(reader, "乳がん");
                        specialInputTwo(breastCancer, textBox14, textBox32);

                        string place = isDBNull(reader, "希望場所");
                        specialInputThree(place, textBox15, textBox33, textBox34);

                        textBox16.Text = "".Equals(isDBNull(reader, "身体不自由")) ? "" : "0";
                    }
                    reader.Close();
                }
            }
            catch (SqlException )
            {
            }

            return err;
        }

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


        private bool isExistData(SaveData sv, string tableName, MySqlConnection conn)
        {
            bool isExist = false;
            MySqlCommand cmd = new MySqlCommand("select exists (select 1 from `" + tableName + "` where `整理番号` = @serialNo)", conn);
            // パラメータ設定
            cmd.Parameters.Add(new MySqlParameter("serialNo", sv.serialNo));

            try
            {
                // 実行
                MySqlDataReader reader = cmd.ExecuteReader();
                reader.Read();
                isExist = reader.GetBoolean(0);
                reader.Close();
            }
            catch (SqlException )
            {
            }

            return isExist;
        }

        private bool matchInput(SaveData sv, string times)
        {
            MySqlConnection conn;
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            conn = new MySqlConnection(connstr);
            conn.Open();

            string tableName = "1回目".Equals(times) ? "2回目" : "1回目";
            MySqlCommand cmd = new MySqlCommand("select * from `" + tableName + "` where `ロット番号` = @lotNo and `整理番号` = @serialNo", conn);
            // パラメータ設定
            cmd.Parameters.Add(new MySqlParameter("lotNo", sv.lotNo));
            cmd.Parameters.Add(new MySqlParameter("serialNo", sv.serialNo));

            bool err = false;

            textBox1.BackColor = Color.Empty;
            textBox2.BackColor = Color.Empty;
            textBox4.BackColor = Color.Empty;
            textBox5.BackColor = Color.Empty;
            textBox6.BackColor = Color.Empty;
            textBox7.BackColor = Color.Empty;
            textBox8.BackColor = Color.Empty;
            textBox9.BackColor = Color.Empty;
            textBox10.BackColor = Color.Empty;
            textBox11.BackColor = Color.Empty;
            textBox12.BackColor = Color.Empty;
            textBox13.BackColor = Color.Empty;
            textBox31.BackColor = Color.Empty;
            textBox14.BackColor = Color.Empty;
            textBox32.BackColor = Color.Empty;
            textBox15.BackColor = Color.Empty;
            textBox33.BackColor = Color.Empty;
            textBox34.BackColor = Color.Empty;
            textBox16.BackColor = Color.Empty;

            try
            {
                MySqlDataReader reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    //無かった
                    if("2回目".Equals(times))
                    {
                        textBox1.BackColor = Color.Pink;
                    }
                    err = true;
                    reader.Close();
                }
                else
                {
                    string[] row = new string[reader.FieldCount];
                    while (reader.Read())
                    {
                        if (!sv.houseNo.Equals(isDBNull(reader, "世帯番号")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox2.BackColor = Color.Pink;
                            }
                            err = true; 
                        }
                        if (!sv.phone.Equals(isDBNull(reader, "電話番号")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox4.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.mobilePhone.Equals(isDBNull(reader, "携帯番号")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox5.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.healthCheck.Equals(isDBNull(reader, "健康診査")))
                        {
                            if ("2回目".Equals(times))
                            {
                                textBox6.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.lungCancer.Equals(isDBNull(reader, "結核肺がん")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox7.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.stomachCancer.Equals(isDBNull(reader, "胃がん")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox8.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.colorectalCancer.Equals(isDBNull(reader, "大腸がん")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox9.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.prostateCancer.Equals(isDBNull(reader, "前立腺がん")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox10.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.osteoporosis.Equals(isDBNull(reader, "骨粗鬆症")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox11.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.dental.Equals(isDBNull(reader, "成人歯科")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox12.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.cervicalCancer.Equals(isDBNull(reader, "子宮頸がん")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox13.BackColor = Color.Pink;
                                textBox31.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.breastCancer.Equals(isDBNull(reader, "乳がん")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox14.BackColor = Color.Pink;
                                textBox32.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.place.Equals(isDBNull(reader, "希望場所")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox15.BackColor = Color.Pink;
                                textBox33.BackColor = Color.Pink;
                                textBox34.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                        if (!sv.handicapped.Equals(isDBNull(reader, "身体不自由")))
                        {
                            if("2回目".Equals(times))
                            {
                                textBox16.BackColor = Color.Pink;
                            }
                            err = true;
                        }
                    }
                    reader.Close();
                }
            }
            catch (SqlException )
            {
            }

            conn.Close();

            return err;
        }


        private bool insertData(SaveData sv, string tableName, MySqlConnection conn)
        {
            bool err = false;
            // コマンドを作成
            MySqlCommand cmd =
                new MySqlCommand("insert into `" + tableName + "` (`ロット番号`,"
                                                                  + "`行政区コード`,"
                                                                  + "`行政区名称`,"
                                                                  + "`保健区`,"
                                                                  + "`整理番号`,"
                                                                  + "`世帯番号`,"
                                                                  + "`健康診査`,"
                                                                  + "`結核肺がん`,"
                                                                  + "`胃がん`,"
                                                                  + "`大腸がん`,"
                                                                  + "`前立腺がん`,"
                                                                  + "`骨粗鬆症`,"
                                                                  + "`成人歯科`,"
                                                                  + "`子宮頸がん`,"
                                                                  + "`乳がん`,"
                                                                  + "`希望場所`,"
                                                                  + "`身体不自由`,"
                                                                  + "`電話番号`,"
                                                                  + "`携帯番号`,"
                                                                  + "`入力日`,"
                                                                  + "`入力者`,"
                                                                  + "`修正日`,"
                                                                  + "`修正者`,"
                                                                  + "`完了`) "
                                                          + "values (@lotNo,"
                                                                  + "@adminCode, "
                                                                  + "@adminName, "
                                                                  + "@healthArea, "
                                                                  + "@serialNo, "
                                                                  + "@houseNo,"
                                                                  + "@healthCheck,"
                                                                  + "@lungCancer,"
                                                                  + "@stomachCancer,"
                                                                  + "@colorectalCancer,"
                                                                  + "@prostateCancer,"
                                                                  + "@osteoporosis,"
                                                                  + "@dental,"
                                                                  + "@cervicalCancer,"
                                                                  + "@breastCancer,"
                                                                  + "@place,"
                                                                  + "@handicapped,"
                                                                  + "@phone,"
                                                                  + "@mobilePhone,"
                                                                  + "@inputDate,"
                                                                  + "@inputUser,"
                                                                  + "@updateDate,"
                                                                  + "@updateUser,"
                                                                  + "@done)", conn);
            // パラメータ設定
            cmd.Parameters.Add(new MySqlParameter("lotNo", sv.lotNo));
            cmd.Parameters.Add(new MySqlParameter("adminCode", sv.adminCode));
            cmd.Parameters.Add(new MySqlParameter("adminName", sv.adminName));
            cmd.Parameters.Add(new MySqlParameter("healthArea", sv.healthArea));
            cmd.Parameters.Add(new MySqlParameter("serialNo", sv.serialNo));
            cmd.Parameters.Add(new MySqlParameter("houseNo", sv.houseNo));
            cmd.Parameters.Add(new MySqlParameter("healthCheck", sv.healthCheck));
            cmd.Parameters.Add(new MySqlParameter("lungCancer", sv.lungCancer));
            cmd.Parameters.Add(new MySqlParameter("stomachCancer", sv.stomachCancer));
            cmd.Parameters.Add(new MySqlParameter("colorectalCancer", sv.colorectalCancer));
            cmd.Parameters.Add(new MySqlParameter("prostateCancer", sv.prostateCancer));
            cmd.Parameters.Add(new MySqlParameter("osteoporosis", sv.osteoporosis));
            cmd.Parameters.Add(new MySqlParameter("dental", sv.dental));
            cmd.Parameters.Add(new MySqlParameter("cervicalCancer", sv.cervicalCancer));
            cmd.Parameters.Add(new MySqlParameter("breastCancer", sv.breastCancer));
            cmd.Parameters.Add(new MySqlParameter("place", sv.place));
            cmd.Parameters.Add(new MySqlParameter("handicapped", sv.handicapped));
            cmd.Parameters.Add(new MySqlParameter("phone", sv.phone));
            cmd.Parameters.Add(new MySqlParameter("mobilePhone", sv.mobilePhone));
            cmd.Parameters.Add(new MySqlParameter("inputDate", DateTime.Now));
            cmd.Parameters.Add(new MySqlParameter("inputUser", sv.inputUser));
            cmd.Parameters.Add(new MySqlParameter("updateDate", sv.updateDate));
            cmd.Parameters.Add(new MySqlParameter("updateUser", sv.updateUser));
            cmd.Parameters.Add(new MySqlParameter("done", sv.done));

            try
            {
                // 実行
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                // 例外処理
                MessageBox.Show("例外発生:" + ex.Message);
                return true;
            }

            return err;
        }

        private bool updateData(SaveData sv, string tableName, MySqlConnection conn)
        {
            bool err = false;
            string sql = "update `" + tableName + "` set `ロット番号` = @lotNo,"
                                                      + "`行政区コード` = @adminCode,"
                                                      + "`行政区名称` = @adminName,"
                                                      + "`保健区` = @healthArea,"
                                                      + "`整理番号` = @serialNo,"
                                                      + "`世帯番号` = @houseNo,"
                                                      + "`健康診査` = @healthCheck,"
                                                      + "`結核肺がん` = @lungCancer,"
                                                      + "`胃がん` = @stomachCancer,"
                                                      + "`大腸がん` = @colorectalCancer,"
                                                      + "`前立腺がん` = @prostateCancer,"
                                                      + "`骨粗鬆症` = @osteoporosis,"
                                                      + "`成人歯科` = @dental,"
                                                      + "`子宮頸がん` = @cervicalCancer,"
                                                      + "`乳がん` = @breastCancer,"
                                                      + "`希望場所` = @place,"
                                                      + "`身体不自由` = @handicapped,"
                                                      + "`電話番号` = @phone,"
                                                      + "`携帯番号` = @mobilePhone,"
                                                      + "`修正日` = @updateDate,"
                                                      + "`修正者` = @updateUser,"
                                                      + "`完了` = @done "
                                                      + "where `ロット番号` = @lotNo and `整理番号` = @serialNo";

            // コマンドを作成
            MySqlCommand cmd = new MySqlCommand(sql, conn);

            // パラメータ設定
            cmd.Parameters.Add(new MySqlParameter("lotNo", sv.lotNo));
            cmd.Parameters.Add(new MySqlParameter("adminCode", sv.adminCode));
            cmd.Parameters.Add(new MySqlParameter("adminName", sv.adminName));
            cmd.Parameters.Add(new MySqlParameter("healthArea", sv.healthArea));
            cmd.Parameters.Add(new MySqlParameter("serialNo", sv.serialNo));
            cmd.Parameters.Add(new MySqlParameter("houseNo", sv.houseNo));
            cmd.Parameters.Add(new MySqlParameter("healthCheck", sv.healthCheck));
            cmd.Parameters.Add(new MySqlParameter("lungCancer", sv.lungCancer));
            cmd.Parameters.Add(new MySqlParameter("stomachCancer", sv.stomachCancer));
            cmd.Parameters.Add(new MySqlParameter("colorectalCancer", sv.colorectalCancer));
            cmd.Parameters.Add(new MySqlParameter("prostateCancer", sv.prostateCancer));
            cmd.Parameters.Add(new MySqlParameter("osteoporosis", sv.osteoporosis));
            cmd.Parameters.Add(new MySqlParameter("dental", sv.dental));
            cmd.Parameters.Add(new MySqlParameter("cervicalCancer", sv.cervicalCancer));
            cmd.Parameters.Add(new MySqlParameter("breastCancer", sv.breastCancer));
            cmd.Parameters.Add(new MySqlParameter("place", sv.place));
            cmd.Parameters.Add(new MySqlParameter("handicapped", sv.handicapped));
            cmd.Parameters.Add(new MySqlParameter("phone", sv.phone));
            cmd.Parameters.Add(new MySqlParameter("mobilePhone", sv.mobilePhone));
            cmd.Parameters.Add(new MySqlParameter("updateDate", DateTime.Now));
            cmd.Parameters.Add(new MySqlParameter("updateUser", sv.updateUser));
            cmd.Parameters.Add(new MySqlParameter("done", sv.done));

            try
            {
                // 実行
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                // 例外処理
                MessageBox.Show("例外発生:" + ex.Message);
                return true;
            }

            return err;

        }

        private bool updateDoneFlag(SaveData sv, string tableName, MySqlConnection conn)
        {
            bool err = false;

            string sql = "update `" + tableName + "` set `完了` = @done "
                                          + "where `ロット番号` = @lotNo and `整理番号` = @serialNo";

            // コマンドを作成
            MySqlCommand cmd = new MySqlCommand(sql, conn);

            // パラメータ設定
            cmd.Parameters.Add(new MySqlParameter("lotNo", sv.lotNo));
            cmd.Parameters.Add(new MySqlParameter("serialNo", sv.serialNo));
            cmd.Parameters.Add(new MySqlParameter("done", sv.done));

            try
            {
                // 実行
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                // 例外処理
                MessageBox.Show("例外発生:" + ex.Message);
                return true;
            }

            return err;
        }

        private bool deleteData(SaveData sv, string tableName, MySqlConnection conn)
        {
            bool err = false;

            string sql = "delete from `" + tableName + "` where `ロット番号` = @lotNo and `整理番号` = @serialNo";

            // コマンドを作成
            MySqlCommand cmd = new MySqlCommand(sql, conn);

            // パラメータ設定
            cmd.Parameters.Add(new MySqlParameter("lotNo", sv.lotNo));
            cmd.Parameters.Add(new MySqlParameter("serialNo", sv.serialNo));

            try
            {
                // 実行
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                // 例外処理
                MessageBox.Show("例外発生:" + ex.Message);
                return true;
            }

            return err;
        }

        private void sendTabKey(TextBox textbox, int length)
        {
            if (textbox.Text.Length == length)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void clearForm()
        {
            textBox1.BackColor = Color.Empty;
            textBox2.BackColor = Color.Empty;
            textBox4.BackColor = Color.Empty;
            textBox5.BackColor = Color.Empty;
            textBox6.BackColor = Color.Empty;
            textBox7.BackColor = Color.Empty;
            textBox8.BackColor = Color.Empty;
            textBox9.BackColor = Color.Empty;
            textBox10.BackColor = Color.Empty;
            textBox11.BackColor = Color.Empty;
            textBox12.BackColor = Color.Empty;
            textBox13.BackColor = Color.Empty;
            textBox31.BackColor = Color.Empty;
            textBox14.BackColor = Color.Empty;
            textBox32.BackColor = Color.Empty;
            textBox15.BackColor = Color.Empty;
            textBox33.BackColor = Color.Empty;
            textBox34.BackColor = Color.Empty;
            textBox16.BackColor = Color.Empty;

            textBox1.Text = "";
            textBox2.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            textBox12.Text = "";
            textBox13.Text = "";
            textBox31.Text = "";
            textBox14.Text = "";
            textBox32.Text = "";
            textBox15.Text = "";
            textBox33.Text = "";
            textBox34.Text = "";
            textBox16.Text = "";

            label20.Text = "";

            textBox1.Focus();

            panel1.Visible = false;
            button6.Text = "1回目の表示";
        }

        private void textBox1_TextChanged(object sender, EventArgs e) //整理番号
        {
            sendTabKey(textBox1, 10);
        }

        private void textBox2_TextChanged(object sender, EventArgs e) //世帯番号
        {
            sendTabKey(textBox2, 10);
        }

        private void textBox4_TextChanged(object sender, EventArgs e) //電話番号
        {
            sendTabKey(textBox4, 15);
        }

        private void textBox5_TextChanged(object sender, EventArgs e) //携帯番号
        {
            sendTabKey(textBox5, 15);
        }

        private void textBox6_TextChanged(object sender, EventArgs e) //健康診査
        {
            dspModify(textBox6, label7);
            sendTabKey(textBox6, 1);
        }

        private void textBox7_TextChanged(object sender, EventArgs e) //結核肺がん
        {
            dspModify(textBox7, label8);
            sendTabKey(textBox7, 1);
        }

        private void textBox8_TextChanged(object sender, EventArgs e) //胃がん
        {
            dspModify(textBox8, label9);
            sendTabKey(textBox8, 1);
        }

        private void textBox9_TextChanged(object sender, EventArgs e) //大腸がん
        {
            dspModify(textBox9, label10);
            sendTabKey(textBox9, 1);
        }

        private void textBox10_TextChanged(object sender, EventArgs e) //前立腺がん
        {
            dspModify(textBox10, label11);
            sendTabKey(textBox10, 1);
        }

        private void textBox11_TextChanged(object sender, EventArgs e) //骨粗鬆症
        {
            dspModify(textBox11, label12);
            sendTabKey(textBox11, 1);
        }

        private void textBox12_TextChanged(object sender, EventArgs e) //成人歯科
        {
            dspModify(textBox12, label13);
            sendTabKey(textBox12, 1);
        }

        private void textBox13_TextChanged(object sender, EventArgs e) //子宮頸がん
        {
            dspModify(textBox13, label14);
            sendTabKey(textBox13, 1);
        }

        private void textBox31_TextChanged(object sender, EventArgs e) //子宮頸がん(指定医療機関)
        {
            dspModify(textBox31, label22);
            sendTabKey(textBox31, 1);
        }

        private void textBox14_TextChanged(object sender, EventArgs e) //乳がん
        {
            dspModify(textBox14, label15);
            sendTabKey(textBox14, 1);
        }

        private void textBox32_TextChanged(object sender, EventArgs e) //石巻市医師会
        {
            dspModify(textBox32, label23);
            sendTabKey(textBox32, 1);

        }

        private void textBox15_TextChanged(object sender, EventArgs e) //希望場所
        {
            sendTabKey(textBox15, 1);
        }

        private void textBox33_TextChanged(object sender, EventArgs e) //希望場所(本庁個別)
        {
            sendTabKey(textBox15, 1);
        }

        private void textBox34_TextChanged(object sender, EventArgs e)//希望場所(石巻ロイヤル)
        {
            sendTabKey(textBox15, 1);
        }


        private void textBox16_TextChanged(object sender, EventArgs e) //身体不自由
        {
            sendTabKey(textBox16, 1);
        }

        //保存して次へ
        private void button2_Click(object sender, EventArgs e)
        {
            bool match = false;
            //入力チェック
            if ("".Equals(textBox1.Text)) //必須は整理番号のみ
            {
                MessageBox.Show("整理番号が入力されていません");
                return;
            }

            SaveData sv = makeSaveData(); //保存用データ作成(パディング済み)

            bool matchErr = matchInput(sv, Global.times);
            if (matchErr)
            {
                if ("2回目".Equals(Global.times))
                {
                    //一致しない場合、エラーダイアログ表示
                    DialogResult result = MessageBox.Show("入力内容が1回目と異なりますが保存しますか?",
                                                          "確認",
                                                          MessageBoxButtons.OKCancel,
                                                          MessageBoxIcon.Exclamation,
                                                          MessageBoxDefaultButton.Button2);
                    //何が選択されたか調べる
                    if (result == DialogResult.OK)
                    {
                        //「OK」が選択された時
                    }
                    else if (result == DialogResult.Cancel)
                    {
                        //「キャンセル」が選択された時
                        return;
                    }
                }
            }
            else
            {
                //エラーなしは完了フラグを立てる
                match = true;
            }

            //保存処理
            if (match)
            {
                sv.done = "1";  //マッチングOKの場合、完了フラグを立てる
            }
            bool res = doSave(sv);

            historyAdd(sv.serialNo);  //履歴に積む

            clearForm();
            dspCount();
        }

        //復元
        private void button3_Click(object sender, EventArgs e)
        {
            if("".Equals(textBox1.Text))
            {
                return;
            }


            MySqlConnection conn;
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            conn = new MySqlConnection(connstr);
            conn.Open();
            string code = textBox1.Text.PadLeft(10, '0');

            clearForm(); //いったんクリア
            textBox1.Text = code; //整理番号だけ戻す
            selectData(Global.times, Global.lotNo, code, conn);

            conn.Close();
        }

        private void historyAdd(string serialNo)
        {
            if(history.Count > 0)  //要素がある
            {
                if (serialNo.Equals(history[historyPos]))  //最後と同じ要素はなにもしない
                {
                    return;
                }
            }

            if((history.Count - 1) > historyPos)  //現在地より多く入っているものは消す
            {
                int cnt = history.Count -1; //削除前最大位置
                for (int i = cnt; i > historyPos; i--)
                {
                    history.RemoveAt(i);
                }
            }
            if (history.Count >= historyMax)
            {
                //最大数入っている
                history.RemoveAt(0);//古いものを削除
            }
            history.Add(serialNo);
            historyPos = history.Count - 1;
        }

        private string historyGet()
        {
            string ret = "";

            if (history.Count > 0)
            {
                if (historyPos >= 0)
                {
                    ret = history[historyPos];
                    historyPos--;
                    if(historyPos < 0)
                    {
                        historyPos = 0;
                    }
                }
            }
            return ret;
        }

        //前の入力へ
        private void button4_Click(object sender, EventArgs e)
        {
            string serialNo = historyGet();
            if("".Equals(serialNo))
            {
                return;
            }

            MySqlConnection conn;
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            conn = new MySqlConnection(connstr);
            conn.Open();

            clearForm(); //いったんクリア
            textBox1.Text = serialNo; //整理番号だけ戻す
            selectData(Global.times, Global.lotNo, serialNo, conn);

            conn.Close();
        }

        //整理番号のチェック
        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            if ("2回目".Equals(Global.times))
            {
                if ("".Equals(textBox1.Text))
                {
                    label20.Text = "";
                    label20.ForeColor = Color.Empty;
                    return;
                }

                bool isExist = false;

                MySqlConnection conn;
                string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
                conn = new MySqlConnection(connstr);
                conn.Open();
                SaveData sv = new SaveData();

                sv.serialNo = textBox1.Text.PadLeft(10, '0');
                isExist = isExistData(sv, "1回目", conn);

                conn.Close();

                if (isExist)
                {
                    label20.Text = "";
                    label20.ForeColor = Color.Empty;
                }
                else
                {
                    label20.Text = "1回目に存在しない整理番号です";
                    label20.ForeColor = Color.Red;
                }
            }
        }

        //入力可能(数字とハイフンとBS)
        private void inputAllNumber(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b') && (e.KeyChar != '-'))
            {
                e.Handled = true;
            }
        }

        //入力可能(0~5とBS)
        private void inputNumber(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0' || e.KeyChar > '5') && (e.KeyChar != '\b'))
            {
                e.Handled = true;
            }
        }

        //入力可能(0とBS)
        private void inputZero(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar != '0') && (e.KeyChar != '\b'))
            {
                e.Handled = true;
            }
        }

        private void dspModify(TextBox tb, Label lb)
        {
            if ("".Equals(tb.Text))
            {
                lb.ForeColor = Color.Black;
                lb.Font = new Font(lb.Font, FontStyle.Regular);
            }
            else
            {
                if ("0".Equals(tb.Text))
                {
                    lb.ForeColor = Color.Red;
                    lb.Font = new Font(lb.Font, FontStyle.Bold);
                }
                else
                {
                    lb.ForeColor = Color.Blue;
                    lb.Font = new Font(lb.Font, FontStyle.Regular);
                }
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputAllNumber(sender, e);
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputAllNumber(sender, e);
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputAllNumber(sender, e);
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputAllNumber(sender, e);
        }

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputNumber(sender, e);
        }

        private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputNumber(sender, e);
        }

        private void textBox8_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputNumber(sender, e);
        }

        private void textBox9_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputNumber(sender, e);
        }

        private void textBox10_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputNumber(sender, e);
        }

        private void textBox11_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputNumber(sender, e);
        }

        private void textBox12_KeyPress(object sender, KeyPressEventArgs e)
        {
            inputNumber(sender, e);
        }

        private void textBox13_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ("".Equals(textBox31.Text)) //どちらかにしか入れさせない
            {
                inputNumber(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox31_KeyPress(object sender, KeyPressEventArgs e) //子宮頸がん(指定医療機関)
        {
            if ("".Equals(textBox13.Text)) //どちらかにしか入れさせない
            {
                inputNumber(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox14_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ("".Equals(textBox32.Text)) //どちらかにしか入れさせない
            {
                inputNumber(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox32_KeyPress(object sender, KeyPressEventArgs e)//乳がん(石巻医師会)
        {
            if ("".Equals(textBox14.Text)) //どちらかにしか入れさせない
            {
                inputNumber(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }


        private void textBox15_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ("".Equals(textBox33.Text) && "".Equals(textBox34.Text)) //どちらかにしか入れさせない
            {
                inputZero(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox33_KeyPress(object sender, KeyPressEventArgs e)//本庁個別
        {
            if ("".Equals(textBox15.Text) && "".Equals(textBox34.Text)) //どちらかにしか入れさせない
            {
                inputZero(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox34_KeyPress(object sender, KeyPressEventArgs e)//石巻ロイヤル
        {
            if ("".Equals(textBox15.Text) && "".Equals(textBox33.Text)) //どちらかにしか入れさせない
            {
                inputZero(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox16_KeyPress(object sender, KeyPressEventArgs e)//身体不自由
        {
            inputZero(sender, e);
        }


        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
                e.Handled = true;
            }

            if(e.KeyCode == Keys.Escape)
            {
                button2.Focus();
                button2.PerformClick();
                e.Handled = true;
            }

        }

        //削除
        private void button5_Click(object sender, EventArgs e)
        {
            if("".Equals(textBox1.Text))
            {
                return;
            }

            MySqlConnection conn;
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            conn = new MySqlConnection(connstr);
            conn.Open();

            SaveData sv1 = new SaveData();
            sv1.lotNo = int.Parse(Global.lotNo);
            sv1.serialNo = textBox1.Text.PadLeft(10,'0');
            string tableName = Global.times;
            deleteData(sv1, tableName, conn);//削除

            sv1.done = "";
            tableName = "1回目".Equals(Global.times) ? "2回目" : "1回目";
            updateDoneFlag(sv1, tableName, conn);//別の回の完了フラグはおろす

            conn.Close();

            clearForm();
            dspCount();
        }

        private void specialInputTwo(string inputChar, TextBox t1, TextBox t2) //2項目で特殊入力
        {
            if ("".Equals(inputChar))
            {
                t1.Text = "";
                t2.Text = "";
            }
            else if ("9".Equals(inputChar))
            {
                t1.Text = "0";
                t2.Text = "";
            }
            else if ("0".Equals(inputChar))
            {
                t1.Text = "";
                t2.Text = "0";
            }
            else
            {
                t1.Text = inputChar;
                t2.Text = "";
            }

        }

        private void specialInputThree(string inputChar, TextBox t1, TextBox t2, TextBox t3) //3項目で特殊入力
        {
            if ("".Equals(inputChar))
            {
                t1.Text = "";
                t2.Text = "";
                t3.Text = "";
            }
            else if ("1".Equals(inputChar))
            {
                t1.Text = "0";
                t2.Text = "";
                t3.Text = "";
            }
            if ("2".Equals(inputChar))
            {
                t1.Text = "";
                t2.Text = "0";
                t3.Text = "";
            }
            if ("3".Equals(inputChar))
            {
                t1.Text = "";
                t2.Text = "";
                t3.Text = "0";
            }
        }

        private string specialReadTwo(TextBox t1, TextBox t2) //2項目で特殊読み取り
        {
            string ret = "";
            if ("".Equals(t1.Text) && "".Equals(t2.Text))
            {
                ret = "";
            }
            else
            {
                if("0".Equals(t1.Text))
                {
                    ret = "9";
                }
                else if("0".Equals(t2.Text))
                {
                    ret = "0";
                }
                else
                {
                    ret = "".Equals(t2) ? t1.Text : t2.Text;   //子宮頸がん
                }
            }

            return ret;
        }

        private string specialReadThree(TextBox t1, TextBox t2, TextBox t3) //3項目で特殊読み取り
        {
            string ret = "";

            if ("".Equals(t1.Text) && "".Equals(t2.Text) && "".Equals(t3.Text))
            {
                ret = "";
            }
            else
            {
                if ("0".Equals(t1.Text))
                {
                    ret = "1";
                }
                else if ("0".Equals(t2.Text))
                {
                    ret = "2"; 
                }
                else if ("0".Equals(t3.Text))
                {
                    ret = "3";
                }
            }
            return ret;
        }

        /***************************************************************/
        /***************2回目時入力時の1回目修正用********************/
        /***************************************************************/

        private bool selectDataOnce(string tableName, string lotNo, string serialNo, MySqlConnection conn)
        {
            bool err = false;
            MySqlCommand cmd = new MySqlCommand("select * from `" + tableName + "` where `ロット番号` = @lotNo and `整理番号` = @serialNo", conn);
            // パラメータ設定
            cmd.Parameters.Add(new MySqlParameter("lotNo", lotNo));
            cmd.Parameters.Add(new MySqlParameter("serialNo", serialNo));

            try
            {
                MySqlDataReader reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    //無かった
                    reader.Close();

                    textBox3.Text = "";
                    textBox17.Text = "";
                    textBox18.Text = "";
                    textBox19.Text = "";
                    textBox20.Text = "";
                    textBox21.Text = "";
                    textBox22.Text = "";
                    textBox23.Text = "";
                    textBox24.Text = "";
                    textBox25.Text = "";
                    textBox26.Text = "";
                    textBox27.Text = "";
                    textBox35.Text = "";
                    textBox28.Text = "";
                    textBox36.Text = "";
                    textBox29.Text = "";
                    textBox37.Text = "";
                    textBox38.Text = "";
                    textBox30.Text = "";
                }
                else
                {
                    string[] row = new string[reader.FieldCount];
                    while (reader.Read())
                    {
                        textBox3.Text = isDBNull(reader, "整理番号");
                        textBox17.Text = isDBNull(reader, "世帯番号");
                        textBox18.Text = isDBNull(reader, "電話番号");
                        textBox19.Text = isDBNull(reader, "携帯番号");
                        textBox20.Text = isDBNull(reader, "健康診査");
                        textBox21.Text = isDBNull(reader, "結核肺がん");
                        textBox22.Text = isDBNull(reader, "胃がん");
                        textBox23.Text = isDBNull(reader, "大腸がん");
                        textBox24.Text = isDBNull(reader, "前立腺がん");
                        textBox25.Text = isDBNull(reader, "骨粗鬆症");
                        textBox26.Text = isDBNull(reader, "成人歯科");

                        string cervicalCancer = isDBNull(reader, "子宮頸がん");
                        specialInputTwo(cervicalCancer, textBox27, textBox35);

                        string breastCancer = isDBNull(reader, "乳がん");
                        specialInputTwo(breastCancer, textBox28, textBox36);

                        string place = isDBNull(reader, "希望場所");
                        specialInputThree(place, textBox29, textBox37, textBox38);

                        textBox30.Text = "".Equals(isDBNull(reader, "身体不自由")) ? "" : "0";
                    }
                    reader.Close();
                }
            }
            catch (SqlException )
            {
            }

            return err;
        }

        //1回目の修正
        private void updateDataOnce(MySqlConnection conn)
        {
            bool match = false;
            //入力チェック
            if ("".Equals(textBox3.Text)) //必須は整理番号のみ
            {
                MessageBox.Show("整理番号が入力されていません");
                return;
            }

            SaveData sv = makeSaveDataOnce(); //保存用データ生成(パディング済み)

            //マッチングチェック
            bool matchErr = matchInput(sv, "1回目");
            //一致しない場合、エラー表示はするが保存は実行してしまう
            if (matchErr)
            {
                //保存へ進む
            }
            else
            {
                //エラーなしは完了フラグを立てる
                match = true;
            }

            //保存処理
            if (match)
            {
                sv.done = "1";  //マッチングOKの場合、完了フラグを立てる
            }

            sv.updateUser = Global.inputUser;
            bool err = updateData(sv, "1回目", conn);

            SaveData sv1 = new SaveData();
            sv1.lotNo = int.Parse(Global.lotNo);
            sv1.serialNo = sv.serialNo;
            sv1.done = sv.done;
            string tableName = "2回目";
            updateDoneFlag(sv1, tableName, conn);//別の回の完了フラグも立てる

        }

        private SaveData makeSaveDataOnce()
        {
            SaveData sv = new SaveData();

            sv.lotNo = int.Parse(Global.lotNo);      //ロット番号
            sv.adminCode = Global.adminCode;         //行政区コード
            sv.adminName = Global.adminName;         //行政区名称
            sv.healthArea = Global.healthArea;       //保健区
            sv.serialNo = textBox3.Text.PadLeft(10, '0');         //整理番号
            if ("".Equals(textBox17.Text))
            {
                sv.houseNo = "";
            }
            else
            {
                sv.houseNo = textBox17.Text.PadLeft(10, '0');          //世帯番号
            }
            sv.healthCheck = textBox20.Text;      //健康診査
            sv.lungCancer = textBox21.Text;       //結核肺がん
            sv.stomachCancer = textBox22.Text;    //胃がん
            sv.colorectalCancer = textBox23.Text; //大腸がん
            sv.prostateCancer = textBox24.Text;   //前立腺がん
            sv.osteoporosis = textBox25.Text;     //骨粗鬆症
            sv.dental = textBox26.Text;           //成人歯科
            sv.cervicalCancer = specialReadTwo(textBox27, textBox35);   //子宮頸がん
            sv.breastCancer = specialReadTwo(textBox28, textBox36);     //乳がん
            sv.place = specialReadThree(textBox29, textBox37, textBox38);            //希望場所
            sv.handicapped = "".Equals(textBox30.Text) ? "" : "8";      //身体不自由
            sv.phone = textBox18.Text;            //電話番号
            sv.mobilePhone = textBox19.Text;      //携帯番号
            sv.inputDate = null;        //入力日
            sv.inputUser = null;        //入力者
            sv.updateDate = null;       //修正日
            sv.updateUser = null;       //修正者
            sv.done = "";             //完了

            return sv;
        }

        private void setEditable(TextBox tb, bool editable)
        {
            if(editable)
            {
                tb.ReadOnly = false; //読み取り専用解除
                tb.TabStop = true; //タブ移動可
            }
            else
            {
                tb.ReadOnly = true; //読み取り専用
                tb.TabStop = false; //タブ移動負荷
            }
        }

        //編集可能に変更
        private void changeEdit(bool editable)
        {
            setEditable(textBox17, editable);
            setEditable(textBox18, editable);
            setEditable(textBox19, editable);
            setEditable(textBox20, editable);
            setEditable(textBox21, editable);
            setEditable(textBox22, editable);
            setEditable(textBox23, editable);
            setEditable(textBox24, editable);
            setEditable(textBox25, editable);
            setEditable(textBox26, editable);
            setEditable(textBox27, editable);
            setEditable(textBox35, editable);
            setEditable(textBox28, editable);
            setEditable(textBox36, editable);
            setEditable(textBox29, editable);
            setEditable(textBox37, editable);
            setEditable(textBox38, editable);
            setEditable(textBox30, editable);
        }

        //1回目の表示
        private void button6_Click(object sender, EventArgs e)
        {
            if("".Equals(textBox1.Text))
            {
                return;
            }

            MySqlConnection conn;
            string connstr = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            conn = new MySqlConnection(connstr);
            conn.Open();

            string serialNo = textBox1.Text.PadLeft(10, '0');

            string btxt = button6.Text;
            if ("1回目の表示".Equals(btxt))
            {
                changeEdit(false);
                selectDataOnce("1回目", Global.lotNo, serialNo, conn);//表示
                button6.Text = "1回目の編集";
            }
            else if ("1回目の編集".Equals(btxt))
            {
                //編集可能に変更(整理番号だけ変えさせない)
                changeEdit(true);
                button6.Text = "1回目の保存";
            }
            else if ("1回目の保存".Equals(btxt))
            {
                //データ保存
                updateDataOnce(conn);
            }

            panel1.Visible = true;

            conn.Close();

        }

        //閉じる
        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e) //整理番号
        {
            inputAllNumber(sender, e);
        }

        private void textBox17_KeyPress(object sender, KeyPressEventArgs e) //世帯番号
        {
            inputAllNumber(sender, e);
        }

        private void textBox18_KeyPress(object sender, KeyPressEventArgs e) //電話
        {
            inputAllNumber(sender, e);
        }

        private void textBox19_KeyPress(object sender, KeyPressEventArgs e) //携帯
        {
            inputAllNumber(sender, e);
        }

        private void textBox20_KeyPress(object sender, KeyPressEventArgs e) //健康診査
        {
            inputNumber(sender, e);
        }

        private void textBox21_KeyPress(object sender, KeyPressEventArgs e)//結核肺がん
        {
            inputNumber(sender, e);
        }

        private void textBox22_KeyPress(object sender, KeyPressEventArgs e) //胃がん
        {
            inputNumber(sender, e);
        }

        private void textBox23_KeyPress(object sender, KeyPressEventArgs e) //大腸がん
        {
            inputNumber(sender, e);
        }

        private void textBox24_KeyPress(object sender, KeyPressEventArgs e) //前立腺がん
        {
            inputNumber(sender, e);
        }

        private void textBox25_KeyPress(object sender, KeyPressEventArgs e) //骨粗鬆症
        {
            inputNumber(sender, e);
        }

        private void textBox26_KeyPress(object sender, KeyPressEventArgs e) //成人歯科
        {
            inputNumber(sender, e);
        }

        private void textBox27_KeyPress(object sender, KeyPressEventArgs e) //子宮頸がん
        {
            if ("".Equals(textBox35.Text)) //どちらかにしか入れさせない
            {
                inputNumber(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox35_KeyPress(object sender, KeyPressEventArgs e) //子宮頸がん(指定医療機関)
        {
            if ("".Equals(textBox27.Text)) //どちらかにしか入れさせない
            {
                inputNumber(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox28_KeyPress(object sender, KeyPressEventArgs e) //乳がん
        {
            if ("".Equals(textBox36.Text)) //どちらかにしか入れさせない
            {
                inputNumber(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox36_KeyPress(object sender, KeyPressEventArgs e) //乳がん(石巻市医師会)
        {
            if ("".Equals(textBox28.Text)) //どちらかにしか入れさせない
            {
                inputNumber(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox29_KeyPress(object sender, KeyPressEventArgs e) //希望場所
        {
            if ("".Equals(textBox37.Text) && "".Equals(textBox38.Text)) //どちらかにしか入れさせない
            {
                inputZero(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox37_KeyPress(object sender, KeyPressEventArgs e) //希望場所(本庁個別)
        {
            if ("".Equals(textBox29.Text) && "".Equals(textBox38.Text)) //どちらかにしか入れさせない
            {
                inputZero(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox38_KeyPress(object sender, KeyPressEventArgs e)//希望場所(石巻ロイヤル)
        {
            if ("".Equals(textBox29.Text) && "".Equals(textBox37.Text)) //どちらかにしか入れさせない
            {
                inputZero(sender, e);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox30_KeyPress(object sender, KeyPressEventArgs e) //身体不自由
        {
            inputZero(sender, e);
        }



    }
}