demo20230512/Service/RiskAssmt/PowerConsumptionDataService.cs
2023-05-12 10:20:28 +08:00

113 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using Weee.DAL;
using Weee.Models;
namespace Weee.Service
{
public class PowerConsumptionDataService : WeeeLCADataService
{
public PowerConsumptionDataService(WeeeDataContext db)
: base(db)
{
}
public IQueryable<LCARiskAssmtSurveyForm_PowerConsumption> GetList(int LCAID)
{
if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized");
return _db.Set<LCARiskAssmtSurveyForm_PowerConsumption>().Where(x => x.LCAID == LCAID)
.OrderByDescending(x => x.ID);
}
public LCARiskAssmtSurveyForm_PowerConsumption Save(LCARiskAssmtSurveyForm_PowerConsumption toBeSave)
{
var user = GetUserContext();
CheckScenario(toBeSave);
if (!AuthorizedLCAs.Contains(toBeSave.LCAID)) throw new Exception("not authorized");
var typeModels = _db.Set<LCARiskAssmtSurveyForm_PowerConsumption>().AsNoTracking().Where(x => x.LCAID == toBeSave.LCAID && x.ModelType == toBeSave.ModelType).ToList();
var entry = _db.Entry(toBeSave);
if (toBeSave.ID == 0)
{
toBeSave.CreatedBy = user.Id;
toBeSave.CreatedDate = DateTime.Now;
entry.State = EntityState.Added;
typeModels.Add(toBeSave);
}
else
{
toBeSave.ModifiedBy = user.Id;
toBeSave.ModifiedDate = DateTime.Now;
entry.State = EntityState.Modified;
entry.Property(x => x.LCAID).IsModified = false;
}
if (typeModels.Count(x => IsStandbyPower(x)) == 1)
{
var standbyPower = typeModels.First(x => IsStandbyPower(x));
if (standbyPower.ID != toBeSave.ID)
{
var sumWorkingHour = typeModels.Where(x => IsNotStandbyPower(x)).Sum(x => x.WorkingHour);
standbyPower.WorkingHour = 8760 - sumWorkingHour;
_db.Entry(standbyPower).State = EntityState.Modified;
}
}
_db.SaveChanges();
return toBeSave;
}
private bool IsStandbyPower(LCARiskAssmtSurveyForm_PowerConsumption powerConsumption)
{
return powerConsumption.Scenario.Contains("待機") || powerConsumption.Scenario.Contains("關機");
}
private bool IsNotStandbyPower(LCARiskAssmtSurveyForm_PowerConsumption powerConsumption)
{
return !IsStandbyPower(powerConsumption);
}
private void CheckScenario(LCARiskAssmtSurveyForm_PowerConsumption toBeSave)
{
if (_db.Set<LCARiskAssmtSurveyForm_PowerConsumption>().Any(i => i.LCAID == toBeSave.LCAID && i.ModelType == toBeSave.ModelType && i.Scenario == toBeSave.Scenario && (toBeSave.ID == 0 || toBeSave.ID != i.ID)))
{
throw new Exception($"同機種型號的使用场景不得重複:{nameof(toBeSave.Scenario)}");
}
}
public void Delete(int ID)
{
var entry = _db.Set<LCARiskAssmtSurveyForm_PowerConsumption>().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 typeModels = _db.Set<LCARiskAssmtSurveyForm_PowerConsumption>()
.AsNoTracking()
.Where(x => x.LCAID == entry.LCAID && x.ModelType == entry.ModelType && x.ID != ID)
.ToList();
if (typeModels.Count(x => IsStandbyPower(x)) == 1)
{
var standbyPower = typeModels.First(x => IsStandbyPower(x));
var sumWorkingHour = typeModels.Where(x => IsNotStandbyPower(x)).Sum(x => x.WorkingHour);
standbyPower.WorkingHour = 8760 - sumWorkingHour;
_db.Entry(standbyPower).State = EntityState.Modified;
}
var ToBeDelete = _db.Entry(entry);
ToBeDelete.State = EntityState.Deleted;
_db.SaveChanges();
}
}
}