Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

252 Zeilen
9.8KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using OpenQA.Selenium;
  6. using OpenQA.Selenium.Chrome;
  7. using CSVDownloader.Code;
  8. using CSVDownloader.Store.CreditCSVData;
  9. using CSVDownloader.Store.QRCSVData;
  10. using CSVDownloader.Exceptions;
  11. using ExpectedConditions = OpenQA.Selenium.Support.UI.ExpectedConditions;
  12. namespace CSVDownloader.Web {
  13. class DaitoController : WebController {
  14. private readonly String url_ = "https://crevation.net";
  15. private readonly String home_url_ = "https://crevation.net/";
  16. private readonly String filename_credit = "daito_credit.csv";
  17. private readonly String filename_qr = "daito_qr.csv";
  18. private enum XpathKey {
  19. INPUT_LOGIN_USERNAME,
  20. INPUT_LOGIN_PASSWORD,
  21. BUTTON_MENU,
  22. BUTTON_KESSAI_KANRI,
  23. BUTTON_CREDIT_KESSAI,
  24. BUTTON_QR_KESSAI,
  25. INPUT_TARGET_FROM,
  26. INPUT_TARGET_TO,
  27. BUTTON_DOWNLOAD_CSV,
  28. BUTTON_LOGOUT,
  29. };
  30. private IDictionary<XpathKey, string> xpath_map_ = new Dictionary<XpathKey, string>()
  31. {
  32. {XpathKey.INPUT_LOGIN_USERNAME,@"//*[@id='wrapper']/div/div/div/div/div[2]/form/fieldset/div[1]/input" },
  33. {XpathKey.INPUT_LOGIN_PASSWORD,@"//*[@id='wrapper']/div/div/div/div/div[2]/form/fieldset/div[2]/input" },
  34. {XpathKey.BUTTON_MENU,@"//*[@id='js-show-popup']" },
  35. {XpathKey.BUTTON_KESSAI_KANRI,@"//*[@id='side-menu']/li[4]/a" },
  36. {XpathKey.BUTTON_CREDIT_KESSAI,@"//*[@id='side-menu']/li[4]/ul/li[1]/a" },
  37. {XpathKey.BUTTON_QR_KESSAI,@"//*[@id='side-menu']/li[4]/ul/li[2]/a" },
  38. {XpathKey.INPUT_TARGET_FROM,@"//*[@id='settlementTimeFrom']" },
  39. {XpathKey.INPUT_TARGET_TO,@"//*[@id='settlementTimeTo']" },
  40. {XpathKey.BUTTON_DOWNLOAD_CSV,@"//*[@id='submitCreateCsv']" },
  41. {XpathKey.BUTTON_LOGOUT,@"//*[@id='side-menu']/li[11]/a" },
  42. };
  43. public DaitoController(ChromeDriver driver) : base(driver) {
  44. agent_ = CreditAgent.Daito;
  45. }
  46. public override ResultCode Login(LoginInfo info) {
  47. bool enter = true;
  48. try {
  49. driver_.Navigate().GoToUrl(url_);
  50. Send(xpath_map_[XpathKey.INPUT_LOGIN_USERNAME], info.user_name);
  51. Send(xpath_map_[XpathKey.INPUT_LOGIN_PASSWORD], info.password, enter);
  52. // ログイン後のメニューボタンが表示されることを確認する。
  53. wait_.Until(ExpectedConditions.ElementExists(By.XPath(xpath_map_[XpathKey.BUTTON_MENU])));
  54. } catch (Exception e) {
  55. logger_.Error(e.Message);
  56. return ResultCode.NG;
  57. }
  58. return ResultCode.OK;
  59. }
  60. public override ResultCode Download(DateTime from, DateTime to) {
  61. // クレジットカード承認結果一覧画面に移行
  62. Click(xpath_map_[XpathKey.BUTTON_MENU]);
  63. Click(xpath_map_[XpathKey.BUTTON_KESSAI_KANRI]);
  64. Click(xpath_map_[XpathKey.BUTTON_CREDIT_KESSAI]);
  65. // 承認処理日のFrom-Toを入力
  66. const String date_format = "yyyy年MM月dd日";
  67. Send(xpath_map_[XpathKey.INPUT_TARGET_FROM], from.ToString(date_format));
  68. Send(xpath_map_[XpathKey.INPUT_TARGET_TO], to.ToString(date_format));
  69. // CSV吐き出しボタンをクリックし、CSVをダウンロード
  70. wait_.Until(ExpectedConditions.ElementToBeClickable(By.XPath(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV])));
  71. Click(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV]);
  72. WaitForDownload(filename_credit);
  73. // QR決済結果結果一覧画面に移行
  74. Click(xpath_map_[XpathKey.BUTTON_MENU]);
  75. Click(xpath_map_[XpathKey.BUTTON_KESSAI_KANRI]);
  76. Click(xpath_map_[XpathKey.BUTTON_QR_KESSAI]);
  77. // 承認処理日のFrom-Toを入力
  78. Send(xpath_map_[XpathKey.INPUT_TARGET_FROM], from.ToString(date_format));
  79. Send(xpath_map_[XpathKey.INPUT_TARGET_TO], to.ToString(date_format));
  80. // CSV吐き出しボタンをクリックし、CSVをダウンロード
  81. wait_.Until(ExpectedConditions.ElementToBeClickable(By.XPath(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV])));
  82. Click(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV]);
  83. WaitForDownload(filename_qr);
  84. return ResultCode.OK;
  85. }
  86. public override ResultCode Logout() {
  87. driver_.Navigate().GoToUrl(home_url_);
  88. Click(xpath_map_[XpathKey.BUTTON_MENU]);
  89. Click(xpath_map_[XpathKey.BUTTON_LOGOUT]);
  90. wait_.Until(ExpectedConditions.ElementIsVisible(By.XPath(xpath_map_[XpathKey.INPUT_LOGIN_USERNAME])));
  91. return ResultCode.OK;
  92. }
  93. public override List<CreditCSVData> GetCreditCSVDataList() {
  94. var result_list = new List<CreditCSVData>();
  95. var list = GetFileContents(filename_credit);
  96. bool failed_flg = false;
  97. var failed_parking_name_hs_table = new HashSet<String>();
  98. foreach (var line in list) {
  99. var parking_name = line[(int)CreditCSVDataDaito.ColName.ParkingName];
  100. try {
  101. result_list.Add(new CreditCSVDataDaito() {
  102. spot_id = GetSpotID(parking_name),
  103. parking_name = parking_name,
  104. developer = line[(int)CreditCSVDataDaito.ColName.Developer],
  105. adjust_type = line[(int)CreditCSVDataDaito.ColName.AdjustType],
  106. fee_type = line[(int)CreditCSVDataDaito.ColName.FeeType],
  107. reception_datetime = DateTime.Parse(line[(int)CreditCSVDataDaito.ColName.ReceptionDatetime]),
  108. amount = int.Parse(line[(int)CreditCSVDataDaito.ColName.Amount]),
  109. reception_no = line[(int)CreditCSVDataDaito.ColName.ReceptionNo],
  110. response_no = line[(int)CreditCSVDataDaito.ColName.ResponseNo],
  111. error_code1 = line[(int)CreditCSVDataDaito.ColName.ErrorCode1],
  112. error_code2 = line[(int)CreditCSVDataDaito.ColName.ErrorCode2]
  113. });
  114. } catch (ArgumentException) {
  115. failed_parking_name_hs_table.Add(parking_name);
  116. failed_flg = true;
  117. continue;
  118. }
  119. }
  120. if (failed_flg) {
  121. var error_list = new List<Store.ParkingCreditCardAgenciesSpotIDError>();
  122. logger_.Error("失敗駐車場名");
  123. foreach (var ele in failed_parking_name_hs_table) {
  124. error_list.Add(new Store.ParkingCreditCardAgenciesSpotIDError() {
  125. creditcard_agencies_id = CreditAgenciesMap.GetID(agent_),
  126. creditcard_agencies_spot_name = ele
  127. });
  128. logger_.Error($"駐車場 \"{ele}\"");
  129. }
  130. throw new SpotNameNotMatchException(error_list);
  131. }
  132. return result_list;
  133. }
  134. public override List<QRCSVData> GetQRCSVDataList() {
  135. var result_list = new List<QRCSVData>();
  136. var list = GetFileContents(filename_qr);
  137. bool failed_flg = false;
  138. var failed_parking_name_hs_table = new HashSet<String>();
  139. foreach (var line in list) {
  140. var parking_name = line[(int)QRCSVDataDaito.ColName.ParkingName];
  141. try {
  142. result_list.Add(new QRCSVDataDaito() {
  143. spot_id = GetSpotID(parking_name),
  144. parking_name = parking_name,
  145. developer = line[(int)QRCSVDataDaito.ColName.Developer],
  146. reception_datetime = DateTime.Parse(line[(int)QRCSVDataDaito.ColName.ReceptionDatetime]),
  147. company = line[(int)QRCSVDataDaito.ColName.Company],
  148. amount = int.Parse(line[(int)QRCSVDataDaito.ColName.Amount]),
  149. adjust_no = line[(int)QRCSVDataDaito.ColName.AdjustNo],
  150. error_code = line[(int)QRCSVDataDaito.ColName.ErrorCode],
  151. });
  152. } catch (ArgumentException) {
  153. failed_parking_name_hs_table.Add(parking_name);
  154. failed_flg = true;
  155. continue;
  156. }
  157. }
  158. if (failed_flg) {
  159. var error_list = new List<Store.ParkingCreditCardAgenciesSpotIDError>();
  160. logger_.Error("失敗駐車場名");
  161. foreach (var ele in failed_parking_name_hs_table) {
  162. error_list.Add(new Store.ParkingCreditCardAgenciesSpotIDError() {
  163. creditcard_agencies_id = CreditAgenciesMap.GetID(agent_),
  164. creditcard_agencies_spot_name = ele
  165. });
  166. logger_.Error($"駐車場 \"{ele}\"");
  167. }
  168. throw new SpotNameNotMatchException(error_list);
  169. }
  170. return result_list;
  171. }
  172. private List<String[]> GetFileContents(String filename) {
  173. var list = new List<String[]>();
  174. var dir = new DirectoryInfo(DriverFactory.GetDownloadDir());
  175. var config = new CSVConfig() {
  176. header = true,
  177. };
  178. config.remove_char.Add("\"");
  179. var sjis_enc = Encoding.GetEncoding("Shift-JIS");
  180. foreach (var file in dir.GetFiles()) {
  181. if (file.Name.Contains(filename)) {
  182. var result = ReadCsv(file.FullName, config, sjis_enc);
  183. list.AddRange(result);
  184. }
  185. }
  186. return list;
  187. }
  188. }
  189. }