using System; using System.Collections; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Net; using System.Web; using System.Web.Http; using System.Web.Http.Description; using DevExpress.Office.Utils; using iBemsDataService.Model; using iBemsDataService.Util; namespace iBemsDataService.Controllers { public class FmsWorkResultCheckItemController : ApiController { private iBemsEntities db = new iBemsEntities(); [Queryable] public IQueryable GetFmsWorkResultCheckItem() { //Uri uri = Request.RequestUri; //var uriQuery = HttpUtility.ParseQueryString( uri.Query ); //int siteId, orderId, workTypeId; //string facilityCode = uriQuery.Get( "FacilityCode" ); //if( int.TryParse( uriQuery.Get( "SiteId" ) , out siteId ) == false || // int.TryParse( uriQuery.Get( "OrderId" ) , out orderId ) == false || // int.TryParse( uriQuery.Get( "WorkTypeId" ) , out workTypeId ) == false || // string.IsNullOrEmpty(facilityCode) ) //{ // throw new Exception("Not Found Parameters."); //} // 2019.07.12 Uri uri = Request.RequestUri; var uriQuery = HttpUtility.ParseQueryString(uri.Query); int siteId; if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false) { throw new Exception("Not Found Parameters."); } var query = from f in db.FmsFacilityCheckItem.Where(x => x.SiteId == siteId) // 2019.07.12 join w in db.FmsWorkResultCheckItem on f.SiteId equals w.SiteId into wg from item in wg.Where( x => x.FacilityCode == f.FacilityCode && x.CheckItemId == f.CheckItemId ).DefaultIfEmpty() // x.CheckItemId == f.CheckItemId && x.OrderId == orderId ).DefaultIfEmpty() //where f.SiteId == siteId && f.FacilityCode == facilityCode && f.WorkTypeId == workTypeId select new WorkResultCheckItemForSelect { SiteId = f.SiteId, FacilityCode = f.FacilityCode, CheckItemId = f.CheckItemId, WorkTypeId = f.WorkTypeId, IsYesNoResult = f.IsYesNoResult, Name = f.Name, // hcLee 2018 01 29 FileId1 = item.FileId1 == null ? null : item.FileId1, FileId2 = item.FileId2 == null ? null : item.FileId2, // <-- hcLee 2018 01 29 WorkRequestId = item == null ? null : (Nullable)item.WorkRequestId , TextResult = item.TextResult == null ? null : item.TextResult , BoolResult = item.BoolResult == null ? null : item.BoolResult }; return query; } // POST: api/FmsWorkResultCheckItem [ResponseType( typeof ( void ) )] public IHttpActionResult PostFmsWorkResultCheckItem( List checkItems ) { if ( !ModelState.IsValid ) { return BadRequest( ModelState ); } Uri uri = Request.RequestUri; var uriQuery = HttpUtility.ParseQueryString( uri.Query ); int siteId, workRequestId, facilityCode; string uriFacilityCode = uriQuery.Get( "FacilityCode" ); if( int.TryParse( uriQuery.Get( "SiteId" ) , out siteId ) == false || int.TryParse( uriQuery.Get( "WorkRequestId" ) , out workRequestId ) == false || int.TryParse( uriFacilityCode, out facilityCode ) == false ) { return BadRequest("Not Found Parameters."); } var query = from d in db.FmsWorkResultCheckItem where d.SiteId == siteId && d.WorkRequestId == workRequestId && d.FacilityCode == facilityCode select d; var qm = new QueryManager( db ); try { qm.DeleteAndInsert( db.FmsWorkResultCheckItem , checkItems , query ); } catch ( Exception ) { throw; } return StatusCode( HttpStatusCode.NoContent ); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } private bool FmsWorkOrderToFacilityExists( int id ) { return db.FmsWorkOrderToFacility.Count( e => e.SiteId == id ) > 0; } } public class WorkResultCheckItemForSelect { public int SiteId { get; set; } public Nullable WorkRequestId { get; set; } public int FacilityCode { get; set; } public int CheckItemId { get; set; } public int WorkTypeId { get; set; } public string Name { get; set; } public bool IsYesNoResult { get; set; } public Nullable BoolResult { get; set; } public string TextResult { get; set; } public Nullable FileId1 { get; set; } public Nullable FileId2 { get; set; } }; public class WorkResultCheckItem { public int SiteId { get; set; } public int WorkOrderId { get; set; } public int FacilityCode { get; set; } public int CheckItemId { get; set; } public bool BoolResult { get; set; } public string TextResult { get; set; } public int FileId1 { get; set; } public int FileId2 { get; set; } }; }