123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- using System.Diagnostics;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web;
- using System.Web.Http;
- using System.Web.Http.Description;
- using iBemsDataService;
- using iBemsDataService.Model;
- using iBemsDataService.Util;
- namespace iBemsDataService.Controllers
- {
- public class FmsEquipmentHistoryController : ApiController
- {
- private iBemsEntities db = new iBemsEntities();
- [ResponseType(typeof(void))]
- [ActionName("AddHistory")]
- public IHttpActionResult PostFmsEquipmentHistory(FmsEquipmentHistory historyInfo)
- {
- if ( historyInfo.EquipmentId == 0) { return BadRequest(ModelState); }
- if (historyInfo.EquipmentRentId == null || historyInfo.EquipmentRentId == 0) { return BadRequest(ModelState); }
- try
- {
- FmsEquipmentHistory previousHistory = db.FmsEquipmentHistory.Where(fe =>
- fe.SiteId == historyInfo.SiteId && //19.07.12
- fe.EquipmentId == historyInfo.EquipmentId &&
- fe.EquipmentRentId == historyInfo.EquipmentRentId)
- .OrderByDescending(fe => fe.EquipmentHistoryId)
- .Take(1).FirstOrDefault();
- if (previousHistory == null)
- {
- previousHistory = db.FmsEquipmentHistory.Where(fe =>
- fe.SiteId == historyInfo.SiteId && //19.07.12
- fe.EquipmentId == historyInfo.EquipmentId)
- .OrderByDescending(fe => fe.EquipmentHistoryId)
- .Take(1).FirstOrDefault();
- }
- FmsEquipmentHistory lastestHistory = db.FmsEquipmentHistory.Where(fe =>
- fe.SiteId == historyInfo.SiteId && //19.07.12
- fe.EquipmentId == historyInfo.EquipmentId)
- .OrderByDescending(fe => fe.EquipmentHistoryId)
- .Take(1).FirstOrDefault();
- if (previousHistory == null || lastestHistory == null) { return BadRequest("Not Found Stored Info."); }
- FmsEquipmentHistory targetHistory = new FmsEquipmentHistory()
- {
- AddDate = DateTime.Now,
- // 현재 수량 = 마지막정보의 현재수량 + 반납수량
- CurrentStockCount = lastestHistory.CurrentStockCount + historyInfo.ReturnCount,
- EquipmentId = historyInfo.EquipmentId,
- EquipmentRentId = historyInfo.EquipmentRentId,
- LossCount = historyInfo.LossCount,
- RentCount = previousHistory.RentCount,
- ReturnCount = historyInfo.ReturnCount,
- SiteId = historyInfo.SiteId,
- StoredCount = 0,
- // 전체수량 = 마지막정보의 전체수량 - 손실수량
- TotalStockCount = lastestHistory.TotalStockCount - historyInfo.LossCount,
- CurrentRentCount = previousHistory.CurrentRentCount - (historyInfo.ReturnCount + historyInfo.LossCount),
- UpdateDate = DateTime.Now,
- };
- if (targetHistory.ReturnCount > 0 && targetHistory.LossCount == 0)
- {
- targetHistory.EquipmentStateTypeId = 3;
- }
- else if (targetHistory.ReturnCount > 0 && targetHistory.LossCount > 0)
- {
- targetHistory.EquipmentStateTypeId = 4;
- }
- else if (targetHistory.ReturnCount == 0 && targetHistory.LossCount > 0)
- {
- targetHistory.EquipmentStateTypeId = 5;
- }
- if (targetHistory.EquipmentStateTypeId > 2)
- {
- // 신규 정보를 대여 로 처리 (ReturnInfo 의 그리드 리스팅을 위함.)
- if (targetHistory.CurrentRentCount > 0) { targetHistory.IsReturned = false; }
- else { targetHistory.IsReturned = true; }
- }
- previousHistory.IsReturned = true; // 이전 정보는 반납완료 로 처리 함
- db.FmsEquipmentHistory.Add(targetHistory);
- // RentInfo 에 대한 반납 처리
- // 현재 대여정보에 해당하는 대여상태의 historyList
- var historyListByCurrentRentInfo = db.FmsEquipmentHistory
- .Where(fe => fe.SiteId == historyInfo.SiteId &&
- fe.EquipmentRentId == historyInfo.EquipmentRentId &&
- fe.IsReturned == false).ToList();
- // SaveChange 이전 이므로 현저 history 를 임의로 추가
- historyListByCurrentRentInfo.Add(targetHistory);
- var count = historyListByCurrentRentInfo.Where(hl =>
- hl.SiteId == targetHistory.SiteId &&
- hl.IsReturned == false).Count();
- if (count == 0)
- {
- var rentInfo = db.FmsEquipmentRentInfo.Where(r =>
- r.SiteId == historyInfo.SiteId &&
- r.EquipmentRentId == historyInfo.EquipmentRentId)
- .FirstOrDefault();
- rentInfo.EquipmentStateTypeId = 3;
- rentInfo.ReturnFixDate = DateTime.Now;
- }
- db.SaveChanges();
- }
- catch (DbUpdateException e) { throw e; }
- return StatusCode(HttpStatusCode.NoContent);
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- }
- }
|