UpdateJXDataFromIPEasyJob.cs 20 KB

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