public static void DeleteWorkZone(int WorkZoneID) { using (DBContext db = new DBContext()) { using (System.Data.Common.DbTransaction tran = db.UseTransaction()) { try { WorkZone wz = new WorkZone(); wz.WorkZoneId = WorkZoneID; wz.Delete(); List <WorkZoneDetail> listWZD = WorkZoneDetail.GetWorkZoneDetailByWorkZoneID(WorkZoneID); foreach (WorkZoneDetail wzd in listWZD) { wzd.Delete(); } List <DetailProblem> listProblem = DetailProblem.GetAllDetailProblem(WorkZoneID); foreach (DetailProblem p in listProblem) { p.Delete(); } } catch (Exception e) { tran.Rollback(); logger.Error("Error DeleteWorkZone", e); throw e; } } } }
public static void InsertUpdateWorkZone(string directoryApp, WorkZone info, List <WorkZoneDetail> listDetail) { logger.InfoFormat("Begin call InsertUpdateWorkZone , workZoneName = {0} , directoryApp = {1}", info.Name, directoryApp); WorkZone workZoneDb = WorkZone.GetWorkZoneFromMachine(info.MachineId, info.CompanyId, info.Name); info.CompanyName = Company.GetCompanyName(info.CompanyId); info.FactoryName = Factory.GetFactoryName(info.FactoryId); int idz = 0; using (DBContext db = new DBContext()) { using (System.Data.Common.DbTransaction tran = db.UseTransaction()) { try { int nextWorkZoneDetailId = 1; if (workZoneDb == null) { idz = InsertWorkZoneWithID(info); info.WorkZoneId = idz; foreach (WorkZoneDetail wzd in listDetail) { wzd.WorkZoneId = info.WorkZoneId; wzd.WorkZoneDetailId = nextWorkZoneDetailId; wzd.CreateAccount = wzd.ModifiedAccount = info.CreateAccount; wzd.Insert(); nextWorkZoneDetailId++; } } else { info.WorkZoneId = workZoneDb.WorkZoneId; info.Update(); // Insert , update , detail List <WorkZoneDetail> listDetailDB = WorkZoneDetail.GetWorkZoneDetailByWorkZoneID(workZoneDb.WorkZoneId); foreach (WorkZoneDetail i in listDetailDB) { i.Delete(); } foreach (WorkZoneDetail i in listDetail) { i.WorkZoneId = info.WorkZoneId; i.WorkZoneDetailId = nextWorkZoneDetailId; i.CreateAccount = i.ModifiedAccount = info.CreateAccount; i.Insert(); nextWorkZoneDetailId++; } } //Upload file WorkZone if (!string.IsNullOrEmpty(info.Base64Data)) { string pathToUpload = directoryApp + Common.GetFolderWorkZone(info.CompanyId, info.CompanyName, info.FactoryId, info.FactoryName, info.WorkZoneId, info.Name); pathToUpload = pathToUpload.Replace("/", "\\"); if (!Directory.Exists(pathToUpload)) { Directory.CreateDirectory(pathToUpload); } pathToUpload = Path.Combine(pathToUpload, info.ImageFile); Common.UploadFileToPath(info.Base64Data, pathToUpload); } //upload Detail foreach (WorkZoneDetail i in listDetail) { if (!string.IsNullOrEmpty(i.Base64Data)) { string pathToUpload = directoryApp + Common.GetFolderWorkZoneDetail(info.CompanyId, info.CompanyName, info.FactoryId, info.FactoryName, info.WorkZoneId, info.Name); pathToUpload = pathToUpload.Replace("/", "\\"); if (!Directory.Exists(pathToUpload)) { Directory.CreateDirectory(pathToUpload); } pathToUpload = Path.Combine(pathToUpload, i.ImageFile); Common.UploadFileToPath(i.Base64Data, pathToUpload); } } tran.Commit(); } catch (Exception ex) { tran.Rollback(); if (idz != 0) { info.Delete(); } logger.Error("Error InsertUpdateWorkZone", ex); throw ex; } } } }