439ac3b502629c679c307669f8bf823373b58b5d.svn-base 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Data;
  5. using System.Data.Entity;
  6. using System.Data.Entity.Infrastructure;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Http;
  10. using System.Web;
  11. using System.Web.Http;
  12. using System.Web.Http.Description;
  13. using System.Web.Http.OData;
  14. using iBemsDataService;
  15. using iBemsDataService.Model;
  16. using iBemsDataService.Util;
  17. namespace iBemsDataService.Controllers
  18. {
  19. public class AvailableDueDateController : ApiController
  20. {
  21. private iBemsEntities db = new iBemsEntities();
  22. [ActionName("GetAvailableDueDate")]
  23. public List<AvailableDueDateType> GetAvailableDueDate()
  24. {
  25. List<AvailableDueDateType> dueDateList = new List<AvailableDueDateType>();
  26. Uri uri = Request.RequestUri;
  27. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  28. int siteId, workRequestId;
  29. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  30. int.TryParse(uriQuery.Get("WorkRequestId"), out workRequestId) == false)
  31. {
  32. dueDateList.Add(new AvailableDueDateType() { DueDate = DateTime.Now });
  33. return dueDateList;
  34. }
  35. // WorkRequest 정보
  36. var workRequest = (
  37. from w in db.FmsWorkRequest
  38. where w.SiteId == siteId &&
  39. w.WorkRequestId == workRequestId
  40. select w).FirstOrDefault();
  41. if (workRequest == null || workRequest.WorkScheduleId == null)
  42. {
  43. dueDateList.Add(new AvailableDueDateType() { DueDate = DateTime.Now });
  44. return dueDateList;
  45. }
  46. // 스케줄 정보
  47. var workSchedule = (
  48. from s in db.FmsWorkSchedule
  49. where s.SiteId == siteId &&
  50. s.WorkScheduleId == workRequest.WorkScheduleId &&
  51. s.IsUse == true
  52. select s)
  53. .FirstOrDefault();
  54. if (workSchedule == null)
  55. {
  56. dueDateList.Add(new AvailableDueDateType() { DueDate = DateTime.Now });
  57. return dueDateList;
  58. }
  59. var workResult = (
  60. from s in db.FmsWorkResult
  61. where s.SiteId == siteId &&
  62. s.WorkRequestId == workRequestId
  63. select s)
  64. .FirstOrDefault();
  65. // 주기 간격
  66. int cycleSize = workSchedule.CycleSize;
  67. // 현재일자
  68. //DateTime currentDueDate = DateTime.Now;
  69. // 종료일자
  70. DateTime EndDueDate = workResult.EndDate;
  71. // 확정일자
  72. DateTime plannDueDate = new DateTime();
  73. // 주기 유형
  74. // CycleUnitId :: 1: 일간 / 2: 주간 / 3: 월간 / 4: 분기 / 5: 반기 / 6: 년간
  75. switch (workSchedule.CycleUnitId)
  76. {
  77. case 1:
  78. plannDueDate = EndDueDate.AddDays(cycleSize);
  79. break;
  80. case 2:
  81. plannDueDate = EndDueDate.AddDays(cycleSize * 7);
  82. break;
  83. case 3:
  84. plannDueDate = EndDueDate.AddMonths(cycleSize);
  85. break;
  86. case 4:
  87. plannDueDate = EndDueDate.AddMonths(cycleSize * 3);
  88. break;
  89. case 5:
  90. plannDueDate = EndDueDate.AddMonths(cycleSize * 6);
  91. break;
  92. case 6:
  93. plannDueDate = EndDueDate.AddYears(cycleSize);
  94. break;
  95. }
  96. // 휴일근무 사용안함 인 경우
  97. if (workSchedule.HolidayWorkTypeId == 0)
  98. {
  99. dueDateList.Add(new AvailableDueDateType() { DueDate = plannDueDate });
  100. return dueDateList;
  101. }
  102. // 휴일 리스트
  103. List<DateTime> holidayList = GetHolidayList(siteId);
  104. // 토요일, 일요일 휴무 여부
  105. bool isUseSaturdayHoliday = false;
  106. bool isUseSundayHoliday = false;
  107. var holidayWeekend = db.CmHolidayWeekend
  108. .Where(hw => hw.SiteId == siteId && hw.IsUse == true)
  109. .FirstOrDefault();
  110. if (holidayWeekend == null)
  111. {
  112. isUseSaturdayHoliday = true;
  113. isUseSundayHoliday = true;
  114. }
  115. else
  116. {
  117. isUseSaturdayHoliday = holidayWeekend.Saturday;
  118. isUseSundayHoliday = holidayWeekend.Sunday;
  119. }
  120. // 확정일자 계산
  121. bool isHoliday = CheckIsHoliday(plannDueDate, isUseSaturdayHoliday, isUseSundayHoliday, holidayList);
  122. // 휴일근무인 경우
  123. if (workSchedule.HolidayWorkTypeId == 1)
  124. {
  125. while (!isHoliday)
  126. {
  127. plannDueDate = plannDueDate.AddDays(1);
  128. isHoliday = CheckIsHoliday(plannDueDate, isUseSaturdayHoliday, isUseSundayHoliday, holidayList);
  129. }
  130. dueDateList.Add(new AvailableDueDateType() { DueDate = plannDueDate });
  131. return dueDateList;
  132. }
  133. // 익일근무인 경우
  134. if (workSchedule.HolidayWorkTypeId == 2)
  135. {
  136. while (isHoliday)
  137. {
  138. plannDueDate = plannDueDate.AddDays(1);
  139. isHoliday = CheckIsHoliday(plannDueDate, isUseSaturdayHoliday, isUseSundayHoliday, holidayList);
  140. }
  141. dueDateList.Add(new AvailableDueDateType() { DueDate = plannDueDate });
  142. return dueDateList;
  143. }
  144. return dueDateList;
  145. }
  146. private bool CheckIsHoliday(DateTime plannDueDate, bool isUseSaturdayHoliday, bool isUseSundayHoliday, List<DateTime> holidayList)
  147. {
  148. // 토,일요일인 경우 리턴
  149. if (plannDueDate.DayOfWeek == DayOfWeek.Saturday && isUseSaturdayHoliday)
  150. {
  151. return true;
  152. }
  153. if (plannDueDate.DayOfWeek == DayOfWeek.Sunday && isUseSundayHoliday)
  154. {
  155. return true;
  156. }
  157. // 휴무일에 포함되어 있는 경우 리턴
  158. foreach (var item in holidayList)
  159. {
  160. if (item.Month == plannDueDate.Month && item.Day == plannDueDate.Day)
  161. {
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. private List<DateTime> GetHolidayList(int siteId)
  168. {
  169. List<DateTime> holidayList = new List<DateTime>();
  170. var cmHolidays = db.CmHoliday
  171. .Where(h => h.SiteId == siteId && h.IsUse == true)
  172. .ToList();
  173. int currentYear = DateTime.Now.Year;
  174. foreach (var item in cmHolidays)
  175. {
  176. if (item.IsLunar)
  177. {
  178. Solar sol = new Solar()
  179. {
  180. solarYear = currentYear,
  181. solarMonth = item.HolidayMonth,
  182. solarDay = item.HolidayDay
  183. };
  184. Lunar lun = LunarSolarConverter.SolarToLunar(sol);
  185. DateTime date = new DateTime(lun.lunarYear, lun.lunarMonth, lun.lunarDay);
  186. holidayList.Add(date);
  187. }
  188. else
  189. {
  190. DateTime date = new DateTime(currentYear, item.HolidayMonth, item.HolidayDay);
  191. holidayList.Add(date);
  192. }
  193. }
  194. // 커스텀 휴일 리스트
  195. var customHolidays = db.CmHolidayCustom
  196. .Where(ch => ch.SiteId == siteId
  197. && ch.HolidayDate >= DateTime.Now
  198. && ch.IsUse == true)
  199. .ToList();
  200. foreach (var item in customHolidays)
  201. {
  202. holidayList.Add(item.HolidayDate);
  203. }
  204. return holidayList;
  205. }
  206. protected override void Dispose(bool disposing)
  207. {
  208. if (disposing)
  209. {
  210. db.Dispose();
  211. }
  212. base.Dispose(disposing);
  213. }
  214. }
  215. public class AvailableDueDateType
  216. {
  217. public DateTime DueDate { get; set; }
  218. }
  219. }