IPEasyController.cs 12 KB

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