UpdateJXDataFromIPEasyJob.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. using Microsoft.Data.SqlClient;
  2. using Microsoft.EntityFrameworkCore;
  3. using Quartz;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Dynamic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net.Http;
  12. using System.Net.Http.Json;
  13. using System.Text.Json;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using wispro.sp.entity;
  17. using wispro.sp.utility;
  18. namespace wispro.sp.api.Job
  19. {
  20. public class UpdateJXDataFromIPEasyJob : IJob
  21. {
  22. public dynamic GetItemFromIPEasyDB(PerformanceItem Item, spDbContext spDb)
  23. {
  24. HttpClient http = new HttpClient();
  25. //http.GetFromJsonAsync($"{ConfigHelper.GetSectionValue("IPEasySetting:ipEeasyApi")}/api/ipEasy/GetItemFromIPEasyDB?CaseNo={Item.CaseNo}&DoItem={Item.DoItem}").Wait();
  26. var response = http.GetFromJsonAsync<Object>($"{ConfigHelper.GetSectionValue("IPEasySetting:ipEeasyApi")}/api/ipEasy/GetItemFromIPEasyDB?CaseNo={Item.CaseNo}&DoItem={Item.DoItem}").Result;
  27. dynamic retItem = new ExpandoObject();
  28. JsonElement json = ((System.Text.Json.JsonElement)response);
  29. retItem.CaseNo = json.GetProperty("CaseNo").GetString();
  30. retItem.ApplicationType = json.GetProperty("ApplicationType").GetString();
  31. retItem.BusinessType = json.GetProperty("BusinessType").GetString();
  32. retItem.DoItem = json.GetProperty("DoItem").GetString();
  33. retItem.CaseStage = json.GetProperty("CaseStage").GetString();
  34. retItem.CaseCoefficient = json.GetProperty("CaseCoefficient").GetString();
  35. retItem.DoItemCoefficient = json.GetProperty("DoItemCoefficient").GetString();
  36. retItem.DoPersons = json.GetProperty("DoPersons").GetString();
  37. retItem.Reviewer = json.GetProperty("Reviewer").GetString();
  38. retItem.CustomerName = json.GetProperty("CustomerName").GetString();
  39. retItem.FinishedDate = json.GetProperty("FinishedDate").GetString();
  40. retItem.WordCount = json.GetProperty("WordCount").GetString();
  41. retItem.ReturnDate = json.GetProperty("ReturnDate").GetString();
  42. retItem.CaseType = json.GetProperty("CaseType").GetString();
  43. retItem.CaseState = json.GetProperty("CaseState").GetString();
  44. retItem.DoItemMemo = json.GetProperty("DoItemMemo").GetString();
  45. retItem.DoItemState = json.GetProperty("DoItemState").GetString();
  46. retItem.CaseName = json.GetProperty("CaseName").GetString();
  47. retItem.EntrustingDate = json.GetProperty("EntrustingDate").GetString();
  48. retItem.CustomerLimitDate = json.GetProperty("CustomerLimitDate").GetString();
  49. retItem.InternalDate = json.GetProperty("InternalDate").GetString();
  50. retItem.FirstDraftDate = json.GetProperty("FirstDraftDate").GetString();
  51. retItem.CaseMemo = json.GetProperty("CaseMemo").GetString();
  52. retItem.FinalizationDate = json.GetProperty("FinalizationDate").GetString();
  53. return retItem;
  54. #region old code 直接从数据库中获取数据
  55. //dynamic retItem = new ExpandoObject();
  56. //var dt = new DataTable();
  57. //using (var conn = new SqlConnection(ConfigHelper.GetSectionValue("IPEasySetting:ConnectionStrings")))
  58. //{
  59. // try
  60. // {
  61. // conn.Open();
  62. // string strSql = @"SELECT distinct p_case_info.case_volume as case_volume,
  63. // i_apply_type.apply_type_zh_cn as apply_type_zh_cn,
  64. // i_business_type.business_type_zh_cn as business_type_zh_cn,
  65. // i_ctrl_proc.ctrl_proc_zh_cn as ctrl_proc_zh_cn,
  66. // (select case_status_zh_cn from i_case_status where case_status_id=p_proc_info.review_stage) as review_stage,
  67. // i_case_coefficient.case_coefficient_zh_cn as case_coefficient,
  68. // i_proc_coefficient.proc_coefficient_zh_cn as proc_coefficient,
  69. // (select proc_coefficient_zh_cn from p_proc_info pr
  70. // left join i_proc_coefficient pc on pc.proc_coefficient_id=pr.proc_coefficient_id
  71. // where case_id=p_case_info.case_id and ctrl_proc_id='8b96378e-05a0-4a8d-b3d1-39af92fddaf5'
  72. // and pr.seq=
  73. // (select max(seq) from p_proc_info pr where case_id=p_case_info.case_id
  74. // and ctrl_proc_id='8b96378e-05a0-4a8d-b3d1-39af92fddaf5'and seq<p_proc_info.seq)
  75. // ) as oa_proc_coefficient,
  76. // (STUFF((SELECT ',' + u.cn_name from p_proc_pic_list as pl
  77. // inner join s_user_info as u on u.user_id = pl.pic_id
  78. // inner join p_proc_info pr1 on pr1.proc_id=pl.obj_id
  79. // where case_id=p_case_info.case_id and
  80. // ctrl_proc_id='8b96378e-05a0-4a8d-b3d1-39af92fddaf5'
  81. // and pr1.seq=(
  82. // select max(seq) from p_proc_info pr2 where case_id=p_case_info.case_id
  83. // and ctrl_proc_id='8b96378e-05a0-4a8d-b3d1-39af92fddaf5'
  84. // and seq<p_proc_info.seq) FOR XML PATH('') ),1,1,''))
  85. // as oa_pic_list,
  86. // STUFF((SELECT ',' + ur.rank_zh_cn from p_proc_pic_list as pl
  87. // inner join s_user_info as u on u.user_id = pl.pic_id
  88. // left join i_user_rank ur on ur.rank_id=u.rank_id
  89. // where pl.obj_id = p_proc_info.proc_id FOR XML PATH('') ),1,1,'') as user_rank,
  90. // STUFF((SELECT ',' + u.cn_name from p_proc_pic_list as pl
  91. // inner join s_user_info as u on u.user_id = pl.pic_id
  92. // where pl.obj_id = p_proc_info.proc_id FOR XML PATH('') ),1,1,'') as pic,
  93. // STUFF((SELECT ',' + u.cn_name from p_revise_user_list as pl
  94. // inner join s_user_info as u on u.user_id = pl.revise_user_id
  95. // where pl.obj_id = p_proc_info.proc_id FOR XML PATH('') ),1,1,'') as reviser,
  96. // c_customer.customer_name as customer_name,
  97. // STUFF((SELECT ',' + a.applicant_name_cn from p_applicant_list as al
  98. // inner join i_applicant as a on a.applicant_id = al.applicant_id
  99. // where al.obj_id = p_case_info.case_id order by al.seq FOR XML PATH('') ),1,1,'') as applicants,
  100. // p_proc_info.finish_date as finish_date,
  101. // p_proc_info.finish_doc_date as finish_doc_date,
  102. // p_proc_info.back_date as back_date,
  103. // i_case_type.case_type_zh_cn as case_type_zh_cn,
  104. // i_case_status.case_status_zh_cn as case_status_zh_cn,
  105. // p_proc_info.proc_note as proc_note,
  106. // (select proc_status_zh_cn from i_proc_status where proc_status_id=p_proc_info.proc_status_id) as proc_status_zh_cn,
  107. // p_case_info.case_name as case_name,
  108. // p_case_info.charge_date as charge_date,
  109. // p_proc_info.cus_due_date as cus_due_date,
  110. // p_proc_info.int_due_date as int_due_date,
  111. // p_proc_info.first_doc_date as first_doc_date,
  112. // p_case_info.remark as case_remark,
  113. // p_proc_info.translate_count as translate_count,
  114. // STUFF((SELECT ',' + ui.cn_name from p_sales_list as sl
  115. // inner join dbo.s_user_info as ui on ui.user_id = sl.sales_user_id
  116. // where sl.obj_id = p_case_info.case_id AND sl.is_enabled=1 order by sl.seq FOR XML PATH('') ),1,1,'') as sales,
  117. // i_country.country_zh_cn as country_zh_cn
  118. // from p_case_info
  119. // inner join p_case_advance_info with(nolock) on p_case_info.case_id=p_case_advance_info.case_id
  120. // inner join i_apply_type with(nolock) on i_apply_type.apply_type_id=p_case_info.apply_type_id
  121. // inner join i_case_type with(nolock) on i_case_type.case_type_id=p_case_info.case_type_id
  122. // inner join i_country with(nolock) on i_country.country_id=p_case_info.country_id
  123. // inner join i_case_status with(nolock) on i_case_status.case_status_id=p_case_info.case_status_id
  124. // inner join c_customer with(nolock) on c_customer.customer_id=p_case_info.customer_id
  125. // left join i_case_coefficient on i_case_coefficient.case_coefficient_id=p_case_info.case_coefficient_id
  126. // inner join p_proc_info with(nolock) on p_case_info.case_id=p_proc_info.case_id
  127. // inner join i_ctrl_proc with(nolock) on p_proc_info.ctrl_proc_id=i_ctrl_proc.ctrl_proc_id
  128. // inner join i_business_type on i_business_type.business_type_id = p_case_info.business_type_id
  129. // left join p_proc_pic_list with(nolock) on p_proc_pic_list.obj_id=p_proc_info.proc_id
  130. // left join s_user_info with(nolock) on s_user_info.user_id=p_proc_pic_list.pic_id
  131. // left join i_proc_coefficient on i_proc_coefficient.proc_coefficient_id=p_proc_info.proc_coefficient_id
  132. // where
  133. // p_case_info.is_enabled=1 and
  134. // p_proc_info.is_enabled=1 and
  135. // s_user_info.dept_id not in ('60e09ee0-fcc7-446f-badc-af9973079fee','34d0e351-71dc-418f-9b6b-bcb67af62fed','599cbe0c-044e-4ffc-9411-96dd9019d8a6') and
  136. // (p_case_info.case_type_id='31D1A147-2931-43B5-94AE-B72B1525BA8A') AND
  137. // i_ctrl_proc.ctrl_proc_zh_cn=@DoItem and
  138. // p_case_info.case_volume =@CaseNo";
  139. // //if (!string.IsNullOrEmpty(Item.CaseStage))
  140. // //{
  141. // // strSql = $"select * from ({strSql}) as t where review_stage=@CaseStage";
  142. // //}
  143. // using (var cmd = conn.CreateCommand())
  144. // {
  145. // cmd.CommandText = strSql;
  146. // cmd.CommandType = CommandType.Text;
  147. // cmd.Parameters.Add(new SqlParameter("DoItem", Item.DoItem.Trim()));
  148. // cmd.Parameters.Add(new SqlParameter("CaseNo", Item.CaseNo.Trim()));
  149. // //if (!string.IsNullOrEmpty(Item.CaseStage))
  150. // //{
  151. // // cmd.Parameters.Add(new SqlParameter("CaseStage", Item.CaseStage));
  152. // //}
  153. // using (var reader = cmd.ExecuteReader())
  154. // {
  155. // dt.Load(reader);
  156. // if (dt.Rows.Count > 1)
  157. // {
  158. // bool bRet = false;
  159. // foreach (DataRow row in dt.Rows)
  160. // {
  161. // retItem.FinishedDate = row["finish_date"].ToString();
  162. // DateTime temDate = DateTime.Now;
  163. // DateTime.TryParse(retItem.FinishedDate,out temDate);
  164. // if ((Item.FinishedDate.HasValue && Item.FinishedDate.Value == temDate) ||
  165. // (!string.IsNullOrEmpty(Item.CaseStage) && Item.CaseStage == row["review_stage"].ToString()))
  166. // {
  167. // GetItemData(retItem, row);
  168. // bRet = true;
  169. // break;
  170. // }
  171. // }
  172. // if (!bRet)
  173. // {
  174. // DataRow row = dt.Rows[0];
  175. // GetItemData(retItem, row);
  176. // }
  177. // }
  178. // else
  179. // {
  180. // if (dt.Rows.Count > 0)
  181. // {
  182. // DataRow row = dt.Rows[0];
  183. // GetItemData(retItem, row);
  184. // }
  185. // }
  186. // }
  187. // }
  188. // }
  189. // catch (Exception ex)
  190. // {
  191. // // error handling
  192. // throw;
  193. // }
  194. // finally
  195. // {
  196. // conn.Close();
  197. // }
  198. //}
  199. //return retItem;
  200. #endregion
  201. }
  202. private static void GetItemData(dynamic retItem, DataRow row)
  203. {
  204. retItem.CaseNo = row["case_volume"].ToString();
  205. retItem.ApplicationType = row["apply_type_zh_cn"].ToString();
  206. retItem.BusinessType = row["business_type_zh_cn"].ToString();
  207. retItem.DoItem = row["ctrl_proc_zh_cn"].ToString(); ;
  208. retItem.CaseStage = row["review_stage"].ToString();
  209. retItem.CaseCoefficient = row["case_coefficient"].ToString();
  210. retItem.DoItemCoefficient = row["proc_coefficient"].ToString();
  211. retItem.DoPersons = row["pic"].ToString();
  212. retItem.Reviewer = row["reviser"].ToString();
  213. retItem.CustomerName = row["customer_name"].ToString();
  214. retItem.FinishedDate = row["finish_date"].ToString();
  215. retItem.WordCount = row["translate_count"].ToString();
  216. retItem.ReturnDate = row["back_date"].ToString();
  217. retItem.CaseType = row["case_type_zh_cn"].ToString();
  218. retItem.CaseState = row["case_status_zh_cn"].ToString();
  219. retItem.DoItemMemo = row["proc_status_zh_cn"].ToString();
  220. retItem.DoItemState = row["proc_note"].ToString();
  221. retItem.CaseName = row["case_name"].ToString();
  222. retItem.EntrustingDate = row["charge_date"].ToString();
  223. retItem.CustomerLimitDate = row["cus_due_date"].ToString();
  224. retItem.InternalDate = row["int_due_date"].ToString();
  225. retItem.FirstDraftDate = row["first_doc_date"].ToString();
  226. retItem.CaseMemo = row["case_remark"].ToString();
  227. retItem.FinalizationDate = row["finish_doc_date"].ToString();
  228. }
  229. public void UpdateFromIPEasy(PerformanceItem Item,spDbContext spDb)
  230. {
  231. List<string> caseNos = new List<string>()
  232. {
  233. "AHK2211737",
  234. "PAHK2211739",
  235. "PACN2120893-TS",
  236. "PAKR2117244",
  237. "PAIN2117242",
  238. "PAEPO1912286",
  239. "PAUS2116581-转所",
  240. "PATW2111942",
  241. "PACN2024870-TS客户自行递交",
  242. "PAUS2010428",
  243. "PAEPO2111073",
  244. "PAUS1915675",
  245. "PAUS2111071",
  246. "PAAU2210170",
  247. "PAUS2010164",
  248. "PACN1914374",
  249. "PAUS2210315",
  250. "PAUS2016669-结案",
  251. "PAEPO1510129",
  252. "PAUS2016669-结案",
  253. "PACN2026619-TS",
  254. "PACN2123589-TS",
  255. "PACN2125458"
  256. };
  257. if (caseNos.Contains(Item.CaseNo))
  258. {
  259. System.Diagnostics.Debug.WriteLine("");
  260. }
  261. dynamic retObj = GetItemFromIPEasyDB(Item, spDb); //utility.IPEasyUtility.GetPerformanceRecord(Item.CaseNo, Item.DoItem, string.IsNullOrEmpty(Item.CaseStage) ? null : Item.CaseStage);
  262. var appealAJXS = spDb.AppealTypes.FirstOrDefault(p => p.Name.Contains("案件系数"));
  263. var appealCLSXXS = spDb.AppealTypes.FirstOrDefault(p => p.Name.Contains("处理事项系数"));
  264. var caseXS = (spDb.AppealRecords.FirstOrDefault(p => p.ItemId == Item.Id && p.TypeId == appealAJXS.Id && p.State == 1) == null);
  265. var doItemXS = (spDb.AppealRecords.FirstOrDefault(p => p.ItemId == Item.Id && p.TypeId == appealCLSXXS.Id && p.State == 1) == null);
  266. IDictionary<String, Object> keyValuePairs = (IDictionary<String, Object>)retObj;
  267. if (keyValuePairs.ContainsKey("DoItemCoefficient") && Item.DoItemCoefficient != retObj.DoItemCoefficient && doItemXS)
  268. {
  269. Item.DoItemCoefficient = retObj.DoItemCoefficient;
  270. }
  271. if (keyValuePairs.ContainsKey("WordCount"))
  272. {
  273. if (!string.IsNullOrEmpty(retObj.WordCount))
  274. {
  275. var wordCount = int.Parse(retObj.WordCount);
  276. if (wordCount != Item.WordCount)
  277. Item.WordCount = wordCount;
  278. }
  279. }
  280. if (keyValuePairs.ContainsKey("Reviewer") && (Item.Reviewer == null || Item.Reviewer.Name != retObj.Reviewer) && !string.IsNullOrEmpty(retObj.Reviewer))
  281. {
  282. string name = retObj.Reviewer;
  283. if (!string.IsNullOrEmpty(name))
  284. {
  285. string temName = name.Split('-')[0].Trim();
  286. if (!name.Contains("君龙"))
  287. {
  288. temName = name;
  289. }
  290. var temReviewer = spDb.Staffs.Where<Staff>(s => s.Name == temName).FirstOrDefault();
  291. if (temReviewer == null)
  292. {
  293. //Item.Reviewer = new Staff() { Name = retObj.Reviewer };
  294. }
  295. else
  296. {
  297. //Item.Reviewer = temReviewer;
  298. Item.ReviewerId = temReviewer.Id;
  299. }
  300. }
  301. }
  302. if (keyValuePairs.ContainsKey("ApplicationType") && Item.ApplicationType != retObj.ApplicationType && !string.IsNullOrEmpty(retObj.ApplicationType))
  303. {
  304. Item.ApplicationType = retObj.ApplicationType;
  305. }
  306. if (keyValuePairs.ContainsKey("CaseCoefficient") && Item.CaseCoefficient != retObj.CaseCoefficient && caseXS)
  307. {
  308. if(!(retObj.CaseCoefficient == null && string.IsNullOrEmpty(Item.CaseCoefficient)))
  309. Item.CaseCoefficient = retObj.CaseCoefficient;
  310. }
  311. if (keyValuePairs.ContainsKey("CaseType") && Item.CaseType != retObj.CaseType && !string.IsNullOrEmpty(retObj.CaseType))
  312. {
  313. Item.CaseType = retObj.CaseType;
  314. }
  315. if (spDb.Entry(Item).State != EntityState.Unchanged)
  316. {
  317. if (Item.AgentFeedbackMemo != "特殊点数申诉")
  318. {
  319. new Controllers.PerformanceItemController(spDb, new Services.FileTaskCacheService()).RefreshPoint(Item);
  320. }
  321. }
  322. }
  323. public void RefreshFromIPEasy(int type)
  324. {
  325. System.Threading.Thread t = new Thread(new ParameterizedThreadStart(RefreshFromIPEasy_BatchThread));
  326. t.Start(type);
  327. }
  328. /// <summary>
  329. /// 批量从IPEasy中更新数据
  330. /// </summary>
  331. /// <param name="type">
  332. /// 0:所有;
  333. /// 1:BasePoint为空记录;
  334. /// 2:新申请案件系数为空记录;
  335. /// 3:BasePoint为空记录 或者 新申请案件系数为空记录
  336. /// </param>
  337. /// <returns></returns>
  338. private void RefreshFromIPEasy_BatchThread(object type)
  339. {
  340. spDbContext spDb = new spDbContext();
  341. var Results = spDb.PerformanceItems.Include(p=>p.Customer).Where(p =>
  342. (p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo == null)
  343. && p.CalMonth.Status == 0
  344. && !p.CaseNo.StartsWith("J"));
  345. switch (type.ToString())
  346. {
  347. case "1":
  348. Results = Results.Where(p => p.BasePoint == null);
  349. break;
  350. case "2":
  351. Results = Results.Where(p => p.Type == "新申请" && p.CaseCoefficient == "");
  352. break;
  353. case "3":
  354. Results = Results.Where(p => p.BasePoint == null || (p.DoItem == "新申请" && p.CaseCoefficient == ""));
  355. break;
  356. }
  357. var listItems = Results.ToList();
  358. int i = 0;
  359. foreach (var Item in listItems)
  360. {
  361. int iTryCount = 0;
  362. TryAgain:
  363. try
  364. {
  365. iTryCount++;
  366. UpdateFromIPEasy(Item, spDb);
  367. Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}");
  368. System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{listItems.Count}\t{Item.CaseNo}");
  369. //}
  370. }
  371. catch (Exception ex)
  372. {
  373. if (iTryCount <= 3)
  374. {
  375. goto TryAgain;
  376. }
  377. System.Diagnostics.Debug.WriteLine(ex.ToString());
  378. Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}\t更新失败!");
  379. }
  380. }
  381. }
  382. public Task Execute(IJobExecutionContext context)
  383. {
  384. spDbContext spDb = new spDbContext();
  385. var lstItem = spDb.PerformanceItems.Where<PerformanceItem>(p =>
  386. ((p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo==null )
  387. && p.CalMonth.Status == 0 &&
  388. !p.CaseNo.StartsWith("J")))
  389. .Include(p=>p.Reviewer)
  390. .Include(p=>p.CalMonth)
  391. .Include(p=>p.Customer)
  392. .Include(p=>p.ItemStaffs).ThenInclude(p=>p.DoPerson)
  393. .ToList<PerformanceItem>();
  394. if (lstItem != null)
  395. {
  396. int i = 0;
  397. foreach (var Item in lstItem)
  398. {
  399. System.Threading.Thread.Sleep(100);
  400. int iTryCount = 0;
  401. TryAgain:
  402. try
  403. {
  404. iTryCount++;
  405. UpdateFromIPEasy(Item, spDb);
  406. Log($"{DateTime.Now}\t{++i}/{lstItem.Count}\t{Item.CaseNo}");
  407. System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{lstItem.Count}\t{Item.CaseNo}");
  408. //}
  409. }
  410. catch(Exception ex)
  411. {
  412. if(iTryCount <= 3)
  413. {
  414. goto TryAgain;
  415. }
  416. System.Diagnostics.Debug.WriteLine(ex.ToString());
  417. Log($"{DateTime.Now}\t{++i}\t{Item.CaseNo}\r\n{ex.ToString()}");
  418. }
  419. }
  420. }
  421. return Task.CompletedTask;
  422. }
  423. private void Log(string strMessage)
  424. {
  425. StreamWriter sw = File.AppendText("c:\\temp\\log.txt");
  426. sw.WriteLine($"{strMessage}");
  427. sw.Flush();
  428. sw.Close();
  429. sw.Dispose();
  430. }
  431. }
  432. }