IPEasyController.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.ApplicationType = retObj.ApplicationType;
  85. Item.BusinessType = retObj.BusinessType;
  86. Item.CaseCoefficient = retObj.CaseCoefficient;
  87. Item.CaseMemo = retObj.CaseMemo;
  88. Item.CaseState = retObj.CaseState;
  89. Item.CaseType = retObj.CaseType;
  90. SaveToCache(Item);
  91. return Item;
  92. }
  93. }
  94. public PerformanceItem GetItemInfo(string CaseNo, string DoItem)
  95. {
  96. var retItem = GetFromCache(CaseNo,DoItem);
  97. if (retItem != null)
  98. {
  99. return retItem;
  100. }
  101. else
  102. {
  103. dynamic retObj = wispro.sp.utility.IPEasyUtility.GetPerformanctRecord(CaseNo, DoItem);
  104. PerformanceItem Item = new PerformanceItem();
  105. Item.CaseName = retObj.CaseName;
  106. Item.CaseNo = retObj.CaseNo;
  107. Item.DoItem = retObj.DoItem;
  108. Item.CustomerLimitDate = string.IsNullOrEmpty(retObj.CustomerLimitDate) ? null : DateTime.Parse(retObj.CustomerLimitDate);
  109. Item.Customer = new Customer();
  110. Item.Customer.Name = retObj.CustomerName;
  111. Item.DoItemCoefficient = retObj.DoItemCoefficient;
  112. Item.DoItemMemo = retObj.DoItemMemo;
  113. Item.DoItemState = retObj.DoItemState;
  114. Item.EntrustingDate = string.IsNullOrEmpty(retObj.EntrustingDate) ? null : DateTime.Parse(retObj.EntrustingDate);
  115. Item.FinalizationDate = string.IsNullOrEmpty(retObj.FinalizationDate) ? null : DateTime.Parse(retObj.FinalizationDate);
  116. Item.FinishedDate = string.IsNullOrEmpty(retObj.FinishedDate) ? null : DateTime.Parse(retObj.FinishedDate);
  117. //Item.FirstDraftDate = string.IsNullOrEmpty(retObj.FirstDraftDate) ? null : DateTime.Parse(retObj.FirstDraftDate);
  118. Item.InternalDate = string.IsNullOrEmpty(retObj.InternalDate) ? null : DateTime.Parse(retObj.InternalDate);
  119. if (!string.IsNullOrEmpty(retObj.DoPersons))
  120. {
  121. Item.ItemStaffs = new List<ItemStaff>();
  122. string[] names = retObj.DoPersons.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  123. foreach (var name in names)
  124. {
  125. ItemStaff iStaff = new ItemStaff();
  126. iStaff.DoPerson = Context.Staffs.Where<Staff>(s => s.Name == name).FirstOrDefault();
  127. if (iStaff.DoPerson == null)
  128. {
  129. iStaff.DoPerson = new Staff() { Name = name };
  130. }
  131. else
  132. {
  133. iStaff.DoPersonId = iStaff.DoPerson.Id;
  134. }
  135. Item.ItemStaffs.Add(iStaff);
  136. }
  137. }
  138. Item.ReturnDate = string.IsNullOrEmpty(retObj.ReturnDate) ? null : DateTime.Parse(retObj.ReturnDate);
  139. if (!string.IsNullOrEmpty(retObj.Reviewer))
  140. {
  141. string name = retObj.Reviewer;
  142. var temReviewer = Context.Staffs.Where<Staff>(s => s.Name == name).FirstOrDefault();
  143. if (temReviewer == null)
  144. {
  145. Item.Reviewer = new Staff() { Name = retObj.Reviewer };
  146. }
  147. else
  148. {
  149. Item.Reviewer = temReviewer;
  150. Item.ReviewerId = temReviewer.Id;
  151. }
  152. }
  153. Item.ApplicationType = retObj.ApplicationType;
  154. Item.BusinessType = retObj.BusinessType;
  155. Item.CaseCoefficient = retObj.CaseCoefficient;
  156. Item.CaseMemo = retObj.CaseMemo;
  157. Item.CaseStage = retObj.CaseStage;
  158. Item.CaseState = retObj.CaseState;
  159. Item.CaseType = retObj.CaseType;
  160. SaveToCache(Item);
  161. return Item;
  162. }
  163. }
  164. }
  165. }