demo20230512/Controllers/Api/DataQualityLevelController.cs
2023-05-12 10:20:28 +08:00

414 lines
19 KiB
C#

using NLog;
using Qcarbon.Interfaces.Database.Lca.Prod;
using Qcarbon.ViewModels.DTO;
using Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Weee.Models;
using Weee.Service;
namespace Weee.Controllers.Api
{
[Authorize]
public class DataQualityLevelController : ApiController
{
private WeeeParameterDataService service;
public DataQualityLevelController(WeeeParameterDataService s)
{
service = s;
}
protected decimal getKgCO2e( DataQualityLevelViewModel inObj)
{
decimal ret = 0;
wordExportDataService weds = new wordExportDataService(
service.GetDataContext());
switch (inObj.tablename)
{
case "ProductLCAProductSurveyForm_Materials":
var qry1 = (from a in service.GetDataContext().ProductLCAProductSurveyForm_Material
where a.ID == inObj.tableId
select a).FirstOrDefault();
if (qry1 != null)
{
ret = qry1.KgCO2e;
inObj.LCAID = qry1.LCAID;
}
break;
case "LCACommonSurveyForm_WorkHours":
var qry2 = (from a in service.GetDataContext().LCACommonSurveyForm_WorkHour
where a.LCAID == inObj.LCAID
select a).ToList();
if (qry2.Any())
{
ret = qry2.Sum(x=>x.KgCO2e);
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationWorkHour);
ret *= ratio;
}
break;
case "LCACommonSurveyForm_PowerUsages":
var qry3 = (from a in service.GetDataContext().LCACommonSurveyForm_PowerUsage
where a.LCAID == inObj.LCAID
select a).ToList();
if (qry3.Any())
{
ret = qry3.Sum(x => x.KgCO2e);
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationPowerUsage);
ret *= ratio;
}
break;
case "ProductLCAFabSurveyForm_WaterUsages":
var qry4 = (from a in service.GetDataContext().ProductLCAFabSurveyForm_WaterUsage
where a.LCAID == inObj.LCAID
select a).ToList();
if (qry4.Any())
{
ret = qry4.Sum(x => x.KgCO2e);
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationWaterUsage);
ret *= ratio;
}
break;
case "ProductLCAFabSurveyForm_Wastes":
var qry5 = (from a in service.GetDataContext().ProductLCAFabSurveyForm_Wastes
where a.ID == inObj.tableId
select a).FirstOrDefault();
if (qry5 != null)
{
ret = qry5.KgCO2e;
inObj.LCAID = qry5.LCAID;
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
if (qry5.Type == ProductLCAFabSurveyForm_Waste.WasteType.other)
{
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationWaste);
ret *= ratio;
}
else if (qry5.Type == ProductLCAFabSurveyForm_Waste.WasteType.water)
{
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationWasteWater);
ret *= ratio;
}
}
break;
case "ProductLCAFabSurveyForm_Transports":
var qry6 = (from a in service.GetDataContext().ProductLCAFabSurveyForm_Transport
where a.ID == inObj.tableId
select a).FirstOrDefault();
if (qry6 != null)
{
ret = qry6.KgCO2e;
inObj.LCAID = qry6.LCAID;
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
if (qry6.TransportType == TransportType.upstream)
{
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationTransportUpstream);
ret *= ratio;
}
else if (qry6.TransportType == TransportType.downstream)
{
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationTransportDownstream);
ret *= ratio;
}
}
break;
case "ProductLCAFabSurveyForm_WasteTransports":
var qry7 = (from a in service.GetDataContext().ProductLCAFabSurveyForm_WasteTransport
where a.ID == inObj.tableId
select a).FirstOrDefault();
if (qry7 != null)
{
ret = qry7.KgCO2e;
inObj.LCAID = qry7.LCAID;
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationWasteTransport);
ret *= ratio;
}
break;
case "LCACommonSurveyForm_Vehicles":
var qry8 = (from a in service.GetDataContext().LCACommonSurveyForm_Vehicle
where a.ID == inObj.tableId
select a).FirstOrDefault();
if (qry8 != null)
{
ret = qry8.KgCO2e;
inObj.LCAID = qry8.LCAID;
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationVehicle);
ret *= ratio;
}
break;
case "LCACommonSurveyForm_GasolineEquipments":
var qry9 = (from a in service.GetDataContext().LCACommonSurveyForm_GasolineEquipment
where a.ID == inObj.tableId
select a).FirstOrDefault();
if (qry9 != null)
{
ret = qry9.KgCO2e;
inObj.LCAID = qry9.LCAID;
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationGasolineEquipment);
ret *= ratio;
}
break;
case "LCACommonSurveyForm_Kitchens":
var qry10 = (from a in service.GetDataContext().LCACommonSurveyForm_Kitchen
where a.ID == inObj.tableId
select a).FirstOrDefault();
if (qry10 != null)
{
ret = qry10.KgCO2e;
inObj.LCAID = qry10.LCAID;
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationKitchen);
ret *= ratio;
}
break;
case "LCACommonSurveyForm_FireEquipments":
var qry11 = (from a in service.GetDataContext().LCACommonSurveyForm_FireEquipment
where a.ID == inObj.tableId
select a).FirstOrDefault();
if (qry11 != null)
{
ret = qry11.KgCO2e;
inObj.LCAID = qry11.LCAID;
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationFireEquipment);
ret *= ratio;
}
break;
case "LCACommonSurveyForm_Refrigerants":
var qry12 = (from a in service.GetDataContext().LCACommonSurveyForm_Refrigerant
where a.ID == inObj.tableId
select a).FirstOrDefault();
if (qry12 != null)
{
ret = qry12.KgCO2e;
inObj.LCAID = qry12.LCAID;
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationRefrigerant);
ret *= ratio;
}
break;
case "LCACommonSurveyForm_SteamUsages":
var qry13 = (from a in service.GetDataContext().LCACommonSurveyForm_SteamUsage
where a.LCAID == inObj.LCAID
select a).ToList();
if (qry13.Any())
{
ret = qry13.Sum(x => x.KgCO2e);
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationSteamUsage);
ret *= ratio;
}
break;
case "ProductLCAFabSurveyForm_OtherCompounds":
var qry14 = (from a in service.GetDataContext().ProductLCAFabSurveyForm_OtherCompound
where a.ID == inObj.tableId
select a).FirstOrDefault();
if (qry14 != null)
{
ret = qry14.KgCO2e;
inObj.LCAID = qry14.LCAID;
// 加上分配比例
var productLCA = weds.getProductLCA(inObj.LCAID);
decimal ratio = weds.getRatioByProductAllocation(inObj.LCAID, productLCA.ProductAllocationOtherCompound);
ret *= ratio;
}
break;
}
return ret;
}
protected decimal getLCAtotalKgCO2e(int LCAID)
{
decimal ret = 0;
wordExportDataService weds = new wordExportDataService(
service.GetDataContext());
ret = weds.getAllKgCO2e(LCAID);
var plca = weds.getProductLCA(LCAID);
ret += weds.getWasteKgCO2e(LCAID,plca);
ret += weds.getWasteWaterKgCO2e(LCAID,plca);
ret += weds.getWasteTransportKgCO2e(LCAID,plca);
return ret;
}
[Route("api/DataQualityLevel/GetByTableId/{tableName}/{tableId}")]
[HttpGet]
public DataQualityLevelViewModel GetByTableId(string tableName, int tableId)
{
DataQualityLevelViewModel ret = null;
if (tableId <= 0)
return null;
ret = new DataQualityLevelViewModel();
var qry = (from a in service.GetDataContext().ProductDataQualityLevels
where a.tablename == tableName && a.tableId == tableId
select a).FirstOrDefault();
if (qry != null)
ret = CScommon.JsonUtl.jsonCopy<ProductDataQualityLevels
, DataQualityLevelViewModel>(qry);
else
{
ret.tablename = tableName;
ret.tableId = tableId;
}
ret.KgCO2e = getKgCO2e(ret);
ret.LCAtotalKgCO2e = getLCAtotalKgCO2e(ret.LCAID);
ret.Fi = ret.KgCO2e / ret.LCAtotalKgCO2e;
ret.DQRw = ret.DQR * ret.Fi;
return ret;
}
[Route("api/DataQualityLevel/GetByTableLCAID/{LCAID}/{tableName}")]
[HttpGet]
public DataQualityLevelViewModel GetByTableLCAID(int LCAID, string tableName)
{
DataQualityLevelViewModel ret = null;
if (LCAID <= 0)
return null;
ret = new DataQualityLevelViewModel();
var qry = (from a in service.GetDataContext().ProductDataQualityLevels
where a.LCAID==LCAID && a.tablename == tableName
select a).FirstOrDefault();
if (qry != null)
ret = CScommon.JsonUtl.jsonCopy<ProductDataQualityLevels
, DataQualityLevelViewModel>(qry);
else
{
ret.LCAID = LCAID;
ret.tablename = tableName;
}
if(string.IsNullOrWhiteSpace(ret.name))
ret.name = getTableName(tableName);
ret.KgCO2e = getKgCO2e(ret);
ret.LCAtotalKgCO2e = getLCAtotalKgCO2e(ret.LCAID);
ret.Fi = ret.KgCO2e / ret.LCAtotalKgCO2e;
ret.DQRw = ret.DQR * ret.Fi;
return ret;
}
[Route("api/DataQualityLevel/GetByID/{ID}")]
[HttpGet]
public DataQualityLevelViewModel GetByID(int ID)
{
DataQualityLevelViewModel ret = null;
if (ID <= 0)
return null;
var qry = (from a in service.GetDataContext().ProductDataQualityLevels
where a.ID==ID
select a).FirstOrDefault();
if (qry != null)
{
ret = CScommon.JsonUtl.jsonCopy<ProductDataQualityLevels
, DataQualityLevelViewModel>(qry);
ret.KgCO2e = getKgCO2e(ret);
ret.LCAtotalKgCO2e = getLCAtotalKgCO2e(ret.LCAID);
ret.Fi = ret.KgCO2e / ret.LCAtotalKgCO2e;
ret.DQRw = ret.DQR * ret.Fi;
}
return ret;
}
private bool tableWith0id(string tablename)
{
bool ret = false;
switch (tablename)
{
case "LCACommonSurveyForm_WorkHours":
case "LCACommonSurveyForm_PowerUsages":
case "ProductLCAFabSurveyForm_WaterUsages":
case "LCACommonSurveyForm_SteamUsages":
ret = true;
break;
}
return ret;
}
[Route("api/DataQualityLevel/Save/")]
[HttpPost]
[Filter.ApiLog]
public void Save(DataQualityLevelViewModel ToBeSave)
{
ProductDataQualityLevels entity = null;
try
{
if (ToBeSave.ID > 0)
{
var qry = (from a in service.GetDataContext().ProductDataQualityLevels
where a.ID == ToBeSave.ID
select a).FirstOrDefault();
if (qry != null)
entity = qry;
else
throw new Exception("找不到對應資料儲存");
}
if (entity==null && ToBeSave.tableId > 0)
{
var qry = (from a in service.GetDataContext().ProductDataQualityLevels
where a.tablename==ToBeSave.tablename
&& a.tableId==ToBeSave.tableId
select a).FirstOrDefault();
if (qry != null)
entity = qry;
}
if (entity == null && ToBeSave.LCAID > 0 && tableWith0id(ToBeSave.tablename))
{
var qry = (from a in service.GetDataContext().ProductDataQualityLevels
where a.LCAID==ToBeSave.LCAID
&& a.tablename == ToBeSave.tablename
select a).FirstOrDefault();
if (qry != null)
entity = qry;
}
if (entity != null)//update
{
ProductDataQualityLevels.copy(ToBeSave, ref entity);
}
else//add
{
entity = new ProductDataQualityLevels();
ProductDataQualityLevels.copy(ToBeSave, ref entity);
service.GetDataContext().ProductDataQualityLevels.Add(entity);
}
service.GetDataContext().SaveChanges();
}
catch (Exception ex)
{
Logger log = NLog.LogManager.GetCurrentClassLogger();
log.Error(ex);
log.Error(CScommon.Exceptions.inner(ex));
log.Error(ex.StackTrace);
throw;
}
}
private string getTableName(string tablename)
{
switch (tablename)
{
case "LCACommonSurveyForm_WorkHours":
return Resource.TotalWorkHours;
case "LCACommonSurveyForm_PowerUsages":
return Resource.PowerUsage;
case "ProductLCAFabSurveyForm_WaterUsages":
return Resource.WaterUsage;
case "LCACommonSurveyForm_SteamUsages":
return Resource.SteamUsage;
}
return "";
}
}
}