UpdateJXDataFromIPEasyJob.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. string temName = name.Split('-')[0].Trim();
  108. var temReviewer = spDb.Staffs.Where<Staff>(s => s.Name == name).FirstOrDefault();
  109. if (temReviewer == null)
  110. {
  111. //Item.Reviewer = new Staff() { Name = retObj.Reviewer };
  112. }
  113. else
  114. {
  115. //Item.Reviewer = temReviewer;
  116. Item.ReviewerId = temReviewer.Id;
  117. }
  118. }
  119. }
  120. if (keyValuePairs.ContainsKey("ApplicationType") && Item.ApplicationType != retObj.ApplicationType && !string.IsNullOrEmpty(retObj.ApplicationType))
  121. {
  122. Item.ApplicationType = retObj.ApplicationType;
  123. }
  124. //if (keyValuePairs.ContainsKey("BusinessType") && Item.BusinessType != retObj.BusinessType && !string.IsNullOrEmpty(retObj.BusinessType))
  125. //{
  126. // Item.BusinessType = retObj.BusinessType;
  127. //}
  128. if (keyValuePairs.ContainsKey("CaseCoefficient") && Item.CaseCoefficient != retObj.CaseCoefficient)
  129. {
  130. Item.CaseCoefficient = retObj.CaseCoefficient;
  131. }
  132. //if (keyValuePairs.ContainsKey("CaseMemo") && Item.CaseMemo != retObj.CaseMemo && !string.IsNullOrEmpty(retObj.CaseMemo))
  133. //{
  134. // Item.CaseMemo = retObj.CaseMemo;
  135. //}
  136. //if (keyValuePairs.ContainsKey("CaseStage") && Item.CaseStage != retObj.CaseStage && !string.IsNullOrEmpty(retObj.CaseStage))
  137. //{
  138. // Item.CaseStage = retObj.CaseStage;
  139. //}
  140. //if (keyValuePairs.ContainsKey("CaseState") && Item.CaseState != retObj.CaseState && !string.IsNullOrEmpty(retObj.CaseState))
  141. //{
  142. // Item.CaseState = retObj.CaseState;
  143. //}
  144. if (keyValuePairs.ContainsKey("CaseType") && Item.CaseType != retObj.CaseType && !string.IsNullOrEmpty(retObj.CaseType))
  145. {
  146. Item.CaseType = retObj.CaseType;
  147. }
  148. if (spDb.Entry(Item).State != EntityState.Unchanged)
  149. {
  150. if (Item.AgentFeedbackMemo != "特殊点数申诉")
  151. {
  152. Utility.Utility.CalBasePoint(Item, spDb.BasePointRules.ToList());
  153. spDb.SaveChanges();
  154. }
  155. }
  156. }
  157. public void RefreshFromIPEasy(int type)
  158. {
  159. System.Threading.Thread t = new Thread(new ParameterizedThreadStart(RefreshFromIPEasy_BatchThread));
  160. t.Start(type);
  161. }
  162. /// <summary>
  163. /// 批量从IPEasy中更新数据
  164. /// </summary>
  165. /// <param name="type">
  166. /// 0:所有;
  167. /// 1:BasePoint为空记录;
  168. /// 2:新申请案件系数为空记录;
  169. /// 3:BasePoint为空记录 或者 新申请案件系数为空记录
  170. /// </param>
  171. /// <returns></returns>
  172. private void RefreshFromIPEasy_BatchThread(object type)
  173. {
  174. spDbContext spDb = new spDbContext();
  175. var Results = spDb.PerformanceItems.Include(p=>p.Customer).Where(p =>
  176. (p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo == null)
  177. && p.CalMonth.Status == 0
  178. && !p.CaseNo.StartsWith("J"));
  179. switch (type.ToString())
  180. {
  181. case "1":
  182. Results = Results.Where(p => p.BasePoint == null);
  183. break;
  184. case "2":
  185. Results = Results.Where(p => p.Type == "新申请" && p.CaseCoefficient == "");
  186. break;
  187. case "3":
  188. Results = Results.Where(p => p.BasePoint == null || (p.DoItem == "新申请" && p.CaseCoefficient == ""));
  189. break;
  190. }
  191. var listItems = Results.ToList();
  192. int i = 0;
  193. foreach (var Item in listItems)
  194. {
  195. int iTryCount = 0;
  196. TryAgain:
  197. try
  198. {
  199. iTryCount++;
  200. UpdateFromIPEasy(Item, spDb);
  201. Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}");
  202. System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{listItems.Count}\t{Item.CaseNo}");
  203. //}
  204. }
  205. catch (Exception ex)
  206. {
  207. if (iTryCount <= 3)
  208. {
  209. goto TryAgain;
  210. }
  211. System.Diagnostics.Debug.WriteLine(ex.ToString());
  212. Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}\t更新失败!");
  213. }
  214. }
  215. }
  216. public Task Execute(IJobExecutionContext context)
  217. {
  218. spDbContext spDb = new spDbContext();
  219. var lstItem = spDb.PerformanceItems.Where<PerformanceItem>(p =>
  220. ((p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo==null ) && p.CalMonth.Status == 0 && !p.CaseNo.StartsWith("J")))
  221. .Include(p=>p.Reviewer)
  222. .Include(p=>p.CalMonth)
  223. .Include(p=>p.Customer)
  224. .ToList<PerformanceItem>();
  225. if (lstItem != null)
  226. {
  227. int i = 0;
  228. foreach (var Item in lstItem)
  229. {
  230. int iTryCount = 0;
  231. TryAgain:
  232. try
  233. {
  234. iTryCount++;
  235. UpdateFromIPEasy(Item, spDb);
  236. Log($"{DateTime.Now}\t{++i}/{lstItem.Count}\t{Item.CaseNo}");
  237. System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{lstItem.Count}\t{Item.CaseNo}");
  238. //}
  239. }
  240. catch(Exception ex)
  241. {
  242. if(iTryCount <= 3)
  243. {
  244. goto TryAgain;
  245. }
  246. System.Diagnostics.Debug.WriteLine(ex.ToString());
  247. Log($"{DateTime.Now}\t{++i}\t{Item.CaseNo}\r\n{ex.ToString()}");
  248. }
  249. }
  250. }
  251. return Task.CompletedTask;
  252. }
  253. private void Log(string strMessage)
  254. {
  255. StreamWriter sw = File.AppendText("c:\\temp\\log.txt");
  256. sw.WriteLine($"{strMessage}");
  257. sw.Flush();
  258. sw.Close();
  259. sw.Dispose();
  260. }
  261. }
  262. }