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

346 行
13KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Threading;
  6. using System.Text;
  7. using OpenQA.Selenium;
  8. using OpenQA.Selenium.Chrome;
  9. using OpenQA.Selenium.Support.UI;
  10. using CSVDownloader.Code;
  11. using CSVDownloader.Store.ElectronicMoneyDataStore;
  12. using CSVDownloader.Store.CreditCSVData;
  13. using CSVDownloader.Exceptions;
  14. using ExpectedConditions = OpenQA.Selenium.Support.UI.ExpectedConditions;
  15. using System.Globalization;
  16. namespace CSVDownloader.Web {
  17. class GMOController : WebController {
  18. private readonly String url_ = "https://webinq.gmo-fg.net/KessaisyoukaiGUI/#no-back";
  19. private readonly String filename_zip = "gmo.zip";
  20. private readonly String filename_credit = "gmo_credit.csv";
  21. private readonly String filename_electronic_money = "gmo_electronic_money.csv";
  22. private enum XpathKey {
  23. INPUT_LOGIN_CUSTOMER_CODE,
  24. INPUT_LOGIN_USERNAME,
  25. INPUT_LOGIN_PASSWORD,
  26. BUTTON_LOGIN,
  27. FRAME_MAIN,
  28. LOADING,
  29. PROGRESS,
  30. INPUT_FROM_DATE,
  31. INPUT_TO_DATE,
  32. BUTTON_SEARCH,
  33. BUTTON_DOWNLOAD_ZIP,
  34. BUTTON_LOGOUT,
  35. };
  36. private IDictionary<XpathKey, string> xpath_map_ = new Dictionary<XpathKey, string>()
  37. {
  38. {XpathKey.INPUT_LOGIN_CUSTOMER_CODE,@"//*[@id='companyCode']"},
  39. {XpathKey.INPUT_LOGIN_USERNAME,@"//*[@id='userName']"},
  40. {XpathKey.INPUT_LOGIN_PASSWORD,@"//*[@id='password']"},
  41. {XpathKey.BUTTON_LOGIN,@"//*[@id='loginForm']//input[@type='submit']"},
  42. {XpathKey.FRAME_MAIN,@"//*[@id='main']"},
  43. {XpathKey.INPUT_FROM_DATE,@"//*[@id='_searchDateFrom']"},
  44. {XpathKey.INPUT_TO_DATE,@"//*[@id='_searchDateTo']"},
  45. {XpathKey.BUTTON_SEARCH,$"//*[@id='Content']//input[@value='検索']" },
  46. {XpathKey.BUTTON_DOWNLOAD_ZIP,$"//*[@id='Content']//input[@value='実売上取引出力']" },
  47. {XpathKey.BUTTON_LOGOUT,@"//a[@href='javascript:logout();']" },
  48. {XpathKey.LOADING,@"//*[@id='loadingFont']"},
  49. {XpathKey.PROGRESS,@"//*[@id='progress']"},
  50. };
  51. public GMOController(ChromeDriver driver) : base(driver) {
  52. agent_ = CreditAgent.HT;
  53. }
  54. public override ResultCode Login(LoginInfo info) {
  55. try {
  56. driver_.Navigate().GoToUrl(url_);
  57. Send(xpath_map_[XpathKey.INPUT_LOGIN_CUSTOMER_CODE], info.company_name);
  58. Send(xpath_map_[XpathKey.INPUT_LOGIN_USERNAME], info.user_name);
  59. Send(xpath_map_[XpathKey.INPUT_LOGIN_PASSWORD], info.password);
  60. Click(xpath_map_[XpathKey.BUTTON_LOGIN]);
  61. CloseAlert();
  62. // ログイン後の「端末決済データダウンロード」が表示されることを確認する。
  63. SwitchToMainFrame();
  64. wait_.Until(ExpectedConditions.ElementToBeClickable(By.XPath(xpath_map_[XpathKey.BUTTON_DOWNLOAD_ZIP])));
  65. SwitchToFrame();
  66. } catch (Exception e) {
  67. logger_.Error(e.Message);
  68. return ResultCode.NG;
  69. }
  70. return ResultCode.OK;
  71. }
  72. public override ResultCode Download(DateTime from, DateTime to) {
  73. SwitchToMainFrame();
  74. // 承認処理日のFrom-Toを入力
  75. Clear(xpath_map_[XpathKey.INPUT_FROM_DATE]);
  76. Clear(xpath_map_[XpathKey.INPUT_TO_DATE]);
  77. Send(xpath_map_[XpathKey.INPUT_FROM_DATE], from.ToString("yyyy/MM/dd"));
  78. Send(xpath_map_[XpathKey.INPUT_TO_DATE], to.ToString("yyyy/MM/dd"));
  79. // ダウンロード開始
  80. Click(xpath_map_[XpathKey.BUTTON_SEARCH]);
  81. wait_.Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath(xpath_map_[XpathKey.LOADING])));
  82. Click(xpath_map_[XpathKey.BUTTON_DOWNLOAD_ZIP]);
  83. CloseAlert();
  84. var long_wait = new WebDriverWait(driver_, TimeSpan.FromSeconds(360));
  85. long_wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath(xpath_map_[XpathKey.PROGRESS])));
  86. WaitForDownload(filename_zip);
  87. // 解凍
  88. ArchiveExtract();
  89. SwitchToFrame();
  90. return ResultCode.OK;
  91. }
  92. public override ResultCode Logout() {
  93. try {
  94. Click(xpath_map_[XpathKey.BUTTON_LOGOUT]);
  95. } catch (Exception e) {
  96. logger_.Error(e.Message);
  97. return ResultCode.NG;
  98. }
  99. return ResultCode.OK;
  100. }
  101. public override List<CreditCSVData> GetCreditCSVDataList() {
  102. var result_list = new List<CreditCSVData>();
  103. var list = GetFileContents(filename_credit);
  104. bool failed_flg = false;
  105. var failed_parking_name_hs_table = new HashSet<String>();
  106. foreach (var line in list) {
  107. var parking_name = line[(int)CreditCSVDataGMO.ColName.ShopName];
  108. try {
  109. int amount = 0;
  110. int.TryParse(line[(int)CreditCSVDataGMO.ColName.Amount], out amount);
  111. int tax_send_amount = 0;
  112. int.TryParse(line[(int)CreditCSVDataGMO.ColName.TaxSendAmount], out tax_send_amount);
  113. DateTime processing_date;
  114. DateTime.TryParseExact(line[(int)CreditCSVDataGMO.ColName.ProcessingDate], "yyMMdd", null, DateTimeStyles.None, out processing_date);
  115. result_list.Add(new CreditCSVDataGMO() {
  116. spot_id = GetSpotID(parking_name),
  117. customer_code = line[(int)CreditCSVDataGMO.ColName.CustomerCode],
  118. output_datetime = line[(int)CreditCSVDataGMO.ColName.OutputDatetime],
  119. processing_code = line[(int)CreditCSVDataGMO.ColName.ProcessingCode],
  120. operation_code = line[(int)CreditCSVDataGMO.ColName.OperationCode],
  121. return_code = line[(int)CreditCSVDataGMO.ColName.ReturnCode],
  122. device_no = line[(int)CreditCSVDataGMO.ColName.DeviceNo],
  123. target_company_code = line[(int)CreditCSVDataGMO.ColName.TargetCompanyCode],
  124. target_company_sub_code = line[(int)CreditCSVDataGMO.ColName.TargetCompanySubCode],
  125. device_trade_no = line[(int)CreditCSVDataGMO.ColName.DeviceTradeNo],
  126. processing_date = processing_date,
  127. approval_no = line[(int)CreditCSVDataGMO.ColName.ApprovalNo],
  128. amount = amount,
  129. tax_send_amount = tax_send_amount,
  130. customer_name = line[(int)CreditCSVDataGMO.ColName.CustomerName],
  131. shop_name = line[(int)CreditCSVDataGMO.ColName.ShopName],
  132. cancel_receipt_no = line[(int)CreditCSVDataGMO.ColName.CancelReceiptNo],
  133. split_count_info = line[(int)CreditCSVDataGMO.ColName.SplitCountInfo],
  134. paying_type = line[(int)CreditCSVDataGMO.ColName.PayingType],
  135. });
  136. } catch (ArgumentException) {
  137. failed_parking_name_hs_table.Add(parking_name);
  138. failed_flg = true;
  139. continue;
  140. }
  141. }
  142. if (failed_flg) {
  143. var error_list = new List<Store.ParkingCreditCardAgenciesSpotIDError>();
  144. logger_.Error("失敗駐車場名");
  145. foreach (var ele in failed_parking_name_hs_table) {
  146. error_list.Add(new Store.ParkingCreditCardAgenciesSpotIDError() {
  147. creditcard_agencies_id = CreditAgenciesMap.GetID(agent_),
  148. creditcard_agencies_spot_name = ele
  149. });
  150. logger_.Error($"駐車場 \"{ele}\"");
  151. }
  152. throw new SpotNameNotMatchException(error_list);
  153. }
  154. return result_list;
  155. }
  156. public override List<ElectronicMoneyCSVData> GetElectronicMoneyCSVDataList() {
  157. var result_list = new List<ElectronicMoneyCSVData>();
  158. var list = GetFileContents(filename_electronic_money);
  159. bool failed_flg = false;
  160. var failed_parking_name_hs_table = new HashSet<String>();
  161. foreach (var line in list) {
  162. var parking_name = line[(int)GMOElectronicMoneyData.ColName.ShopName];
  163. try {
  164. int amount = 0;
  165. int.TryParse(line[(int)GMOElectronicMoneyData.ColName.Amount], out amount);
  166. int auto_charge_amount = 0;
  167. int.TryParse(line[(int)GMOElectronicMoneyData.ColName.AutoChargeAmount], out auto_charge_amount);
  168. int before_deposit = 0;
  169. int.TryParse(line[(int)GMOElectronicMoneyData.ColName.BeforeDeposit], out before_deposit);
  170. //DateTime adjust_datetime;
  171. //DateTime.TryParseExact(line[(int)GMOElectronicMoneyData.ColName.AdjustDatetime], "yyMMdd", null, DateTimeStyles.None, out adjust_datetime);
  172. result_list.Add(new GMOElectronicMoneyData() {
  173. spot_id = GetSpotID(parking_name),
  174. customer_code = line[(int)GMOElectronicMoneyData.ColName.CustomerCode],
  175. acquirer_id = line[(int)GMOElectronicMoneyData.ColName.AcquirerID],
  176. acquirer_name = line[(int)GMOElectronicMoneyData.ColName.AcquirerName],
  177. customer_id = line[(int)GMOElectronicMoneyData.ColName.CustomerID],
  178. customer_name = line[(int)GMOElectronicMoneyData.ColName.CustomerName],
  179. shop_name = line[(int)GMOElectronicMoneyData.ColName.ShopName],
  180. device_id = line[(int)GMOElectronicMoneyData.ColName.DeviceID],
  181. host_device_id = line[(int)GMOElectronicMoneyData.ColName.HostDeviceID],
  182. device_no = line[(int)GMOElectronicMoneyData.ColName.DeviceNo],
  183. adjust_datetime = DateTime.Parse(line[(int)GMOElectronicMoneyData.ColName.AdjustDatetime]),
  184. trade_code = line[(int)GMOElectronicMoneyData.ColName.TradeCode],
  185. amount = amount,
  186. auto_charge_amount = auto_charge_amount,
  187. before_deposit = before_deposit,
  188. brand = line[(int)GMOElectronicMoneyData.ColName.Brand],
  189. card_id = line[(int)GMOElectronicMoneyData.ColName.CardID],
  190. trade_status = line[(int)GMOElectronicMoneyData.ColName.TradeStatus],
  191. last_proccesing_datetime = line[(int)GMOElectronicMoneyData.ColName.LastProccesingDatetime],
  192. point = line[(int)GMOElectronicMoneyData.ColName.Point],
  193. trade_no = line[(int)GMOElectronicMoneyData.ColName.TradeNo],
  194. origin_trade_no = line[(int)GMOElectronicMoneyData.ColName.OriginTradeNo],
  195. });
  196. } catch (ArgumentException) {
  197. failed_parking_name_hs_table.Add(parking_name);
  198. failed_flg = true;
  199. continue;
  200. }
  201. }
  202. if (failed_flg) {
  203. var error_list = new List<Store.ParkingCreditCardAgenciesSpotIDError>();
  204. logger_.Error("失敗駐車場名");
  205. foreach (var ele in failed_parking_name_hs_table) {
  206. error_list.Add(new Store.ParkingCreditCardAgenciesSpotIDError() {
  207. creditcard_agencies_id = CreditAgenciesMap.GetID(agent_),
  208. creditcard_agencies_spot_name = ele
  209. });
  210. logger_.Error($"駐車場 \"{ele}\"");
  211. }
  212. throw new SpotNameNotMatchException(error_list);
  213. }
  214. return result_list;
  215. }
  216. private List<String[]> GetFileContents(String filename) {
  217. var list = new List<String[]>();
  218. var dir = new DirectoryInfo(DriverFactory.GetDownloadDir());
  219. var config = new CSVConfig() {
  220. header = true,
  221. };
  222. config.remove_char.Add("\"");
  223. var sjis_enc = Encoding.GetEncoding("Shift-JIS");
  224. foreach (var file in dir.GetFiles()) {
  225. if (file.Name.Contains(filename)) {
  226. var result = ReadCsv(file.FullName, config, sjis_enc);
  227. list.AddRange(result);
  228. }
  229. }
  230. return list;
  231. }
  232. private void ArchiveExtract() {
  233. using (ZipArchive archive = ZipFile.OpenRead(Path.Combine(DriverFactory.GetDownloadDir(), filename_zip))) {
  234. foreach (ZipArchiveEntry entry in archive.Entries) {
  235. if (entry.FullName.Contains("FEP")) {
  236. entry.ExtractToFile(Path.Combine(DriverFactory.GetDownloadDir(), filename_credit));
  237. }
  238. if (entry.FullName.Contains("DEN")) {
  239. entry.ExtractToFile(Path.Combine(DriverFactory.GetDownloadDir(), filename_electronic_money));
  240. }
  241. }
  242. }
  243. }
  244. private void SwitchToMainFrame() {
  245. SwitchToFrame(xpath_map_[XpathKey.FRAME_MAIN]);
  246. }
  247. }
  248. }