123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- 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 CmPatrolCourseController : ApiController
- {
- private iBemsEntities db = new iBemsEntities();
- [ResponseType(typeof(void))]
- [ActionName("Insert")]
- public IHttpActionResult PostCmPatrolCourseInsert(PatrolCourse PatrolCourse)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- try
- {
- // 그룹을 추가하면서 메뉴들을 그룹에 맞는 권한 테이블로 모두 옮겨 놓는다.
- using (var transaction = new TransactionScope())
- {
- var newPatrolCourse = db.CmPatrolCourse.Add(new CmPatrolCourse
- {
- SiteId = PatrolCourse.SiteId,
- Name = PatrolCourse.Name
- });
- db.SaveChanges();
- /*
- foreach (var user in PatrolCourse.PatrolCourseUsers)
- {
- //menu.SiteId = newUserGroup.SiteId;
- //menu.UserGroupId = newUserGroup.UserGroupId;
- //db.CmUserGroupPermission.Add( menu );
- //Trace.WriteLine( string.Format( "{0}-{1}" , newUserGroup.UserGroupId , menu.MenuId ) );
- db.CmPatrolCourseUser.Add( new CmPatrolCourseUser
- {
- SiteId = newPatrolCourse.SiteId,
- PatrolCourseId = newPatrolCourse.PatrolCourseId,
- UserId = user.UserId,
- } );
- db.SaveChanges();
- }*/
- transaction.Complete();
- }
- }
- catch (Exception)
- {
- throw;
- }
- return StatusCode(HttpStatusCode.NoContent);
- }
- [ResponseType(typeof(void))]
- [ActionName("AddCoursePos")]
- public IHttpActionResult PostCmPatrolCourseAddCoursePos(List<PatrolCoursePos> patrolCoursePoss)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- try
- {
- using (var transaction = new TransactionScope())
- {
- //개념이 다른듯 2016 02 14
- int currentMaxOrder = 1;
- int site = patrolCoursePoss[0].SiteId;
- int patrolCourseId = patrolCoursePoss[0].PatrolCourseId;
- IEnumerable<CmPatrolCoursePos> newKeyData = (from data in db.CmPatrolCoursePos
- where data.SiteId == site && data.PatrolCourseId == patrolCourseId
- orderby data.CourseOrder descending
- select data).Take(1);
- if (newKeyData == null || newKeyData.Count() == 0) currentMaxOrder = 1;
- else
- {
- CmPatrolCoursePos tf = newKeyData.First();
- currentMaxOrder = tf.CourseOrder + 1;
- }
- foreach (var pos in patrolCoursePoss)
- {
- var foundQuery = db.CmPatrolCoursePos.Where(
- x =>
- x.SiteId == pos.SiteId &&
- x.PatrolCourseId == pos.PatrolCourseId &&
- x.PosId == pos.PosId);
- if (foundQuery.Any() == false)
- {
- var newUser = db.CmPatrolCoursePos.Add(new CmPatrolCoursePos
- {
- SiteId = pos.SiteId,
- PatrolCourseId = pos.PatrolCourseId,
- BuildingId = pos.BuildingId,
- FloorId = pos.FloorId,
- PosId = pos.PosId,
- CourseOrder = currentMaxOrder++
- });
- }
- }
- db.SaveChanges();
- transaction.Complete();
- }
- }
- catch (Exception)
- {
- throw;
- }
- return StatusCode(HttpStatusCode.NoContent);
- }
- [ResponseType(typeof(void))]
- [ActionName("UpdateCourseOrder")]
- public IHttpActionResult PostCmPatrolCourseUpdateCourseOrder(List<PatrolCoursePos> patrolCoursePoss)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- try
- {
- using (var transaction = new TransactionScope())
- {
- foreach (var pos in patrolCoursePoss)
- {
- var foundQuery = db.CmPatrolCoursePos.Where(
- x =>
- x.SiteId == pos.SiteId && x.PatrolCourseId == pos.PatrolCourseId && x.PosId == pos.PosId);
- if (foundQuery.Any())
- {
- var found = foundQuery.First();
- found.CourseOrder = pos.CourseOrder;
- }
- }
- db.SaveChanges();
- transaction.Complete();
- }
- }
- catch (Exception)
- {
- throw;
- }
- return StatusCode(HttpStatusCode.NoContent);
- }
- [ResponseType(typeof(void))]
- [ActionName("ChangeCourseOrder")]
- public IHttpActionResult PostCmPatrolCourseChangeCourseOrder(List<PatrolCoursePos> patrolCoursePoss)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- try
- {
- using (var transaction = new TransactionScope())
- {
- int site = patrolCoursePoss[0].SiteId;
- int patrolCourseId = patrolCoursePoss[0].PatrolCourseId;
- int newOrder1 = patrolCoursePoss[0].CourseOrder;
- int newOrder2 = patrolCoursePoss[1].CourseOrder;
- int posid1 = patrolCoursePoss[0].PosId;
- int posid2 = patrolCoursePoss[1].PosId;
- IEnumerable<CmPatrolCoursePos> newKeyData = (from data in db.CmPatrolCoursePos
- where data.SiteId == site && data.PatrolCourseId == patrolCourseId && data.PosId == posid1
- select data).Take(1);
- if (newKeyData != null && newKeyData.Count() == 1)
- {
- CmPatrolCoursePos tf = newKeyData.First();
- tf.CourseOrder = newOrder2;
- db.SaveChanges();
- }
- newKeyData = (from data in db.CmPatrolCoursePos
- where data.SiteId == site && data.PatrolCourseId == patrolCourseId && data.PosId == posid2
- select data).Take(1);
- if (newKeyData != null && newKeyData.Count() == 1)
- {
- CmPatrolCoursePos tf = newKeyData.First();
- tf.CourseOrder = newOrder1;
- db.SaveChanges();
- }
- transaction.Complete();
- }
- }
- catch (Exception)
- {
- throw;
- }
- return StatusCode(HttpStatusCode.NoContent);
- }
- [ResponseType(typeof(void))]
- [ActionName("DeleteCoursePos")]
- public IHttpActionResult PostCmPatrolCourseDeleteCoursePos(List<PatrolCoursePos> PatrolCoursePoss)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- try
- {
- using (var transaction = new TransactionScope())
- {
- //개념이 다른듯 2016 02 14
- foreach (var pos in PatrolCoursePoss)
- {
- var foundQuery = db.CmPatrolCoursePos.Where(x =>
- x.SiteId == pos.SiteId &&
- x.PatrolCourseId == pos.PatrolCourseId &&
- x.PosId == pos.PosId);
- db.CmPatrolCoursePos.Remove(foundQuery.FirstOrDefault());
- }
- db.SaveChanges();
- transaction.Complete();
- }
- }
- catch (Exception)
- {
- throw;
- }
- return StatusCode(HttpStatusCode.NoContent);
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- private bool CmPatrolCourseExists(int id)
- {
- return db.CmPatrolCourse.Count(e => e.SiteId == id) > 0;
- }
- public class PatrolCourse
- {
- public int SiteId { get; set; }
- public int PatrolCourseId { get; set; }
- public string Name { get; set; }
- public List<PatrolCoursePos> PatrolCoursePoss { get; set; }
- };
- public class PatrolCoursePos
- {
- public int SiteId { get; set; }
- public int PatrolCourseId { get; set; }
- public int BuildingId { get; set; }
- public int FloorId { get; set; }
- public int PosId { get; set; }
- public int CourseOrder { get; set; }
- };
- }
- }
|