demo20230512/Service/RiskAssmt/WasteMaterialDataService.cs

109 lines
4.2 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using Weee.DAL;
using Weee.Models;
using Weee.ViewModels.DataTransferObject;
namespace Weee.Service
{
public class WasteMaterialDataService : WeeeLCADataService
{
public WasteMaterialDataService(WeeeDataContext db)
: base(db)
{
}
public IQueryable<LCARiskAssmtSurveyForm_WasteMaterial> GetList(int LCAID)
{
if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized");
return _db.Set<LCARiskAssmtSurveyForm_WasteMaterial>().Where(x => x.LCAID == LCAID)
.OrderByDescending(x => x.ID);
}
public LCARiskAssmtSurveyForm_WasteMaterial Save(LCARiskAssmtSurveyForm_WasteMaterial toBeSave, out decimal totalQuantity)
{
var user = GetUserContext();
if (!AuthorizedLCAs.Contains(toBeSave.LCAID)) throw new Exception("not authorized");
var entry = _db.Entry(toBeSave);
var list = GetList(toBeSave.LCAID).Select(x => new { x.ID, x.TotalQuantity }).ToList();
var lca = GetLCA(toBeSave.LCAID);
CheckMaterialName(toBeSave);
if (toBeSave.ID == 0)
{
toBeSave.CreatedBy = user.Id;
toBeSave.CreatedDate = DateTime.Now;
entry.State = EntityState.Added;
totalQuantity = list.Sum(x => x.TotalQuantity);
}
else
{
toBeSave.ModifiedBy = user.Id;
toBeSave.ModifiedDate = DateTime.Now;
entry.State = EntityState.Modified;
entry.Property(x => x.LCAID).IsModified = false;
totalQuantity = list.Where(x => x.ID != toBeSave.ID).Sum(x => x.TotalQuantity);
}
totalQuantity += toBeSave.TotalQuantity;
lca.WasteMaterialTotalQuantity = totalQuantity;
_db.SaveChanges();
return toBeSave;
}
public LCA UpdateLCA(int LCAID, LCAWasteMaterialDTO ToBeSave)
{
if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized");
var lca = base.GetLCA(LCAID);
lca.WasteMaterialTotalQuantity = ToBeSave.WasteMaterialTotalQuantity;
lca.WasteMaterialShippingDistance = ToBeSave.WasteMaterialShippingDistance;
lca.WasteMaterialTKM1 = ToBeSave.WasteMaterialTKM1;
lca.WasteMaterialNumOfBarrels = ToBeSave.WasteMaterialNumOfBarrels;
lca.WasteMaterialWeight = ToBeSave.WasteMaterialWeight;
lca.WasteMaterialWorkingDays = ToBeSave.WasteMaterialWorkingDays;
lca.WasteMaterialTotalOutputKg = ToBeSave.WasteMaterialTotalOutputKg;
lca.WasteMaterialTotalOutputT = ToBeSave.WasteMaterialTotalOutputT;
lca.WasteMaterialTreatmentPlantShippingDistance = ToBeSave.WasteMaterialTreatmentPlantShippingDistance;
lca.WasteMaterialTKM2 = ToBeSave.WasteMaterialTKM2;
_db.SaveChanges();
return lca;
}
private void CheckMaterialName(LCARiskAssmtSurveyForm_WasteMaterial toBeSave)
{
if (_db.Set<LCARiskAssmtSurveyForm_WasteMaterial>().Any(i => i.LCAID == toBeSave.LCAID && i.MaterialName == toBeSave.MaterialName && (toBeSave.ID == 0 || toBeSave.ID != i.ID)))
{
throw new Exception($"物品名称不得重複:{nameof(toBeSave.MaterialName)}");
}
}
public void Delete(int ID)
{
var entry = _db.Set<LCARiskAssmtSurveyForm_WasteMaterial>().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 data in this status: " + LCA.Status.ToString());
var ToBeDelete = _db.Entry(entry);
ToBeDelete.State = EntityState.Deleted;
var left = GetList(entry.LCAID).Where(x => x.ID != ID);
LCA.WasteMaterialTotalQuantity = left.Count() > 0 ? left.Sum(x => x.TotalQuantity) : 0;
_db.SaveChanges();
}
}
}