您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

192 行
7.0KB

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