UpdateJXDataFromIPEasyJob.cs 12 KB

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