UpdateJXDataFromIPEasyJob.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 = $"{strSql} and 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. {
  132. retItem.CaseNo = row["case_volume"].ToString();
  133. retItem.CaseCoefficient = row["case_coefficient"].ToString();
  134. retItem.DoItemCoefficient = row["proc_coefficient"].ToString();
  135. retItem.Reviewer = row["reviser"].ToString();
  136. retItem.FinishedDate = row["finish_date"].ToString();
  137. retItem.WordCount = row["translate_count"].ToString();
  138. bRet = true;
  139. break;
  140. }
  141. }
  142. if (!bRet)
  143. {
  144. DataRow row = dt.Rows[0];
  145. retItem.CaseNo = row["case_volume"].ToString();
  146. retItem.CaseCoefficient = row["case_coefficient"].ToString();
  147. retItem.DoItemCoefficient = row["proc_coefficient"].ToString();
  148. retItem.Reviewer = row["reviser"].ToString();
  149. retItem.FinishedDate = row["finish_date"].ToString();
  150. retItem.WordCount = row["translate_count"].ToString();
  151. }
  152. }
  153. else
  154. {
  155. if (dt.Rows.Count > 0)
  156. {
  157. DataRow row = dt.Rows[0];
  158. retItem.CaseNo = row["case_volume"].ToString();
  159. retItem.CaseCoefficient = row["case_coefficient"].ToString();
  160. retItem.DoItemCoefficient = row["proc_coefficient"].ToString();
  161. retItem.Reviewer = row["reviser"].ToString();
  162. retItem.FinishedDate = row["finish_date"].ToString();
  163. retItem.WordCount = row["translate_count"].ToString();
  164. }
  165. }
  166. }
  167. }
  168. }
  169. catch (Exception ex)
  170. {
  171. // error handling
  172. throw;
  173. }
  174. finally
  175. {
  176. conn.Close();
  177. }
  178. }
  179. return retItem;
  180. }
  181. public void UpdateFromIPEasy(PerformanceItem Item,spDbContext spDb)
  182. {
  183. dynamic retObj = GetItemFromIPEasyDB(Item, spDb); //utility.IPEasyUtility.GetPerformanceRecord(Item.CaseNo, Item.DoItem, string.IsNullOrEmpty(Item.CaseStage) ? null : Item.CaseStage);
  184. var appealAJXS = spDb.AppealTypes.FirstOrDefault(p => p.Name.Contains("案件系数"));
  185. var appealCLSXXS = spDb.AppealTypes.FirstOrDefault(p => p.Name.Contains("处理事项系数"));
  186. var caseXS = (spDb.AppealRecords.FirstOrDefault(p => p.ItemId == Item.Id && p.TypeId == appealAJXS.Id && p.State == 1) == null);
  187. var doItemXS = (spDb.AppealRecords.FirstOrDefault(p => p.ItemId == Item.Id && p.TypeId == appealCLSXXS.Id && p.State == 1) == null);
  188. IDictionary<String, Object> keyValuePairs = (IDictionary<String, Object>)retObj;
  189. if (keyValuePairs.ContainsKey("DoItemCoefficient") && Item.DoItemCoefficient != retObj.DoItemCoefficient && doItemXS)
  190. {
  191. Item.DoItemCoefficient = retObj.DoItemCoefficient;
  192. }
  193. if (keyValuePairs.ContainsKey("WordCount"))
  194. {
  195. if (!string.IsNullOrEmpty(retObj.WordCount))
  196. {
  197. var wordCount = int.Parse(retObj.WordCount);
  198. if (wordCount != Item.WordCount)
  199. Item.WordCount = wordCount;
  200. }
  201. }
  202. if (keyValuePairs.ContainsKey("Reviewer") && (Item.Reviewer == null || Item.Reviewer.Name != retObj.Reviewer) && !string.IsNullOrEmpty(retObj.Reviewer))
  203. {
  204. string name = retObj.Reviewer;
  205. if (!string.IsNullOrEmpty(name))
  206. {
  207. string temName = name.Split('-')[0].Trim();
  208. if (!name.Contains("君龙"))
  209. {
  210. temName = name;
  211. }
  212. var temReviewer = spDb.Staffs.Where<Staff>(s => s.Name == temName).FirstOrDefault();
  213. if (temReviewer == null)
  214. {
  215. //Item.Reviewer = new Staff() { Name = retObj.Reviewer };
  216. }
  217. else
  218. {
  219. //Item.Reviewer = temReviewer;
  220. Item.ReviewerId = temReviewer.Id;
  221. }
  222. }
  223. }
  224. if (keyValuePairs.ContainsKey("ApplicationType") && Item.ApplicationType != retObj.ApplicationType && !string.IsNullOrEmpty(retObj.ApplicationType))
  225. {
  226. Item.ApplicationType = retObj.ApplicationType;
  227. }
  228. if (keyValuePairs.ContainsKey("CaseCoefficient") && Item.CaseCoefficient != retObj.CaseCoefficient && caseXS)
  229. {
  230. if(!(retObj.CaseCoefficient == null && string.IsNullOrEmpty(Item.CaseCoefficient)))
  231. Item.CaseCoefficient = retObj.CaseCoefficient;
  232. }
  233. if (keyValuePairs.ContainsKey("CaseType") && Item.CaseType != retObj.CaseType && !string.IsNullOrEmpty(retObj.CaseType))
  234. {
  235. Item.CaseType = retObj.CaseType;
  236. }
  237. if (spDb.Entry(Item).State != EntityState.Unchanged)
  238. {
  239. if (Item.AgentFeedbackMemo != "特殊点数申诉")
  240. {
  241. new Controllers.PerformanceItemController(spDb, new Services.FileTaskCacheService()).RefreshPoint(Item);
  242. }
  243. }
  244. }
  245. public void RefreshFromIPEasy(int type)
  246. {
  247. System.Threading.Thread t = new Thread(new ParameterizedThreadStart(RefreshFromIPEasy_BatchThread));
  248. t.Start(type);
  249. }
  250. /// <summary>
  251. /// 批量从IPEasy中更新数据
  252. /// </summary>
  253. /// <param name="type">
  254. /// 0:所有;
  255. /// 1:BasePoint为空记录;
  256. /// 2:新申请案件系数为空记录;
  257. /// 3:BasePoint为空记录 或者 新申请案件系数为空记录
  258. /// </param>
  259. /// <returns></returns>
  260. private void RefreshFromIPEasy_BatchThread(object type)
  261. {
  262. spDbContext spDb = new spDbContext();
  263. var Results = spDb.PerformanceItems.Include(p=>p.Customer).Where(p =>
  264. (p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo == null)
  265. && p.CalMonth.Status == 0
  266. && !p.CaseNo.StartsWith("J"));
  267. switch (type.ToString())
  268. {
  269. case "1":
  270. Results = Results.Where(p => p.BasePoint == null);
  271. break;
  272. case "2":
  273. Results = Results.Where(p => p.Type == "新申请" && p.CaseCoefficient == "");
  274. break;
  275. case "3":
  276. Results = Results.Where(p => p.BasePoint == null || (p.DoItem == "新申请" && p.CaseCoefficient == ""));
  277. break;
  278. }
  279. var listItems = Results.ToList();
  280. int i = 0;
  281. foreach (var Item in listItems)
  282. {
  283. int iTryCount = 0;
  284. TryAgain:
  285. try
  286. {
  287. iTryCount++;
  288. UpdateFromIPEasy(Item, spDb);
  289. Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}");
  290. System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{listItems.Count}\t{Item.CaseNo}");
  291. //}
  292. }
  293. catch (Exception ex)
  294. {
  295. if (iTryCount <= 3)
  296. {
  297. goto TryAgain;
  298. }
  299. System.Diagnostics.Debug.WriteLine(ex.ToString());
  300. Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}\t更新失败!");
  301. }
  302. }
  303. }
  304. public Task Execute(IJobExecutionContext context)
  305. {
  306. spDbContext spDb = new spDbContext();
  307. var lstItem = spDb.PerformanceItems.Where<PerformanceItem>(p =>
  308. ((p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo==null )
  309. && p.CalMonth.Status == 0 &&
  310. !p.CaseNo.StartsWith("J")))
  311. .Include(p=>p.Reviewer)
  312. .Include(p=>p.CalMonth)
  313. .Include(p=>p.Customer)
  314. .Include(p=>p.ItemStaffs).ThenInclude(p=>p.DoPerson)
  315. .ToList<PerformanceItem>();
  316. if (lstItem != null)
  317. {
  318. int i = 0;
  319. foreach (var Item in lstItem)
  320. {
  321. System.Threading.Thread.Sleep(100);
  322. int iTryCount = 0;
  323. TryAgain:
  324. try
  325. {
  326. iTryCount++;
  327. UpdateFromIPEasy(Item, spDb);
  328. Log($"{DateTime.Now}\t{++i}/{lstItem.Count}\t{Item.CaseNo}");
  329. System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{lstItem.Count}\t{Item.CaseNo}");
  330. //}
  331. }
  332. catch(Exception ex)
  333. {
  334. if(iTryCount <= 3)
  335. {
  336. goto TryAgain;
  337. }
  338. System.Diagnostics.Debug.WriteLine(ex.ToString());
  339. Log($"{DateTime.Now}\t{++i}\t{Item.CaseNo}\r\n{ex.ToString()}");
  340. }
  341. }
  342. }
  343. return Task.CompletedTask;
  344. }
  345. private void Log(string strMessage)
  346. {
  347. StreamWriter sw = File.AppendText("c:\\temp\\log.txt");
  348. sw.WriteLine($"{strMessage}");
  349. sw.Flush();
  350. sw.Close();
  351. sw.Dispose();
  352. }
  353. }
  354. }