demo20230512/Controllers/Api/ProductLCA/KitchenController.cs

205 lines
9.6 KiB
C#
Raw Permalink Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Weee.Filter;
using Weee.Models;
using Weee.Models.Paramemter;
using Weee.Service;
using PagedList;
using Resources;
using System.Web;
using System.IO;
namespace Weee.Controllers
{
//[MvcMultilanguage]disable obsolete warning, not sure OK or not
public class KitchenController : ApiControllerBase
{
private readonly WeeeParameterDataService parameterservice;
private readonly WeeeSheetDataService service;
public KitchenController(WeeeSheetDataService d, WeeeParameterDataService s) : base(d)
{
service = d;
parameterservice = s;
}
[Route("api/Kitchen/GetByLcaId/{LCAID}/")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetByLcaId(int LCAID)
{
LCA lca = service.GetLCA(LCAID);
var query = //service.GetSheet<LCACommonSurveyForm_Kitchens>(LCAID);
service.getKitchenWithFactor(LCAID);
object ret= new
{
Kitchens = query.ToList(),
Options = parameterservice.GetYearlyParameterOptions((lca.LCAType.Name == "ProductLCA") ? Categories.KitchenProduct : Categories.Kitchen),
WarmGasTypes = parameterservice.GetYearlyParameterWarmGasTypes((lca.LCAType.Name == "ProductLCA") ? Categories.KitchenProduct : Categories.Kitchen),
SheetHeader = service.GetSheetHeader(LCAID, (lca.LCAType.Name == "ProductLCA") ? Categories.KitchenProduct : Categories.Kitchen),
ActivityOptions = activityItems,
EmitParaOptions = emitParaItems
};
return ret;
}
[Route("api/Kitchen/SaveAll")]
[HttpPost]
[ApiEditableAttribute("toBeSave", true)]
public object SaveAll(IEnumerable<LCACommonSurveyForm_Kitchens> toBeSave)
{
if (!ModelState.IsValid)
{
throw new Exception("error");
}
var dic = service.getN2OandCH4Factors(toBeSave.First().LCAID);
LCA lca = service.GetLCA(toBeSave.First().LCAID);
foreach (var rec in toBeSave)
{
if (rec.ParameterID > 0)
{
if (rec.Parameter == null)
rec.Parameter = parameterservice.GetYearlyParameter((int)rec.ParameterID);
rec.ParameterValue = (lca.LCAType.Name == "ProductLCA") ? rec.Parameter.Value : (rec.Parameter.CO2Value + rec.Parameter.CH4Value * dic["CH4"] + rec.Parameter.N2OValue * dic["N2O"]);
//rec.ParameterValue = rec.Parameter.CO2Value + rec.Parameter.CH4Value * dic["CH4"] + rec.Parameter.N2OValue * dic["N2O"];
rec.KgCO2e = rec.ParameterValue * rec.Scalar;
if (rec.activityDataType == 0) // 帶出年度預設值
{
rec.activityDataType = rec.Parameter.Type.activityDataType;
}
if (rec.emitParaType == 0) // 帶出年度預設值
{
rec.emitParaType = rec.Parameter.Type.emitParaType;
}
}
}
var result = service.SaveSheet(toBeSave);
var resultIDs = result.Select(y => y.ID).ToList();
var query = service.getKitchenWithFactor(result.First().LCAID).Where(x => resultIDs.Contains(x.ID)).ToList();
return new { result = query };
}
[Route("api/Kitchen/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveHeader(SheetHeader header)
{
LCA lca = service.GetLCA(header.LCAID);
return this.SaveSheetHeader(service, (lca.LCAType.Name == "ProductLCA") ? Categories.KitchenProduct : Categories.Kitchen, header);
}
[Route("api/Kitchen/Save/{LCAID}/{id}")]
[HttpPost]
[ApiEditableAttribute("toBeSave", false)]
public Object Save(int LCAID, int id, LCACommonSurveyForm_Kitchens toBeSave)
{
//toBeSave.ReferenceFileUrl=getBaseUrl()+toBeSave.ReferenceFileUrl;
//toBeSave.ReferencePhotoUrl = getBaseUrl() + toBeSave.ReferencePhotoUrl;
if (!ModelState.IsValid) {
return BadRequest();
}
if (id != toBeSave.ID || LCAID != toBeSave.LCAID) {
return BadRequest();
}
toBeSave.Scalar = Math.Round(toBeSave.Scalar, 4);
if (toBeSave.ParameterID > 0)
{
if (toBeSave.Parameter == null)
toBeSave.Parameter = parameterservice.GetYearlyParameter((int)toBeSave.ParameterID);
var dic = service.getN2OandCH4Factors(LCAID);
LCA lca = service.GetLCA(LCAID);
//toBeSave.ParameterValue = toBeSave.Parameter.CO2Value + toBeSave.Parameter.CH4Value * dic["CH4"] + toBeSave.Parameter.N2OValue * dic["N2O"];
toBeSave.ParameterValue = (lca.LCAType.Name == "ProductLCA") ? toBeSave.Parameter.Value : (toBeSave.Parameter.CO2Value + toBeSave.Parameter.CH4Value * dic["CH4"] + toBeSave.Parameter.N2OValue * dic["N2O"]);
toBeSave.KgCO2e = toBeSave.ParameterValue * toBeSave.Scalar;
if (toBeSave.activityDataType == 0) // 帶出年度預設值
{
toBeSave.activityDataType = toBeSave.Parameter.Type.activityDataType;
}
if (toBeSave.emitParaType == 0) // 帶出年度預設值
{
toBeSave.emitParaType = toBeSave.Parameter.Type.emitParaType;
}
}
if (toBeSave.Scalar==0)
toBeSave.KgCO2e = 0;
service.SaveSheetItem(toBeSave);
var storage = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(Storage.AzureStorage)) as Storage.AzureStorage;
if (!string.IsNullOrWhiteSpace(toBeSave.ReferenceFileUrl) && toBeSave.ReferenceFileUrl.Contains("kf_"))
{
var uri = toBeSave.ReferenceFileUrl;
uri = uri.Replace("kf_", "kf" + toBeSave.ID + "_");
storage.MoveToAzure(toBeSave.ReferenceFileUrl, uri);
toBeSave.ReferenceFileUrl = uri;
}
if (!string.IsNullOrWhiteSpace(toBeSave.ReferencePhotoUrl) && toBeSave.ReferencePhotoUrl.Contains("kp_"))
{
var uri = toBeSave.ReferencePhotoUrl;
uri = uri.Replace("kp_", "kp" + toBeSave.ID + "_");
storage.MoveToAzure(toBeSave.ReferencePhotoUrl, uri);
toBeSave.ReferencePhotoUrl = uri;
}
service.SaveSheetItem(toBeSave);
var query = service.getKitchenWithFactor(LCAID).Where(x => x.ID == toBeSave.ID);
return query.ToList().First();
}
[Route("api/Kitchen/DeleteAll/{LCAID}")]
[HttpDelete]
public IHttpActionResult DeleteAll(int LCAID)
{
service.DeleteSheet<LCACommonSurveyForm_Kitchens>(LCAID);
return Ok();
}
[Route("api/Kitchen/Delete/{ID}")]
[HttpDelete]
public IHttpActionResult Delete(int ID)
{
service.DeleteSheetItem<LCACommonSurveyForm_Kitchens>(ID);
return Ok();
}
[Route("api/Kitchen/GetWarmGasType")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetWarmGasType()
{
object[] warmGasType = {
new { Id = WarmGasType., Text = Resource.fixedSource },
new { Id = WarmGasType., Text = Resource.movingSource },
new { Id = WarmGasType., Text = Resource.manufactureSource },
new { Id = WarmGasType., Text = Resource.escapeSource },
new { Id = WarmGasType.使, Text = Resource.landUsageSource },
new { Id = WarmGasType., Text = Resource.externalEnergySource }
};
object ret = new
{
WarmGasType = warmGasType
};
return ret;
}
[Route("api/Kitchen/GetByLcaIdAndWarmGasType/{LCAID}/{WarmGasType}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetByLcaIdAndWarmGasType(int LCAID, int WarmGasType)
{
LCA lca = service.GetLCA(LCAID);
var query = //service.GetSheet<LCACommonSurveyForm_Kitchens>(LCAID);
service.getKitchenFilterWarmGasTypeWithFactor(LCAID, WarmGasType);
object ret = new
{
Kitchens = query.ToList(),
//Options = parameterservice.GetYearlyParameterOptions(Categories.Kitchen),
//WarmGasTypes = parameterservice.GetYearlyParameterWarmGasTypes(Categories.Kitchen),
//SheetHeader = service.GetSheetHeader(LCAID, Categories.Kitchen),
Options = parameterservice.GetYearlyParameterOptions((lca.LCAType.Name == "ProductLCA") ? Categories.KitchenProduct : Categories.Kitchen),
WarmGasTypes = parameterservice.GetYearlyParameterWarmGasTypes((lca.LCAType.Name == "ProductLCA") ? Categories.KitchenProduct : Categories.Kitchen),
SheetHeader = service.GetSheetHeader(LCAID, (lca.LCAType.Name == "ProductLCA") ? Categories.KitchenProduct : Categories.Kitchen),
ActivityOptions = activityItems,
EmitParaOptions = emitParaItems
};
return ret;
}
}
}