IPEasyUtility.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. using System;
  2. using System.Data;
  3. using System.Dynamic;
  4. using System.Linq;
  5. using OpenQA.Selenium;
  6. namespace wispro.sp.utility
  7. {
  8. public class IPEasyUtility
  9. {
  10. public static void DownloadReport(string strId,string filename)
  11. {
  12. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  13. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  14. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  15. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  16. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  17. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  18. options.AddUserProfilePreference("intl.accept_languages", "nl");
  19. options.AddUserProfilePreference("disable-popup-blocking", "true");
  20. if (isheadless)
  21. {
  22. options.AddArgument("headless");
  23. }
  24. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
  25. {
  26. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
  27. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(500);
  28. //进入登录界面
  29. driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
  30. //输入用户名和密码
  31. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  32. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  33. //点击登录按钮
  34. driver.FindElement(By.Id("btnLogin")).Click();
  35. //关闭提示遮罩层
  36. driver.FindElement(By.Id("jpwClose")).Click();
  37. //点击顶部菜单栏中的报表管理菜单
  38. driver.FindElement(By.Name("970d33d5-c728-41b8-a060-4330610706b9")).Click();
  39. //点击左侧 自定义报表 菜单
  40. driver.FindElement(By.Name("642fa96f-1e1f-46fd-aaa4-cb461ee8df5b")).Click();
  41. //切换到自定义报表Frame
  42. driver.SwitchTo().Frame(1);
  43. //调用报表导出JS
  44. ((IJavaScriptExecutor)driver).ExecuteScript($"Report.Export('{strId}');");
  45. //切换到弹出的导出报表窗口,点击导出按钮
  46. driver.SwitchTo().DefaultContent();
  47. var ihg_export = driver.FindElement(By.Name("ihg_export"));
  48. driver.SwitchTo().Frame(ihg_export);
  49. driver.FindElement(By.Id("btnSubmit")).Click();
  50. string strFilePath = System.IO.Path.Combine(strFileSavePath, filename);
  51. while (true)
  52. {
  53. System.IO.FileInfo file = new System.IO.FileInfo(strFilePath);
  54. if (!file.Exists || file.Length == 0) {
  55. System.Threading.Thread.Sleep(5000);
  56. }
  57. else
  58. {
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// 根据报表名称导出报表
  66. /// </summary>
  67. /// <param name="ReportName">报告名称</param>
  68. /// <param name="isModifyDate">是否手动修改日期范围</param>
  69. public static void DownloadReport(string ReportName,bool isModifyDate)
  70. {
  71. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  72. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  73. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  74. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  75. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  76. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  77. options.AddUserProfilePreference("intl.accept_languages", "nl");
  78. options.AddUserProfilePreference("disable-popup-blocking", "true");
  79. if (isheadless)
  80. {
  81. options.AddArgument("headless");
  82. }
  83. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
  84. {
  85. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
  86. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(500);
  87. //进入登录界面
  88. driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
  89. //输入用户名和密码
  90. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  91. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  92. //点击登录按钮
  93. driver.FindElement(By.Id("btnLogin")).Click();
  94. //关闭提示遮罩层
  95. driver.FindElement(By.Id("jpwClose")).Click();
  96. //点击顶部菜单栏中的报表管理菜单
  97. driver.FindElement(By.Name("970d33d5-c728-41b8-a060-4330610706b9")).Click();
  98. //点击左侧 自定义报表 菜单
  99. driver.FindElement(By.Name("642fa96f-1e1f-46fd-aaa4-cb461ee8df5b")).Click();
  100. //切换到自定义报表Frame
  101. driver.SwitchTo().Frame(1);
  102. var inputSearch = driver.FindElement(By.Id("customizedList_TxtSheetSearchKey"));
  103. inputSearch.SendKeys(ReportName);
  104. var btnSearch = driver.FindElement(By.ClassName("btn-search"));
  105. btnSearch.Click();
  106. var reportRecord = driver.FindElement(By.XPath($"//td[contains(text(),'{ReportName}')]"));
  107. reportRecord.Click();
  108. System.Threading.Thread.Sleep(1000);
  109. if (isModifyDate)
  110. {
  111. var btnEdit = driver.FindElement(By.ClassName("edit"));
  112. btnEdit.Click();
  113. System.Threading.Thread.Sleep(500);
  114. var doItemFinished = driver.FindElement(By.XPath("//span[contains(@objcvalue,'_date') and contains(@id,'ST;')]"));
  115. doItemFinished.Click();
  116. System.Threading.Thread.Sleep(500);
  117. var startDate = driver.FindElement(By.XPath("//input[contains(@id,'dt_s_')]"));
  118. startDate.Click();
  119. System.Threading.Thread.Sleep(500);
  120. startDate.SendKeys(new DateTime(DateTime.Now.AddMonths(-1).Year, DateTime.Now.AddMonths(-1).Month, 1).ToString("yyyy-MM-dd"));
  121. var endDate = driver.FindElement(By.XPath("//input[contains(@id,'dt_e_')]"));//.Id("dt_e_4F8FE88D-9040-45F1-9723-45699BCD4CAF"));
  122. endDate.Click();
  123. System.Threading.Thread.Sleep(500);
  124. endDate.SendKeys(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1).ToString("yyyy-MM-dd"));
  125. var addDate = driver.FindElement(By.XPath("//a[contains(@onclick,'ReportSub.ConditionDTSelect')]"));
  126. addDate.Click();
  127. System.Threading.Thread.Sleep(500);
  128. var btnSave = driver.FindElement(By.Id("save"));
  129. btnSave.Click();
  130. System.Threading.Thread.Sleep(3000);
  131. }
  132. var linkExport = driver.FindElement(By.XPath("//a[contains(@onclick,'-') and @class='tbexcel']"));
  133. linkExport.Click();
  134. //切换到弹出的导出报表窗口,点击导出按钮
  135. driver.SwitchTo().DefaultContent();
  136. var ihg_export = driver.FindElement(By.Name("ihg_export"));
  137. driver.SwitchTo().Frame(ihg_export);
  138. driver.FindElement(By.Id("btnSubmit")).Click();
  139. string strFilePath = System.IO.Path.Combine(strFileSavePath, $"{ReportName.Trim()}.xlsx");
  140. int iwaiting = 0;
  141. while (true)
  142. {
  143. System.IO.FileInfo file = new System.IO.FileInfo(strFilePath.Replace("~","_"));
  144. if (!file.Exists || file.Length == 0)
  145. {
  146. System.Threading.Thread.Sleep(5000);
  147. iwaiting += 5000;
  148. if(iwaiting > 360000)
  149. {
  150. break;
  151. }
  152. }
  153. else
  154. {
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// 获取案件基本信息
  162. /// </summary>
  163. /// <param name="caseNo">我方文号</param>
  164. /// <returns></returns>
  165. public static dynamic GetCaseInfo(string caseNo)
  166. {
  167. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  168. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  169. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  170. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  171. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  172. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  173. options.AddUserProfilePreference("intl.accept_languages", "nl");
  174. options.AddUserProfilePreference("disable-popup-blocking", "true");
  175. options.AddUserProfilePreference("profile", new { default_content_setting_values = new { images = 2 } });
  176. if (isheadless)
  177. {
  178. options.AddArgument("headless");
  179. }
  180. dynamic retObject = new ExpandoObject();
  181. retObject.CaseNo = caseNo;
  182. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
  183. {
  184. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
  185. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(100);
  186. //进入登录界面
  187. driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
  188. //输入用户名和密码
  189. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  190. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  191. //点击登录按钮
  192. driver.FindElement(By.Id("btnLogin")).Click();
  193. //关闭提示遮罩层
  194. driver.FindElement(By.Id("jpwClose")).Click();
  195. //点击顶部菜单栏中的案件管理菜单
  196. driver.FindElement(By.Name("71A7CC35-F597-40E1-9FEF-BE622A3A3B63")).Click();
  197. //点击左侧 查询 菜单
  198. driver.FindElement(By.Name("c3266ab3-521a-4815-8aaf-7dd0bc5a76af")).Click();
  199. //切换到自定义报表Frame
  200. driver.SwitchTo().Frame(1);
  201. var inputSearch = driver.FindElement(By.Id("case_volume"));
  202. inputSearch.SendKeys(caseNo);
  203. var btnSearch = driver.FindElement(By.Id("btn_Search"));
  204. btnSearch.Click();
  205. var caseLink = driver.FindElement(By.XPath($"//a[contains(text(),'{caseNo}')]"));
  206. caseLink.Click();
  207. System.Threading.Thread.Sleep(500);
  208. driver.SwitchTo().ParentFrame().SwitchTo().Frame(2);
  209. driver.FindElement(By.Id("libase")).Click();
  210. retObject.CaseName = driver.FindElement(By.Id("p_case_info__case_name")).GetAttribute("value"); //案件名称
  211. retObject.CustomerName = driver.FindElement(By.Id("p_case_info__customer_id")).GetAttribute("value"); //客户名称
  212. retObject.BusinessType = driver.FindElement(By.Id("p_case_info__business_type_id")).GetAttribute("value"); //业务类型
  213. retObject.CaseState = GetSelectText(driver.FindElement(By.Id("p_case_info__case_status_id")));
  214. retObject.ApplicationType = GetSelectText(driver.FindElement(By.Id("p_case_info__apply_type_id")));
  215. retObject.CaseType = driver.FindElement(By.Id("p_case_info__case_type_id")).GetAttribute("value"); //案件类型
  216. retObject.EntrustingDate = driver.FindElement(By.Id("p_case_info__charge_date")).GetAttribute("value"); //委案日期
  217. retObject.CaseMemo = driver.FindElement(By.Id("p_case_info__remark")).GetAttribute("value"); //案件备注
  218. driver.FindElement(By.XPath($"//span[contains(text(),'扩展信息')]")).Click();
  219. //select[@id='p_case_info__case_coefficient_id']
  220. retObject.CaseCoefficient = GetSelectText(driver.FindElement(By.Id("p_case_info__case_coefficient_id")));
  221. }
  222. return retObject;
  223. }
  224. /// <summary>
  225. /// 获取案件处理事项记录
  226. /// </summary>
  227. /// <param name="caseNo">我方文号</param>
  228. /// <param name="doItemName">处理事项</param>
  229. /// <returns></returns>
  230. public static dynamic GetPerformanceRecord(string caseNo,string doItemName,string caseStage=null)
  231. {
  232. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  233. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  234. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  235. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  236. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  237. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  238. options.AddUserProfilePreference("intl.accept_languages", "nl");
  239. options.AddUserProfilePreference("disable-popup-blocking", "true");
  240. options.AddUserProfilePreference("profile", new { default_content_setting_values = new { images = 2 } });
  241. if (isheadless)
  242. {
  243. options.AddArgument("headless");
  244. }
  245. dynamic retObject = new ExpandoObject();
  246. retObject.CaseNo = caseNo;
  247. retObject.DoItem = doItemName;
  248. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
  249. {
  250. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
  251. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(100);
  252. //进入登录界面
  253. driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
  254. //输入用户名和密码
  255. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  256. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  257. //点击登录按钮
  258. driver.FindElement(By.Id("btnLogin")).Click();
  259. //关闭提示遮罩层
  260. driver.FindElement(By.Id("jpwClose")).Click();
  261. //点击顶部菜单栏中的案件管理菜单
  262. driver.FindElement(By.Name("71A7CC35-F597-40E1-9FEF-BE622A3A3B63")).Click();
  263. System.Threading.Thread.Sleep(500);
  264. //点击左侧 查询 菜单
  265. driver.FindElement(By.Name("c3266ab3-521a-4815-8aaf-7dd0bc5a76af")).Click();
  266. System.Threading.Thread.Sleep(500);
  267. //切换到自定义报表Frame
  268. driver.SwitchTo().Frame(1);
  269. var inputSearch = driver.FindElement(By.Id("case_volume"));
  270. inputSearch.SendKeys(caseNo);
  271. var btnSearch = driver.FindElement(By.Id("btn_Search"));
  272. btnSearch.Click();
  273. System.Threading.Thread.Sleep(500);
  274. var caseLink = driver.FindElement(By.XPath($"//a[normalize-space()='{caseNo}']"));
  275. caseLink.Click();
  276. System.Threading.Thread.Sleep(500);
  277. driver.SwitchTo().ParentFrame().SwitchTo().Frame(2);
  278. var DoItemLinks = driver.FindElements(By.XPath($"//td[@colname='ctrl_proc'][normalize-space()='{doItemName}']"));
  279. if (DoItemLinks.Count > 0)
  280. {
  281. if (!string.IsNullOrEmpty(caseStage))
  282. {
  283. foreach(var DoItemLink in DoItemLinks)
  284. {
  285. var temCaseStage = DoItemLink.FindElement(By.XPath("following-sibling::td[1]")).Text;
  286. if(temCaseStage == caseStage)
  287. {
  288. retObject.CaseStage = DoItemLink.FindElement(By.XPath("following-sibling::td[1]")).Text; //案件阶段
  289. retObject.DoItemState = DoItemLink.FindElement(By.XPath("following-sibling::td[2]")).Text; //处理事项处理状态
  290. retObject.InternalDate = DoItemLink.FindElement(By.XPath("following-sibling::td[4]")).Text; //内部期限
  291. retObject.CustomerLimitDate = DoItemLink.FindElement(By.XPath("following-sibling::td[5]")).Text; //客户期限
  292. retObject.FinishedDate = DoItemLink.FindElement(By.XPath("following-sibling::td[7]")).Text; //处理事项完成日
  293. retObject.DoPersons = DoItemLink.FindElement(By.XPath("following-sibling::td[8]")).Text; //处理人
  294. retObject.DoItemMemo = DoItemLink.FindElement(By.XPath("following-sibling::td[9]")).Text; //处理事项备注
  295. DoItemLink.Click();
  296. break;
  297. }
  298. }
  299. }
  300. else
  301. {
  302. var DoItemLink = DoItemLinks[DoItemLinks.Count - 1];
  303. retObject.CaseStage = DoItemLink.FindElement(By.XPath("following-sibling::td[1]")).Text; //案件阶段
  304. retObject.DoItemState = DoItemLink.FindElement(By.XPath("following-sibling::td[2]")).Text; //处理事项处理状态
  305. retObject.InternalDate = DoItemLink.FindElement(By.XPath("following-sibling::td[4]")).Text; //内部期限
  306. retObject.CustomerLimitDate = DoItemLink.FindElement(By.XPath("following-sibling::td[5]")).Text; //客户期限
  307. retObject.FinishedDate = DoItemLink.FindElement(By.XPath("following-sibling::td[7]")).Text; //处理事项完成日
  308. retObject.DoPersons = DoItemLink.FindElement(By.XPath("following-sibling::td[8]")).Text; //处理人
  309. retObject.DoItemMemo = DoItemLink.FindElement(By.XPath("following-sibling::td[9]")).Text; //处理事项备注
  310. DoItemLink.Click();
  311. }
  312. }
  313. System.Threading.Thread.Sleep(500);
  314. retObject.FinalizationDate = driver.FindElement(By.Id("p_proc_info__finish_doc_date")).GetAttribute("value"); //定稿日
  315. retObject.ReturnDate = driver.FindElement(By.Id("p_proc_info__back_date")).GetAttribute("value"); //返稿日
  316. retObject.Reviewer = driver.FindElement(By.Id("p_proc_info__revise_user")).GetAttribute("value"); //核稿人
  317. retObject.DoItemCoefficient = driver.FindElement(By.Id("p_proc_info__proc_coefficient")).GetAttribute("value"); //处理事项系数
  318. retObject.WordCount = driver.FindElement(By.Id("p_proc_info__translate_count")).GetAttribute("value"); //翻译字数
  319. driver.FindElement(By.Id("libase")).Click();
  320. System.Threading.Thread.Sleep(500);
  321. retObject.CaseName = driver.FindElement(By.Id("p_case_info__case_name")).GetAttribute("value"); //案件名称
  322. retObject.CustomerName = driver.FindElement(By.Id("p_case_info__customer_id")).GetAttribute("value"); //客户名称
  323. retObject.BusinessType = driver.FindElement(By.Id("p_case_info__business_type_id")).GetAttribute("value"); //业务类型
  324. retObject.CaseState = GetSelectText(driver.FindElement(By.Id("p_case_info__case_status_id")));
  325. retObject.ApplicationType = GetSelectText(driver.FindElement(By.Id("p_case_info__apply_type_id")));
  326. retObject.CaseType = driver.FindElement(By.Id("p_case_info__case_type_id")).GetAttribute("value"); //案件类型
  327. retObject.EntrustingDate = driver.FindElement(By.Id("p_case_info__charge_date")).GetAttribute("value"); //委案日期
  328. retObject.CaseMemo = driver.FindElement(By.Id("p_case_info__remark")).GetAttribute("value"); //案件备注
  329. driver.FindElement(By.XPath($"//span[contains(text(),'扩展信息')]")).Click();
  330. //select[@id='p_case_info__case_coefficient_id']
  331. retObject.CaseCoefficient = GetSelectText(driver.FindElement(By.Id("p_case_info__case_coefficient_id")));
  332. }
  333. return retObject;
  334. }
  335. private static string GetSelectText(IWebElement element)
  336. {
  337. var strValue = element.GetAttribute("value");
  338. if (string.IsNullOrEmpty(strValue))
  339. {
  340. return null;
  341. }
  342. var ops = element.FindElements(By.TagName("option"));
  343. foreach (var op in ops)
  344. {
  345. if (op.GetAttribute("value") == strValue)
  346. {
  347. return op.Text;
  348. }
  349. }
  350. return null;
  351. }
  352. }
  353. }