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

137 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Weee.Models;
using Weee.Service;
using Weee.DAL;
using Weee.Models.ExtensionMethods;
using Weee.Filter;
using Microsoft.AspNet.Identity;
namespace Weee.Controllers
{
public class SteamUsageController : ApiControllerBase
{
private readonly WeeeMonthlyDataService service;
private readonly WeeeParameterDataService parameterservice;
private readonly WeeeDataContext _db;
private WeeeLCADataService serviceLCA;
public SteamUsageController(WeeeMonthlyDataService d, WeeeParameterDataService s
, WeeeDataContext db, WeeeLCADataService serviceLCA)
: base(d)
{
service = d;
parameterservice = s;
_db = db;
this.serviceLCA = serviceLCA;
}
[Route("api/SteamUsage/GetSteamUsages/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object Get(int LCAID)
{
LCA lca = service.GetLCA(LCAID);
object ret = new
{
SteamUsages = service.GetSheet<LCACommonSurveyForm_SteamUsages>(LCAID),
// 檢查目前的盤查是否為產品型,用於讀取不同的係數
options = parameterservice.GetYearlyParameterOptions((lca.LCAType.Name == "ProductLCA") ? Models.Paramemter.Categories.SteamProduct : Models.Paramemter.Categories.Steam),
SheetHeader = service.GetSheetHeader(LCAID, (lca.LCAType.Name == "ProductLCA") ? Models.Paramemter.Categories.SteamProduct : Models.Paramemter.Categories.Steam),
ActivityOptions = activityItems,
EmitParaOptions = emitParaItems,
factor = parameterservice.LCAID2steamPara(LCAID)
};
//if (ret.Str)
return ret;
}
[Route("api/SteamUsage/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveHeader(SheetHeader header)
{
return this.SaveSheetHeader(service, Weee.Models.Paramemter.Categories.Steam, header);
}
[Route("api/SteamUsage/SaveSteamUsages/{LCAID}/{activity}/{emitPara}")]
[HttpPost]
[ApiEditableAttribute("ToBeSave", true)]
public object SaveAll(int LCAID, int activity, int emitPara,
IEnumerable<LCACommonSurveyForm_SteamUsages> ToBeSave)
{
if (!ModelState.IsValid)
{
throw new Exception("error");
}
if (ToBeSave == null || ToBeSave.Count() == 0)
return null;
serviceLCA.Initialize(User.Identity.GetUserId());
LCA lca = serviceLCA.GetLCA(LCAID);
lca.activityDataTypePowerUsage = (ActivityDataType)activity;
lca.emitParaTypePowerUsage = (EmitParaType)emitPara;
_db.SaveChanges();
//OrganizationLCA orgLCA = serviceLCA.GetOrganizationLCA(LCAID);
//orgLCA.activityDataTypeSteamUsage = (ActivityDataType)activity;
//orgLCA.emitParaTypeSteamUsage = (EmitParaType)emitPara;
//serviceLCA.SaveOrganizationLCA(orgLCA);
var parameterIDs = ToBeSave
.Select(x => x.ParameterID)
.Where(x => x != null)
.Distinct()
.Cast<int>();
var parameters = _db.Parameters
.Where(x => parameterIDs.Contains(x.ID))
.Select(x => new { x.ID, x.Value });
foreach (var item in ToBeSave)
{
item.ID = 0;
item.Scalar = Math.Round(item.Scalar, 4);
if (item.Factor != null)
{
item.Factor = Math.Round((decimal)item.Factor, 10);
item.UpdateKgCO2e(item.Factor.Value);
}
else if (item.ParameterID != null)
item.UpdateKgCO2e(parameters.First(x => x.ID == item.ParameterID).Value);
else
item.KgCO2e = 0;
}
service.DeleteSheet<LCACommonSurveyForm_SteamUsages>(ToBeSave.First().LCAID);
var result = service.SaveSheet(ToBeSave);
return new { result = result };
}
//[Route("api/SteamUsage/GetCoeff/{LCAID}/{id}")]
////[HttpPost]
//public object GetCoeff(int LCAID, int id)
//{
// object ret = null;
// var qry = (from a in _db.Parameters
// where a.ID==id
// select a.Value).FirstOrDefault();
// if (qry != null)
// ret = qry;
// return ret;
//}
//[Route("api/SteamUsage/SaveSteamUsage/{LCAID}/{id}")]
//[HttpPost]
//public int Save(int LCAID, int id, SteamUsage steamusage)
//{
// if (!ModelState.IsValid) { return -1; }
// if (id != steamusage.ID || LCAID != steamusage.LCAID) { return -1; }
// service.SaveMonthlyData(steamusage);
// return steamusage.ID;
//}
//[Route("api/SteamUsage/DeleteSteamUsages/{LCAID}")]
//[HttpDelete]
//public IHttpActionResult DeleteAll(int LCAID)
//{
// service.DeleteSheet<SteamUsage>(LCAID);
// return Ok();
//}
}
}