eb3ead43c0034bc5a1b5f8dc53114d5c91d4d733.svn-base 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Infrastructure;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Http;
  10. using System.Web;
  11. using System.Web.Http;
  12. using System.Web.Http.Description;
  13. using iBemsDataService;
  14. using iBemsDataService.Model;
  15. using iBemsDataService.Util;
  16. namespace iBemsDataService.Controllers
  17. {
  18. public class FmsEquipmentHistoryController : ApiController
  19. {
  20. private iBemsEntities db = new iBemsEntities();
  21. [ResponseType(typeof(void))]
  22. [ActionName("AddHistory")]
  23. public IHttpActionResult PostFmsEquipmentHistory(FmsEquipmentHistory historyInfo)
  24. {
  25. if ( historyInfo.EquipmentId == 0) { return BadRequest(ModelState); }
  26. if (historyInfo.EquipmentRentId == null || historyInfo.EquipmentRentId == 0) { return BadRequest(ModelState); }
  27. try
  28. {
  29. FmsEquipmentHistory previousHistory = db.FmsEquipmentHistory.Where(fe =>
  30. fe.SiteId == historyInfo.SiteId && //19.07.12
  31. fe.EquipmentId == historyInfo.EquipmentId &&
  32. fe.EquipmentRentId == historyInfo.EquipmentRentId)
  33. .OrderByDescending(fe => fe.EquipmentHistoryId)
  34. .Take(1).FirstOrDefault();
  35. if (previousHistory == null)
  36. {
  37. previousHistory = db.FmsEquipmentHistory.Where(fe =>
  38. fe.SiteId == historyInfo.SiteId && //19.07.12
  39. fe.EquipmentId == historyInfo.EquipmentId)
  40. .OrderByDescending(fe => fe.EquipmentHistoryId)
  41. .Take(1).FirstOrDefault();
  42. }
  43. FmsEquipmentHistory lastestHistory = db.FmsEquipmentHistory.Where(fe =>
  44. fe.SiteId == historyInfo.SiteId && //19.07.12
  45. fe.EquipmentId == historyInfo.EquipmentId)
  46. .OrderByDescending(fe => fe.EquipmentHistoryId)
  47. .Take(1).FirstOrDefault();
  48. if (previousHistory == null || lastestHistory == null) { return BadRequest("Not Found Stored Info."); }
  49. FmsEquipmentHistory targetHistory = new FmsEquipmentHistory()
  50. {
  51. AddDate = DateTime.Now,
  52. // 현재 수량 = 마지막정보의 현재수량 + 반납수량
  53. CurrentStockCount = lastestHistory.CurrentStockCount + historyInfo.ReturnCount,
  54. EquipmentId = historyInfo.EquipmentId,
  55. EquipmentRentId = historyInfo.EquipmentRentId,
  56. LossCount = historyInfo.LossCount,
  57. RentCount = previousHistory.RentCount,
  58. ReturnCount = historyInfo.ReturnCount,
  59. SiteId = historyInfo.SiteId,
  60. StoredCount = 0,
  61. // 전체수량 = 마지막정보의 전체수량 - 손실수량
  62. TotalStockCount = lastestHistory.TotalStockCount - historyInfo.LossCount,
  63. CurrentRentCount = previousHistory.CurrentRentCount - (historyInfo.ReturnCount + historyInfo.LossCount),
  64. UpdateDate = DateTime.Now,
  65. };
  66. if (targetHistory.ReturnCount > 0 && targetHistory.LossCount == 0)
  67. {
  68. targetHistory.EquipmentStateTypeId = 3;
  69. }
  70. else if (targetHistory.ReturnCount > 0 && targetHistory.LossCount > 0)
  71. {
  72. targetHistory.EquipmentStateTypeId = 4;
  73. }
  74. else if (targetHistory.ReturnCount == 0 && targetHistory.LossCount > 0)
  75. {
  76. targetHistory.EquipmentStateTypeId = 5;
  77. }
  78. if (targetHistory.EquipmentStateTypeId > 2)
  79. {
  80. // 신규 정보를 대여 로 처리 (ReturnInfo 의 그리드 리스팅을 위함.)
  81. if (targetHistory.CurrentRentCount > 0) { targetHistory.IsReturned = false; }
  82. else { targetHistory.IsReturned = true; }
  83. }
  84. previousHistory.IsReturned = true; // 이전 정보는 반납완료 로 처리 함
  85. db.FmsEquipmentHistory.Add(targetHistory);
  86. // RentInfo 에 대한 반납 처리
  87. // 현재 대여정보에 해당하는 대여상태의 historyList
  88. var historyListByCurrentRentInfo = db.FmsEquipmentHistory
  89. .Where(fe => fe.SiteId == historyInfo.SiteId &&
  90. fe.EquipmentRentId == historyInfo.EquipmentRentId &&
  91. fe.IsReturned == false).ToList();
  92. // SaveChange 이전 이므로 현저 history 를 임의로 추가
  93. historyListByCurrentRentInfo.Add(targetHistory);
  94. var count = historyListByCurrentRentInfo.Where(hl =>
  95. hl.SiteId == targetHistory.SiteId &&
  96. hl.IsReturned == false).Count();
  97. if (count == 0)
  98. {
  99. var rentInfo = db.FmsEquipmentRentInfo.Where(r =>
  100. r.SiteId == historyInfo.SiteId &&
  101. r.EquipmentRentId == historyInfo.EquipmentRentId)
  102. .FirstOrDefault();
  103. rentInfo.EquipmentStateTypeId = 3;
  104. rentInfo.ReturnFixDate = DateTime.Now;
  105. }
  106. db.SaveChanges();
  107. }
  108. catch (DbUpdateException e) { throw e; }
  109. return StatusCode(HttpStatusCode.NoContent);
  110. }
  111. protected override void Dispose(bool disposing)
  112. {
  113. if (disposing)
  114. {
  115. db.Dispose();
  116. }
  117. base.Dispose(disposing);
  118. }
  119. }
  120. }