Form3.cs 12.5 KB
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 = 100;
        private int currentPageNo = 1;
        private int maxPageNo = 1;

        public Form3()
        {
            InitializeComponent();

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

        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 pageing = " 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)";
            }

            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 += pageing;

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

            this.dataGridView1.Rows.Clear();
            //adapter.Fill(ds);
            while (reader.Read())
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(this.dataGridView1);
                row.Cells[0].Value = (this.isDBNull(reader, "s_完了") == "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 button1_Click(object sender, EventArgs e)
        {
            this.setPage();
            this.search();
        }

    }
}