using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ISBN_Check_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] api_list = { "알라딘", "네이버" };
string[] filter_list = { "제목+저자", "제목", "저자", "출판사" };
cb_api.Items.AddRange(api_list);
cb_filter.Items.AddRange(filter_list);
}
private void button2_Click(object sender, EventArgs e)
{
for(int a = 0; a < dataGridView1.Rows.Count - 1; a++)
{
if (dataGridView1.Rows[a].DefaultCellStyle.BackColor != Color.Yellow) {
dataGridView1.Rows[a].Cells["Column1"].Value = "";
dataGridView1.Rows[a].DefaultCellStyle.BackColor = Color.Empty;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if(cb_api.SelectedIndex == -1) { MessageBox.Show("조건이 선택되지 않았습니다."); return; }
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int start = Convert.ToInt32(start_idx.Text) - 1;
int end = Convert.ToInt32(end_idx.Text) - 1;
if (start_idx.Text != "1" || end_idx.Text != dataGridView1.Rows.Count.ToString()) {
start = Convert.ToInt32(start_idx.Text) - 1;
end = Convert.ToInt32(end_idx.Text) - 1;
}
switch (cb_api.SelectedIndex)
{
case 0:
if(cb_filter.SelectedIndex == -1) { MessageBox.Show("조건이 선택되지 않았습니다."); return; }
Aladin_API(dataGridView1, start, end);
break;
case 1:
Naver_API(dataGridView1, start, end);
break;
}
stopwatch.Stop();
TimeSpan ts = stopwatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
label1.Text = elapsedTime;
// 총 검색 횟수, 일치, 중복
MessageBox.Show("검색이 완료되었습니다!");
}
///
/// 알라딘 API
///
///
private void Aladin_API(DataGridView gridview, int start, int end)
{
string temp = string.Empty;
string type = string.Empty;
string query = string.Empty;
// 도서명 / 저자 / 출판사 / isbn / 정가
// 발행일 / 도서분류 / 재고
string[] param = { "title", "author", "publisher", "isbn13", "priceStandard",
"pubDate", "categoryName", "stockStatus" };
API api = new API();
switch (cb_filter.SelectedIndex)
{
case 0: // 제목+저자
type = "Keyword";
break;
case 1: // 제목
type = "Title";
break;
case 2: // 저자
type = "Author";
break;
case 3: // 출판사
type = "Publisher";
break;
}
for (int a = start; a < end; a++)
{
if (gridview.Rows[a].DefaultCellStyle.BackColor == Color.Yellow)
continue;
else if (gridview.Rows[a].DefaultCellStyle.BackColor == Color.LightGray)
gridview.Rows[a].DefaultCellStyle.BackColor = Color.Empty;
query = Set_query(type, a);
insert_By_Aladin(api.Aladin(query, type, param), a);
}
}
private void Naver_API(DataGridView gridview, int start, int end)
{
API api = new API();
// 도서명 / 저자 / 출판사 / isbn / 정가
// 발행일 / 도서분류 / 재고
string[] param = { "title", "author", "publisher", "isbn", "price",
"pubdate", "discount" };
for(int a = start; a < end; a++)
{
string[] ArrayValue = { gridview.Rows[a].Cells["book_name"].Value.ToString(),
gridview.Rows[a].Cells["author"].Value.ToString(),
gridview.Rows[a].Cells["book_comp"].Value.ToString() };
string result = api.Naver(ArrayValue, param);
insert_By_Naver(result, a);
}
}
string Set_query(string type, int idx)
{
string result = string.Empty;
if(type == "Keyword")
result = dataGridView1.Rows[idx].Cells["book_name"].Value.ToString() +
dataGridView1.Rows[idx].Cells["author"].Value.ToString();
if (type == "Title")
result = dataGridView1.Rows[idx].Cells["book_name"].Value.ToString();
if (type == "Author")
result = dataGridView1.Rows[idx].Cells["author"].Value.ToString();
if (type == "Publisher")
result = dataGridView1.Rows[idx].Cells["book_comp"].Value.ToString();
return result;
}
void insert_By_Aladin(string data, int row)
{
if (row > 0) { dataGridView1.Rows[row - 1].Selected = false; }
dataGridView1.Rows[row].Selected = true;
if (data.Length > 0) {
dataGridView1.Rows[row].Cells["Column1"].Value = data;
dataGridView1.Rows[row].DefaultCellStyle.BackColor = Color.LightGray;
}
bool[] chk = { false, false, false }; // 도서명 저자 출판사 체크.
string book_name = dataGridView1.Rows[row].Cells["book_name"].Value.ToString();
string author = dataGridView1.Rows[row].Cells["author"].Value.ToString();
string book_comp = dataGridView1.Rows[row].Cells["book_comp"].Value.ToString();
string[] insert = data.Split('|');
string newstring = string.Empty;
if (data == "") { return; }
// pubDate형 보기편하게 DateTime형으로 재정리
newstring = String.Format("{0:yyyy/MM/dd HH:mm}",
DateTime.Parse(insert[5].Remove(insert[5].IndexOf(" G"))));
// 도서 분류 필요한 데이터로 재정리
int top = insert[6].IndexOf('>');
int mid = insert[6].IndexOf('>', top + 1);
int bot = insert[6].IndexOf('>', mid + 1);
if (bot < 0) { insert[6] = insert[6].Substring(top + 1); }
else { insert[6] = insert[6].Substring(top + 1, bot - top - 1); }
if (insert.Length > 10)
return;
if (insert[0] == book_name) { chk[0] = true; }
if (insert[1].Contains(author) == true) { chk[1] = true; }
if (insert[2] == book_comp) { chk[2] = true; }
if (chk[0] == true && chk[1] == true && chk[2] == true)
{
dataGridView1.Rows[row].Cells["isbn"].Value = insert[3];
dataGridView1.Rows[row].Cells["price2"].Value = insert[4];
dataGridView1.Rows[row].Cells["pubDate"].Value = newstring;
dataGridView1.Rows[row].Cells["category"].Value = insert[6];
dataGridView1.Rows[row].Cells["sold_out"].Value = insert[7];
dataGridView1.Rows[row].DefaultCellStyle.BackColor = Color.Yellow;
}
}
void insert_By_Naver(string value, int row)
{
value = value.Replace("", "");
value = value.Replace("", "");
if (row > 0) { dataGridView1.Rows[row - 1].Selected = false; }
dataGridView1.Rows[row].Selected = true;
if (value.Length > 0) {
dataGridView1.Rows[row].Cells["Column1"].Value = value;
dataGridView1.Rows[row].DefaultCellStyle.BackColor = Color.LightGray;
}
if (value == "") return;
bool[] chk = { false, false, false };
string book_name = dataGridView1.Rows[row].Cells["book_name"].Value.ToString();
string author = dataGridView1.Rows[row].Cells["author"].Value.ToString();
string book_comp = dataGridView1.Rows[row].Cells["book_comp"].Value.ToString();
string[] sp_data = value.Split('\t');
if (sp_data.Length > 1) return;
/// TODO: 밑에 작업하세요
#region 이게 그 작업할 밑에임
// title author publisher isbn price pubdate / discount
// title author publisher isbn price pubdate discount
string[] grid = { "", "", "", "", "", "", "", "" };
for (int a = 0; a < sp_data.Length; a++)
{
string[] data = sp_data[a].Split('|');
int idx = data.Length - 2;
grid[0] = data[0];
grid[1] = data[1];
for (int b = 2; b < idx - 4; b++)
{
grid[1] += ", " + data[b];
}
grid[2] = data[idx - 4];
if (data[idx - 3].Contains(" ") == true)
{
string[] isbn = data[idx - 3].Split(' ');
grid[3] = isbn[1];
}
else
grid[3] = data[idx - 3];
grid[5] = data[idx - 2];
grid[6] = data[idx - 1];
if (data[idx] == "")
grid[8] = "절판";
else
grid[8] = "";
for(int b = 0; b < 9; b++) {
grid[9] += grid[b];
}
}
#endregion
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
Skill_Grid sg = new Skill_Grid();
if ((e.Shift && e.KeyCode == Keys.Insert) || (e.Control && e.KeyCode == Keys.V)) {
sg.Excel_to_DataGridView(sender, e);
}
else if (e.KeyCode == Keys.Delete) {
sg.DataGrid_to_Delete(sender, e);
}
end_idx.Text = dataGridView1.Rows.Count.ToString();
}
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
Skill_Grid sg = new Skill_Grid();
sg.Print_Grid_Num(sender, e);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value == null)
{
dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value = "";
}
richTextBox1.Text = dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value.ToString();
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if(dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value == null) { return; }
Form2 f2 = new Form2(this);
f2.row = e.RowIndex;
f2.Call_API = cb_api.Text;
f2.Show();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(char.IsDigit(e.KeyChar) || e.KeyChar == Convert.ToChar(Keys.Back)))
{
e.Handled = true;
}
}
private void cb_api_SelectedIndexChanged(object sender, EventArgs e)
{
if(cb_api.SelectedIndex == 1)
cb_filter.Enabled = false;
else cb_filter.Enabled = true;
}
}
}