SiteService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Microsoft.Extensions.Logging;
  2. using FMSAdmin.Data;
  3. using System.Linq;
  4. using FMSAdmin.Helpers;
  5. using FMSAdmin.Entities;
  6. using Microsoft.EntityFrameworkCore;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. namespace FMSAdmin.Services {
  11. public class SiteService {
  12. private readonly ILogger<SiteService> _logger;
  13. private readonly FMSContext _context;
  14. private readonly StorageHelper _storage;
  15. public SiteService(
  16. ILogger<SiteService> logger,
  17. FMSContext context,
  18. StorageHelper storage
  19. ) {
  20. _logger = logger;
  21. _context = context;
  22. _storage = storage;
  23. }
  24. public IQueryable<CmSite> GetAll() {
  25. var query = _context.CmSite.AsNoTracking();
  26. return query;
  27. }
  28. public IQueryable<CmSite> Get(int siteId) {
  29. var data = _context.CmSite.Where(x => x.SiteId == siteId);
  30. return data;
  31. }
  32. public void Create(CmSite data) {
  33. using (var transaction = _context.Database.BeginTransaction()) {
  34. checkName(data);
  35. var image = data.ImageFile;
  36. data.ImageFile = null;
  37. _context.CmSite.Add(data);
  38. _context.SaveChanges();
  39. if (image != null && image.IsUpload) {
  40. var category = _context.CmFileCategory.First(x => x.Name == "site");
  41. data.ImageFile = image;
  42. data.ImageFile.SiteId = data.SiteId;
  43. data.ImageFile.FileCategory = category;
  44. data.ImageFile.CreatedDate = DateTime.Now;
  45. _context.CmSite.Update(data);
  46. _context.SaveChanges();
  47. }
  48. if (data.ImageFile != null && data.ImageFile.IsUpload) {
  49. // 카피
  50. _storage.CopyEntity(data.ImageFile.Path, data.ImageFile);
  51. }
  52. var businessField = new CmBusinessField {
  53. Name = "공통",
  54. IsUse = true
  55. };
  56. _context.CmBusinessField.Add(businessField);
  57. _context.SaveChanges();
  58. if (!_context.SiteConfig.Single().UseMaterialType) {
  59. var meterialType = new FmsMaterialCodeType {
  60. //SiteId = data.SiteId,
  61. Name = "자재구분미사용",
  62. Nickname = "자재구분미사용",
  63. IsUse = true
  64. };
  65. _context.FmsMaterialCodeType.Add(meterialType);
  66. _context.SaveChanges();
  67. }
  68. //휴일 작업 구분 자동 생성
  69. CreateWorkCodeHolidayWorkType(data.SiteId);
  70. //작업 주기 코드 자동생성
  71. CreateWorkCodeCycleUnit(data.SiteId);
  72. //휴일(토/일) 주말 설정 자동생성
  73. CreateHolidayWeekend(data.SiteId);
  74. //CmPartnerType 자동생성 최초 사이트 생성시
  75. if (_context.CmPartnerType.Count() == 0) {
  76. var partnerType = new CmPartnerType {
  77. Name = "검사기관",
  78. IsUse = true
  79. };
  80. _context.CmPartnerType.Add(partnerType);
  81. _context.SaveChanges();
  82. }
  83. //빌딩 자동 생성
  84. var building = new CmBuilding {
  85. SiteId = data.SiteId,
  86. Name = data.Name,
  87. IsUse = true
  88. };
  89. _context.CmBuilding.Add(building);
  90. _context.SaveChanges();
  91. transaction.Commit();
  92. }
  93. }
  94. public void Edit(int siteId, CmSite data) {
  95. using (var transaction = _context.Database.BeginTransaction()) {
  96. if (data.ImageFile != null && data.ImageFile.IsUpload) {
  97. data.ImageFile.SiteId = data.SiteId;
  98. var category = _context.CmFileCategory.First(x => x.Name == "site");
  99. data.ImageFile.FileCategory = category;
  100. data.ImageFile.CreatedDate = DateTime.Now;
  101. }
  102. var persist = _context.CmSite
  103. .Where(x => x.SiteId == siteId).FirstOrDefault();
  104. if (persist == null) {
  105. throw new ServiceException("정보를 찾을 수 없습니다.");
  106. }
  107. if (!persist.Name.Equals(data.Name)) {
  108. checkName(data);
  109. }
  110. _logger.LogInformation("수정");
  111. persist.Name = data.Name;
  112. persist.PhoneNo = data.PhoneNo;
  113. persist.AddressZip1 = data.AddressZip1;
  114. persist.CompletionDate = data.CompletionDate;
  115. persist.Address1 = data.Address1;
  116. persist.Adderss2 = data.Adderss2;
  117. persist.BuildingSize = data.BuildingSize;
  118. persist.GrossArea = data.GrossArea;
  119. persist.BuildingArea = data.BuildingArea;
  120. persist.FloorAreaRatio = data.FloorAreaRatio;
  121. persist.BuildingCoverageRatio = data.BuildingCoverageRatio;
  122. persist.MainUse = data.MainUse;
  123. persist.MainUseIndex = data.MainUseIndex;
  124. persist.IsControlSchedule = data.IsControlSchedule;
  125. persist.ScheduleCheckHour = data.ScheduleCheckHour;
  126. persist.Standard = data.Standard;
  127. persist.Hdd = data.Hdd;
  128. persist.Cdd = data.Cdd;
  129. persist.Url = data.Url;
  130. persist.IsUse = data.IsUse;
  131. persist.ApiSido = data.ApiSido;
  132. persist.ApiSigun = data.ApiSigun;
  133. persist.ImageMapX = data.ImageMapX;
  134. persist.ImageMapY = data.ImageMapY;
  135. persist.DesignSupervision = data.DesignSupervision;
  136. persist.ConstructionCompany = data.ConstructionCompany;
  137. persist.ConstructionPeriod = data.ConstructionPeriod;
  138. persist.Structure = data.Structure;
  139. persist.LobbyFloor = data.LobbyFloor;
  140. persist.OfficeFloor = data.OfficeFloor;
  141. persist.ToiletFloor = data.ToiletFloor;
  142. persist.EVHall = data.EVHall;
  143. persist.ParkingLot = data.ParkingLot;
  144. persist.ParkingFacilities = data.ParkingFacilities;
  145. persist.OuterWall = data.OuterWall;
  146. persist.LandArea = data.LandArea;
  147. persist.LandscapingArea = data.LandscapingArea;
  148. persist.BuildingFloors = data.BuildingFloors;
  149. persist.BuildingHeight = data.BuildingHeight;
  150. persist.ConversionRate = data.ConversionRate;
  151. persist.AuditoriumFacilities = data.AuditoriumFacilities;
  152. if (data.ImageFile != null && data.ImageFile.IsUpload) {
  153. persist.ImageFile = data.ImageFile;
  154. }
  155. persist.Comment = data.Comment;
  156. _context.CmSite.Update(persist);
  157. _context.SaveChanges();
  158. if (data.ImageFile != null && data.ImageFile.IsUpload) {
  159. // 카피
  160. _storage.CopyEntity(data.ImageFile.Path, persist.ImageFile);
  161. if (persist.ImageFile != null) {
  162. // 파일 삭제시 진짜 DB삭제?
  163. }
  164. }
  165. transaction.Commit();
  166. }
  167. }
  168. //작업주기 자동 생성
  169. public void CreateWorkCodeCycleUnit(int siteId) {
  170. IList<FmsWorkCodeCycleUnit> workCodeCyleUnits = new List<FmsWorkCodeCycleUnit>();
  171. workCodeCyleUnits.Add(new FmsWorkCodeCycleUnit {
  172. SiteId = siteId,
  173. CycleUnitId = 1,
  174. Name = "일간",
  175. Description = "매일 처리해야하는 작업"
  176. });
  177. workCodeCyleUnits.Add(new FmsWorkCodeCycleUnit {
  178. SiteId = siteId,
  179. CycleUnitId = 2,
  180. Name = "주간",
  181. Description = "일주일 단위로 처리해야 하는작업"
  182. });
  183. workCodeCyleUnits.Add(new FmsWorkCodeCycleUnit {
  184. SiteId = siteId,
  185. CycleUnitId = 3,
  186. Name = "월간",
  187. Description = "매월 처리해야하는 작업"
  188. });
  189. workCodeCyleUnits.Add(new FmsWorkCodeCycleUnit {
  190. SiteId = siteId,
  191. CycleUnitId = 4,
  192. Name = "분기",
  193. Description = "분기에 1회씩 처리해야하는 작업"
  194. });
  195. workCodeCyleUnits.Add(new FmsWorkCodeCycleUnit {
  196. SiteId = siteId,
  197. CycleUnitId = 5,
  198. Name = "반기",
  199. Description = "6개월에 1회씩 처리해야하는 작업"
  200. });
  201. workCodeCyleUnits.Add(new FmsWorkCodeCycleUnit {
  202. SiteId = siteId,
  203. CycleUnitId = 6,
  204. Name = "년간",
  205. Description = "1년에 한번씩 처리해야 하는 작업"
  206. });
  207. _context.FmsWorkCodeCycleUnit.AddRange(workCodeCyleUnits);
  208. _context.SaveChanges();
  209. }
  210. //휴일 작업 구분 자동 생성
  211. public void CreateWorkCodeHolidayWorkType(int siteId) {
  212. IList<FmsWorkCodeHolidayWorkType> holidayWorkTypes = new List<FmsWorkCodeHolidayWorkType>();
  213. holidayWorkTypes.Add(new FmsWorkCodeHolidayWorkType {
  214. SiteId = siteId,
  215. HolidayWorkTypeId = 0,
  216. Name = "사용 안 함",
  217. IsUse = true
  218. });
  219. holidayWorkTypes.Add(new FmsWorkCodeHolidayWorkType {
  220. SiteId = siteId,
  221. HolidayWorkTypeId = 1,
  222. Name = "휴일작업",
  223. IsUse = true
  224. });
  225. holidayWorkTypes.Add(new FmsWorkCodeHolidayWorkType {
  226. SiteId = siteId,
  227. HolidayWorkTypeId = 2,
  228. Name = "익일작업",
  229. IsUse = true
  230. });
  231. _context.FmsWorkCodeHolidayWorkType.AddRange(holidayWorkTypes);
  232. _context.SaveChanges();
  233. }
  234. public void checkName(CmSite data) {
  235. var check = _context.CmSite.Where(x => x.Name == data.Name).Count();
  236. if (check > 0) {
  237. throw new ServiceException("이미 동일 이름이 존재합니다.");
  238. }
  239. }
  240. public void Delete(int siteId) {
  241. var data = _context.CmSite.First(x => x.SiteId == siteId);
  242. data.IsUse = false;
  243. _context.SaveChanges();
  244. }
  245. //휴일(토/일) 주말 설정 자동생성
  246. public void CreateHolidayWeekend(int siteId) {
  247. CmHolidayWeekend holiday = new CmHolidayWeekend {
  248. SiteId = siteId,
  249. Saturday = true,
  250. Sunday = true,
  251. IsUse = true
  252. };
  253. _context.CmHolidayWeekend.Add(holiday);
  254. _context.SaveChanges();
  255. }
  256. }
  257. }