demo20230512/Controllers/Api/ProductLCA/RefrigerantController.cs

149 lines
6.3 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.Models;
using Weee.Models.Paramemter;
using Weee.Service;
using PagedList;
using Weee.Filter;
namespace Weee.Controllers
{
public class RefrigerantController : ApiControllerBase
{
private readonly WeeeParameterDataService parameterservice;
private readonly WeeeSheetDataService service;
public RefrigerantController(WeeeSheetDataService d, WeeeParameterDataService s) : base(d)
{
service = d;
parameterservice = s;
}
[Route("api/Refrigerant/GetByLcaId/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetByLcaId(int LCAID, int page = 1, string search = "", int pagesize = 15)
{
var type = service.GetLCAType(LCAID);
//var gwpOptions = //type == typeof(ProductLCA) ?
// parameterservice.GetNonYearlyParameterOptions(Categories.RefrigerantGWP);//:
// //parameterservice.GetNonYearlyParameterOptions(Categories.RefrigerantGWPOrganization);
var query = //service.GetSheet<LCACommonSurveyForm_Refrigerants>(LCAID);
service.getRefrigerantWithFactor(LCAID);
var option1 = parameterservice.GetARnGWPoptions(GWPparameterType.Refrigerant, LCAID);
var option2 = parameterservice.GetARnGWPoptions(GWPparameterType.FireEquipmentAndRefrigerant, LCAID);
object ret= new
{
Refrigerants = query.ToList(),
OptionsOne = //gwpOptions,
option1.Concat(option2.Where(kvp => !option1.ContainsKey(kvp.Key))).ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
OptionsTwo = parameterservice.GetNonYearlyParameterOptions(Categories.Refrigerant),
SheetHeader = service.GetSheetHeader(LCAID, Categories.Refrigerant),
ActivityOptions = activityItems,
EmitParaOptions = emitParaItems
};
return ret;
}
[Route("api/Refrigerant/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveHeader(SheetHeader header)
{
return this.SaveSheetHeader(service, Weee.Models.Paramemter.Categories.Refrigerant, header);
}
[Route("api/Refrigerant/SaveAll")]
[HttpPost]
[ApiEditableAttribute("ToBeSave", true)]
public object SaveAll(IEnumerable<LCACommonSurveyForm_Refrigerants> ToBeSave)
{
if (!ModelState.IsValid)
{
throw new Exception("error");
}
foreach (var rec in ToBeSave)
{
if (rec.ParameterID > 0)
{
rec.ARnGWPid = rec.ParameterID;
rec.ParameterID = null;
}
if (rec.Factor == null)
rec.Factor = parameterservice.GetNonYearlyParameter((int)rec.ParameterID2);
decimal paraValue = parameterservice.getARnGWPvalue(rec.LCAID, (int)rec.ARnGWPid);
rec.KgCO2e = paraValue * rec.Factor.Value * rec.Scalar * rec.TotalNumber * rec.UsedMonth / 12;
//if (rec.Parameter == null)
// rec.Parameter = parameterservice.GetNonYearlyParameter((int)rec.ParameterID);
//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.getRefrigerantWithFactor(result.First().LCAID).Where(x => resultIDs.Contains(x.ID)).ToList();
return new { result = query };
}
[Route("api/Refrigerant/Save/{LCAID}/{id}")]
[HttpPost]
[ApiEditableAttribute("ToBeSave", false)]
public object Save(int LCAID, int id, LCACommonSurveyForm_Refrigerants ToBeSave)
{
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)
{
ToBeSave.ARnGWPid = ToBeSave.ParameterID;
ToBeSave.ParameterID = null;
}
if (ToBeSave.Factor == null)
ToBeSave.Factor = parameterservice.GetNonYearlyParameter((int)ToBeSave.ParameterID2);
decimal paraValue = parameterservice.getARnGWPvalue(ToBeSave.LCAID, (int)ToBeSave.ARnGWPid);
ToBeSave.KgCO2e = paraValue * ToBeSave.Factor.Value * ToBeSave.Scalar * ToBeSave.TotalNumber * ToBeSave.UsedMonth / 12;
//if (ToBeSave.Parameter == null)
// ToBeSave.Parameter = parameterservice.GetNonYearlyParameter((int)ToBeSave.ParameterID);
//if (ToBeSave.activityDataType == 0) // 帶出年度預設值
//{
// ToBeSave.activityDataType = ToBeSave.Parameter.Type.activityDataType;
//}
//if (ToBeSave.emitParaType == 0) // 帶出年度預設值
//{
// ToBeSave.emitParaType = ToBeSave.Parameter.Type.emitParaType;
//}
service.SaveSheetItem(ToBeSave);
var query = service.getRefrigerantWithFactor(LCAID).Where(x => x.ID == ToBeSave.ID);
return query.ToList().First();
}
[Route("api/Refrigerant/DeleteAll/{LCAID}")]
[HttpDelete]
public IHttpActionResult DeleteAll(int LCAID)
{
service.DeleteSheet<LCACommonSurveyForm_Refrigerants>(LCAID);
return Ok();
}
[Route("api/Refrigerant/Delete/{ID}")]
[HttpDelete]
public IHttpActionResult Delete(int ID)
{
service.DeleteSheetItem<LCACommonSurveyForm_Refrigerants>(ID);
return Ok();
}
}
}