using Microsoft.EntityFrameworkCore; using Quartz; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using wispro.sp.entity; namespace wispro.sp.api.Job { public class UpdateJXDataFromIPEasyJob : IJob { public void UpdateFromIPEasy(PerformanceItem Item,spDbContext spDb) { dynamic retObj = utility.IPEasyUtility.GetPerformanceRecord(Item.CaseNo, Item.DoItem, string.IsNullOrEmpty(Item.CaseStage) ? null : Item.CaseStage); IDictionary keyValuePairs = (IDictionary)retObj; if (keyValuePairs.ContainsKey("DoItemCoefficient") && Item.DoItemCoefficient != retObj.DoItemCoefficient) { Item.DoItemCoefficient = retObj.DoItemCoefficient; } //if (keyValuePairs.ContainsKey("DoItemMemo") && Item.DoItemMemo != retObj.DoItemMemo && !string.IsNullOrEmpty(retObj.DoItemMemo)) //{ // Item.DoItemMemo = retObj.DoItemMemo; //} //if (keyValuePairs.ContainsKey("DoItemState") && Item.DoItemState != retObj.DoItemState && !string.IsNullOrEmpty(retObj.DoItemState)) //{ // Item.DoItemState = retObj.DoItemState; //} //if (keyValuePairs.ContainsKey("CustomerLimitDate")) //{ // if (!string.IsNullOrEmpty(retObj.CustomerLimitDate)) // { // DateTime date = DateTime.Parse(retObj.CustomerLimitDate); // if (date != Item.CustomerLimitDate) // Item.CustomerLimitDate = date; // } //} //if (keyValuePairs.ContainsKey("EntrustingDate")) //{ // if (!string.IsNullOrEmpty(retObj.EntrustingDate)) // { // DateTime date = DateTime.Parse(retObj.EntrustingDate); // if (date != Item.EntrustingDate) // Item.EntrustingDate = date; // } //} //if (keyValuePairs.ContainsKey("FinalizationDate")) //{ // if (!string.IsNullOrEmpty(retObj.FinalizationDate)) // { // DateTime date = DateTime.Parse(retObj.FinalizationDate); // if (date != Item.FinalizationDate) // Item.FinalizationDate = date; // } //} //if (keyValuePairs.ContainsKey("FinishedDate")) //{ // if (!string.IsNullOrEmpty(retObj.FinishedDate)) // { // DateTime date = DateTime.Parse(retObj.FinishedDate); // if (date != Item.FinishedDate) // Item.FinishedDate = date; // } //} //if (keyValuePairs.ContainsKey("FirstDraftDate")) //{ // if (!string.IsNullOrEmpty(retObj.FirstDraftDate)) // { // DateTime date = DateTime.Parse(retObj.FirstDraftDate); // if (date != Item.FirstDraftDate) // Item.FirstDraftDate = date; // } //} //if (keyValuePairs.ContainsKey("InternalDate")) //{ // if (!string.IsNullOrEmpty(retObj.InternalDate)) // { // DateTime date = DateTime.Parse(retObj.InternalDate); // if (date != Item.InternalDate) // Item.InternalDate = date; // } //} //if (keyValuePairs.ContainsKey("ReturnDate")) //{ // if (!string.IsNullOrEmpty(retObj.ReturnDate)) // { // DateTime date = DateTime.Parse(retObj.ReturnDate); // if (date != Item.ReturnDate) // Item.ReturnDate = date; // } //} if (keyValuePairs.ContainsKey("WordCount")) { if (!string.IsNullOrEmpty(retObj.WordCount)) { var wordCount = int.Parse(retObj.WordCount); if (wordCount != Item.WordCount) Item.WordCount = wordCount; } } //if (keyValuePairs.ContainsKey("Reviewer") && (Item.Reviewer == null || Item.Reviewer.Name != retObj.Reviewer) && !string.IsNullOrEmpty(retObj.Reviewer)) //{ // string name = retObj.Reviewer; // if (!string.IsNullOrEmpty(name)) // { // var temReviewer = spDb.Staffs.Where(s => s.Name == name).FirstOrDefault(); // if (temReviewer == null) // { // //Item.Reviewer = new Staff() { Name = retObj.Reviewer }; // } // else // { // //Item.Reviewer = temReviewer; // Item.ReviewerId = temReviewer.Id; // } // } //} //if (keyValuePairs.ContainsKey("ApplicationType") && Item.ApplicationType != retObj.ApplicationType && !string.IsNullOrEmpty(retObj.ApplicationType)) //{ // Item.ApplicationType = retObj.ApplicationType; //} //if (keyValuePairs.ContainsKey("BusinessType") && Item.BusinessType != retObj.BusinessType && !string.IsNullOrEmpty(retObj.BusinessType)) //{ // Item.BusinessType = retObj.BusinessType; //} if (keyValuePairs.ContainsKey("CaseCoefficient") && Item.CaseCoefficient != retObj.CaseCoefficient && !string.IsNullOrEmpty(retObj.CaseCoefficient)) { if (string.IsNullOrEmpty(Item.CaseCoefficient) && !string.IsNullOrEmpty(retObj.CaseCoefficient)) { Item.CaseCoefficient = retObj.CaseCoefficient; } } //if (keyValuePairs.ContainsKey("CaseMemo") && Item.CaseMemo != retObj.CaseMemo && !string.IsNullOrEmpty(retObj.CaseMemo)) //{ // Item.CaseMemo = retObj.CaseMemo; //} //if (keyValuePairs.ContainsKey("CaseStage") && Item.CaseStage != retObj.CaseStage && !string.IsNullOrEmpty(retObj.CaseStage)) //{ // Item.CaseStage = retObj.CaseStage; //} //if (keyValuePairs.ContainsKey("CaseState") && Item.CaseState != retObj.CaseState && !string.IsNullOrEmpty(retObj.CaseState)) //{ // Item.CaseState = retObj.CaseState; //} //if (keyValuePairs.ContainsKey("CaseType") && Item.CaseType != retObj.CaseType && !string.IsNullOrEmpty(retObj.CaseType)) //{ // Item.CaseType = retObj.CaseType; //} if (spDb.Entry(Item).State != EntityState.Unchanged) { if (Item.AgentFeedbackMemo != "特殊点数申诉") { Utility.Utility.CalBasePoint(Item, spDb.BasePointRules.ToList()); spDb.SaveChanges(); } } } public void RefreshFromIPEasy(int type) { System.Threading.Thread t = new Thread(new ParameterizedThreadStart(RefreshFromIPEasy_BatchThread)); t.Start(type); } /// /// 批量从IPEasy中更新数据 /// /// /// 0:所有; /// 1:BasePoint为空记录; /// 2:新申请案件系数为空记录; /// 3:BasePoint为空记录 或者 新申请案件系数为空记录 /// /// private void RefreshFromIPEasy_BatchThread(object type) { spDbContext spDb = new spDbContext(); var Results = spDb.PerformanceItems.Include(p=>p.Customer).Where(p => (p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo == null) && p.CalMonth.Status == 0 && !p.CaseNo.StartsWith("J")); switch (type.ToString()) { case "1": Results = Results.Where(p => p.BasePoint == null); break; case "2": Results = Results.Where(p => p.Type == "新申请" && p.CaseCoefficient == ""); break; case "3": Results = Results.Where(p => p.BasePoint == null || (p.DoItem == "新申请" && p.CaseCoefficient == "")); break; } var listItems = Results.ToList(); int i = 0; foreach (var Item in listItems) { int iTryCount = 0; TryAgain: try { iTryCount++; UpdateFromIPEasy(Item, spDb); Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}"); System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{listItems.Count}\t{Item.CaseNo}"); //} } catch (Exception ex) { if (iTryCount <= 3) { goto TryAgain; } System.Diagnostics.Debug.WriteLine(ex.ToString()); Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}\t更新失败!"); } } } public Task Execute(IJobExecutionContext context) { spDbContext spDb = new spDbContext(); var lstItem = spDb.PerformanceItems.Where(p => ((p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo==null ) && p.CalMonth.Status == 0 && !p.CaseNo.StartsWith("J"))) .Include(p=>p.Reviewer) .Include(p=>p.CalMonth) .Include(p=>p.Customer) .ToList(); if (lstItem != null) { int i = 0; foreach (var Item in lstItem) { int iTryCount = 0; TryAgain: try { iTryCount++; UpdateFromIPEasy(Item, spDb); Log($"{DateTime.Now}\t{++i}/{lstItem.Count}\t{Item.CaseNo}"); System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{lstItem.Count}\t{Item.CaseNo}"); //} } catch(Exception ex) { if(iTryCount <= 3) { goto TryAgain; } System.Diagnostics.Debug.WriteLine(ex.ToString()); Log($"{DateTime.Now}\t{++i}\t{Item.CaseNo}\r\n{ex.ToString()}"); } } } return Task.CompletedTask; } private void Log(string strMessage) { StreamWriter sw = File.AppendText("c:\\temp\\log.txt"); sw.WriteLine($"{strMessage}"); sw.Flush(); sw.Close(); sw.Dispose(); } } }