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; using Weee.Models.Paramemter; namespace Weee.Service { public class WaterUsageDataService : WeeeLCADataService { public WaterUsageDataService(WeeeDataContext db) : base(db) { } #region // if the sheet is monthly data : page , search page size do not care public IQueryable GetSheet(int LCAID) where TEntity : class, RiskAssmtSheetData { if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized"); return _db.Set().Where(x => x.LCAID == LCAID) .OrderByDescending(x => x.ID); } public IEnumerable SaveSheet(IEnumerable ToBeSave) { var user = GetUserContext(); foreach (var item in ToBeSave) { if (!AuthorizedLCAs.Contains(item.LCAID)) throw new Exception("not authorized"); var entry = _db.Entry(item); if (item.ID == 0) { item.CreatedBy = user.Id; item.CreatedDate = DateTime.Now; entry.State = EntityState.Added; } else { item.ModifiedBy= user.Id; item.ModifiedDate = DateTime.Now; entry.State = EntityState.Modified; } } _db.SaveChanges(); return ToBeSave; } public SheetHeader GetSheetHeader(int LCAID, Categories cate) { if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized"); var sheetheader = _db.SheetHeaders.Find(new object[] { LCAID, cate }); if (sheetheader == null) return new SheetHeader(); else return _db.SheetHeaders.Find(new object[] { LCAID, cate }); } public void SaveSheetHeader(SheetHeader header) { if (!AuthorizedLCAs.Contains(header.LCAID)) throw new Exception("not authorized"); var a = _db.SheetHeaders.Find(new object[] { header.LCAID, header.Category }); if (a == null) _db.SheetHeaders.Add(header); else { _db.Entry(a).State = EntityState.Detached; _db.Entry(header).State = EntityState.Modified; } _db.SaveChanges(); } public RiskAssmtSheetData SaveSheetItem(RiskAssmtSheetData toBeSave) { var user = GetUserContext(); if (!AuthorizedLCAs.Contains(toBeSave.LCAID)) throw new Exception("not authorized"); var entry = _db.Entry(toBeSave); if (toBeSave.ID == 0) { toBeSave.CreatedBy = user.Id; toBeSave.CreatedDate = DateTime.Now; entry.State = EntityState.Added; } else { toBeSave.ModifiedBy = user.Id; toBeSave.ModifiedDate = DateTime.Now; entry.State = EntityState.Modified; entry.Property(x => x.LCAID).IsModified = false; } _db.SaveChanges(); return toBeSave; } public void DeleteSheet(int LCAID) where TEntity : class, RiskAssmtSheetData { var LCA = GetLCA(LCAID); if (LCA.Status != LCAStatus.New && LCA.Status != LCAStatus.Processing && LCA.Status != LCAStatus.Rejected) throw new Exception("Business logic error , should not delete sheet data in this status: " + LCA.Status.ToString()); var entries = _db.Set().Where(x => x.LCAID == LCAID); foreach (var entry in entries) { var ToBeDelete = _db.Entry(entry); ToBeDelete.State = EntityState.Deleted; } _db.SaveChanges(); } public void DeleteMaterials(int LCAID, MaterialType MType) { if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("Not authroize"); var mats = _db.ProductLCAProductSurveyForm_Material.Where(x => x.LCAID == LCAID && x.MaterialType == MType); _db.ProductLCAProductSurveyForm_Material.RemoveRange(mats); _db.SaveChanges(); } public void DeleteSheetItem(int ID) where TEntity : class, RiskAssmtSheetData { var entry = _db.Set().Where(x => x.ID == ID).Single(); var LCA = GetLCA(entry.LCAID); if (LCA.Status != LCAStatus.New && LCA.Status != LCAStatus.Processing) throw new Exception("Business logic error , should not delete sheet data in this status: " + LCA.Status.ToString()); var ToBeDelete = _db.Entry(entry); ToBeDelete.State = EntityState.Deleted; _db.SaveChanges(); } #endregion } }