UpdateJXDataFromIPEasyJob.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using Microsoft.EntityFrameworkCore;
  2. using Quartz;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using wispro.sp.entity;
  10. namespace wispro.sp.api.Job
  11. {
  12. public class UpdateJXDataFromIPEasyJob : IJob
  13. {
  14. public void UpdateFromIPEasy(PerformanceItem Item,spDbContext spDb)
  15. {
  16. dynamic retObj = utility.IPEasyUtility.GetPerformanceRecord(Item.CaseNo, Item.DoItem, string.IsNullOrEmpty(Item.CaseStage) ? null : Item.CaseStage);
  17. IDictionary<String, Object> keyValuePairs = (IDictionary<String, Object>)retObj;
  18. if (keyValuePairs.ContainsKey("DoItemCoefficient") && Item.DoItemCoefficient != retObj.DoItemCoefficient)
  19. {
  20. Item.DoItemCoefficient = retObj.DoItemCoefficient;
  21. }
  22. if (keyValuePairs.ContainsKey("DoItemMemo") && Item.DoItemMemo != retObj.DoItemMemo && !string.IsNullOrEmpty(retObj.DoItemMemo))
  23. {
  24. Item.DoItemMemo = retObj.DoItemMemo;
  25. }
  26. if (keyValuePairs.ContainsKey("DoItemState") && Item.DoItemState != retObj.DoItemState && !string.IsNullOrEmpty(retObj.DoItemState))
  27. {
  28. Item.DoItemState = retObj.DoItemState;
  29. }
  30. if (keyValuePairs.ContainsKey("CustomerLimitDate"))
  31. {
  32. if (!string.IsNullOrEmpty(retObj.CustomerLimitDate))
  33. {
  34. DateTime date = DateTime.Parse(retObj.CustomerLimitDate);
  35. if (date != Item.CustomerLimitDate)
  36. Item.CustomerLimitDate = date;
  37. }
  38. }
  39. if (keyValuePairs.ContainsKey("EntrustingDate"))
  40. {
  41. if (!string.IsNullOrEmpty(retObj.EntrustingDate))
  42. {
  43. DateTime date = DateTime.Parse(retObj.EntrustingDate);
  44. if (date != Item.EntrustingDate)
  45. Item.EntrustingDate = date;
  46. }
  47. }
  48. if (keyValuePairs.ContainsKey("FinalizationDate"))
  49. {
  50. if (!string.IsNullOrEmpty(retObj.FinalizationDate))
  51. {
  52. DateTime date = DateTime.Parse(retObj.FinalizationDate);
  53. if (date != Item.FinalizationDate)
  54. Item.FinalizationDate = date;
  55. }
  56. }
  57. if (keyValuePairs.ContainsKey("FinishedDate"))
  58. {
  59. if (!string.IsNullOrEmpty(retObj.FinishedDate))
  60. {
  61. DateTime date = DateTime.Parse(retObj.FinishedDate);
  62. if (date != Item.FinishedDate)
  63. Item.FinishedDate = date;
  64. }
  65. }
  66. if (keyValuePairs.ContainsKey("FirstDraftDate"))
  67. {
  68. if (!string.IsNullOrEmpty(retObj.FirstDraftDate))
  69. {
  70. DateTime date = DateTime.Parse(retObj.FirstDraftDate);
  71. if (date != Item.FirstDraftDate)
  72. Item.FirstDraftDate = date;
  73. }
  74. }
  75. if (keyValuePairs.ContainsKey("InternalDate"))
  76. {
  77. if (!string.IsNullOrEmpty(retObj.InternalDate))
  78. {
  79. DateTime date = DateTime.Parse(retObj.InternalDate);
  80. if (date != Item.InternalDate)
  81. Item.InternalDate = date;
  82. }
  83. }
  84. if (keyValuePairs.ContainsKey("ReturnDate"))
  85. {
  86. if (!string.IsNullOrEmpty(retObj.ReturnDate))
  87. {
  88. DateTime date = DateTime.Parse(retObj.ReturnDate);
  89. if (date != Item.ReturnDate)
  90. Item.ReturnDate = date;
  91. }
  92. }
  93. if (keyValuePairs.ContainsKey("WordCount"))
  94. {
  95. if (!string.IsNullOrEmpty(retObj.WordCount))
  96. {
  97. var wordCount = int.Parse(retObj.WordCount);
  98. if (wordCount != Item.WordCount)
  99. Item.WordCount = wordCount;
  100. }
  101. }
  102. if (keyValuePairs.ContainsKey("Reviewer") && (Item.Reviewer == null || Item.Reviewer.Name != retObj.Reviewer) && !string.IsNullOrEmpty(retObj.Reviewer))
  103. {
  104. string name = retObj.Reviewer;
  105. if (!string.IsNullOrEmpty(name))
  106. {
  107. var temReviewer = spDb.Staffs.Where<Staff>(s => s.Name == name).FirstOrDefault();
  108. if (temReviewer == null)
  109. {
  110. //Item.Reviewer = new Staff() { Name = retObj.Reviewer };
  111. }
  112. else
  113. {
  114. //Item.Reviewer = temReviewer;
  115. Item.ReviewerId = temReviewer.Id;
  116. }
  117. }
  118. }
  119. if (keyValuePairs.ContainsKey("ApplicationType") && Item.ApplicationType != retObj.ApplicationType && !string.IsNullOrEmpty(retObj.ApplicationType))
  120. {
  121. Item.ApplicationType = retObj.ApplicationType;
  122. }
  123. if (keyValuePairs.ContainsKey("BusinessType") && Item.BusinessType != retObj.BusinessType && !string.IsNullOrEmpty(retObj.BusinessType))
  124. {
  125. Item.BusinessType = retObj.BusinessType;
  126. }
  127. if (keyValuePairs.ContainsKey("CaseCoefficient") && Item.CaseCoefficient != retObj.CaseCoefficient && !string.IsNullOrEmpty(retObj.CaseCoefficient))
  128. {
  129. if (!string.IsNullOrEmpty(retObj.CaseCoefficient))
  130. {
  131. Item.CaseCoefficient = retObj.CaseCoefficient;
  132. }
  133. }
  134. //if (keyValuePairs.ContainsKey("CaseMemo") && Item.CaseMemo != retObj.CaseMemo && !string.IsNullOrEmpty(retObj.CaseMemo))
  135. //{
  136. // Item.CaseMemo = retObj.CaseMemo;
  137. //}
  138. if (keyValuePairs.ContainsKey("CaseStage") && Item.CaseStage != retObj.CaseStage && !string.IsNullOrEmpty(retObj.CaseStage))
  139. {
  140. Item.CaseStage = retObj.CaseStage;
  141. }
  142. if (keyValuePairs.ContainsKey("CaseState") && Item.CaseState != retObj.CaseState && !string.IsNullOrEmpty(retObj.CaseState))
  143. {
  144. Item.CaseState = retObj.CaseState;
  145. }
  146. if (keyValuePairs.ContainsKey("CaseType") && Item.CaseType != retObj.CaseType && !string.IsNullOrEmpty(retObj.CaseType))
  147. {
  148. Item.CaseType = retObj.CaseType;
  149. }
  150. if (spDb.Entry(Item).State != EntityState.Unchanged)
  151. {
  152. if (Item.AgentFeedbackMemo != "特殊点数申诉")
  153. {
  154. Utility.Utility.CalBasePoint(Item, spDb.BasePointRules.ToList());
  155. spDb.SaveChanges();
  156. }
  157. }
  158. }
  159. public void RefreshFromIPEasy(int type)
  160. {
  161. System.Threading.Thread t = new Thread(new ParameterizedThreadStart(RefreshFromIPEasy_BatchThread));
  162. t.Start(type);
  163. }
  164. /// <summary>
  165. /// 批量从IPEasy中更新数据
  166. /// </summary>
  167. /// <param name="type">
  168. /// 0:所有;
  169. /// 1:BasePoint为空记录;
  170. /// 2:新申请案件系数为空记录;
  171. /// 3:BasePoint为空记录 或者 新申请案件系数为空记录
  172. /// </param>
  173. /// <returns></returns>
  174. private void RefreshFromIPEasy_BatchThread(object type)
  175. {
  176. spDbContext spDb = new spDbContext();
  177. var Results = spDb.PerformanceItems.Include(p=>p.Customer).Where(p =>
  178. (p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo == null)
  179. && p.CalMonth.Status == 0
  180. && !p.CaseNo.StartsWith("J"));
  181. switch (type.ToString())
  182. {
  183. case "1":
  184. Results = Results.Where(p => p.BasePoint == null);
  185. break;
  186. case "2":
  187. Results = Results.Where(p => p.Type == "新申请" && p.CaseCoefficient == "");
  188. break;
  189. case "3":
  190. Results = Results.Where(p => p.BasePoint == null || (p.DoItem == "新申请" && p.CaseCoefficient == ""));
  191. break;
  192. }
  193. var listItems = Results.ToList();
  194. int i = 0;
  195. foreach (var Item in listItems)
  196. {
  197. int iTryCount = 0;
  198. TryAgain:
  199. try
  200. {
  201. iTryCount++;
  202. UpdateFromIPEasy(Item, spDb);
  203. Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}");
  204. System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{listItems.Count}\t{Item.CaseNo}");
  205. //}
  206. }
  207. catch (Exception ex)
  208. {
  209. if (iTryCount <= 3)
  210. {
  211. goto TryAgain;
  212. }
  213. System.Diagnostics.Debug.WriteLine(ex.ToString());
  214. Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}\t更新失败!");
  215. }
  216. }
  217. }
  218. public Task Execute(IJobExecutionContext context)
  219. {
  220. spDbContext spDb = new spDbContext();
  221. var lstItem = spDb.PerformanceItems.Where<PerformanceItem>(p =>
  222. ((p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo==null ) && p.CalMonth.Status == 0 && !p.CaseNo.StartsWith("J")))
  223. .Include(p=>p.Reviewer)
  224. .Include(p=>p.CalMonth)
  225. .Include(p=>p.Customer)
  226. .ToList<PerformanceItem>();
  227. if (lstItem != null)
  228. {
  229. int i = 0;
  230. foreach (var Item in lstItem)
  231. {
  232. int iTryCount = 0;
  233. TryAgain:
  234. try
  235. {
  236. iTryCount++;
  237. UpdateFromIPEasy(Item, spDb);
  238. Log($"{DateTime.Now}\t{++i}\t{Item.CaseNo}");
  239. System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}\t{Item.CaseNo}");
  240. //}
  241. }
  242. catch(Exception ex)
  243. {
  244. if(iTryCount <= 3)
  245. {
  246. goto TryAgain;
  247. }
  248. System.Diagnostics.Debug.WriteLine(ex.ToString());
  249. Log($"{DateTime.Now}\t{++i}\t{Item.CaseNo}\r\n{ex.ToString()}");
  250. }
  251. }
  252. }
  253. return Task.CompletedTask;
  254. }
  255. private void Log(string strMessage)
  256. {
  257. StreamWriter sw = File.AppendText("c:\\temp\\log.txt");
  258. sw.WriteLine($"{strMessage}");
  259. sw.Flush();
  260. sw.Close();
  261. sw.Dispose();
  262. }
  263. }
  264. }