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

124 lines
4.5 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 WeeeProductInventoryDataService :WeeeDataAuthorizeService
{
public WeeeProductInventoryDataService(WeeeDataContext d) : base(d)
{
}
public IEnumerable<ProductInventoryStageData> GetAllProductInventoryStageData(int LCAID)
{
var productID = _db.ProductLCAs.Find(LCAID).ProductID;
IEnumerable <ProductInventoryStageData> ret = _db.ProductInventoryStageData.Where(x => x.ProductID == productID);
return ret;
}
public IEnumerable<ProductInventoryParameterData> GetAllProductInventoryParameterData(int LCAID)
{
var productID = _db.ProductLCAs.Find(LCAID).ProductID;
IEnumerable<ProductInventoryParameterData> ret = _db.ProductInventoryParameterData.Where(x => x.ProductID == productID);
return ret;
}
public int SaveStageData(ProductInventoryStageData sour)
{
var totalHoursQueryable = _db.ProductInventoryStageData.Where(x => x.ProductID == sour.ProductID);
var totalHours = 0M;
if (totalHoursQueryable.Any())
{
totalHours = totalHoursQueryable.Sum(x => x.HourPerDayInOperation);
}
if (sour.ID == 0)
totalHours += sour.HourPerDayInOperation;
//if (totalHours > 24)
// throw new Exception("錯誤:各階段使用時數總和不得大於24個小時");
int re = 0;
if (sour.ID == 0)
{
var entity = _db.ProductInventoryStageData.Add(sour);
_db.SaveChanges();
re = entity.ID;
}
else
{
var list = _db.ProductInventoryStageData.Where(x => x.ID == sour.ID).ToList();
if (list != null && list.Count() > 0)
{
re = sour.ID;
var dbIt = list.FirstOrDefault();
dbIt.OperationName = sour.OperationName;
dbIt.HourPerDayInOperation = sour.HourPerDayInOperation;
dbIt.WattInOperation = sour.WattInOperation;
dbIt.LCAID= sour.LCAID;
_db.SaveChanges();
}
else
throw new Exception("查無Id: " + sour.ID.ToString() + "資料");
}
return re;
}
public int SaveParameterData(ProductInventoryParameterData sour)
{
int re = 0;
if (sour.ID == 0)
{
var entity = _db.ProductInventoryParameterData.Add(sour);
_db.SaveChanges();
re = entity.ID;
}
else
{
var list = _db.ProductInventoryParameterData.Where(x => x.ID == sour.ID).ToList();
if (list != null && list.Count() > 0)
{
re = sour.ID;
var dbIt = list.FirstOrDefault();
dbIt.ParameterCountry = sour.ParameterCountry;
dbIt.ParameterDescription = sour.ParameterDescription;
dbIt.ParameterID = sour.ParameterID;
dbIt.ParameterRegion = sour.ParameterRegion;
dbIt.ParameterUnit = sour.ParameterUnit;
dbIt.ParameterValue = sour.ParameterValue;
dbIt.RegionAmount = sour.RegionAmount;
dbIt.LCAID= sour.LCAID;
_db.SaveChanges();
}
else
throw new Exception("查無Id: " + sour.ID.ToString() + "資料");
}
return re;
}
public void DeleteStage(int Id)
{
var list = _db.ProductInventoryStageData.Where(x => x.ID == Id).ToList();
if (list != null && list.Count > 0)
{
var dbIt = list.FirstOrDefault();
_db.ProductInventoryStageData.Remove(dbIt);
_db.SaveChanges();
}
}
public void DeleteParameter(int Id)
{
var list = _db.ProductInventoryParameterData.Where(x => x.ID == Id).ToList();
if (list != null && list.Count > 0)
{
var dbIt = list.FirstOrDefault();
_db.ProductInventoryParameterData.Remove(dbIt);
_db.SaveChanges();
}
}
}
}