using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; using Weee.DAL; using Weee.Models; using Weee.Models.ExtensionMethods; namespace Weee.Service { public class WeeeMonthlyDataService : WeeeSheetDataService// power water workhour steam , 每個月必須填寫之資料 , 其中work hour 之係數為non yearly , 其餘為yearly { public WeeeMonthlyDataService(WeeeDataContext db) : base(db) { } public MonthlyData SaveMonthlyData(MonthlyData ToBeSave) { if (!AuthorizedLCAs.Contains(ToBeSave.LCAID)) throw new Exception("not authorized"); var LCA = _db.LCAs.AsNoTracking().Where(x => x.ID == ToBeSave.LCAID).Single(); if (LCA.Status != LCAStatus.Processing && LCA.Status != LCAStatus.Rejected) throw new Exception("a Monthly data should not be change in this LCA status " + LCA.Status); var Months = LCA.Months(); if (ToBeSave.Index >= Months) throw new Exception("error , the Index is larger then the months of LCA"); var dbset = _db.Set(ToBeSave.GetType()); var entry = _db.Entry(ToBeSave); if (ToBeSave.ID != 0) { entry.State = EntityState.Modified; if (entry.GetDatabaseValues().GetValue("LCAID") != entry.Property(x => x.LCAID).CurrentValue) throw new Exception("error , LCAID should not be change"); if (entry.GetDatabaseValues().GetValue("Index") != entry.Property(x => x.Index).CurrentValue) throw new Exception("error , Index should not be change"); } else if (ToBeSave.GetType() == typeof(LCACommonSurveyForm_SteamUsages)) { if (dbset.Cast().Where(x => x.LCAID == ToBeSave.LCAID && x.Index == ToBeSave.Index).Count() == 0) entry.State = EntityState.Added; } else if (ToBeSave.GetType() == typeof(LCACommonSurveyForm_PowerUsages)) { if (dbset.Cast().Where(x => x.LCAID == ToBeSave.LCAID && x.Index == ToBeSave.Index).Count() == 0) entry.State = EntityState.Added; } else if (ToBeSave.GetType() == typeof(ProductLCAFabSurveyForm_WaterUsages)) { if (dbset.Cast().Where(x => x.LCAID == ToBeSave.LCAID && x.Index == ToBeSave.Index).Count() == 0) entry.State = EntityState.Added; } else if (ToBeSave.GetType() == typeof(LCACommonSurveyForm_WorkHours)) { if (dbset.Cast().Where(x => x.LCAID == ToBeSave.LCAID && x.Index == ToBeSave.Index&&x.Type==((LCACommonSurveyForm_WorkHours)ToBeSave).Type).Count() == 0) entry.State = EntityState.Added; } _db.SaveChanges(); return ToBeSave; } } }