You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 satır
7.4KB

  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.Exceptions;
  10. using ExpectedConditions = OpenQA.Selenium.Support.UI.ExpectedConditions;
  11. namespace CSVDownloader.Web {
  12. class ItecController : WebController {
  13. private readonly String url_ = "https://www.itc-offerinfo.net/webap/Login";
  14. private enum XpathKey {
  15. INPUT_LOGIN_USERNAME,
  16. INPUT_LOGIN_PASSWORD,
  17. BUTTON_LOGIN,
  18. BUTTON_CREDIT_KESSAI,
  19. RADIO_TARGET_SPAN,
  20. SELECT_TARGET_FROM_YYYYMM,
  21. SELECT_TARGET_FROM_DD,
  22. SELECT_TARGET_TO_YYYYMM,
  23. SELECT_TARGET_TO_DD,
  24. BUTTON_CONFIRM_CSV,
  25. BUTTON_DOWNLOAD_CSV,
  26. BUTTON_LOGOUT,
  27. };
  28. private IDictionary<XpathKey, string> xpath_map_ = new Dictionary<XpathKey, string>()
  29. {
  30. {XpathKey.INPUT_LOGIN_USERNAME,@"//*[@id='panel']/div[2]/input" },
  31. {XpathKey.INPUT_LOGIN_PASSWORD,@"//*[@id='panel']/div[4]/input"},
  32. {XpathKey.BUTTON_LOGIN,@"//*[@id='area_m']/form/div[2]/input[1]"},
  33. {XpathKey.BUTTON_CREDIT_KESSAI,@"//*[@id='menuRa']" },
  34. {XpathKey.RADIO_TARGET_SPAN,@"//*[@id='kikantype1']" },
  35. {XpathKey.SELECT_TARGET_FROM_YYYYMM,$"//*[@id='kikan_from_month']" },
  36. {XpathKey.SELECT_TARGET_FROM_DD,$"//*[@id='kikan_from_day']" },
  37. {XpathKey.SELECT_TARGET_TO_YYYYMM,$"//*[@id='kikan_to_month']" },
  38. {XpathKey.SELECT_TARGET_TO_DD,$"//*[@id='kikan_to_day']" },
  39. {XpathKey.BUTTON_CONFIRM_CSV,$"//*[@id='btn-exec']" },
  40. {XpathKey.BUTTON_DOWNLOAD_CSV,$"//*[@id='dl-detail']" },
  41. {XpathKey.BUTTON_LOGOUT,$"//*[@id='btn_logout']" },
  42. };
  43. public ItecController(ChromeDriver driver) : base(driver) {
  44. agent_ = CreditAgent.ITEC;
  45. }
  46. public override void SetParkingDic(IDictionary<String, String> dic) {
  47. dic_ = new Dictionary<String, String>();
  48. // IPコードと駐車場名を分離。プログラムではIPコードのみ利用する。
  49. foreach (var ele in dic) {
  50. String parking_name = ele.Key.Split(" ")[0].Trim();
  51. dic_.Add(parking_name, ele.Value);
  52. }
  53. }
  54. public override ResultCode Login(LoginInfo info) {
  55. try {
  56. driver_.Navigate().GoToUrl(url_);
  57. Send(xpath_map_[XpathKey.INPUT_LOGIN_USERNAME], info.user_name);
  58. Send(xpath_map_[XpathKey.INPUT_LOGIN_PASSWORD], info.password);
  59. Click(xpath_map_[XpathKey.BUTTON_LOGIN]);
  60. // ログイン後の明細データダウンロードが表示されることを確認する。
  61. wait_.Until(ExpectedConditions.ElementToBeClickable(By.XPath(xpath_map_[XpathKey.BUTTON_CREDIT_KESSAI])));
  62. } catch (Exception e) {
  63. logger_.Error(e.Message);
  64. return ResultCode.NG;
  65. }
  66. return ResultCode.OK;
  67. }
  68. public override ResultCode Download(DateTime from, DateTime to) {
  69. // クレジットカード承認結果一覧画面に移行
  70. Click(xpath_map_[XpathKey.BUTTON_CREDIT_KESSAI]);
  71. // 承認処理日のFrom-Toを入力
  72. Select(xpath_map_[XpathKey.SELECT_TARGET_FROM_YYYYMM], from.ToString("yyyyMM"));
  73. Select(xpath_map_[XpathKey.SELECT_TARGET_FROM_DD], from.ToString("dd"));
  74. Select(xpath_map_[XpathKey.SELECT_TARGET_TO_YYYYMM], to.ToString("yyyyMM"));
  75. Select(xpath_map_[XpathKey.SELECT_TARGET_TO_DD], to.ToString("dd"));
  76. // 各種ラジオチェックを入力
  77. Click(xpath_map_[XpathKey.RADIO_TARGET_SPAN]);
  78. // 条件確定
  79. Click(xpath_map_[XpathKey.BUTTON_CONFIRM_CSV]);
  80. // ダウンロード開始
  81. Click(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV]);
  82. String filename = $"itec.csv";
  83. WaitForDownload(filename);
  84. return ResultCode.OK;
  85. }
  86. public override ResultCode Logout() {
  87. Click(xpath_map_[XpathKey.BUTTON_LOGOUT]);
  88. return ResultCode.OK;
  89. }
  90. public override List<CreditCSVData> GetCreditCSVDataList() {
  91. var result_list = new List<CreditCSVData>();
  92. // csvファイルパスの取得
  93. var config = new CSVConfig() {
  94. header = true,
  95. };
  96. var dir = new DirectoryInfo(DriverFactory.GetDownloadDir());
  97. var sjis_enc = Encoding.GetEncoding("Shift-JIS");
  98. var list = new List<String[]>();
  99. foreach (var file in dir.GetFiles()) {
  100. var result = ReadCsv(file.FullName, config, sjis_enc);
  101. list.AddRange(result);
  102. }
  103. bool failed_flg = false;
  104. var failed_parking_name_hs_table = new HashSet<String>();
  105. foreach (var line in list) {
  106. // 空の行はスキップする
  107. if (line.Length == 0) {
  108. continue;
  109. }
  110. // 不要な行はスキップする
  111. if (line.Length != 10) {
  112. continue;
  113. }
  114. var parking_name = line[(int)CreditCSVDataItec.ColName.ParkingName];
  115. try {
  116. result_list.Add(new CreditCSVDataItec() {
  117. spot_id = GetSpotID(parking_name),
  118. code = line[(int)CreditCSVDataItec.ColName.Code],
  119. parking_name = parking_name,
  120. device_type = line[(int)CreditCSVDataItec.ColName.DeviceType],
  121. use_datetime = DateTime.Parse(line[(int)CreditCSVDataItec.ColName.UseDatetime]),
  122. trade = line[(int)CreditCSVDataItec.ColName.Trade],
  123. card_company_name = line[(int)CreditCSVDataItec.ColName.CardCompanyName],
  124. amount = int.Parse(line[(int)CreditCSVDataItec.ColName.Amount]),
  125. tax_send_fee = int.Parse(line[(int)CreditCSVDataItec.ColName.TaxSendFee]),
  126. total_amount = int.Parse(line[(int)CreditCSVDataItec.ColName.TotalAmount]),
  127. error_code = line[(int)CreditCSVDataItec.ColName.ErrorCode],
  128. });
  129. } catch (ArgumentException) {
  130. failed_parking_name_hs_table.Add(parking_name);
  131. failed_flg = true;
  132. continue;
  133. }
  134. }
  135. if (failed_flg) {
  136. if (failed_flg) {
  137. var error_list = new List<Store.ParkingCreditCardAgenciesSpotIDError>();
  138. logger_.Error("失敗駐車場名");
  139. foreach (var ele in failed_parking_name_hs_table) {
  140. error_list.Add(new Store.ParkingCreditCardAgenciesSpotIDError() {
  141. creditcard_agencies_id = CreditAgenciesMap.GetID(agent_),
  142. creditcard_agencies_spot_name = ele
  143. });
  144. logger_.Error($"駐車場 \"{ele}\"");
  145. }
  146. throw new SpotNameNotMatchException(error_list);
  147. }
  148. }
  149. return result_list;
  150. }
  151. }
  152. }