Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

321 linhas
13KB

  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. using CSVDownloader.Store.ElectronicMoneyDataStore;
  13. namespace CSVDownloader.Web {
  14. class DaitoController : WebController {
  15. private readonly String url_ = "https://crevation.net";
  16. private readonly String home_url_ = "https://crevation.net/";
  17. private readonly String filename_credit_ = "daito_credit.csv";
  18. private readonly String filename_qr_ = "daito_qr.csv";
  19. private readonly String filename_electronic_money_ = "daito_electronic_money.csv";
  20. private enum XpathKey {
  21. INPUT_LOGIN_USERNAME,
  22. INPUT_LOGIN_PASSWORD,
  23. BUTTON_MENU,
  24. BUTTON_KESSAI_KANRI,
  25. BUTTON_CREDIT_KESSAI,
  26. BUTTON_QR_KESSAI,
  27. BUTTON_ELECTORONIC_MONEY_KESSAI,
  28. INPUT_TARGET_FROM,
  29. INPUT_TARGET_TO,
  30. BUTTON_DOWNLOAD_CSV,
  31. BUTTON_LOGOUT,
  32. };
  33. private IDictionary<XpathKey, string> xpath_map_ = new Dictionary<XpathKey, string>()
  34. {
  35. {XpathKey.INPUT_LOGIN_USERNAME,@"//*[@id='wrapper']/div/div/div/div/div[2]/form/fieldset/div[1]/input" },
  36. {XpathKey.INPUT_LOGIN_PASSWORD,@"//*[@id='wrapper']/div/div/div/div/div[2]/form/fieldset/div[2]/input" },
  37. {XpathKey.BUTTON_MENU,@"//*[@id='js-show-popup']" },
  38. {XpathKey.BUTTON_KESSAI_KANRI,@"//*[@id='side-menu']/li[4]/a" },
  39. {XpathKey.BUTTON_CREDIT_KESSAI,@"//*[@id='side-menu']/li[4]/ul/li[1]/a" },
  40. {XpathKey.BUTTON_QR_KESSAI,@"//*[@id='side-menu']/li[4]/ul/li[2]/a" },
  41. {XpathKey.BUTTON_ELECTORONIC_MONEY_KESSAI,@"//*[@id='side-menu']/li[4]/ul/li[3]/a" },
  42. {XpathKey.INPUT_TARGET_FROM,@"//*[@id='settlementTimeFrom']" },
  43. {XpathKey.INPUT_TARGET_TO,@"//*[@id='settlementTimeTo']" },
  44. {XpathKey.BUTTON_DOWNLOAD_CSV,@"//*[@id='submitCreateCsv']" },
  45. {XpathKey.BUTTON_LOGOUT,@"//*[@id='side-menu']/li[11]/a" },
  46. };
  47. public DaitoController(ChromeDriver driver) : base(driver) {
  48. agent_ = CreditAgent.Daito;
  49. }
  50. public override ResultCode Login(LoginInfo info) {
  51. bool enter = true;
  52. try {
  53. driver_.Navigate().GoToUrl(url_);
  54. Send(xpath_map_[XpathKey.INPUT_LOGIN_USERNAME], info.user_name);
  55. Send(xpath_map_[XpathKey.INPUT_LOGIN_PASSWORD], info.password, enter);
  56. // ログイン後のメニューボタンが表示されることを確認する。
  57. wait_.Until(ExpectedConditions.ElementExists(By.XPath(xpath_map_[XpathKey.BUTTON_MENU])));
  58. } catch (Exception e) {
  59. logger_.Error(e.Message);
  60. return ResultCode.NG;
  61. }
  62. return ResultCode.OK;
  63. }
  64. public override ResultCode Download(DateTime from, DateTime to) {
  65. // クレジットカード承認結果一覧画面に移行
  66. Click(xpath_map_[XpathKey.BUTTON_MENU]);
  67. Click(xpath_map_[XpathKey.BUTTON_KESSAI_KANRI]);
  68. Click(xpath_map_[XpathKey.BUTTON_CREDIT_KESSAI]);
  69. // 承認処理日のFrom-Toを入力
  70. const String date_format = "yyyy年MM月dd日";
  71. Send(xpath_map_[XpathKey.INPUT_TARGET_FROM], from.ToString(date_format));
  72. Send(xpath_map_[XpathKey.INPUT_TARGET_TO], to.ToString(date_format));
  73. // CSV吐き出しボタンをクリックし、CSVをダウンロード
  74. wait_.Until(ExpectedConditions.ElementToBeClickable(By.XPath(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV])));
  75. Click(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV]);
  76. WaitForDownload(filename_credit_);
  77. // QR決済結果結果一覧画面に移行
  78. Click(xpath_map_[XpathKey.BUTTON_MENU]);
  79. Click(xpath_map_[XpathKey.BUTTON_KESSAI_KANRI]);
  80. Click(xpath_map_[XpathKey.BUTTON_QR_KESSAI]);
  81. // 承認処理日のFrom-Toを入力
  82. Send(xpath_map_[XpathKey.INPUT_TARGET_FROM], from.ToString(date_format));
  83. Send(xpath_map_[XpathKey.INPUT_TARGET_TO], to.ToString(date_format));
  84. // CSV吐き出しボタンをクリックし、CSVをダウンロード
  85. wait_.Until(ExpectedConditions.ElementToBeClickable(By.XPath(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV])));
  86. Click(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV]);
  87. WaitForDownload(filename_qr_);
  88. // 電子マネー決済結果結果一覧画面に移行
  89. Click(xpath_map_[XpathKey.BUTTON_MENU]);
  90. //Click(xpath_map_[XpathKey.BUTTON_KESSAI_KANRI]); なぜかタブがひらいたままなので、これはスキップする。
  91. Click(xpath_map_[XpathKey.BUTTON_ELECTORONIC_MONEY_KESSAI]);
  92. // 承認処理日のFrom-Toを入力
  93. Send(xpath_map_[XpathKey.INPUT_TARGET_FROM], from.ToString(date_format));
  94. Send(xpath_map_[XpathKey.INPUT_TARGET_TO], to.ToString(date_format));
  95. // CSV吐き出しボタンをクリックし、CSVをダウンロード
  96. wait_.Until(ExpectedConditions.ElementToBeClickable(By.XPath(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV])));
  97. Click(xpath_map_[XpathKey.BUTTON_DOWNLOAD_CSV]);
  98. WaitForDownload(filename_electronic_money_);
  99. return ResultCode.OK;
  100. }
  101. public override ResultCode Logout() {
  102. driver_.Navigate().GoToUrl(home_url_);
  103. Click(xpath_map_[XpathKey.BUTTON_MENU]);
  104. Click(xpath_map_[XpathKey.BUTTON_LOGOUT]);
  105. wait_.Until(ExpectedConditions.ElementIsVisible(By.XPath(xpath_map_[XpathKey.INPUT_LOGIN_USERNAME])));
  106. return ResultCode.OK;
  107. }
  108. public override List<CreditCSVData> GetCreditCSVDataList() {
  109. var result_list = new List<CreditCSVData>();
  110. var list = GetFileContents(filename_credit_);
  111. bool failed_flg = false;
  112. var failed_parking_name_hs_table = new HashSet<String>();
  113. foreach (var line in list) {
  114. var parking_name = line[(int)CreditCSVDataDaito.ColName.ParkingName];
  115. try {
  116. result_list.Add(new CreditCSVDataDaito() {
  117. spot_id = GetSpotID(parking_name),
  118. parking_name = parking_name,
  119. developer = line[(int)CreditCSVDataDaito.ColName.Developer],
  120. adjust_type = line[(int)CreditCSVDataDaito.ColName.AdjustType],
  121. fee_type = line[(int)CreditCSVDataDaito.ColName.FeeType],
  122. reception_datetime = DateTime.Parse(line[(int)CreditCSVDataDaito.ColName.ReceptionDatetime]),
  123. amount = int.Parse(line[(int)CreditCSVDataDaito.ColName.Amount]),
  124. reception_no = line[(int)CreditCSVDataDaito.ColName.ReceptionNo],
  125. response_no = line[(int)CreditCSVDataDaito.ColName.ResponseNo],
  126. error_code1 = line[(int)CreditCSVDataDaito.ColName.ErrorCode1],
  127. error_code2 = line[(int)CreditCSVDataDaito.ColName.ErrorCode2]
  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. var error_list = new List<Store.ParkingCreditCardAgenciesSpotIDError>();
  137. logger_.Error("失敗駐車場名");
  138. foreach (var ele in failed_parking_name_hs_table) {
  139. error_list.Add(new Store.ParkingCreditCardAgenciesSpotIDError() {
  140. creditcard_agencies_id = CreditAgenciesMap.GetID(agent_),
  141. creditcard_agencies_spot_name = ele
  142. });
  143. logger_.Error($"駐車場 \"{ele}\"");
  144. }
  145. throw new SpotNameNotMatchException(error_list);
  146. }
  147. return result_list;
  148. }
  149. public override List<QRCSVData> GetQRCSVDataList() {
  150. var result_list = new List<QRCSVData>();
  151. var list = GetFileContents(filename_qr_);
  152. bool failed_flg = false;
  153. var failed_parking_name_hs_table = new HashSet<String>();
  154. foreach (var line in list) {
  155. var parking_name = line[(int)QRCSVDataDaito.ColName.ParkingName];
  156. try {
  157. result_list.Add(new QRCSVDataDaito() {
  158. spot_id = GetSpotID(parking_name),
  159. parking_name = parking_name,
  160. developer = line[(int)QRCSVDataDaito.ColName.Developer],
  161. reception_datetime = DateTime.Parse(line[(int)QRCSVDataDaito.ColName.ReceptionDatetime]),
  162. company = line[(int)QRCSVDataDaito.ColName.Company],
  163. amount = int.Parse(line[(int)QRCSVDataDaito.ColName.Amount]),
  164. adjust_no = line[(int)QRCSVDataDaito.ColName.AdjustNo],
  165. error_code = line[(int)QRCSVDataDaito.ColName.ErrorCode],
  166. });
  167. } catch (ArgumentException) {
  168. failed_parking_name_hs_table.Add(parking_name);
  169. failed_flg = true;
  170. continue;
  171. }
  172. }
  173. if (failed_flg) {
  174. var error_list = new List<Store.ParkingCreditCardAgenciesSpotIDError>();
  175. logger_.Error("失敗駐車場名");
  176. foreach (var ele in failed_parking_name_hs_table) {
  177. error_list.Add(new Store.ParkingCreditCardAgenciesSpotIDError() {
  178. creditcard_agencies_id = CreditAgenciesMap.GetID(agent_),
  179. creditcard_agencies_spot_name = ele
  180. });
  181. logger_.Error($"駐車場 \"{ele}\"");
  182. }
  183. throw new SpotNameNotMatchException(error_list);
  184. }
  185. return result_list;
  186. }
  187. public override List<ElectronicMoneyCSVData> GetElectronicMoneyCSVDataList() {
  188. var result_list = new List<ElectronicMoneyCSVData>();
  189. var list = GetFileContents(filename_electronic_money_);
  190. bool failed_flg = false;
  191. var failed_parking_name_hs_table = new HashSet<String>();
  192. foreach (var line in list) {
  193. var parking_name = line[(int)QRCSVDataDaito.ColName.ParkingName];
  194. try {
  195. result_list.Add(new DaitoElectronicMoneyData() {
  196. spot_id = GetSpotID(parking_name),
  197. parking_name = parking_name,
  198. reception_datetime = DateTime.Parse(line[(int)DaitoElectronicMoneyData.ColName.ReceptionDatetime]),
  199. adjust_type = line[(int)DaitoElectronicMoneyData.ColName.AdjustType],
  200. amount = int.Parse(line[(int)DaitoElectronicMoneyData.ColName.Amount]),
  201. trade_id = line[(int)DaitoElectronicMoneyData.ColName.TradeID],
  202. adjust_result = line[(int)DaitoElectronicMoneyData.ColName.AdjustResult],
  203. });
  204. } catch (ArgumentException) {
  205. failed_parking_name_hs_table.Add(parking_name);
  206. failed_flg = true;
  207. continue;
  208. }
  209. }
  210. if (failed_flg) {
  211. var error_list = new List<Store.ParkingCreditCardAgenciesSpotIDError>();
  212. logger_.Error("失敗駐車場名");
  213. foreach (var ele in failed_parking_name_hs_table) {
  214. error_list.Add(new Store.ParkingCreditCardAgenciesSpotIDError() {
  215. creditcard_agencies_id = CreditAgenciesMap.GetID(agent_),
  216. creditcard_agencies_spot_name = ele
  217. });
  218. logger_.Error($"駐車場 \"{ele}\"");
  219. }
  220. throw new SpotNameNotMatchException(error_list);
  221. }
  222. return result_list;
  223. }
  224. private List<String[]> GetFileContents(String filename) {
  225. var list = new List<String[]>();
  226. var dir = new DirectoryInfo(DriverFactory.GetDownloadDir());
  227. var config = new CSVConfig() {
  228. header = true,
  229. };
  230. config.remove_char.Add("\"");
  231. var sjis_enc = Encoding.GetEncoding("Shift-JIS");
  232. foreach (var file in dir.GetFiles()) {
  233. if (file.Name.Contains(filename)) {
  234. var result = ReadCsv(file.FullName, config, sjis_enc);
  235. list.AddRange(result);
  236. }
  237. }
  238. return list;
  239. }
  240. }
  241. }