2668395b17b32bddcd5f63cd4cbea3be9429577e.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 CmPatrolCourseController : ApiController
  19. {
  20. private iBemsEntities db = new iBemsEntities();
  21. [ResponseType(typeof(void))]
  22. [ActionName("Insert")]
  23. public IHttpActionResult PostCmPatrolCourseInsert(PatrolCourse PatrolCourse)
  24. {
  25. if (!ModelState.IsValid)
  26. {
  27. return BadRequest(ModelState);
  28. }
  29. try
  30. {
  31. // 그룹을 추가하면서 메뉴들을 그룹에 맞는 권한 테이블로 모두 옮겨 놓는다.
  32. using (var transaction = new TransactionScope())
  33. {
  34. var newPatrolCourse = db.CmPatrolCourse.Add(new CmPatrolCourse
  35. {
  36. SiteId = PatrolCourse.SiteId,
  37. Name = PatrolCourse.Name
  38. });
  39. db.SaveChanges();
  40. /*
  41. foreach (var user in PatrolCourse.PatrolCourseUsers)
  42. {
  43. //menu.SiteId = newUserGroup.SiteId;
  44. //menu.UserGroupId = newUserGroup.UserGroupId;
  45. //db.CmUserGroupPermission.Add( menu );
  46. //Trace.WriteLine( string.Format( "{0}-{1}" , newUserGroup.UserGroupId , menu.MenuId ) );
  47. db.CmPatrolCourseUser.Add( new CmPatrolCourseUser
  48. {
  49. SiteId = newPatrolCourse.SiteId,
  50. PatrolCourseId = newPatrolCourse.PatrolCourseId,
  51. UserId = user.UserId,
  52. } );
  53. db.SaveChanges();
  54. }*/
  55. transaction.Complete();
  56. }
  57. }
  58. catch (Exception)
  59. {
  60. throw;
  61. }
  62. return StatusCode(HttpStatusCode.NoContent);
  63. }
  64. [ResponseType(typeof(void))]
  65. [ActionName("AddCoursePos")]
  66. public IHttpActionResult PostCmPatrolCourseAddCoursePos(List<PatrolCoursePos> patrolCoursePoss)
  67. {
  68. if (!ModelState.IsValid)
  69. {
  70. return BadRequest(ModelState);
  71. }
  72. try
  73. {
  74. using (var transaction = new TransactionScope())
  75. {
  76. //개념이 다른듯 2016 02 14
  77. int currentMaxOrder = 1;
  78. int site = patrolCoursePoss[0].SiteId;
  79. int patrolCourseId = patrolCoursePoss[0].PatrolCourseId;
  80. IEnumerable<CmPatrolCoursePos> newKeyData = (from data in db.CmPatrolCoursePos
  81. where data.SiteId == site && data.PatrolCourseId == patrolCourseId
  82. orderby data.CourseOrder descending
  83. select data).Take(1);
  84. if (newKeyData == null || newKeyData.Count() == 0) currentMaxOrder = 1;
  85. else
  86. {
  87. CmPatrolCoursePos tf = newKeyData.First();
  88. currentMaxOrder = tf.CourseOrder + 1;
  89. }
  90. foreach (var pos in patrolCoursePoss)
  91. {
  92. var foundQuery = db.CmPatrolCoursePos.Where(
  93. x =>
  94. x.SiteId == pos.SiteId &&
  95. x.PatrolCourseId == pos.PatrolCourseId &&
  96. x.PosId == pos.PosId);
  97. if (foundQuery.Any() == false)
  98. {
  99. var newUser = db.CmPatrolCoursePos.Add(new CmPatrolCoursePos
  100. {
  101. SiteId = pos.SiteId,
  102. PatrolCourseId = pos.PatrolCourseId,
  103. BuildingId = pos.BuildingId,
  104. FloorId = pos.FloorId,
  105. PosId = pos.PosId,
  106. CourseOrder = currentMaxOrder++
  107. });
  108. }
  109. }
  110. db.SaveChanges();
  111. transaction.Complete();
  112. }
  113. }
  114. catch (Exception)
  115. {
  116. throw;
  117. }
  118. return StatusCode(HttpStatusCode.NoContent);
  119. }
  120. [ResponseType(typeof(void))]
  121. [ActionName("UpdateCourseOrder")]
  122. public IHttpActionResult PostCmPatrolCourseUpdateCourseOrder(List<PatrolCoursePos> patrolCoursePoss)
  123. {
  124. if (!ModelState.IsValid)
  125. {
  126. return BadRequest(ModelState);
  127. }
  128. try
  129. {
  130. using (var transaction = new TransactionScope())
  131. {
  132. foreach (var pos in patrolCoursePoss)
  133. {
  134. var foundQuery = db.CmPatrolCoursePos.Where(
  135. x =>
  136. x.SiteId == pos.SiteId && x.PatrolCourseId == pos.PatrolCourseId && x.PosId == pos.PosId);
  137. if (foundQuery.Any())
  138. {
  139. var found = foundQuery.First();
  140. found.CourseOrder = pos.CourseOrder;
  141. }
  142. }
  143. db.SaveChanges();
  144. transaction.Complete();
  145. }
  146. }
  147. catch (Exception)
  148. {
  149. throw;
  150. }
  151. return StatusCode(HttpStatusCode.NoContent);
  152. }
  153. [ResponseType(typeof(void))]
  154. [ActionName("ChangeCourseOrder")]
  155. public IHttpActionResult PostCmPatrolCourseChangeCourseOrder(List<PatrolCoursePos> patrolCoursePoss)
  156. {
  157. if (!ModelState.IsValid)
  158. {
  159. return BadRequest(ModelState);
  160. }
  161. try
  162. {
  163. using (var transaction = new TransactionScope())
  164. {
  165. int site = patrolCoursePoss[0].SiteId;
  166. int patrolCourseId = patrolCoursePoss[0].PatrolCourseId;
  167. int newOrder1 = patrolCoursePoss[0].CourseOrder;
  168. int newOrder2 = patrolCoursePoss[1].CourseOrder;
  169. int posid1 = patrolCoursePoss[0].PosId;
  170. int posid2 = patrolCoursePoss[1].PosId;
  171. IEnumerable<CmPatrolCoursePos> newKeyData = (from data in db.CmPatrolCoursePos
  172. where data.SiteId == site && data.PatrolCourseId == patrolCourseId && data.PosId == posid1
  173. select data).Take(1);
  174. if (newKeyData != null && newKeyData.Count() == 1)
  175. {
  176. CmPatrolCoursePos tf = newKeyData.First();
  177. tf.CourseOrder = newOrder2;
  178. db.SaveChanges();
  179. }
  180. newKeyData = (from data in db.CmPatrolCoursePos
  181. where data.SiteId == site && data.PatrolCourseId == patrolCourseId && data.PosId == posid2
  182. select data).Take(1);
  183. if (newKeyData != null && newKeyData.Count() == 1)
  184. {
  185. CmPatrolCoursePos tf = newKeyData.First();
  186. tf.CourseOrder = newOrder1;
  187. db.SaveChanges();
  188. }
  189. transaction.Complete();
  190. }
  191. }
  192. catch (Exception)
  193. {
  194. throw;
  195. }
  196. return StatusCode(HttpStatusCode.NoContent);
  197. }
  198. [ResponseType(typeof(void))]
  199. [ActionName("DeleteCoursePos")]
  200. public IHttpActionResult PostCmPatrolCourseDeleteCoursePos(List<PatrolCoursePos> PatrolCoursePoss)
  201. {
  202. if (!ModelState.IsValid)
  203. {
  204. return BadRequest(ModelState);
  205. }
  206. try
  207. {
  208. using (var transaction = new TransactionScope())
  209. {
  210. //개념이 다른듯 2016 02 14
  211. foreach (var pos in PatrolCoursePoss)
  212. {
  213. var foundQuery = db.CmPatrolCoursePos.Where(x =>
  214. x.SiteId == pos.SiteId &&
  215. x.PatrolCourseId == pos.PatrolCourseId &&
  216. x.PosId == pos.PosId);
  217. db.CmPatrolCoursePos.Remove(foundQuery.FirstOrDefault());
  218. }
  219. db.SaveChanges();
  220. transaction.Complete();
  221. }
  222. }
  223. catch (Exception)
  224. {
  225. throw;
  226. }
  227. return StatusCode(HttpStatusCode.NoContent);
  228. }
  229. protected override void Dispose(bool disposing)
  230. {
  231. if (disposing)
  232. {
  233. db.Dispose();
  234. }
  235. base.Dispose(disposing);
  236. }
  237. private bool CmPatrolCourseExists(int id)
  238. {
  239. return db.CmPatrolCourse.Count(e => e.SiteId == id) > 0;
  240. }
  241. public class PatrolCourse
  242. {
  243. public int SiteId { get; set; }
  244. public int PatrolCourseId { get; set; }
  245. public string Name { get; set; }
  246. public List<PatrolCoursePos> PatrolCoursePoss { get; set; }
  247. };
  248. public class PatrolCoursePos
  249. {
  250. public int SiteId { get; set; }
  251. public int PatrolCourseId { get; set; }
  252. public int BuildingId { get; set; }
  253. public int FloorId { get; set; }
  254. public int PosId { get; set; }
  255. public int CourseOrder { get; set; }
  256. };
  257. }
  258. }