IPEasyController.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using wispro.sp.entity;
  10. namespace wispro.sp.api.Controllers
  11. {
  12. [Route("api/[controller]/[action]")]
  13. [ApiController]
  14. public class IPEasyController : ControllerBase
  15. {
  16. spDbContext Context;
  17. public IPEasyController(spDbContext context)
  18. {
  19. Context = context;
  20. }
  21. #region 将从IPEasy中获得的信息保存到临时文件或从文件中取出已存在的信息
  22. private PerformanceItem GetFromCache(string CaseNo,string DoItem = null)
  23. {
  24. var attachfileSavePath = utility.ConfigHelper.GetSectionValue("AttachFileSavePath");
  25. string fileName = $"{CaseNo}{(string.IsNullOrEmpty(DoItem) ? "" : $"-{DoItem}")}.json";
  26. var path = Path.Combine(attachfileSavePath,
  27. fileName);
  28. if (System.IO.File.Exists(path))
  29. {
  30. if(new FileInfo(path).CreationTime < DateTime.Now.AddDays(-1))
  31. {
  32. return null;
  33. }
  34. using (var file = System.IO.File.OpenText(path))
  35. {
  36. string strJson = file.ReadToEnd();
  37. JsonSerializerOptions options = new System.Text.Json.JsonSerializerOptions()
  38. {
  39. IgnoreNullValues = true
  40. };
  41. return System.Text.Json.JsonSerializer.Deserialize<PerformanceItem>(strJson,options);
  42. }
  43. }
  44. else
  45. {
  46. return null;
  47. }
  48. }
  49. private void SaveToCache(PerformanceItem Item)
  50. {
  51. var attachfileSavePath = utility.ConfigHelper.GetSectionValue("AttachFileSavePath");
  52. string fileName = $"{Item.CaseNo}{(string.IsNullOrEmpty(Item.DoItem) ? "" : $"-{Item.DoItem}")}.json";
  53. var path = Path.Combine(attachfileSavePath,
  54. fileName);
  55. if (System.IO.File.Exists(path))
  56. {
  57. System.IO.File.Delete(path);
  58. }
  59. JsonSerializerOptions options = new System.Text.Json.JsonSerializerOptions() {
  60. IgnoreNullValues = true
  61. };
  62. string strJson = System.Text.Json.JsonSerializer.Serialize<PerformanceItem>(Item,options);
  63. using (var file = System.IO.File.CreateText(path))
  64. {
  65. file.Write(strJson);
  66. }
  67. }
  68. #endregion
  69. public PerformanceItem GetCaseInfo(string CaseNo)
  70. {
  71. var retItem = GetFromCache(CaseNo);
  72. if (retItem != null)
  73. {
  74. return retItem;
  75. }
  76. else
  77. {
  78. dynamic retObj = wispro.sp.utility.IPEasyUtility.GetCaseInfo(CaseNo);
  79. PerformanceItem Item = new PerformanceItem();
  80. Item.CaseName = retObj.CaseName;
  81. Item.CaseNo = retObj.CaseNo;
  82. Item.Customer = new Customer();
  83. Item.Customer.Name = retObj.CustomerName;
  84. Item.Customer.Name = Item.Customer.Name.Replace("(null)", "");
  85. Item.ApplicationType = retObj.ApplicationType;
  86. Item.BusinessType = retObj.BusinessType;
  87. Item.CaseCoefficient = retObj.CaseCoefficient;
  88. Item.CaseMemo = retObj.CaseMemo;
  89. Item.CaseState = retObj.CaseState;
  90. Item.CaseType = retObj.CaseType;
  91. SaveToCache(Item);
  92. return Item;
  93. }
  94. }
  95. public PerformanceItem GetItemInfo(string CaseNo, string DoItem)
  96. {
  97. var retItem = GetFromCache(CaseNo,DoItem);
  98. if (retItem != null)
  99. {
  100. return retItem;
  101. }
  102. else
  103. {
  104. dynamic retObj = wispro.sp.utility.IPEasyUtility.GetPerformanctRecord(CaseNo, DoItem);
  105. PerformanceItem Item = new PerformanceItem();
  106. Item.CaseName = retObj.CaseName;
  107. Item.CaseNo = retObj.CaseNo;
  108. Item.DoItem = retObj.DoItem;
  109. Item.CustomerLimitDate = string.IsNullOrEmpty(retObj.CustomerLimitDate) ? null : DateTime.Parse(retObj.CustomerLimitDate);
  110. Item.Customer = new Customer();
  111. Item.Customer.Name = retObj.CustomerName;
  112. Item.Customer.Name = Item.Customer.Name.Replace("(null)", "");
  113. Item.DoItemCoefficient = retObj.DoItemCoefficient;
  114. Item.DoItemMemo = retObj.DoItemMemo;
  115. Item.DoItemState = retObj.DoItemState;
  116. Item.EntrustingDate = string.IsNullOrEmpty(retObj.EntrustingDate) ? null : DateTime.Parse(retObj.EntrustingDate);
  117. Item.FinalizationDate = string.IsNullOrEmpty(retObj.FinalizationDate) ? null : DateTime.Parse(retObj.FinalizationDate);
  118. Item.FinishedDate = string.IsNullOrEmpty(retObj.FinishedDate) ? null : DateTime.Parse(retObj.FinishedDate);
  119. //Item.FirstDraftDate = string.IsNullOrEmpty(retObj.FirstDraftDate) ? null : DateTime.Parse(retObj.FirstDraftDate);
  120. Item.InternalDate = string.IsNullOrEmpty(retObj.InternalDate) ? null : DateTime.Parse(retObj.InternalDate);
  121. if (!string.IsNullOrEmpty(retObj.DoPersons))
  122. {
  123. Item.ItemStaffs = new List<ItemStaff>();
  124. string[] names = retObj.DoPersons.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  125. foreach (var name in names)
  126. {
  127. ItemStaff iStaff = new ItemStaff();
  128. iStaff.DoPerson = Context.Staffs.Where<Staff>(s => s.Name == name).FirstOrDefault();
  129. if (iStaff.DoPerson == null)
  130. {
  131. iStaff.DoPerson = new Staff() { Name = name };
  132. }
  133. else
  134. {
  135. iStaff.DoPersonId = iStaff.DoPerson.Id;
  136. }
  137. Item.ItemStaffs.Add(iStaff);
  138. }
  139. }
  140. Item.ReturnDate = string.IsNullOrEmpty(retObj.ReturnDate) ? null : DateTime.Parse(retObj.ReturnDate);
  141. if (!string.IsNullOrEmpty(retObj.Reviewer))
  142. {
  143. string name = retObj.Reviewer;
  144. var temReviewer = Context.Staffs.Where<Staff>(s => s.Name == name).FirstOrDefault();
  145. if (temReviewer == null)
  146. {
  147. Item.Reviewer = new Staff() { Name = retObj.Reviewer };
  148. }
  149. else
  150. {
  151. Item.Reviewer = temReviewer;
  152. Item.ReviewerId = temReviewer.Id;
  153. }
  154. }
  155. Item.ApplicationType = retObj.ApplicationType;
  156. Item.BusinessType = retObj.BusinessType;
  157. Item.CaseCoefficient = retObj.CaseCoefficient;
  158. Item.CaseMemo = retObj.CaseMemo;
  159. Item.CaseStage = retObj.CaseStage;
  160. Item.CaseState = retObj.CaseState;
  161. Item.CaseType = retObj.CaseType;
  162. SaveToCache(Item);
  163. return Item;
  164. }
  165. }
  166. }
  167. }