BemsSitePriceController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Infrastructure;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Web;
  9. using System.Web.Http;
  10. using System.Web.Http.Description;
  11. using DevExpress.Office.Utils;
  12. using iBemsDataService.Model;
  13. using iBemsDataService.Util;
  14. using System.Transactions;
  15. using System.Diagnostics;
  16. namespace iBemsDataService.Controllers
  17. {
  18. public class BemsSitePriceController : ApiController
  19. {
  20. private iBemsEntities db = new iBemsEntities();
  21. [ResponseType(typeof(void))]
  22. [ActionName("Insert")]
  23. public IHttpActionResult PostBemsSitePriceInsert(SitePrice SitePrice)
  24. {
  25. if (!ModelState.IsValid)
  26. {
  27. return BadRequest(ModelState);
  28. }
  29. try
  30. {
  31. // 그룹을 추가하면서 메뉴들을 그룹에 맞는 권한 테이블로 모두 옮겨 놓는다.
  32. using (var transaction = new TransactionScope())
  33. {
  34. var newBemsSitePrice = db.BemsSitePrice.Add(new BemsSitePrice
  35. {
  36. SiteID = SitePrice.SiteId,
  37. FuelTypeId = SitePrice.FuelTypeId,
  38. PriceTypeId = SitePrice.PriceTypeId,
  39. PriceCode = SitePrice.PriceCode,
  40. UseYN = SitePrice.UseYN
  41. });
  42. db.SaveChanges();
  43. /*
  44. foreach (var user in Menu.MenuUsers)
  45. {
  46. //menu.SiteId = newUserGroup.SiteId;
  47. //menu.UserGroupId = newUserGroup.UserGroupId;
  48. //db.CmUserGroupPermission.Add( menu );
  49. //Trace.WriteLine( string.Format( "{0}-{1}" , newUserGroup.UserGroupId , menu.MenuId ) );
  50. db.BemsSitePriceUser.Add( new BemsSitePriceUser
  51. {
  52. SiteId = newMenu.SiteId,
  53. MenuId = newMenu.MenuId,
  54. UserId = user.UserId,
  55. } );
  56. db.SaveChanges();
  57. }*/
  58. transaction.Complete();
  59. }
  60. }
  61. catch (Exception)
  62. {
  63. throw;
  64. }
  65. return StatusCode(HttpStatusCode.NoContent);
  66. }
  67. [ResponseType(typeof(void))]
  68. [ActionName("InsertAll")]
  69. public IHttpActionResult PostBemsSitePriceInsertAll(List<SitePrice> SitePrice)
  70. {
  71. if (!ModelState.IsValid)
  72. {
  73. return BadRequest(ModelState);
  74. }
  75. try
  76. {
  77. using (var transaction = new TransactionScope())
  78. {
  79. foreach (var m in SitePrice)
  80. {
  81. var newM = db.BemsSitePrice.Add(new BemsSitePrice
  82. {
  83. SiteID = m.SiteId,
  84. FuelTypeId = m.FuelTypeId,
  85. PriceTypeId = m.PriceTypeId,
  86. PriceCode = m.PriceCode,
  87. UseYN = m.UseYN
  88. });
  89. }
  90. db.SaveChanges();
  91. transaction.Complete();
  92. }
  93. }
  94. catch (Exception)
  95. {
  96. throw;
  97. }
  98. return StatusCode(HttpStatusCode.NoContent);
  99. }
  100. [ResponseType(typeof(void))]
  101. [ActionName("Delete")]
  102. public IHttpActionResult PostBemsSitePriceDelete(SitePrice SitePrice)
  103. {
  104. if (!ModelState.IsValid)
  105. {
  106. return BadRequest(ModelState);
  107. }
  108. try
  109. {
  110. using (var transaction = new TransactionScope())
  111. {
  112. var foundQuery = db.BemsSitePrice.Where(x =>
  113. x.SiteID == SitePrice.SiteId &&
  114. x.FuelTypeId == SitePrice.FuelTypeId &&
  115. x.PriceTypeId == SitePrice.PriceTypeId &&
  116. x.PriceCode == SitePrice.PriceCode);
  117. db.BemsSitePrice.Remove(foundQuery.FirstOrDefault()); //한개만 지운다
  118. //new QueryManager<BemsSitePrice>(db).Delete(db.BemsSitePrice, foundQuery);//여러개
  119. db.SaveChanges();
  120. transaction.Complete();
  121. }
  122. }
  123. catch (Exception)
  124. {
  125. throw;
  126. }
  127. return StatusCode(HttpStatusCode.NoContent);
  128. }
  129. [ResponseType(typeof(void))]
  130. [ActionName("DeleteAll")]
  131. public IHttpActionResult PostBemsSitePriceDeleteAll(SitePrice SitePrice)
  132. {
  133. if (!ModelState.IsValid)
  134. {
  135. return BadRequest(ModelState);
  136. }
  137. try
  138. {
  139. using (var transaction = new TransactionScope())
  140. {
  141. var foundQuery = db.BemsSitePrice.Where(x =>
  142. x.SiteID == SitePrice.SiteId &&
  143. x.PriceTypeId == SitePrice.PriceTypeId);
  144. //db.BemsSitePrice.Remove(foundQuery.FirstOrDefault()); 한개만 지운다
  145. new QueryManager<BemsSitePrice>(db).Delete(db.BemsSitePrice, foundQuery);
  146. db.SaveChanges();
  147. transaction.Complete();
  148. }
  149. }
  150. catch (Exception)
  151. {
  152. throw;
  153. }
  154. return StatusCode(HttpStatusCode.NoContent);
  155. }
  156. protected override void Dispose(bool disposing)
  157. {
  158. if (disposing)
  159. {
  160. db.Dispose();
  161. }
  162. base.Dispose(disposing);
  163. }
  164. private bool BemsSitePriceExists(int id)
  165. {
  166. return db.BemsSitePrice.Count(e => e.SiteID == id) > 0;
  167. }
  168. public class SitePrice
  169. {
  170. public int SiteId { get; set; }
  171. public Int16 FuelTypeId { get; set; }
  172. public int PriceTypeId { get; set; }
  173. public string PriceCode { get; set; }
  174. public string UseYN { get; set; }
  175. };
  176. }
  177. }