private void btnTransJson_Click(object sender, EventArgs e) { TemplateMethods.WatiCursorAndCatchAndLog(this, () => { // resource DirectoryInfo tgtFolder = new DirectoryInfo(txtTargetBaseFolder.Text); _logger.Info("# 開始產生json檔:"); using (var ctx = new MetaDatabaseEntities()) { string[] bookBlogList = new string[5] { "book01", "book02", "book03", "book04", "book05" }; string[] jsonNameList = new string[5] { "books_01.json", "books_02.json", "books_03.json", "books_04.json", "books_05.json" }; int totalBooksCnt = 0; for (int idx = 0; idx < 5; idx++) { string bookBlog = bookBlogList[idx]; string jsonName = jsonNameList[idx]; // query 五大落書 var qry = from c in ctx.BOOKLIST where c.書落 == bookBlog orderby c.順序 select new BookAtom { id = c.獨立編號, title = "書本" + c.獨立編號, note = "簡介" + c.獨立編號 }; BookAtom[] booksBlog = qry.ToArray(); _logger.Info("書落:{0} 筆數:{1}", bookBlog, booksBlog.Length); // target folder & filepath DirectoryInfo jsonTgtFolder = new DirectoryInfo(string.Format("{0}\\book\\js", tgtFolder.FullName)); if (!jsonTgtFolder.Exists) { jsonTgtFolder.Create(); } FileInfo jsonTgtFilepath = new FileInfo(jsonTgtFolder.FullName + "\\" + jsonName); // save as json OutputBooksBlogAsJson(booksBlog, jsonTgtFilepath); _logger.Info("Output Json: {0}", jsonTgtFilepath.FullName); totalBooksCnt += booksBlog.Length; } _logger.Info("處理 {0:N0} 筆", totalBooksCnt); } }); }
private void btnClear_Click(object sender, EventArgs e) { TemplateMethods.WatiCursorAndCatchAndLog(this, () => { using (var ctx = new MetaDatabaseEntities()) { //# clear BOOKLIST in MetaDatabase.mdf _logger.Info("# 清除書單統整資料:"); ctx.Database.ExecuteSqlCommand("DELETE FROM [BOOKLIST]"); // refresh UI bsBOOKLIST.DataSource = ctx.BOOKLIST.Local; _logger.Info("清除完成。"); } }); }
private void btnImport_Click(object sender, EventArgs e) { TemplateMethods.WatiCursorAndCatchAndLog(this, () => { FileInfo fi = new FileInfo(txtBookListFilepath.Text); if (!fi.Exists) { Misc.ShowMessageBox("檔案不存在。"); return; } using (var ctx = new MetaDatabaseEntities()) { //# import to MetaDatabase.mdf _logger.Info("# 匯入書單統整檔:"); int rowCnt = DoImportBooklist(fi, ctx); _logger.Info("匯入完成。匯入 {0:N0} 筆。", rowCnt); // refresh UI bsBOOKLIST.DataSource = ctx.BOOKLIST.Local; this.MainForm.UpdateStatusLabel1(string.Format("筆數:{0:N0}", rowCnt)); } }); }
private void btnCheck_Click(object sender, EventArgs e) { TemplateMethods.WatiCursorAndCatchAndLog(this, () => { using (var ctx = new MetaDatabaseEntities()) { // GO //## 檢查「屬性」與推薦欄位 /// 與產生 right.html 相關 /// [#屬性#] == "總編|兩者" => [#推薦單位#] [#推薦總編#] [#職銜#] [#總編推薦文#] /// [#屬性#] == "職人|兩者" => [#推薦職人店別#] [#職人姓名#] [#職人推薦文#] //## 檢查圖檔來源是否存在 int notExistsCounter = 0; int counter = 0; StringBuilder sbNotExistFields = new StringBuilder(); _logger.Info("# 檢查「屬性」與推薦欄位:"); foreach (var info in ctx.BOOKLIST) { sbNotExistFields.Clear(); if (info.屬性 == "總編" || info.屬性 == "兩者") { sbNotExistFields.Clear(); if (string.IsNullOrWhiteSpace(info.推薦單位)) { sbNotExistFields.Append("推薦單位; "); } if (string.IsNullOrWhiteSpace(info.推薦總編)) { sbNotExistFields.Append("推薦總編; "); } if (string.IsNullOrWhiteSpace(info.職銜)) { sbNotExistFields.Append("職銜; "); } if (string.IsNullOrWhiteSpace(info.總編推薦文)) { sbNotExistFields.Append("總編推薦文; "); } } else if (info.屬性 == "職人" || info.屬性 == "兩者") { if (string.IsNullOrWhiteSpace(info.推薦職人店別)) { sbNotExistFields.Append("推薦職人店別; "); } if (string.IsNullOrWhiteSpace(info.職人姓名)) { sbNotExistFields.Append("職人姓名; "); } if (string.IsNullOrWhiteSpace(info.職人推薦文)) { sbNotExistFields.Append("職人推薦文; "); } } else { _logger.Info("* 項次:{0} - {1} 屬性='{2}' 預期之外的屬性。", info.項次, info.獨立編號, info.屬性); notExistsCounter++; } // 但推薦相關欄位無值 if (sbNotExistFields.Length > 0) { _logger.Info("* 項次:{0} - {1} 屬性='{2}',但推薦相關欄位無值:{3}。", info.項次, info.獨立編號, info.屬性, sbNotExistFields); notExistsCounter++; } // next counter++; } _logger.Info("檢查完成。處理 {0:N0} 筆,發現 {1:N0} 個問題。", counter, notExistsCounter); } }); }
private void btnTransHtml_Click(object sender, EventArgs e) { TemplateMethods.WatiCursorAndCatchAndLog(this, () => { // resource DirectoryInfo tgtFolder = new DirectoryInfo(txtTargetBaseFolder.Text); // UI: show progress bool isTrial = chkTrial.Checked; int trialCount = (int)numTrialCount.Value; int counter = 0; using (var ctx = new MetaDatabaseEntities()) { // 取各大分類的 第一筆 與 最末筆 編號 var qryFirstLast = from c in ctx.BOOKLIST group c by c.大分類 into g select new BookBlogFirstLastUid { PrimaryClass = g.Key, FirstUid = g.Min(c => c.獨立編號), LastIUid = g.Max(c => c.獨立編號) }; var dict = qryFirstLast.ToDictionary(c => c.PrimaryClass); // UI: show progress prgBar.Value = 0; prgBar.Maximum = isTrial ? trialCount : ctx.BOOKLIST.Count(); // GO _logger.Info("# 開始產生html檔:"); foreach (var info in ctx.BOOKLIST) { // target folder & filepath DirectoryInfo htmlTgtFolder = new DirectoryInfo(string.Format("{0}\\book\\{1}\\{2}\\", tgtFolder.FullName, info.書落, info.獨立編號)); if (!htmlTgtFolder.Exists) { htmlTgtFolder.Create(); } FileInfo fiLeft = new FileInfo(htmlTgtFolder.FullName + "\\left.html"); FileInfo fiRight = new FileInfo(htmlTgtFolder.FullName + "\\right.html"); DoGenerateLeftHtml(info, fiLeft); //if (!fiLeft.Exists) throw new ApplicationException(string.Format("left.html產生失敗!目的檔不存在{0}。", fiLeft.FullName)); DoGenerateRightHtml(info, fiRight, dict[info.大分類]); //if (!fiRight.Exists) throw new ApplicationException(string.Format("right.html產生失敗!目的檔不存在{0}。", fiRight.FullName)); // next counter++; // UI: show progress lblStepMsg.Text = string.Format("項次:{0} {1}", info.項次, info.推薦書籍); prgBar.PerformStep(); // 防止「沒有回應」 Application.DoEvents(); // trial if (isTrial && counter >= trialCount) { break; } } _logger.Info("處理 {0:N0} 筆", counter); } }); }
private void btnTransImgPrecheck_Click(object sender, EventArgs e) { TemplateMethods.WatiCursorAndCatchAndLog(this, () => { // resource DirectoryInfo srcFolder = new DirectoryInfo(txtBookPhotoFolder.Text); DirectoryInfo tgtFolder = new DirectoryInfo(txtTargetBaseFolder.Text); // UI: show progress bool isTrial = chkTrial.Checked; int trialCount = (int)numTrialCount.Value; int counter = 0; using (var ctx = new MetaDatabaseEntities()) { // UI: show progress prgBar.Value = 0; prgBar.Maximum = isTrial ? trialCount : ctx.BOOKLIST.Count(); // GO //## 檢查圖檔來源是否存在 int notExistsCounter = 0; _logger.Info("# 檢查圖檔來源不存在:"); foreach (var info in ctx.BOOKLIST) { string imageSrcFolder = DoDetermineImageSourceFolder(srcFolder, info); FileInfo img01src = new FileInfo(imageSrcFolder + "01.png"); FileInfo img02src = new FileInfo(imageSrcFolder + "02.jpg"); FileInfo img03src = new FileInfo(imageSrcFolder + "03.jpg"); if (!img01src.Exists) { notExistsCounter++; _logger.Info("* 圖檔來源不存在,項次:{0} - {1} - 01.png 推薦書籍:{2}", info.項次, info.獨立編號, info.推薦書籍); _logger.Info("Image path: {0}", img01src.FullName); } if (!img02src.Exists) { notExistsCounter++; _logger.Info("* 圖檔來源不存在,項次:{0} - {1} - 02.jpg 推薦書籍:{2}", info.項次, info.獨立編號, info.推薦書籍); _logger.Info("Image path: {0}", img02src.FullName); } if (!img03src.Exists) { notExistsCounter++; _logger.Info("* 圖檔來源不存在,項次:{0} - {1} - 03.jpg 推薦書籍:{2}", info.項次, info.獨立編號, info.推薦書籍); _logger.Info("Image path: {0}", img03src.FullName); } // next counter++; // UI: show progress lblStepMsg.Text = string.Format("項次:{0} {1}", info.項次, info.推薦書籍); prgBar.PerformStep(); // 防止「沒有回應」 Application.DoEvents(); // trial if (isTrial && counter >= trialCount) { break; } } _logger.Info("檢查完成。處理 {0:N0} 筆,有 {1:N0} 張圖檔不存在。", counter, notExistsCounter); } }); }
private void btnTransImg_Click(object sender, EventArgs e) { TemplateMethods.WatiCursorAndCatchAndLog(this, () => { // resource DirectoryInfo srcFolder = new DirectoryInfo(txtBookPhotoFolder.Text); DirectoryInfo tgtFolder = new DirectoryInfo(txtTargetBaseFolder.Text); // UI: show progress bool isTrial = chkTrial.Checked; int trialCount = (int)numTrialCount.Value; int counter = 0; using (var ctx = new MetaDatabaseEntities()) { // UI: show progress prgBar.Value = 0; prgBar.Maximum = isTrial ? trialCount : ctx.BOOKLIST.Count(); // GO _logger.Info("# 開始轉換圖檔:"); foreach (var info in ctx.BOOKLIST) { string imageSrcFolder = DoDetermineImageSourceFolder(srcFolder, info); FileInfo img01src = new FileInfo(imageSrcFolder + "01.png"); FileInfo img02src = new FileInfo(imageSrcFolder + "02.jpg"); FileInfo img03src = new FileInfo(imageSrcFolder + "03.jpg"); DirectoryInfo imgTgtFolder = DoDetermineImageTargetFolder(tgtFolder, info); FileInfo img01tgt = new FileInfo(imgTgtFolder.FullName + "\\01.png"); FileInfo img02tgt = new FileInfo(imgTgtFolder.FullName + "\\02.jpg"); FileInfo img03tgt = new FileInfo(imgTgtFolder.FullName + "\\03.jpg"); // copy image if (!img01src.Exists) { throw new ApplicationException(string.Format("01.png複製失敗!來源檔不存在{0}。", img01src.FullName)); } img01src.CopyTo(img01tgt.FullName, true); _logger.Info("Copy 01.png: 項次:{0} - {1} - {2}", info.項次, info.獨立編號, info.推薦書籍); if (!img01tgt.Exists) { throw new ApplicationException(string.Format("01.png複製失敗!目的檔不存在{0}。", img01tgt.FullName)); } if (!img02src.Exists) { throw new ApplicationException(string.Format("02.jpg複製失敗!來源檔不存在{0}。", img02src.FullName)); } img02src.CopyTo(img02tgt.FullName, true); _logger.Info("Copy 02.jgp: 項次:{0} - {1} - {2}", info.項次, info.獨立編號, info.推薦書籍); if (!img02tgt.Exists) { throw new ApplicationException(string.Format("02.jpg複製失敗!目的檔不存在{0}。", img02tgt.FullName)); } if (!img03src.Exists) { throw new ApplicationException(string.Format("03.jpg複製失敗!來源檔不存在{0}。", img03src.FullName)); } img03src.CopyTo(img03tgt.FullName, true); _logger.Info("Copy 03.jgp: 項次:{0} - {1} - {2}", info.項次, info.獨立編號, info.推薦書籍); if (!img03tgt.Exists) { throw new ApplicationException(string.Format("03.jpg複製失敗!目的檔不存在{0}。", img03tgt.FullName)); } //## 縮圖 DirectoryInfo imgBkTgtFolder = DoDetermineImageTargetFolder_Books(tgtFolder, info); FileInfo imgBKtgt = new FileInfo(string.Format("{0}\\{1}.jpg", imgBkTgtFolder.FullName, info.獨立編號)); DirectoryInfo imgBkcTgtFolder = DoDetermineImageTargetFolder_BooksC(tgtFolder, info); FileInfo imgBKCtgt = new FileInfo(string.Format("{0}\\{1}.jpg", imgBkcTgtFolder.FullName, info.獨立編號)); //# copy 03.jpb → images_books, 固定寬度 300px if (!img03src.Exists) { throw new ApplicationException(string.Format("03.jpg複製失敗!來源檔不存在{0}。", img03src.FullName)); } DoMakeImageThumbnail(img03src, imgBKtgt, fixWidth: 300); _logger.Info("Thumbnail images_books: 項次:{0} - {1} - {2}", info.項次, info.獨立編號, info.推薦書籍); if (!imgBKtgt.Exists) { throw new ApplicationException(string.Format("images_books產生失敗!目的檔不存在{0}。", imgBKtgt.FullName)); } //# copy 02.jpb → images_books_c, 固定高度 600px if (!img02src.Exists) { throw new ApplicationException(string.Format("02.jpg複製失敗!來源檔不存在{0}。", img02src.FullName)); } DoMakeImageThumbnail(img02src, imgBKCtgt, fixHeight: 600); _logger.Info("Thumbnail images_books_c: 項次:{0} - {1} - {2}", info.項次, info.獨立編號, info.推薦書籍); if (!imgBKCtgt.Exists) { throw new ApplicationException(string.Format("images_books_c產生失敗!目的檔不存在{0}。", imgBKCtgt.FullName)); } // next counter++; // UI: show progress lblStepMsg.Text = string.Format("項次:{0} {1}", info.項次, info.推薦書籍); prgBar.PerformStep(); // 防止「沒有回應」 Application.DoEvents(); // trial if (isTrial && counter >= trialCount) { break; } } _logger.Info("處理 {0:N0} 筆", counter); } }); }