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; using System.Transactions; using System.Diagnostics; namespace iBemsDataService.Controllers { public class BemsEnergyConfigPercentMonthController : ApiController { private iBemsEntities db = new iBemsEntities(); [ResponseType(typeof(void))] [ActionName("Insert")] public IHttpActionResult PostBemsEnergyConfigPercentMonthInsert(EnergyConfigPercentMonth EnergyConfigPercentMonth) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { // 그룹을 추가하면서 메뉴들을 그룹에 맞는 권한 테이블로 모두 옮겨 놓는다. using (var transaction = new TransactionScope()) { var newPriceFormula = db.BemsEnergyConfigPercentMonth.Add(new BemsEnergyConfigPercentMonth { SiteId = EnergyConfigPercentMonth.SiteId, Year = EnergyConfigPercentMonth.Year, Month = EnergyConfigPercentMonth.Month, Electricity = Math.Round(EnergyConfigPercentMonth.Electricity * 100) / 100.0, Gas = Math.Round(EnergyConfigPercentMonth.Gas * 100) / 100.0, Water = Math.Round(EnergyConfigPercentMonth.Water * 100) / 100.0 }); db.SaveChanges(); transaction.Complete(); } } catch (Exception) { throw; } return StatusCode(HttpStatusCode.NoContent); } [ResponseType(typeof(void))] [ActionName("InsertAll")] public IHttpActionResult PostBemsPriceFormulaInsertAll(List EnergyConfigPercentMonth) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { using (var transaction = new TransactionScope()) { foreach (var m in EnergyConfigPercentMonth) { var foundQuery = db.BemsEnergyConfigPercentMonth.Where( x => x.SiteId == m.SiteId && x.Year == m.Year && x.Month == m.Month); if (foundQuery.Any() == false) { var newM = db.BemsEnergyConfigPercentMonth.Add(new BemsEnergyConfigPercentMonth { SiteId = m.SiteId, Year = m.Year, Month = m.Month, Electricity = m.Electricity, Gas = m.Gas, Water = m.Water }); } } db.SaveChanges(); transaction.Complete(); } } catch (Exception) { throw; } return StatusCode(HttpStatusCode.NoContent); } [ResponseType(typeof(void))] [ActionName("DeleteAll")] public IHttpActionResult PostBemsPriceFormulaDeleteAll(EnergyConfigPercentMonth EnergyConfigPercentMonth) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { using (var transaction = new TransactionScope()) { var foundQuery = db.BemsEnergyConfigPercentMonth.Where( x => x.SiteId == EnergyConfigPercentMonth.SiteId && x.Year == EnergyConfigPercentMonth.Year && x.Month == EnergyConfigPercentMonth.Month); //db.BemsPriceFormula.Remove(foundQuery.FirstOrDefault()); 한개만 지운다 new QueryManager(db).Delete(db.BemsEnergyConfigPercentMonth, foundQuery); db.SaveChanges(); transaction.Complete(); } } catch (Exception) { throw; } return StatusCode(HttpStatusCode.NoContent); } [ResponseType(typeof(void))] [ActionName("Update")] public IHttpActionResult PostBemsPriceFormulaUpdate(EnergyConfigPercentMonth EnergyConfigPercentMonth) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { using (var transaction = new TransactionScope()) { var foundQuery = db.BemsEnergyConfigPercentMonth.Where( x => x.SiteId == EnergyConfigPercentMonth.SiteId && x.Year == EnergyConfigPercentMonth.Year && x.Month == EnergyConfigPercentMonth.Month ); new QueryManager(db).Delete(db.BemsEnergyConfigPercentMonth, foundQuery); var newPriceFormula = db.BemsEnergyConfigPercentMonth.Add(new BemsEnergyConfigPercentMonth { SiteId = EnergyConfigPercentMonth.SiteId, Year = EnergyConfigPercentMonth.Year, Month = EnergyConfigPercentMonth.Month, Electricity = Math.Round(EnergyConfigPercentMonth.Electricity * 100) / 100.0, Gas = Math.Round(EnergyConfigPercentMonth.Gas * 100) / 100.0, Water = Math.Round(EnergyConfigPercentMonth.Water * 100) / 100.0 }); db.SaveChanges(); transaction.Complete(); } } catch (Exception) { throw; } return StatusCode(HttpStatusCode.NoContent); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } public class EnergyConfigPercentMonth { public int SiteId { get; set; } public int Year { get; set; } public int Month { get; set; } public float Electricity { get; set; } public float Gas { get; set; } public float Water { get; set; } }; } }